2 // @codingStandardsIgnoreFile
4 namespace Drupal\Core\DependencyInjection;
6 use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
7 use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
8 use Symfony\Component\DependencyInjection\Definition;
9 use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
10 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
13 * Drupal's dependency injection container builder.
15 * @todo Submit upstream patches to Symfony to not require these overrides.
19 class ContainerBuilder extends SymfonyContainerBuilder {
22 * @var \Doctrine\Instantiator\InstantiatorInterface|null
24 private $proxyInstantiator;
29 public function __construct(ParameterBagInterface $parameterBag = NULL) {
30 $this->setResourceTracking(FALSE);
31 parent::__construct($parameterBag);
35 * Retrieves the currently set proxy instantiator or instantiates one.
37 * @return InstantiatorInterface
39 private function getProxyInstantiator()
41 if (!$this->proxyInstantiator) {
42 $this->proxyInstantiator = new RealServiceInstantiator();
45 return $this->proxyInstantiator;
49 * Direct copy of the parent function.
51 protected function shareService(Definition $definition, $service, $id)
53 if ($definition->isShared() && self::SCOPE_PROTOTYPE !== $scope = $definition->getScope(false)) {
54 if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
55 throw new InactiveScopeException($id, $scope);
58 $this->services[$lowerId = strtolower($id)] = $service;
60 if (self::SCOPE_CONTAINER !== $scope) {
61 $this->scopedServices[$scope][$lowerId] = $service;
67 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
69 * Drupal's container builder can be used at runtime after compilation, so we
70 * override Symfony's ContainerBuilder's restriction on setting services in a
73 * @todo Restrict this to synthetic services only. Ideally, the upstream
74 * ContainerBuilder class should be fixed to allow setting synthetic
75 * services in a frozen builder.
77 public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
78 if (strtolower($id) !== $id) {
79 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
81 SymfonyContainer::set($id, $service, $scope);
83 // Ensure that the _serviceId property is set on synthetic services as well.
84 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
85 $this->services[$id]->_serviceId = $id;
92 public function register($id, $class = null) {
93 if (strtolower($id) !== $id) {
94 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
96 return parent::register($id, $class);
102 public function setParameter($name, $value) {
103 if (strtolower($name) !== $name) {
104 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
106 parent::setParameter($name, $value);
110 * A 1to1 copy of parent::callMethod.
112 protected function callMethod($service, $call) {
113 $services = self::getServiceConditionals($call[1]);
115 foreach ($services as $s) {
116 if (!$this->has($s)) {
121 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
127 public function __sleep() {
128 assert(FALSE, 'The container was serialized.');
129 return array_keys(get_object_vars($this));