Put simply, a Dependency Injection Container alleviates the need for factories and the use of new in your code. Think of DiC as the new new. You will still need to write factories in some cases, but your code will not depend directly on them. Your code will be easier to change, unit test and reuse in other contexts.
This module enables Symfony's Dependency Injection component for Drupal 7 modules.
- Add a dependency on this module to your module's .info file:
dependencies = inject
- Thick Configuration > Performance > Dump configuration to disk checkbox to cache the dumped version of the container to disk.
- Thick Configuration > Performance > Compile configuration checkbox to compile the container before dumping.
The module comes with container dumping activated by default. In a developer environment you may deactivate this functionality to debug the container building process. Deactivating this feature adds a substantial performance hit as more meta-data (Yaml files, config, ...) has to be loaded into memory than might actually be necessary. This may not be relevant to scenarios where caching is in place, however hits hardly in scenarios where no caching is used.
New in 7.x-2.0-beta7: You can now register services with Yaml files à-la Drupal 8. Name your file
$module.services.yml and register your parameters and services directly in Yaml. See the
test_inject.services.yml file provided as an example.
Deprecated: hook_inject_boot and hook_inject_init has been deprecated in favor of a unique hook_inject_build. This is more in-line with Drupal 8 where you register and build your container in Bundle through a unique method build. The two former hook will be removed in the 2.0 stable release!
Functional tests has been added in this release, you can see them in the Automated Test tab of project.
For a real example of a module leveraging the dependency overriding mechanism, you might have a look at Doctrine module. The Entity integration module overrides the doctrine.orm.driver to support both Schema API and Annotation Driven entities.