From d4bfcb2b87bd096388c9a259f9e63720d5d511b4 Mon Sep 17 00:00:00 2001 From: matlec Date: Wed, 17 Dec 2025 13:07:08 +0100 Subject: [PATCH] Fix mention of `CompilerPassInterface` support for bundles --- service_container/compiler_passes.rst | 45 ++++++++++++++------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/service_container/compiler_passes.rst b/service_container/compiler_passes.rst index 096c60c2642..cec6b3d0e16 100644 --- a/service_container/compiler_passes.rst +++ b/service_container/compiler_passes.rst @@ -67,52 +67,55 @@ method of the application kernel:: Working with Compiler Passes in Bundles --------------------------------------- -If your compiler pass is relatively small, you can add it directly in the main -bundle class. To do so, make your bundle implement the -:class:`Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface` -and place the compiler pass code inside the ``process()`` method of the main -bundle class:: +:doc:`Bundles ` can define compiler passes in the ``build()`` method of +the main bundle class (this is not needed when implementing the ``process()`` +method in the extension):: // src/MyBundle/MyBundle.php namespace App\MyBundle; use App\DependencyInjection\Compiler\CustomPass; - use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; - class MyBundle extends AbstractBundle implements CompilerPassInterface + class MyBundle extends AbstractBundle { - public function process(ContainerBuilder $container): void + public function build(ContainerBuilder $container): void { - // in this method you can manipulate the service container: - // for example, changing some container service: - $container->getDefinition('app.some_private_service')->setPublic(true); - - // or processing tagged services: - foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) { - // ... - } + $container->addCompilerPass(new CustomPass()); } } -Alternatively, when using :ref:`separate compiler pass classes `, -bundles can enable them in the ``build()`` method of their main bundle class:: +If your compiler pass is relatively small, you can make the main bundle class implements +:class:`Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface` so that +it can add itself:: // src/MyBundle/MyBundle.php namespace App\MyBundle; use App\DependencyInjection\Compiler\CustomPass; + use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; - class MyBundle extends AbstractBundle + class MyBundle extends AbstractBundle implements CompilerPassInterface { + public function build(ContainerBuilder $container): void { - parent::build($container); + $container->addCompilerPass($this); + } - $container->addCompilerPass(new CustomPass()); + public function process(ContainerBuilder $container): void + { + // in this method you can manipulate the service container: + // for example, changing some container service: + $container->getDefinition('app.some_private_service')->setPublic(true); + + // or processing tagged services: + foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) { + // ... + } } }