|
|
9 лет назад | |
|---|---|---|
| .. | ||
| lib | 9 лет назад | |
| modules | 9 лет назад | |
| src | 9 лет назад | |
| tests | 9 лет назад | |
| .travis.yml | 9 лет назад | |
| HACK.md | 9 лет назад | |
| LICENSE.txt | 9 лет назад | |
| README.md | 9 лет назад | |
| composer.json | 9 лет назад | |
| service_container.info | 9 лет назад | |
| service_container.install | 9 лет назад | |
| service_container.module | 9 лет назад | |
Service Container is an API module based on ctools to enable a Drupal 7 quick and easy lightweight service container with 100% unit test coverage.
It is very similar in syntax to a Symfony container, but was written from scratch as a symfony dependency was not wanted - using some of Drupal 8 Core and Component directly. They will likely depend on a drupal8core project in the future - but for now the copy is fine.
This allows to use an extensible service container (like in Drupal 8) and write modules in Drupal 7 as if they were using Drupal 8.
The main benefit is being able to use unit testing but also to write Drupal 7 module with Drupal 8 style of coding in mind.
The module uses PHP Unit and travis.yml, but the tests and a composer.json are isolated in the tests/ directory, so no vendor or composer multi map is needed by default.
It was originally written for the render_cache module, but since then others have expressed interest in using it, so it was split it out and made a dependency of render_cache instead.
You need:
or any other PSR-4 autoloader.
By default the service_container supports CTools discovery, to register your plugins all you need to do is:
$parameters['ctools_plugins_auto_discovery']['render_cache'] = TRUE
And you can then get a plugin via:
$rcc = \Drupal::service('render_cache.controller')->createInstance('block');
Because the plugin managers implement the whole discovery interface, you can get all definitions with ease.
$plugins = \Drupal::service('render_cache.controller')->getDefinitions();
Your plugin itself looks like:
cat modules/render_cache_block/src/RenderCache/Controller/block.inc
$plugin = array(
'class' => "\\Drupal\\render_cache_block\\RenderCache\\Controller\\BlockController",
'arguments' => array('@render_stack', '@render_cache.cache'),
);
So you can use normal container parameter syntax.
See the file HACK.md for more details.