Skip to content

Commit 353df25

Browse files
committed
Document automatic registration of extension compiler passes
1 parent 3ebf2d0 commit 353df25

File tree

3 files changed

+107
-52
lines changed

3 files changed

+107
-52
lines changed

Diff for: components/dependency_injection/compilation.rst

+78-34
Original file line numberDiff line numberDiff line change
@@ -306,46 +306,94 @@ For more details, see :doc:`/cookbook/bundles/prepend_extension`, which
306306
is specific to the Symfony Framework, but contains more details about this
307307
feature.
308308

309-
Creating a Compiler Pass
310-
------------------------
309+
.. _creating-a-compiler-pass:
310+
.. _components-di-compiler-pass:
311311

312-
You can also create and register your own compiler passes with the container.
313-
To create a compiler pass it needs to implement the
314-
:class:`Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface`
315-
interface. The compiler pass gives you an opportunity to manipulate the
316-
service definitions that have been compiled. This can be very powerful,
317-
but is not something needed in everyday use.
312+
Execute Code During Compilation
313+
-------------------------------
318314

319-
The compiler pass must have the ``process`` method which is passed the container
320-
being compiled::
315+
You can also execute custom code during compilation by writing your own
316+
compiler pass. By implementing
317+
:class:`Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface`
318+
in your extension, the added ``process()`` method will be called during
319+
compilation::
321320

321+
// ...
322322
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
323-
use Symfony\Component\DependencyInjection\ContainerBuilder;
324323

325-
class CustomCompilerPass implements CompilerPassInterface
324+
class AcmeDemoExtension implements ExtensionInterface, CompilerPassInterface
326325
{
327326
public function process(ContainerBuilder $container)
328327
{
329-
// ...
328+
// ... do something during the compilation
330329
}
330+
331+
// ...
331332
}
332333

334+
.. versionadded:: 2.8
335+
Prior to Symfony 2.8, extensions implementing ``CompilerPassInterface``
336+
were not automatically registered. You needed to register them as explained
337+
in :ref:`the next section <components-di-separate-compiler-passes>`.
338+
339+
As ``process()`` is called *after* all extensions are loaded, it allows you to
340+
edit service definitions of other extensions as well as retrieving information
341+
about service definitions.
342+
333343
The container's parameters and definitions can be manipulated using the
334-
methods described in the :doc:`/components/dependency_injection/definitions`.
335-
One common thing to do in a compiler pass is to search for all services
336-
that have a certain tag in order to process them in some way or dynamically
337-
plug each into some other service.
344+
methods described in :doc:`/components/dependency_injection/definitions`.
345+
346+
.. note::
347+
348+
Please note that the ``process()`` method in the extension class is
349+
called during the optimization step. You can read
350+
:ref:`the next section <components-di-separate-compiler-passes>` if you
351+
need to edit the container during another step.
352+
353+
.. note::
354+
355+
As a rule, only work with services definition in a compiler pass and do not
356+
create service instances. In practice, this means using the methods
357+
``has()``, ``findDefinition()``, ``getDefinition()``, ``setDefinition()``,
358+
etc. instead of ``get()``, ``set()``, etc.
359+
360+
.. tip::
361+
362+
Make sure your compiler pass does not require services to exist. Abort the
363+
method call if some required service is not available.
338364

339-
Registering a Compiler Pass
340-
---------------------------
365+
A common use-case of compiler passes is to search for all service definitions
366+
that have a certain tag in order to process dynamically plug each into some
367+
other service. See the section on :ref:`service tags <components-di-compiler-pass-tags>`
368+
for an example.
341369

342-
You need to register your custom pass with the container. Its process method
343-
will then be called when the container is compiled::
370+
.. _components-di-separate-compiler-passes:
371+
372+
Creating Separate Compiler Passes
373+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
374+
375+
Sometimes, you need to do more than one thing during compliation, want to use
376+
compiler passes without an extension or you need to execute some code at
377+
another step in the compilation process. In these cases, you can create a new
378+
class implementing the ``CompilerPassInterface``::
379+
380+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
381+
use Symfony\Component\DependencyInjection\ContainerBuilder;
382+
383+
class CustomPass implements CompilerPassInterface
384+
{
385+
public function process(ContainerBuilder $container)
386+
{
387+
// ... do something during the compilation
388+
}
389+
}
390+
391+
You then need to register your custom pass with the container::
344392

345393
use Symfony\Component\DependencyInjection\ContainerBuilder;
346394

347395
$container = new ContainerBuilder();
348-
$container->addCompilerPass(new CustomCompilerPass);
396+
$container->addCompilerPass(new CustomPass());
349397

350398
.. note::
351399

@@ -354,17 +402,16 @@ will then be called when the container is compiled::
354402
more details.
355403

356404
Controlling the Pass Ordering
357-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
405+
.............................
358406

359407
The default compiler passes are grouped into optimization passes and removal
360408
passes. The optimization passes run first and include tasks such as resolving
361409
references within the definitions. The removal passes perform tasks such
362-
as removing private aliases and unused services. You can choose where in
363-
the order any custom passes you add are run. By default they will be run
364-
before the optimization passes.
410+
as removing private aliases and unused services. When registering compiler
411+
passes using ``addCompilerPass()``, you can configure when your compiler pass
412+
is run. By default, they are run before the optimization passes.
365413

