Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions service_container/compiler_passes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 </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 <components-di-separate-compiler-passes>`,
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) {
// ...
}
}
}

Expand Down