Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Component\DependencyInjection;
|
Chris@0
|
4
|
Chris@0
|
5 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
6 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
7 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * Provides a container optimized for Drupal's needs.
|
Chris@0
|
11 *
|
Chris@0
|
12 * This container implementation is compatible with the default Symfony
|
Chris@0
|
13 * dependency injection container and similar to the Symfony ContainerBuilder
|
Chris@0
|
14 * class, but optimized for speed.
|
Chris@0
|
15 *
|
Chris@0
|
16 * It is based on a human-readable PHP array container definition with a
|
Chris@0
|
17 * structure very similar to the YAML container definition.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @see \Drupal\Component\DependencyInjection\Container
|
Chris@0
|
20 * @see \Drupal\Component\DependencyInjection\Dumper\PhpArrayDumper
|
Chris@0
|
21 * @see \Drupal\Component\DependencyInjection\DependencySerializationTrait
|
Chris@0
|
22 *
|
Chris@0
|
23 * @ingroup container
|
Chris@0
|
24 */
|
Chris@0
|
25 class PhpArrayContainer extends Container {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * {@inheritdoc}
|
Chris@0
|
29 */
|
Chris@0
|
30 public function __construct(array $container_definition = []) {
|
Chris@0
|
31 if (isset($container_definition['machine_format']) && $container_definition['machine_format'] === TRUE) {
|
Chris@0
|
32 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.');
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 // Do not call the parent's constructor as it would bail on the
|
Chris@0
|
36 // machine-optimized format.
|
Chris@0
|
37 $this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : [];
|
Chris@0
|
38 $this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : [];
|
Chris@0
|
39 $this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : [];
|
Chris@0
|
40 $this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE;
|
Chris@0
|
41
|
Chris@0
|
42 // Register the service_container with itself.
|
Chris@0
|
43 $this->services['service_container'] = $this;
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * {@inheritdoc}
|
Chris@0
|
48 */
|
Chris@0
|
49 protected function createService(array $definition, $id) {
|
Chris@0
|
50 // This method is a verbatim copy of
|
Chris@0
|
51 // \Drupal\Component\DependencyInjection\Container::createService
|
Chris@0
|
52 // except for the following difference:
|
Chris@0
|
53 // - There are no instanceof checks on \stdClass, which are used in the
|
Chris@0
|
54 // parent class to avoid resolving services and parameters when it is
|
Chris@0
|
55 // known from dumping that there is nothing to resolve.
|
Chris@0
|
56 if (isset($definition['synthetic']) && $definition['synthetic'] === TRUE) {
|
Chris@0
|
57 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));
|
Chris@0
|
58 }
|
Chris@0
|
59
|
Chris@0
|
60 $arguments = [];
|
Chris@0
|
61 if (isset($definition['arguments'])) {
|
Chris@0
|
62 $arguments = $this->resolveServicesAndParameters($definition['arguments']);
|
Chris@0
|
63 }
|
Chris@0
|
64
|
Chris@0
|
65 if (isset($definition['file'])) {
|
Chris@0
|
66 $file = $this->frozen ? $definition['file'] : current($this->resolveServicesAndParameters([$definition['file']]));
|
Chris@0
|
67 require_once $file;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 if (isset($definition['factory'])) {
|
Chris@0
|
71 $factory = $definition['factory'];
|
Chris@0
|
72 if (is_array($factory)) {
|
Chris@0
|
73 $factory = $this->resolveServicesAndParameters([$factory[0], $factory[1]]);
|
Chris@0
|
74 }
|
Chris@0
|
75 elseif (!is_string($factory)) {
|
Chris@0
|
76 throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 $service = call_user_func_array($factory, $arguments);
|
Chris@0
|
80 }
|
Chris@0
|
81 else {
|
Chris@0
|
82 $class = $this->frozen ? $definition['class'] : current($this->resolveServicesAndParameters([$definition['class']]));
|
Chris@0
|
83 $length = isset($definition['arguments_count']) ? $definition['arguments_count'] : count($arguments);
|
Chris@0
|
84
|
Chris@0
|
85 // Optimize class instantiation for services with up to 10 parameters as
|
Chris@0
|
86 // reflection is noticeably slow.
|
Chris@0
|
87 switch ($length) {
|
Chris@0
|
88 case 0:
|
Chris@0
|
89 $service = new $class();
|
Chris@0
|
90 break;
|
Chris@0
|
91
|
Chris@0
|
92 case 1:
|
Chris@0
|
93 $service = new $class($arguments[0]);
|
Chris@0
|
94 break;
|
Chris@0
|
95
|
Chris@0
|
96 case 2:
|
Chris@0
|
97 $service = new $class($arguments[0], $arguments[1]);
|
Chris@0
|
98 break;
|
Chris@0
|
99
|
Chris@0
|
100 case 3:
|
Chris@0
|
101 $service = new $class($arguments[0], $arguments[1], $arguments[2]);
|
Chris@0
|
102 break;
|
Chris@0
|
103
|
Chris@0
|
104 case 4:
|
Chris@0
|
105 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
|
Chris@0
|
106 break;
|
Chris@0
|
107
|
Chris@0
|
108 case 5:
|
Chris@0
|
109 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
|
Chris@0
|
110 break;
|
Chris@0
|
111
|
Chris@0
|
112 case 6:
|
Chris@0
|
113 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]);
|
Chris@0
|
114 break;
|
Chris@0
|
115
|
Chris@0
|
116 case 7:
|
Chris@0
|
117 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]);
|
Chris@0
|
118 break;
|
Chris@0
|
119
|
Chris@0
|
120 case 8:
|
Chris@0
|
121 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]);
|
Chris@0
|
122 break;
|
Chris@0
|
123
|
Chris@0
|
124 case 9:
|
Chris@0
|
125 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8]);
|
Chris@0
|
126 break;
|
Chris@0
|
127
|
Chris@0
|
128 case 10:
|
Chris@0
|
129 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8], $arguments[9]);
|
Chris@0
|
130 break;
|
Chris@0
|
131
|
Chris@0
|
132 default:
|
Chris@0
|
133 $r = new \ReflectionClass($class);
|
Chris@0
|
134 $service = $r->newInstanceArgs($arguments);
|
Chris@0
|
135 break;
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 if (!isset($definition['shared']) || $definition['shared'] !== FALSE) {
|
Chris@0
|
140 $this->services[$id] = $service;
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 if (isset($definition['calls'])) {
|
Chris@0
|
144 foreach ($definition['calls'] as $call) {
|
Chris@0
|
145 $method = $call[0];
|
Chris@0
|
146 $arguments = [];
|
Chris@0
|
147 if (!empty($call[1])) {
|
Chris@0
|
148 $arguments = $call[1];
|
Chris@0
|
149 $arguments = $this->resolveServicesAndParameters($arguments);
|
Chris@0
|
150 }
|
Chris@0
|
151 call_user_func_array([$service, $method], $arguments);
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|
Chris@0
|
154
|
Chris@0
|
155 if (isset($definition['properties'])) {
|
Chris@0
|
156 $definition['properties'] = $this->resolveServicesAndParameters($definition['properties']);
|
Chris@0
|
157 foreach ($definition['properties'] as $key => $value) {
|
Chris@0
|
158 $service->{$key} = $value;
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 if (isset($definition['configurator'])) {
|
Chris@0
|
163 $callable = $definition['configurator'];
|
Chris@0
|
164 if (is_array($callable)) {
|
Chris@0
|
165 $callable = $this->resolveServicesAndParameters($callable);
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 if (!is_callable($callable)) {
|
Chris@0
|
169 throw new InvalidArgumentException(sprintf('The configurator for class "%s" is not a callable.', get_class($service)));
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 call_user_func($callable, $service);
|
Chris@0
|
173 }
|
Chris@0
|
174
|
Chris@0
|
175 return $service;
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 /**
|
Chris@0
|
179 * {@inheritdoc}
|
Chris@0
|
180 */
|
Chris@0
|
181 protected function resolveServicesAndParameters($arguments) {
|
Chris@0
|
182 // This method is different from the parent method only for the following
|
Chris@0
|
183 // cases:
|
Chris@0
|
184 // - A service is denoted by '@service' and not by a \stdClass object.
|
Chris@0
|
185 // - A parameter is denoted by '%parameter%' and not by a \stdClass object.
|
Chris@0
|
186 // - The depth of the tree representing the arguments is not known in
|
Chris@0
|
187 // advance, so it needs to be fully traversed recursively.
|
Chris@0
|
188 foreach ($arguments as $key => $argument) {
|
Chris@0
|
189 if ($argument instanceof \stdClass) {
|
Chris@0
|
190 $type = $argument->type;
|
Chris@0
|
191
|
Chris@0
|
192 // Private services are a special flavor: In case a private service is
|
Chris@0
|
193 // only used by one other service, the ContainerBuilder uses a
|
Chris@0
|
194 // Definition object as an argument, which does not have an ID set.
|
Chris@0
|
195 // Therefore the format uses a \stdClass object to store the definition
|
Chris@0
|
196 // and to be able to create the service on the fly.
|
Chris@0
|
197 //
|
Chris@0
|
198 // Note: When constructing a private service by hand, 'id' must be set.
|
Chris@0
|
199 //
|
Chris@0
|
200 // The PhpArrayDumper just uses the hash of the private service
|
Chris@0
|
201 // definition to generate a unique ID.
|
Chris@0
|
202 //
|
Chris@17
|
203 // @see \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::getPrivateServiceCall
|
Chris@0
|
204 if ($type == 'private_service') {
|
Chris@0
|
205 $id = $argument->id;
|
Chris@0
|
206
|
Chris@0
|
207 // Check if the private service already exists - in case it is shared.
|
Chris@0
|
208 if (!empty($argument->shared) && isset($this->privateServices[$id])) {
|
Chris@0
|
209 $arguments[$key] = $this->privateServices[$id];
|
Chris@0
|
210 continue;
|
Chris@0
|
211 }
|
Chris@0
|
212
|
Chris@0
|
213 // Create a private service from a service definition.
|
Chris@0
|
214 $arguments[$key] = $this->createService($argument->value, $id);
|
Chris@0
|
215 if (!empty($argument->shared)) {
|
Chris@0
|
216 $this->privateServices[$id] = $arguments[$key];
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 continue;
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 if ($type !== NULL) {
|
Chris@0
|
223 throw new InvalidArgumentException("Undefined type '$type' while resolving parameters and services.");
|
Chris@0
|
224 }
|
Chris@0
|
225 }
|
Chris@0
|
226
|
Chris@0
|
227 if (is_array($argument)) {
|
Chris@0
|
228 $arguments[$key] = $this->resolveServicesAndParameters($argument);
|
Chris@0
|
229 continue;
|
Chris@0
|
230 }
|
Chris@0
|
231
|
Chris@0
|
232 if (!is_string($argument)) {
|
Chris@0
|
233 continue;
|
Chris@0
|
234 }
|
Chris@0
|
235
|
Chris@0
|
236 // Resolve parameters.
|
Chris@0
|
237 if ($argument[0] === '%') {
|
Chris@0
|
238 $name = substr($argument, 1, -1);
|
Chris@0
|
239 if (!isset($this->parameters[$name])) {
|
Chris@0
|
240 $arguments[$key] = $this->getParameter($name);
|
Chris@0
|
241 // This can never be reached as getParameter() throws an Exception,
|
Chris@0
|
242 // because we already checked that the parameter is not set above.
|
Chris@0
|
243 }
|
Chris@0
|
244 $argument = $this->parameters[$name];
|
Chris@0
|
245 $arguments[$key] = $argument;
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 // Resolve services.
|
Chris@0
|
249 if ($argument[0] === '@') {
|
Chris@0
|
250 $id = substr($argument, 1);
|
Chris@0
|
251 $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
Chris@0
|
252 if ($id[0] === '?') {
|
Chris@0
|
253 $id = substr($id, 1);
|
Chris@0
|
254 $invalid_behavior = ContainerInterface::NULL_ON_INVALID_REFERENCE;
|
Chris@0
|
255 }
|
Chris@0
|
256 if (isset($this->services[$id])) {
|
Chris@0
|
257 $arguments[$key] = $this->services[$id];
|
Chris@0
|
258 }
|
Chris@0
|
259 else {
|
Chris@0
|
260 $arguments[$key] = $this->get($id, $invalid_behavior);
|
Chris@0
|
261 }
|
Chris@0
|
262 }
|
Chris@0
|
263 }
|
Chris@0
|
264
|
Chris@0
|
265 return $arguments;
|
Chris@0
|
266 }
|
Chris@0
|
267
|
Chris@0
|
268 }
|