Add dumpContainer to container factory
[MAILPOET-1605]
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -24,6 +24,7 @@ lang
|
|||||||
/nbproject/
|
/nbproject/
|
||||||
tests/_data/acceptanceGenerated.sql
|
tests/_data/acceptanceGenerated.sql
|
||||||
lib/Dependencies
|
lib/Dependencies
|
||||||
|
lib/DI/CachedContainer.php
|
||||||
mozart/Dependencies
|
mozart/Dependencies
|
||||||
mozart/Classes
|
mozart/Classes
|
||||||
mozart/vendor
|
mozart/vendor
|
||||||
|
@@ -4,6 +4,7 @@ namespace MailPoet\Config;
|
|||||||
|
|
||||||
use MailPoet\API;
|
use MailPoet\API;
|
||||||
use MailPoet\Cron\CronTrigger;
|
use MailPoet\Cron\CronTrigger;
|
||||||
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Container;
|
||||||
use MailPoet\DI\ContainerFactory;
|
use MailPoet\DI\ContainerFactory;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Router;
|
use MailPoet\Router;
|
||||||
@@ -19,6 +20,7 @@ require_once(ABSPATH . 'wp-admin/includes/plugin.php');
|
|||||||
class Initializer {
|
class Initializer {
|
||||||
private $access_control;
|
private $access_control;
|
||||||
private $renderer;
|
private $renderer;
|
||||||
|
/** @var Container */
|
||||||
private $container;
|
private $container;
|
||||||
|
|
||||||
const INITIALIZED = 'MAILPOET_INITIALIZED';
|
const INITIALIZED = 'MAILPOET_INITIALIZED';
|
||||||
@@ -52,7 +54,7 @@ class Initializer {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->compileContainer();
|
$this->loadContainer();
|
||||||
|
|
||||||
// activation function
|
// activation function
|
||||||
register_activation_hook(
|
register_activation_hook(
|
||||||
@@ -94,9 +96,9 @@ class Initializer {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
function compileContainer() {
|
function loadContainer() {
|
||||||
$this->container = ContainerFactory::getContainer();
|
$container_factory = new ContainerFactory(WP_DEBUG);
|
||||||
$this->container->compile();
|
$this->container = $container_factory->getContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkRequirements() {
|
function checkRequirements() {
|
||||||
|
@@ -2,31 +2,69 @@
|
|||||||
|
|
||||||
namespace MailPoet\DI;
|
namespace MailPoet\DI;
|
||||||
|
|
||||||
use MailPoet\Dependencies\Symfony\Component\Config\FileLocator;
|
|
||||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\ContainerBuilder;
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
use MailPoet\Dependencies\Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
||||||
|
|
||||||
class ContainerFactory {
|
class ContainerFactory {
|
||||||
|
|
||||||
/** @var ContainerBuilder */
|
/** @var ContainerBuilder */
|
||||||
private static $container;
|
private $container;
|
||||||
|
|
||||||
static function getContainer() {
|
/** @var string */
|
||||||
if(!self::$container) {
|
private $dump_file = 'CachedContainer.php';
|
||||||
self::createContainer();
|
|
||||||
}
|
/** @var string */
|
||||||
return self::$container;
|
private $dump_class = 'CachedContainer';
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
private $debug;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ContainerFactory constructor.
|
||||||
|
* @param bool $debug
|
||||||
|
*/
|
||||||
|
public function __construct($debug = false) {
|
||||||
|
$this->debug = $debug;
|
||||||
}
|
}
|
||||||
|
|
||||||
static function createContainer() {
|
function getContainer() {
|
||||||
self::$container = new ContainerBuilder();
|
if($this->container) {
|
||||||
self::$container->autowire(\MailPoet\Config\AccessControl::class);
|
return $this->container;
|
||||||
self::$container->autowire(\MailPoet\Cron\Daemon::class);
|
}
|
||||||
self::$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class);
|
|
||||||
self::$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class);
|
$dump_file = __DIR__ . '/' . $this->dump_file;
|
||||||
self::$container->autowire(\MailPoet\Router\Endpoints\Subscription::class);
|
if(!$this->debug && file_exists($dump_file)) {
|
||||||
self::$container->autowire(\MailPoet\Router\Endpoints\Track::class);
|
require_once $dump_file;
|
||||||
self::$container->autowire(\MailPoet\Router\Endpoints\ViewInBrowser::class);
|
$this->container = new $this->dump_class();
|
||||||
return self::$container;
|
} else {
|
||||||
|
$this->container = $this->createContainer();
|
||||||
|
$this->container->compile();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->container;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContainer() {
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
$container->autowire(\MailPoet\Config\AccessControl::class);
|
||||||
|
$container->autowire(\MailPoet\Cron\Daemon::class);
|
||||||
|
$container->autowire(\MailPoet\Cron\DaemonHttpRunner::class);
|
||||||
|
$container->autowire(\MailPoet\Router\Endpoints\CronDaemon::class);
|
||||||
|
$container->autowire(\MailPoet\Router\Endpoints\Subscription::class);
|
||||||
|
$container->autowire(\MailPoet\Router\Endpoints\Track::class);
|
||||||
|
$container->autowire(\MailPoet\Router\Endpoints\ViewInBrowser::class);
|
||||||
|
return $container;
|
||||||
|
}
|
||||||
|
|
||||||
|
function dumpContainer() {
|
||||||
|
$container = $this->createContainer();
|
||||||
|
$container->compile();
|
||||||
|
$dumper = new PhpDumper($container);
|
||||||
|
file_put_contents(
|
||||||
|
__DIR__ . '/' . $this->dump_file,
|
||||||
|
$dumper->dump([
|
||||||
|
'class' => $this->dump_class
|
||||||
|
])
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -26,7 +26,8 @@ class RouterTest extends \MailPoetTest {
|
|||||||
'data' => base64_encode(json_encode(array('data' => 'dummy data')))
|
'data' => base64_encode(json_encode(array('data' => 'dummy data')))
|
||||||
);
|
);
|
||||||
$this->access_control = new AccessControl();
|
$this->access_control = new AccessControl();
|
||||||
$this->container = ContainerFactory::createContainer();
|
$container_factory = new ContainerFactory(true);
|
||||||
|
$this->container = $container_factory->createContainer();
|
||||||
$this->container->register(RouterTestMockEndpoint::class)->setPublic(true);
|
$this->container->register(RouterTestMockEndpoint::class)->setPublic(true);
|
||||||
$this->container->compile();
|
$this->container->compile();
|
||||||
$this->router = new Router($this->access_control, $this->container, $this->router_data);
|
$this->router = new Router($this->access_control, $this->container, $this->router_data);
|
||||||
|
Reference in New Issue
Block a user