From b02990564aab19c2cd7c42edf6afc1a1d5038a2c Mon Sep 17 00:00:00 2001 From: Yonel Ceruto Date: Wed, 17 Dec 2025 11:56:53 +0100 Subject: [PATCH] [HttpKernel] Add support for bundles as compiler passes --- src/Symfony/Component/HttpKernel/CHANGELOG.md | 5 ++++ src/Symfony/Component/HttpKernel/Kernel.php | 4 +++ .../HttpKernel/Tests/Bundle/BundleTest.php | 26 +++++++++++++++++++ .../BundleAsCompilerPassBundle.php | 25 ++++++++++++++++++ 4 files changed, 60 insertions(+) create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleCompilerPass/BundleAsCompilerPassBundle.php diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 61f5c9a95ea9e..d858d72857071 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +8.1 +--- + + * Add support for bundles as compiler pass + 8.0 --- diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index 6d5f80c329351..bb73e0895e36d 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -616,6 +616,10 @@ protected function prepareContainer(ContainerBuilder $container): void if ($this->debug) { $container->addObjectResource($bundle); } + + if ($bundle instanceof CompilerPassInterface) { + $container->addCompilerPass($bundle, PassConfig::TYPE_BEFORE_OPTIMIZATION, -10000); + } } foreach ($this->bundles as $bundle) { diff --git a/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php b/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php index b0035e395d187..63af89998ac4e 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php @@ -12,7 +12,10 @@ namespace Symfony\Component\HttpKernel\Tests\Bundle; use PHPUnit\Framework\TestCase; +use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\HttpKernel\Bundle\Bundle; +use Symfony\Component\HttpKernel\Kernel; +use Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass\BundleAsCompilerPassBundle; use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle; class BundleTest extends TestCase @@ -43,6 +46,29 @@ public function testBundleNameCanBeExplicitlyProvided() $this->assertSame('Symfony\Component\HttpKernel\Tests\Bundle', $bundle->getNamespace()); $this->assertSame('ExplicitlyNamedBundle', $bundle->getName()); } + + public function testBundleAsCompilerPass() + { + $kernel = new class('test', true) extends Kernel { + public function registerBundles(): iterable + { + yield new BundleAsCompilerPassBundle(); + } + + public function registerContainerConfiguration(LoaderInterface $loader): void + { + } + + public function getProjectDir(): string + { + return sys_get_temp_dir().'/bundle_as_compiler_pass'; + } + }; + + $kernel->boot(); + + $this->assertTrue($kernel->getContainer()->has('foo')); + } } class NamedBundle extends Bundle diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleCompilerPass/BundleAsCompilerPassBundle.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleCompilerPass/BundleAsCompilerPassBundle.php new file mode 100644 index 0000000000000..98c66eb844b05 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/BundleCompilerPass/BundleAsCompilerPassBundle.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\Fixtures\BundleCompilerPass; + +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\HttpKernel\Bundle\AbstractBundle; + +class BundleAsCompilerPassBundle extends AbstractBundle implements CompilerPassInterface +{ + public function process(ContainerBuilder $container): void + { + $container->register('foo', \stdClass::class) + ->setPublic(true); + } +}