366-
You can use the following constants as the second argument when registering
367-
a pass with the container to control where it goes in the order:
414+
You can use the following constants to determine when your pass is executed:
368415

369416
* ``PassConfig::TYPE_BEFORE_OPTIMIZATION``
370417
* ``PassConfig::TYPE_OPTIMIZE``
@@ -373,14 +420,11 @@ a pass with the container to control where it goes in the order:
373420
* ``PassConfig::TYPE_AFTER_REMOVING``
374421

375422
For example, to run your custom pass after the default removal passes have
376-
been run::
423+
been run, use::
377424

378-
use Symfony\Component\DependencyInjection\ContainerBuilder;
379-
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
380-
381-
$container = new ContainerBuilder();
425+
// ...
382426
$container->addCompilerPass(
383-
new CustomCompilerPass,
427+
new CustomPass(),
384428
PassConfig::TYPE_AFTER_REMOVING
385429
);
386430

Diff for: components/dependency_injection/tags.rst

+15-6
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@ Notice that each was given a tag named ``acme_mailer.transport``. This is
117117
the custom tag that you'll use in your compiler pass. The compiler pass
118118
is what makes this tag "mean" something.
119119

120-
Create a ``CompilerPass``
121-
-------------------------
120+
.. _components-di-compiler-pass-tags:
121+
.. _create-a-compilerpass:
122122

123-
Your compiler pass can now ask the container for any services with the
124-
custom tag::
123+
Create a Compiler Pass
124+
----------------------
125+
126+
You can now use a :ref:`compiler pass <components-di-separate-compiler-passes>` to ask the
127+
container for any services with the ``acme_mailer.transport`` tag::
125128

126129
use Symfony\Component\DependencyInjection\ContainerBuilder;
127130
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -154,7 +157,7 @@ custom tag::
154157
The ``process()`` method checks for the existence of the ``acme_mailer.transport_chain``
155158
service, then looks for all services tagged ``acme_mailer.transport``. It
156159
adds to the definition of the ``acme_mailer.transport_chain`` service a
157-
call to ``addTransport()`` for each "acme_mailer.transport" service it has
160+
call to ``addTransport()`` for each ``acme_mailer.transport`` service it has
158161
found. The first argument of each of these calls will be the mailer transport
159162
service itself.
160163

@@ -175,6 +178,13 @@ run when the container is compiled::
175178
framework. See :doc:`/cookbook/service_container/compiler_passes` for
176179
more details.
177180

181+
.. tip::
182+
183+
When implementing the ``CompilerPassInterface`` in a service extension, you
184+
do not need to register it. See the
185+
:ref:`components documentation <components-di-compiler-pass>` for more
186+
information.
187+
178188
Adding Additional Attributes on Tags
179189
------------------------------------
180190

@@ -296,4 +306,3 @@ The double loop may be confusing. This is because a service can have more
296306
than one tag. You tag a service twice or more with the ``acme_mailer.transport``
297307
tag. The second foreach loop iterates over the ``acme_mailer.transport``
298308
tags set for the current service and gives you the attributes.
299-

Diff for: cookbook/service_container/compiler_passes.rst

+14-12
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@ How to Work with Compiler Passes in Bundles
77

88
Compiler passes give you an opportunity to manipulate other service
99
definitions that have been registered with the service container. You
10-
can read about how to create them in the components section ":doc:`/components/dependency_injection/compilation`".
11-
To register a compiler pass from a bundle you need to add it to the build
12-
method of the bundle definition class::
10+
can read about how to create them in the components section
11+
":ref:`components-di-compiler-pass`".
1312

14-
// src/Acme/MailerBundle/AcmeMailerBundle.php
15-
namespace Acme\MailerBundle;
13+
When using :ref:`separate compiler passes <components-di-separate-compiler-passes>`,
14+
you need to register them in the ``build()`` method of the bundle class (this
15+
is not needed when implementing the ``process()`` method in the extension)::
16+
17+
// src/AppBundle/AppBundle.php
18+
namespace AppBundle;
1619

1720
use Symfony\Component\HttpKernel\Bundle\Bundle;
1821
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use AppBundle\DependencyInjection\Compiler\CustomPass;
1923

20-
use Acme\MailerBundle\DependencyInjection\Compiler\CustomCompilerPass;
21-
22-
class AcmeMailerBundle extends Bundle
24+
class AppBundle extends Bundle
2325
{
2426
public function build(ContainerBuilder $container)
2527
{
2628
parent::build($container);
2729

28-
$container->addCompilerPass(new CustomCompilerPass());
30+
$container->addCompilerPass(new CustomPass());
2931
}
3032
}
3133

3234
One of the most common use-cases of compiler passes is to work with tagged services
3335
(read more about tags in the components section ":doc:`/components/dependency_injection/tags`").
3436
If you are using custom tags in a bundle then by convention, tag names consist
3537
of the name of the bundle (lowercase, underscores as separators), followed
36-
by a dot, and finally the "real" name. For example, if you want to introduce
37-
some sort of "transport" tag in your AcmeMailerBundle, you should call it
38-
``acme_mailer.transport``.
38+
by a dot and finally the "real" name. For example, if you want to introduce
39+
some sort of "mail_transport" tag in your AppBundle, you should call it
40+
``app.mail_transport``.

0 commit comments

Comments
 (0)