@@ -67,52 +67,59 @@ 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);
85+ parent::build($container);
9186
92- // or processing tagged services:
93- foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) {
94- // ...
95- }
87+ $container->addCompilerPass(new CustomPass());
9688 }
9789 }
9890
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::
91+ If your compiler pass is relatively small, you can make the main bundle class implements
92+ :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface ` so that
93+ it can add itself::
10194
10295 // src/MyBundle/MyBundle.php
10396 namespace App\MyBundle;
10497
10598 use App\DependencyInjection\Compiler\CustomPass;
99+ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
106100 use Symfony\Component\DependencyInjection\ContainerBuilder;
107101 use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
108102
109- class MyBundle extends AbstractBundle
103+ class MyBundle extends AbstractBundle implements CompilerPassInterface
110104 {
105+
111106 public function build(ContainerBuilder $container): void
112107 {
113108 parent::build($container);
114109
115- $container->addCompilerPass(new CustomPass());
110+ $container->addCompilerPass($this);
111+ }
112+
113+ public function process(ContainerBuilder $container): void
114+ {
115+ // in this method you can manipulate the service container:
116+ // for example, changing some container service:
117+ $container->getDefinition('app.some_private_service')->setPublic(true);
118+
119+ // or processing tagged services:
120+ foreach ($container->findTaggedServiceIds('some_tag') as $id => $tags) {
121+ // ...
122+ }
116123 }
117124 }
118125
0 commit comments