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