|
| 1 | +.. index:: |
| 2 | + single: Dependency Injection; Workflow |
| 3 | + |
| 4 | +Container Building Workflow |
| 5 | +=========================== |
| 6 | + |
| 7 | +In the preceding pages of this section, there has been little to say about |
| 8 | +where the various files and classes should be located. This is because this |
| 9 | +depends on the application, library or framework in which you want to use |
| 10 | +the container. Looking at how the container is configured and built in the |
| 11 | +Symfony2 full stack framework will help you see how this all fits together, |
| 12 | +whether you are using the full stack framework or looking to use the service |
| 13 | +container in another application. |
| 14 | + |
| 15 | +The full stack framework uses the ``HttpKernel`` component to manage the loading |
| 16 | +of the service container configuration from the application and bundles and |
| 17 | +also handles the compilation and caching. Even if you are not using ``HttpKernel``, |
| 18 | +it should give you an idea of one way of organizing configuration in a modular |
| 19 | +application. |
| 20 | + |
| 21 | +Working with cached Container |
| 22 | +----------------------------- |
| 23 | + |
| 24 | +Before building it, the kernel checks to see if a cached version of the container |
| 25 | +exists. The ``HttpKernel`` has a debug setting and if this is false, the |
| 26 | +cached version is used if it exists. If debug is true then the kernel |
| 27 | +:doc:`checks to see if configuration is fresh<components/config/caching>` |
| 28 | +and if it is, the cached version of the container is. If not then the container |
| 29 | +is built from the application-level configuration and the bundles's extension |
| 30 | +configuration. |
| 31 | + |
| 32 | +Read :ref:`Dumping the Configuration for Performance<components-dependency-injection-dumping>` |
| 33 | +for more details. |
| 34 | + |
| 35 | +Application-level Configuration |
| 36 | +------------------------------- |
| 37 | + |
| 38 | +Application level config is loaded from the ``app/config`` directory. Multiple |
| 39 | +files are loaded which are then merged when the extensions are processed. This |
| 40 | +allows for different configuration for different environments e.g. dev, prod. |
| 41 | + |
| 42 | +These files contain parameters and services that are loaded directly into |
| 43 | +the container as per :ref:`Setting Up the Container with Configuration Files<components-dependency-injection-loading-config>`. |
| 44 | +They also contain configuration that is processed by extensions as per |
| 45 | +:ref:`Managing Configuration with Extensions<components-dependency-injection-extension>`. |
| 46 | +These are considered to be bundle configuration since each bundle contains |
| 47 | +an Extension class. |
| 48 | + |
| 49 | +Bundle-level Configuration with Extensions |
| 50 | +------------------------------------------ |
| 51 | + |
| 52 | +By convention, each bundle contains an Extension class which is in the bundle's |
| 53 | +``DependencyInjection`` directory. These are registered with the ``ContainerBuilder`` |
| 54 | +when the kernel is booted. When the ``ContainerBuilder`` is :doc:`compiled<components/dependency-injection/compilation>`, |
| 55 | +the application-level configuration relevant to the bundle's extension is |
| 56 | +passed to the Extension which also usually loads its own config file(s), typically from the bundle's |
| 57 | +``Resources/config`` directory. The application-level config is usually processed |
| 58 | +with a :doc:`Configuration object<components/config/definition>` also stored |
| 59 | +in the bundle's ``DependencyInjection`` directory. |
| 60 | + |
| 61 | +Compiler passes to allow Interaction between Bundles |
| 62 | +---------------------------------------------------- |
| 63 | + |
| 64 | +:ref:`Compiler passes<components-dependency-injection-compiler-passes>` are |
| 65 | +used to allow interaction between different bundles as they cannot affect |
| 66 | +each others configuration in the extension classes. One of the main uses is |
| 67 | +to process tagged services, allowing bundles to register services to picked |
| 68 | +up by other bundles, such as Monolog loggers, Twig extensions and Data Collectors |
| 69 | +for the Web Profiler. Compiler passes are usually placed in the bundle's |
| 70 | +``DependencyInjection/Compiler`` directory. |
| 71 | + |
| 72 | +Compilation and Caching |
| 73 | +----------------------- |
| 74 | + |
| 75 | +After the compilation process has loaded the services from the configuration, |
| 76 | +extensions and the compiler passes, it is dumped so that the cache can be used |
| 77 | +next time. The dumped version is then used during subsequent request as it |
| 78 | +is more efficient. |
0 commit comments