@@ -31,9 +31,12 @@ something needed in everyday use.
31
31
The compiler pass must have the ``process `` method which is passed the container
32
32
being compiled::
33
33
34
- public function process(ContainerBuilder $container)
34
+ class CustomCompilerPass
35
35
{
36
- //--
36
+ public function process(ContainerBuilder $container)
37
+ {
38
+ //--
39
+ }
37
40
}
38
41
39
42
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
42
45
have a certain tag in order to process them in some way or dynamically plug
43
46
each into some other service.
44
47
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
+
45
85
Managing Configuration with Extensions
46
86
--------------------------------------
47
87
0 commit comments