e45cb884aa26960fe6d62ac27a46302fb3c97c9d
[yaffs-website] / web / core / lib / Drupal / Core / DependencyInjection / ContainerBuilder.php
1 <?php
2 // @codingStandardsIgnoreFile
3
4 namespace Drupal\Core\DependencyInjection;
5
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;
11
12 /**
13  * Drupal's dependency injection container builder.
14  *
15  * @todo Submit upstream patches to Symfony to not require these overrides.
16  *
17  * @ingroup container
18  */
19 class ContainerBuilder extends SymfonyContainerBuilder {
20
21   /**
22    * @var \Doctrine\Instantiator\InstantiatorInterface|null
23    */
24   private $proxyInstantiator;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function __construct(ParameterBagInterface $parameterBag = NULL) {
30     $this->setResourceTracking(FALSE);
31     parent::__construct($parameterBag);
32   }
33
34   /**
35    * Retrieves the currently set proxy instantiator or instantiates one.
36    *
37    * @return InstantiatorInterface
38    */
39   private function getProxyInstantiator()
40   {
41     if (!$this->proxyInstantiator) {
42       $this->proxyInstantiator = new RealServiceInstantiator();
43     }
44
45     return $this->proxyInstantiator;
46   }
47
48   /**
49    * Direct copy of the parent function.
50    */
51   protected function shareService(Definition $definition, $service, $id)
52   {
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);
56       }
57
58       $this->services[$lowerId = strtolower($id)] = $service;
59
60       if (self::SCOPE_CONTAINER !== $scope) {
61         $this->scopedServices[$scope][$lowerId] = $service;
62       }
63     }
64   }
65
66   /**
67    * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
68    *
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
71    * frozen builder.
72    *
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.
76    */
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");
80     }
81     SymfonyContainer::set($id, $service, $scope);
82
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;
86     }
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function register($id, $class = null) {
93     if (strtolower($id) !== $id) {
94       throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
95     }
96     return parent::register($id, $class);
97   }
98
99   /**
100    * {@inheritdoc}
101    */
102   public function setParameter($name, $value) {
103     if (strtolower($name) !== $name) {
104       throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
105     }
106     parent::setParameter($name, $value);
107   }
108
109   /**
110    * A 1to1 copy of parent::callMethod.
111    */
112   protected function callMethod($service, $call) {
113     $services = self::getServiceConditionals($call[1]);
114
115     foreach ($services as $s) {
116       if (!$this->has($s)) {
117         return;
118       }
119     }
120
121     call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
122   }
123
124   /**
125    * {@inheritdoc}
126    */
127   public function __sleep() {
128     assert(FALSE, 'The container was serialized.');
129     return array_keys(get_object_vars($this));
130   }
131
132 }