@@ -306,46 +306,85 @@ For more details, see :doc:`/cookbook/bundles/prepend_extension`, which
306306is specific to the Symfony Framework, but contains more details about this
307307feature.
308308
309- Creating a Compiler Pass
310- ------------------------
309+ .. _components-di-compiler-pass :
311310
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.
311+ Execute Code During Compilation
312+ -------------------------------
318313
319- The compiler pass must have the ``process `` method which is passed the container
320- being compiled::
314+ You can also execute custom code during compilation by writing your own
315+ compiler pass. By implementing
316+ :class: `Symfony\\ Component\\ DependencyInjection\\ Compiler\\ CompilerPassInterface `
317+ in your extension, the added ``process() `` method will be called during
318+ compilation::
321319
320+ // ...
322321 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
323- use Symfony\Component\DependencyInjection\ContainerBuilder;
324322
325- class CustomCompilerPass implements CompilerPassInterface
323+ class AcmeDemoExtension implements ExtensionInterface, CompilerPassInterface
326324 {
327325 public function process(ContainerBuilder $container)
328326 {
329- // ...
327+ // ... do something during the compilation
330328 }
329+
330+ // ...
331331 }
332332
333+ .. versionadded :: 2.8
334+ Prior to Symfony 2.8, extensions implementing ``CompilerPassInterface ``
335+ were not automatically registered. You need to register it as explained in
336+ :ref: `the next section <components-di-separate-compiler-passes >`.
337+
338+ As ``process() `` is called *after * all extensions are loaded, it allows you to
339+ edit service definitions of other extensions as well as retrieving information
340+ about service definitions.
341+
333342The 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.
343+ methods described in :doc: `/components/dependency_injection/definitions `.
344+
345+ .. note ::
346+
347+ As a rule, only work with services definition in a compiler pass and do not
348+ create service instances. Practically, this means using methods ``has() ``,
349+ ``findDefinition() ``, ``getDefinition() ``, ``setDefinition() ``, etc.
350+ instead of ``get() ``, ``set() ``, etc.
351+
352+ .. tip ::
353+
354+ Make sure your compiler pass does not require services to exist. Abort the
355+ method call if some required service is not available.
356+
357+ A common use-case of compiler passes is to search for all service definitions
358+ that have a certain tag in order to process dynamically plug each into some
359+ other service. See the section on :ref: `service tags <components-di-compiler-pass-tags >`
360+ for an example.
361+
362+ .. _components-di-separate-compiler-passes :
338363
339- Registering a Compiler Pass
340- ---------------------------
364+ Creating Separate Compiler Passes
365+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
341366
342- You need to register your custom pass with the container. Its process method
343- will then be called when the container is compiled::
367+ Sometimes, you need to do more than one thing during compliation or want to use
368+ compiler passes without an extension. In this case, you can create a new class
369+ implementing the ``CompilerPassInterface ``::
370+
371+ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
372+ use Symfony\Component\DependencyInjection\ContainerBuilder;
373+
374+ class CustomPass implements CompilerPassInterface
375+ {
376+ public function process(ContainerBuilder $container)
377+ {
378+ // ... do something during the compilation
379+ }
380+ }
381+
382+ You then need to register your custom pass with the container::
344383
345384 use Symfony\Component\DependencyInjection\ContainerBuilder;
346385
347386 $container = new ContainerBuilder();
348- $container->addCompilerPass(new CustomCompilerPass );
387+ $container->addCompilerPass(new CustomPass() );
349388
350389.. note ::
351390
@@ -354,17 +393,16 @@ will then be called when the container is compiled::
354393 more details.
355394
356395Controlling the Pass Ordering
357- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
396+ .............................
358397
359398The default compiler passes are grouped into optimization passes and removal
360399passes. The optimization passes run first and include tasks such as resolving
361400references 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.
401+ as removing private aliases and unused services. When registering compiler
402+ passes using `` addCompilerPass() ``, you can configure when your compiler pass
403+ is run. By default, they are run before the optimization passes.
365404
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:
405+ You can use the following constants to determine when your pass is executed:
368406
369407* ``PassConfig::TYPE_BEFORE_OPTIMIZATION ``
370408* ``PassConfig::TYPE_OPTIMIZE ``
@@ -373,14 +411,11 @@ a pass with the container to control where it goes in the order:
373411* ``PassConfig::TYPE_AFTER_REMOVING ``
374412
375413For example, to run your custom pass after the default removal passes have
376- been run::
414+ been run, use ::
377415
378- use Symfony\Component\DependencyInjection\ContainerBuilder;
379- use Symfony\Component\DependencyInjection\Compiler\PassConfig;
380-
381- $container = new ContainerBuilder();
416+ // ...
382417 $container->addCompilerPass(
383- new CustomCompilerPass ,
418+ new CustomPass() ,
384419 PassConfig::TYPE_AFTER_REMOVING
385420 );
386421
0 commit comments