Get rid of static instantiation of JSON API
[MAILPOET-1689]
This commit is contained in:
@ -3,41 +3,17 @@
|
|||||||
namespace MailPoet\API;
|
namespace MailPoet\API;
|
||||||
|
|
||||||
use MailPoet\DI\ContainerWrapper;
|
use MailPoet\DI\ContainerWrapper;
|
||||||
use MailPoetVendor\Psr\Container\ContainerInterface;
|
|
||||||
use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
use MailPoetVendor\Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class API {
|
class API {
|
||||||
|
|
||||||
/** @var ContainerInterface */
|
|
||||||
private static $container;
|
|
||||||
|
|
||||||
static function injectContainer(ContainerInterface $container) {
|
|
||||||
self::$container = $container;
|
|
||||||
}
|
|
||||||
|
|
||||||
static function JSON() {
|
|
||||||
return self::$container->get(JSON\API::class);
|
|
||||||
}
|
|
||||||
|
|
||||||
static function MP($version) {
|
static function MP($version) {
|
||||||
self::ensureContainerIsLoaded();
|
|
||||||
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
|
$api_class = sprintf('%s\MP\%s\API', __NAMESPACE__, $version);
|
||||||
try {
|
try {
|
||||||
return self::$container->get($api_class);
|
return ContainerWrapper::getInstance()->get($api_class);
|
||||||
} catch (ServiceNotFoundException $e) {
|
} catch (ServiceNotFoundException $e) {
|
||||||
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
throw new \Exception(__('Invalid API version.', 'mailpoet'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* MP API is used by third party plugins so we have to ensure that container is loaded
|
|
||||||
* @see https://kb.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin
|
|
||||||
*/
|
|
||||||
private static function ensureContainerIsLoaded() {
|
|
||||||
if(!self::$container) {
|
|
||||||
self::$container = ContainerWrapper::getInstance();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,6 @@ class Initializer {
|
|||||||
|
|
||||||
function loadContainer() {
|
function loadContainer() {
|
||||||
$this->container = ContainerWrapper::getInstance(WP_DEBUG);
|
$this->container = ContainerWrapper::getInstance(WP_DEBUG);
|
||||||
API\API::injectContainer($this->container);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRequirements() {
|
function checkRequirements() {
|
||||||
@ -260,8 +259,7 @@ class Initializer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function setupJSONAPI() {
|
function setupJSONAPI() {
|
||||||
$json_api = API\API::JSON();
|
$this->container->get(API\JSON\API::class)->init();
|
||||||
$json_api->init();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupRouter() {
|
function setupRouter() {
|
||||||
|
@ -5,9 +5,6 @@ use MailPoet\API\API;
|
|||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
|
|
||||||
class APITest extends \MailPoetTest {
|
class APITest extends \MailPoetTest {
|
||||||
function testItCallsJSONAPI() {
|
|
||||||
expect(API::JSON())->isInstanceOf('MailPoet\API\JSON\API');
|
|
||||||
}
|
|
||||||
|
|
||||||
function testItCallsMPAPI() {
|
function testItCallsMPAPI() {
|
||||||
expect(API::MP('v1'))->isInstanceOf('MailPoet\API\MP\v1\API');
|
expect(API::MP('v1'))->isInstanceOf('MailPoet\API\MP\v1\API');
|
||||||
|
Reference in New Issue
Block a user