ContainerBuilder.php in Service Container 7
File
src/DependencyInjection/ContainerBuilder.php
View source
<?php
namespace Drupal\service_container\DependencyInjection;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Utility\NestedArray;
class ContainerBuilder implements ContainerBuilderInterface {
protected $serviceProviderManager;
protected $containerClass = '\\Drupal\\service_container\\DependencyInjection\\Container';
public function __construct(PluginManagerInterface $service_provider_manager) {
$this->serviceProviderManager = $service_provider_manager;
}
public function getContainerDefinition() {
$definitions = $this->serviceProviderManager
->getDefinitions();
$container_definition = array();
$service_providers = array();
foreach ($definitions as $plugin_id => $definition) {
$service_providers[$plugin_id] = $this->serviceProviderManager
->createInstance($plugin_id);
}
foreach ($definitions as $plugin_id => $definition) {
$service_provider = $service_providers[$plugin_id];
$container_definition = NestedArray::mergeDeep($container_definition, $service_provider
->getContainerDefinition());
}
$container_definition += array(
'services' => array(),
'parameters' => array(),
);
$container_definition['tags'] = array();
foreach ($container_definition['services'] as $service => $definition) {
if (isset($definition['tags'])) {
foreach ($definition['tags'] as $tag) {
$tag_name = $tag['name'];
unset($tag['name']);
$container_definition['tags'][$tag_name][$service][] = $tag;
}
}
}
foreach ($definitions as $plugin_id => $definition) {
$service_provider = $service_providers[$plugin_id];
$service_provider
->alterContainerDefinition($container_definition);
}
$this
->moduleAlter($container_definition);
unset($container_definition['tags']);
return $container_definition;
}
public function compile() {
$definition = $this
->getContainerDefinition();
if (!empty($definition['services']['service_container']['class'])) {
$this->containerClass = $definition['services']['service_container']['class'];
}
return new $this->containerClass($definition);
}
protected function moduleAlter(&$container_definition) {
drupal_alter('service_container_container_build', $container_definition);
}
}
Classes
Name |
Description |
ContainerBuilder |
ContainerBuilder retrieves container definitions from service providers to
build a Container. |