comparison core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
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 * {@inheritdoc}
50 */
51 protected function shareService(Definition $definition, $service, $id)
52 {
53 if ($definition->isShared()) {
54 $this->services[$lowerId = strtolower($id)] = $service;
55 }
56 }
57
58 /**
59 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
60 *
61 * Drupal's container builder can be used at runtime after compilation, so we
62 * override Symfony's ContainerBuilder's restriction on setting services in a
63 * frozen builder.
64 *
65 * @todo Restrict this to synthetic services only. Ideally, the upstream
66 * ContainerBuilder class should be fixed to allow setting synthetic
67 * services in a frozen builder.
68 */
69 public function set($id, $service) {
70 if (strtolower($id) !== $id) {
71 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
72 }
73 SymfonyContainer::set($id, $service);
74
75 // Ensure that the _serviceId property is set on synthetic services as well.
76 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
77 $this->services[$id]->_serviceId = $id;
78 }
79 }
80
81 /**
82 * {@inheritdoc}
83 */
84 public function register($id, $class = null) {
85 if (strtolower($id) !== $id) {
86 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
87 }
88 return parent::register($id, $class);
89 }
90
91 /**
92 * {@inheritdoc}
93 */
94 public function setParameter($name, $value) {
95 if (strtolower($name) !== $name) {
96 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
97 }
98 parent::setParameter($name, $value);
99 }
100
101 /**
102 * A 1to1 copy of parent::callMethod.
103 */
104 protected function callMethod($service, $call) {
105 $services = self::getServiceConditionals($call[1]);
106
107 foreach ($services as $s) {
108 if (!$this->has($s)) {
109 return;
110 }
111 }
112
113 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
114 }
115
116 /**
117 * {@inheritdoc}
118 */
119 public function __sleep() {
120 assert(FALSE, 'The container was serialized.');
121 return array_keys(get_object_vars($this));
122 }
123
124 }