View source
<?php
namespace Drupal\Component\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
class PhpArrayContainer extends Container {
public function __construct(array $container_definition = array()) {
if (isset($container_definition['machine_format']) && $container_definition['machine_format'] === TRUE) {
throw new InvalidArgumentException('The machine-optimized format is not supported by this class. Use a human-readable format instead, e.g. as produced by \\Drupal\\Component\\DependencyInjection\\Dumper\\PhpArrayDumper.');
}
$this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : array();
$this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : array();
$this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : array();
$this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE;
$this->services['service_container'] = $this;
}
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 = array();
if (isset($definition['arguments'])) {
$arguments = $this
->resolveServicesAndParameters($definition['arguments']);
}
if (isset($definition['file'])) {
$file = $this->frozen ? $definition['file'] : current($this
->resolveServicesAndParameters(array(
$definition['file'],
)));
require_once $file;
}
if (isset($definition['factory'])) {
$factory = $definition['factory'];
if (is_array($factory)) {
$factory = $this
->resolveServicesAndParameters(array(
$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(array(
$definition['class'],
)));
$length = isset($definition['arguments_count']) ? $definition['arguments_count'] : count($arguments);
switch ($length) {
case 0:
$service = new $class();
break;
case 1:
$service = new $class($arguments[0]);
break;
case 2:
$service = new $class($arguments[0], $arguments[1]);
break;
case 3:
$service = new $class($arguments[0], $arguments[1], $arguments[2]);
break;
case 4:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
break;
case 5:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
break;
case 6:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]);
break;
case 7:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]);
break;
case 8:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]);
break;
case 9:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8]);
break;
case 10:
$service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8], $arguments[9]);
break;
default:
$r = new \ReflectionClass($class);
$service = $r
->newInstanceArgs($arguments);
break;
}
}
if (!isset($definition['public']) || $definition['public'] !== FALSE) {
if (!isset($definition['shared']) || $definition['shared'] !== FALSE) {
$this->services[$id] = $service;
}
}
if (isset($definition['calls'])) {
foreach ($definition['calls'] as $call) {
$method = $call[0];
$arguments = array();
if (!empty($call[1])) {
$arguments = $call[1];
$arguments = $this
->resolveServicesAndParameters($arguments);
}
call_user_func_array(array(
$service,
$method,
), $arguments);
}
}
if (isset($definition['properties'])) {
$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;
}
protected function resolveServicesAndParameters($arguments) {
foreach ($arguments as $key => $argument) {
if ($argument instanceof \stdClass) {
$type = $argument->type;
if ($type == 'private_service') {
$id = $argument->id;
if (!empty($argument->shared) && isset($this->privateServices[$id])) {
$arguments[$key] = $this->privateServices[$id];
continue;
}
$arguments[$key] = $this
->createService($argument->value, $id);
if (!empty($argument->shared)) {
$this->privateServices[$id] = $arguments[$key];
}
continue;
}
if ($type !== NULL) {
throw new InvalidArgumentException("Undefined type '{$type}' while resolving parameters and services.");
}
}
if (is_array($argument)) {
$arguments[$key] = $this
->resolveServicesAndParameters($argument);
continue;
}
if (!is_string($argument)) {
continue;
}
if ($argument[0] === '%') {
$name = substr($argument, 1, -1);
if (!isset($this->parameters[$name])) {
$arguments[$key] = $this
->getParameter($name);
}
$argument = $this->parameters[$name];
$arguments[$key] = $argument;
}
if ($argument[0] === '@') {
$id = substr($argument, 1);
$invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
if ($id[0] === '?') {
$id = substr($id, 1);
$invalid_behavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
}
if (isset($this->services[$id])) {
$arguments[$key] = $this->services[$id];
}
else {
$arguments[$key] = $this
->get($id, $invalid_behavior);
}
}
}
return $arguments;
}
}