Skip to content

Commit da3325e

Browse files
committed
Merge pull request #1399 from richardmiller/adding_compiler_pass_constants
Added a section on registering compiler passes to the compilation article
2 parents 74b0a16 + f6dfe3d commit da3325e

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

components/dependency_injection/compilation.rst

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,12 @@ something needed in everyday use.
3131
The compiler pass must have the ``process`` method which is passed the container
3232
being compiled::
3333

34-
public function process(ContainerBuilder $container)
34+
class CustomCompilerPass
3535
{
36-
//--
36+
public function process(ContainerBuilder $container)
37+
{
38+
//--
39+
}
3740
}
3841

3942
The container's parameters and definitions can be manipulated using the
@@ -42,6 +45,43 @@ One common thing to do in a compiler pass is to search for all services that
4245
have a certain tag in order to process them in some way or dynamically plug
4346
each into some other service.
4447

48+
Registering a Compiler Pass
49+
---------------------------
50+
51+
You need to register your custom pass with the container, its process method
52+
will then be called when the container is compiled::
53+
54+
use Symfony\Component\DependencyInjection\ContainerBuilder;
55+
56+
$container = new ContainerBuilder();
57+
$container->addCompilerPass(new CustomCompilerPass);
58+
59+
Controlling the Pass Ordering
60+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
61+
62+
The default compiler passes are grouped into optimization passes and removal
63+
passes. The optimization passes run first and include tasks such as resolving
64+
references within the definitions. The removal passes perform tasks such removing
65+
private aliases and unused services. You can choose where in the order any custom
66+
passes you add are run. By default they will be run before the optimization passes.
67+
68+
You can use the following constants as the second argument when registering
69+
a pass with the container to control where it goes in the order:
70+
71+
* ``PassConfig::TYPE_BEFORE_OPTIMIZATION``
72+
* ``PassConfig::TYPE_OPTIMIZE``
73+
* ``PassConfig::TYPE_BEFORE_REMOVING``
74+
* ``PassConfig::TYPE_REMOVE``
75+
* ``PassConfig::TYPE_AFTER_REMOVING``
76+
77+
For example, to run your custom pass after the default removal passes have been run::
78+
79+
use Symfony\Component\DependencyInjection\ContainerBuilder;
80+
81+
$container = new ContainerBuilder();
82+
$container->addCompilerPass(new CustomCompilerPass, PassConfig::TYPE_AFTER_REMOVING);
83+
84+
4585
Managing Configuration with Extensions
4686
--------------------------------------
4787

0 commit comments

Comments
 (0)