From f032cb8bf132c602dffa0b07e3c34cf436056318 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Tue, 19 Sep 2017 12:45:06 +0200 Subject: [PATCH 1/2] [HttpKernel][DI] Enable Kernel to implement CompilerPassInterface --- .../Compiler/ExtensionCompilerPass.php | 4 ++++ .../DependencyInjection/ContainerBuilder.php | 12 ++++++++++ src/Symfony/Component/HttpKernel/Kernel.php | 4 ++++ .../Component/HttpKernel/Tests/KernelTest.php | 23 +++++++++++++++++++ 4 files changed, 43 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php index 27e5048245914..3696c73eb3092 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php @@ -33,5 +33,9 @@ public function process(ContainerBuilder $container) $extension->process($container); } + + if ($container->getKernelPass()) { + $container->getKernelPass()->process($container); + } } } diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index d46da389f05b4..5b711d7fbeddb 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -121,6 +121,8 @@ class ContainerBuilder extends Container implements TaggedContainerInterface private $autoconfiguredInstanceof = array(); + private $kernelPass; + public function __construct(ParameterBagInterface $parameterBag = null) { parent::__construct($parameterBag); @@ -717,6 +719,16 @@ public function prependExtensionConfig($name, array $config) array_unshift($this->extensionConfigs[$name], $config); } + public function setKernelPass(CompilerPassInterface $kernelPass) + { + $this->kernelPass = $kernelPass; + } + + public function getKernelPass() + { + return $this->kernelPass; + } + /** * Compiles the container. * diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 4f7ec2c289bac..d609290418dda 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -13,6 +13,7 @@ use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Dumper\PhpDumper; @@ -767,6 +768,9 @@ protected function getContainerBuilder() $container = new ContainerBuilder(); $container->getParameterBag()->add($this->getKernelParameters()); + if ($this instanceof CompilerPassInterface) { + $container->setKernelPass($this); + } if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { $container->setProxyInstantiator(new RuntimeInstantiator()); } diff --git a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php index a408f87f87a32..fd7f6f09da13d 100644 --- a/src/Symfony/Component/HttpKernel/Tests/KernelTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/KernelTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Config\Loader\LoaderInterface; +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Bundle\BundleInterface; @@ -831,6 +832,14 @@ public function testKernelReset() $this->assertFileNotExists($containerFile); } + public function testKernelPass() + { + $kernel = new PassKernel(); + $kernel->boot(); + + $this->assertTrue($kernel->getContainer()->getParameter('test.processed')); + } + /** * Returns a mock for the BundleInterface. * @@ -967,3 +976,17 @@ protected function build(ContainerBuilder $container) } } } + +class PassKernel extends CustomProjectDirKernel implements CompilerPassInterface +{ + public function __construct(\Closure $buildContainer = null) + { + parent::__construct(); + Kernel::__construct('pass', true); + } + + public function process(ContainerBuilder $container) + { + $container->setParameter('test.processed', true); + } +} From b3b8e24b145931ed160c9949997fd4ba289cf579 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Wed, 20 Sep 2017 07:16:56 +0200 Subject: [PATCH 2/2] [DI] reorder ExtensionCompilerPass --- UPGRADE-4.0.md | 2 ++ .../Component/DependencyInjection/CHANGELOG.md | 1 + .../Compiler/ExtensionCompilerPass.php | 4 ---- .../DependencyInjection/Compiler/PassConfig.php | 2 +- .../DependencyInjection/ContainerBuilder.php | 12 ------------ src/Symfony/Component/HttpKernel/CHANGELOG.md | 1 + src/Symfony/Component/HttpKernel/Kernel.php | 3 ++- 7 files changed, 7 insertions(+), 18 deletions(-) diff --git a/UPGRADE-4.0.md b/UPGRADE-4.0.md index fba86ef01c971..3aecb70070f0d 100644 --- a/UPGRADE-4.0.md +++ b/UPGRADE-4.0.md @@ -184,6 +184,8 @@ DependencyInjection * Top-level anonymous services in XML are no longer supported. + * The `ExtensionCompilerPass` has been moved to before-optimization passes with priority -1000. + EventDispatcher --------------- diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index bc8fd3051870c..cea65b20b7504 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * moved the `ExtensionCompilerPass` to before-optimization passes with priority -1000 * deprecated "public-by-default" definitions and aliases, the new default will be "private" in 4.0 * added `EnvVarProcessorInterface` and corresponding "container.env_var_processor" tag for processing env vars * added support for ignore-on-uninitialized references diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php index 3696c73eb3092..27e5048245914 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ExtensionCompilerPass.php @@ -33,9 +33,5 @@ public function process(ContainerBuilder $container) $extension->process($container); } - - if ($container->getKernelPass()) { - $container->getKernelPass()->process($container); - } } } diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php index 9e00be6827109..79792bee4a106 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php @@ -45,10 +45,10 @@ public function __construct() new ResolveInstanceofConditionalsPass(), new RegisterEnvVarProcessorsPass(), ), + -1000 => array(new ExtensionCompilerPass()), ); $this->optimizationPasses = array(array( - new ExtensionCompilerPass(), new ResolveChildDefinitionsPass(), new ServiceLocatorTagPass(), new DecoratorServicePass(), diff --git a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php index 5b711d7fbeddb..d46da389f05b4 100644 --- a/src/Symfony/Component/DependencyInjection/ContainerBuilder.php +++ b/src/Symfony/Component/DependencyInjection/ContainerBuilder.php @@ -121,8 +121,6 @@ class ContainerBuilder extends Container implements TaggedContainerInterface private $autoconfiguredInstanceof = array(); - private $kernelPass; - public function __construct(ParameterBagInterface $parameterBag = null) { parent::__construct($parameterBag); @@ -719,16 +717,6 @@ public function prependExtensionConfig($name, array $config) array_unshift($this->extensionConfigs[$name], $config); } - public function setKernelPass(CompilerPassInterface $kernelPass) - { - $this->kernelPass = $kernelPass; - } - - public function getKernelPass() - { - return $this->kernelPass; - } - /** * Compiles the container. * diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 702394ade5bd4..8732b17dd83f5 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 3.4.0 ----- + * made kernels implementing `CompilerPassInterface` able to process the container * deprecated bundle inheritance * added `RebootableInterface` and implemented it in `Kernel` * deprecated commands auto registration diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index d609290418dda..cd4c26063d5d9 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -14,6 +14,7 @@ use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Dumper\PhpDumper; @@ -769,7 +770,7 @@ protected function getContainerBuilder() $container->getParameterBag()->add($this->getKernelParameters()); if ($this instanceof CompilerPassInterface) { - $container->setKernelPass($this); + $container->addCompilerPass($this, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000); } if (class_exists('ProxyManager\Configuration') && class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { $container->setProxyInstantiator(new RuntimeInstantiator());