@@ -67,52 +67,55 @@ method of the application kernel::
6767Working with Compiler Passes in Bundles
6868---------------------------------------
6969
70- If your compiler pass is relatively small, you can add it directly in the main
71- bundle class. To do so, make your bundle implement the
72- :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface `
73- and place the compiler pass code inside the ``process() `` method of the main
74- bundle class::
70+ :doc: `Bundles </bundles >` can define compiler passes in the ``build() `` method of
71+ the main bundle class (this is not needed when implementing the ``process() ``
72+ method in the extension)::
7573
7674 // src/MyBundle/MyBundle.php
7775 namespace App\MyBundle;
7876
7977 use App\DependencyInjection\Compiler\CustomPass;
80- use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
8178 use Symfony\Component\DependencyInjection\ContainerBuilder;
8279 use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
8380
84- class MyBundle extends AbstractBundle implements CompilerPassInterface
81+ class MyBundle extends AbstractBundle
8582 {
86- public function process (ContainerBuilder $container): void
83+ public function build (ContainerBuilder $container): void
8784 {
88- // in this method you can manipulate the service container:
89- // for example, changing some container service:
90- $container->getDefinition('app.some_private_service')->setPublic(true);
91-
92- // or processing tagged services:
93- foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) {
94- // ...
95- }
85+ $container->addCompilerPass(new CustomPass());
9686 }
9787 }
9888
99- Alternatively, when using :ref: `separate compiler pass classes <components-di-separate-compiler-passes >`,
100- bundles can enable them in the ``build() `` method of their main bundle class::
89+ If your compiler pass is relatively small, you can make the main bundle class implements
90+ :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface ` so that
91+ it can add itself::
10192
10293 // src/MyBundle/MyBundle.php
10394 namespace App\MyBundle;
10495
10596 use App\DependencyInjection\Compiler\CustomPass;
97+ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10698 use Symfony\Component\DependencyInjection\ContainerBuilder;
10799 use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
108100
109- class MyBundle extends AbstractBundle
101+ class MyBundle extends AbstractBundle implements CompilerPassInterface
110102 {
103+
111104 public function build(ContainerBuilder $container): void
112105 {
113- parent::build($container);
106+ $container->addCompilerPass($this);
107+ }
114108
115- $container->addCompilerPass(new CustomPass());
109+ public function process(ContainerBuilder $container): void
110+ {
111+ // in this method you can manipulate the service container:
112+ // for example, changing some container service:
113+ $container->getDefinition('app.some_private_service')->setPublic(true);
114+
115+ // or processing tagged services:
116+ foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) {
117+ // ...
118+ }
116119 }
117120 }
118121
0 commit comments