View source
<?php
namespace Drupal\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Contracts\Service\ResetInterface;
class Container implements ContainerInterface, ResetInterface {
protected $parameters = [];
protected $aliases = [];
protected $serviceDefinitions = [];
protected $services = [];
protected $privateServices = [];
protected $loading = [];
protected $frozen = TRUE;
public function __construct(array $container_definition = []) {
if (!empty($container_definition) && (!isset($container_definition['machine_format']) || $container_definition['machine_format'] !== TRUE)) {
throw new InvalidArgumentException('The non-optimized format is not supported by this class. Use an optimized machine-readable format instead, e.g. as produced by \\Drupal\\Component\\DependencyInjection\\Dumper\\OptimizedPhpArrayDumper.');
}
$this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : [];
$this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : [];
$this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : [];
$this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE;
$this->services['service_container'] = $this;
}
public function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
if ($this
->hasParameter('_deprecated_service_list')) {
if ($deprecation = $this
->getParameter('_deprecated_service_list')[$id] ?? '') {
@trigger_error($deprecation, E_USER_DEPRECATED);
}
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
if (isset($this->services[$id]) || $invalid_behavior === ContainerInterface::NULL_ON_INVALID_REFERENCE && array_key_exists($id, $this->services)) {
return $this->services[$id];
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}
$definition = isset($this->serviceDefinitions[$id]) ? $this->serviceDefinitions[$id] : NULL;
if (!$definition && $invalid_behavior === ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
if (!$id) {
throw new ServiceNotFoundException('');
}
throw new ServiceNotFoundException($id, NULL, NULL, $this
->getServiceAlternatives($id));
}
if (!$definition) {
return;
}
if (isset($definition[0])) {
$definition = unserialize($definition);
}
$this->loading[$id] = TRUE;
try {
$service = $this
->createService($definition, $id);
} catch (\Exception $e) {
unset($this->loading[$id]);
unset($this->services[$id]);
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) {
return;
}
throw $e;
}
unset($this->loading[$id]);
return $service;
}
public function reset() {
if (!empty($this->scopedServices)) {
throw new LogicException('Resetting the container is not allowed when a scope is active.');
}
$this->services = [];
}
protected function createService(array $definition, $id) {
if (isset($definition['synthetic']) && $definition['synthetic'] === TRUE) {
throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The service container does not know how to construct this service. The service will need to be set before it is first used.', $id));
}
$arguments = [];
if (isset($definition['arguments'])) {
$arguments = $definition['arguments'];
if ($arguments instanceof \stdClass) {
$arguments = $this
->resolveServicesAndParameters($arguments);
}
}
if (isset($definition['file'])) {
$file = $this->frozen ? $definition['file'] : current($this
->resolveServicesAndParameters([
$definition['file'],
]));
require_once $file;
}
if (isset($definition['factory'])) {
$factory = $definition['factory'];
if (is_array($factory)) {
$factory = $this
->resolveServicesAndParameters([
$factory[0],
$factory[1],
]);
}
elseif (!is_string($factory)) {
throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
}
$service = call_user_func_array($factory, $arguments);
}
else {
$class = $this->frozen ? $definition['class'] : current($this
->resolveServicesAndParameters([
$definition['class'],
]));
$service = new $class(...$arguments);
}
if (!isset($definition['shared']) || $definition['shared'] !== FALSE) {
$this->services[$id] = $service;
}
if (isset($definition['calls'])) {
foreach ($definition['calls'] as $call) {
$method = $call[0];
$arguments = [];
if (!empty($call[1])) {
$arguments = $call[1];
if ($arguments instanceof \stdClass) {
$arguments = $this
->resolveServicesAndParameters($arguments);
}
}
call_user_func_array([
$service,
$method,
], $arguments);
}
}
if (isset($definition['properties'])) {
if ($definition['properties'] instanceof \stdClass) {
$definition['properties'] = $this
->resolveServicesAndParameters($definition['properties']);
}
foreach ($definition['properties'] as $key => $value) {
$service->{$key} = $value;
}
}
if (isset($definition['configurator'])) {
$callable = $definition['configurator'];
if (is_array($callable)) {
$callable = $this
->resolveServicesAndParameters($callable);
}
if (!is_callable($callable)) {
throw new InvalidArgumentException(sprintf('The configurator for class "%s" is not a callable.', get_class($service)));
}
call_user_func($callable, $service);
}
return $service;
}
public function set($id, $service) {
$this->services[$id] = $service;
}
public function has($id) {
return isset($this->aliases[$id]) || isset($this->services[$id]) || isset($this->serviceDefinitions[$id]);
}
public function getParameter($name) {
if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
if (!$name) {
throw new ParameterNotFoundException('');
}
throw new ParameterNotFoundException($name, NULL, NULL, NULL, $this
->getParameterAlternatives($name));
}
return $this->parameters[$name];
}
public function hasParameter($name) {
return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
}
public function setParameter($name, $value) {
if ($this->frozen) {
throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
}
$this->parameters[$name] = $value;
}
public function initialized($id) {
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
return isset($this->services[$id]) || array_key_exists($id, $this->services);
}
protected function resolveServicesAndParameters($arguments) {
if ($arguments instanceof \stdClass) {
if ($arguments->type !== 'collection') {
throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $arguments->type));
}
if (!$arguments->resolve) {
return $arguments->value;
}
$arguments = $arguments->value;
}
foreach ($arguments as $key => $argument) {
if ($argument instanceof \stdClass) {
$type = $argument->type;
if ($type == 'parameter') {
$name = $argument->name;
if (!isset($this->parameters[$name])) {
$arguments[$key] = $this
->getParameter($name);
}
$argument = $arguments[$key] = $this->parameters[$name];
if (!$argument instanceof \stdClass) {
continue;
}
$type = $argument->type;
}
if ($type == 'service') {
$id = $argument->id;
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
if (isset($this->services[$id])) {
$arguments[$key] = $this->services[$id];
continue;
}
$arguments[$key] = $this
->get($id, $argument->invalidBehavior);
continue;
}
elseif ($type == 'private_service') {
$id = $argument->id;
if (isset($this->privateServices[$id])) {
$arguments[$key] = $this->privateServices[$id];
continue;
}
$arguments[$key] = $this
->createService($argument->value, $id);
if ($argument->shared) {
$this->privateServices[$id] = $arguments[$key];
}
continue;
}
elseif ($type == 'collection') {
$value = $argument->value;
if ($argument->resolve) {
$arguments[$key] = $this
->resolveServicesAndParameters($value);
}
else {
$arguments[$key] = $value;
}
continue;
}
elseif ($type == 'raw') {
$arguments[$key] = $argument->value;
continue;
}
if ($type !== NULL) {
throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type));
}
}
}
return $arguments;
}
protected function getAlternatives($search_key, array $keys) {
$alternatives = [];
foreach ($keys as $key) {
$lev = levenshtein($search_key, $key);
if ($lev <= strlen($search_key) / 3 || strpos($key, $search_key) !== FALSE) {
$alternatives[] = $key;
}
}
return $alternatives;
}
protected function getServiceAlternatives($id) {
$all_service_keys = array_unique(array_merge(array_keys($this->services), array_keys($this->serviceDefinitions)));
return $this
->getAlternatives($id, $all_service_keys);
}
protected function getParameterAlternatives($name) {
return $this
->getAlternatives($name, array_keys($this->parameters));
}
public function getServiceIds() {
return array_keys($this->serviceDefinitions + $this->services);
}
private function __clone() {
}
}