|
| 1 | +Compiling the Container |
| 2 | +======================= |
| 3 | + |
| 4 | +The service container can be compiled for various reasons. These reasons |
| 5 | +include checking for any potential issues such as circular references and |
| 6 | +making the container more efficient by resolving parameters and removing |
| 7 | +unused services. |
| 8 | + |
| 9 | +It is compiled by running:: |
| 10 | + |
| 11 | + $container->compile(); |
| 12 | + |
| 13 | +The compile method uses ``Compiler Passes`` for the compilation. The ``Dependency Injection`` |
| 14 | +component comes with several passes which are automatically registered for |
| 15 | +compilation. For example the :class:`Symfony\\Component\\DependencyInjection\\Compiler\\CheckDefinitionValidityPass` |
| 16 | +checks for various potential issues with the definitions that have been set |
| 17 | +in the container. After this and several other passes are used to check |
| 18 | +the container's validity, further compiler passes are used for various tasks to optimize |
| 19 | +the configuration before it is cached. For example, private services and |
| 20 | +abstract services are removed, and aliases are resolved. |
| 21 | + |
| 22 | +Creating a Compiler Pass |
| 23 | +------------------------ |
| 24 | + |
| 25 | +You can also create and register your own compiler passes with the container. |
| 26 | +To create a compiler pass it needs to implements the :class:`Symfony\\Component\\DependencyInjection\\Compiler\\CompilerPassInterface` |
| 27 | +interface. The compiler gives you an opportunity to manipulate the service |
| 28 | +definitions that have been compiled. Hence this being not something needed |
| 29 | +in everyday use. |
| 30 | + |
| 31 | +The compiler pass must have the ``process`` method which is passed the container being compiled:: |
| 32 | + |
| 33 | + public function process(ContainerBuilder $container) |
| 34 | + { |
| 35 | + //-- |
| 36 | + } |
| 37 | + |
| 38 | +The container's parameters and definitions can be manipulated using the |
| 39 | +methods described in the :doc:`/components/dependency_injection/definitions`. |
| 40 | + |
| 41 | +Managing Configuration with Extensions |
| 42 | +-------------------------------------- |
| 43 | + |
| 44 | +As well as loading configuration directly into the container as shown in |
| 45 | +:doc:`/components/dependency_injection/introduction` you can manage it by registering |
| 46 | +extensions with the container. The extensions must implement :class:`Symfony\\Component\\DependencyInjection\\Extension\\ExtensionInterface` |
| 47 | +and can be registered with the container with:: |
| 48 | + |
| 49 | + $container->registerExtension($extension); |
| 50 | + |
| 51 | +The main work of the extension is done in the ``load`` method. In the load method |
| 52 | +you can load configuration from one or more configuration files as well as |
| 53 | +manipulating the container definitions using the methods shown in :doc:`/components/dependency_injection/definitions`. |
| 54 | + |
| 55 | +The ``load`` is passed a fresh container to set up which is then merged into |
| 56 | +the container it is registered with. This allows you to have several extension |
| 57 | +managing container definitions independently. The extensions do not add |
| 58 | +to the containers configuration when they are added but are processed when |
| 59 | +the container's ``compile`` method is called. |
| 60 | + |
| 61 | +.. note:: |
| 62 | + |
| 63 | + If you need to manipulate the configuration loaded by an extension then |
| 64 | + you cannot do it from another extension as it uses a fresh container. |
| 65 | + You should instead use a compiler pass which works with the full container |
| 66 | + after the extension have been processed. |
| 67 | + |
0 commit comments