Are you missing the Symfony's dependency injection container? This is what you're looking for!

Usage

To start using this package you must install it via Composer. Thereafter add the next line to the *.info file of the module which is going to populate dependencies to the container:

container = TRUE

Now you're good to go and declare services in a *.services.yml within the module (exactly the same as in Drupal 8).

Besides of that, every module can have a service provider, which must implement the Drupal\container\ServiceProviderInterface, be named as MyModuleNameServiceProvider and be in the Drupal\my_module_name namespace (again, the same as in Drupal 8).

Important recommendation

Set the following configuration to your development settings to reflect the changes of services in YMLs and/or service providers immediately:

// Keep DI container up to date during development.
$conf['container_debug'] = TRUE;

Production environment

During development container tracks the modifications of itself and keeps compiled version up to date. To do this it uses a lot of resource trackers which may produce the overhead to your application.

For any sort of stable environments (production, live or whatever you call it) the container must be treated as finalized and be represented by a single file, which must be deployed alongside with the application (commit the container.php to the VCS once ready, for instance).

What is in the container out of the box?

- The modules parameter which contains a list of modules that uses the container (keys are module names, values - relative paths).
- The validator service allowing you to use the Validator Component of Symfony.
- The annotation.reader and the annotation.discovery services.

A bare minimum, as you can see.

Factory of tagged services

The definition inside of the YAML.

services:
  name.factory:
    class: Drupal\module_name\Factory
    tags:
      - name: service_collector
        call: addChild
        tag: factory_identifier

  name.factory.child:
    class: Drupal\module_name\FactoryChild
    tags:
      - name: factory_identifier
        priority: 0

As a result, all services having the factory_identifier tag will be passed to addChild method of the name.factory.

Supporting organizations: 

Project information

  • caution Maintenance fixes only
    Considered feature-complete by its maintainers.
  • Module categories: Developer Tools
  • Created by BR0kEN on , updated
  • shieldStable releases for this project are covered by the security advisory policy.
    There are currently no supported stable releases.

Releases