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\ResettableContainerInterface;
|
Chris@0
|
7 use Symfony\Component\DependencyInjection\Exception\LogicException;
|
Chris@0
|
8 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
|
Chris@0
|
9 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
|
Chris@0
|
11 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Provides a container optimized for Drupal's needs.
|
Chris@0
|
16 *
|
Chris@0
|
17 * This container implementation is compatible with the default Symfony
|
Chris@0
|
18 * dependency injection container and similar to the Symfony ContainerBuilder
|
Chris@0
|
19 * class, but optimized for speed.
|
Chris@0
|
20 *
|
Chris@0
|
21 * It is based on a PHP array container definition dumped as a
|
Chris@0
|
22 * performance-optimized machine-readable format.
|
Chris@0
|
23 *
|
Chris@0
|
24 * The best way to initialize this container is to use a Container Builder,
|
Chris@0
|
25 * compile it and then retrieve the definition via
|
Chris@0
|
26 * \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::getArray().
|
Chris@0
|
27 *
|
Chris@0
|
28 * The retrieved array can be cached safely and then passed to this container
|
Chris@0
|
29 * via the constructor.
|
Chris@0
|
30 *
|
Chris@0
|
31 * As the container is unfrozen by default, a second parameter can be passed to
|
Chris@0
|
32 * the container to "freeze" the parameter bag.
|
Chris@0
|
33 *
|
Chris@0
|
34 * This container is different in behavior from the default Symfony container in
|
Chris@0
|
35 * the following ways:
|
Chris@0
|
36 *
|
Chris@0
|
37 * - It only allows lowercase service and parameter names, though it does only
|
Chris@0
|
38 * enforce it via assertions for performance reasons.
|
Chris@0
|
39 * - The following functions, that are not part of the interface, are explicitly
|
Chris@0
|
40 * not supported: getParameterBag(), isFrozen(), compile(),
|
Chris@0
|
41 * getAServiceWithAnIdByCamelCase().
|
Chris@0
|
42 * - The function getServiceIds() was added as it has a use-case in core and
|
Chris@0
|
43 * contrib.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @ingroup container
|
Chris@0
|
46 */
|
Chris@0
|
47 class Container implements ContainerInterface, ResettableContainerInterface {
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * The parameters of the container.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @var array
|
Chris@0
|
53 */
|
Chris@0
|
54 protected $parameters = [];
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * The aliases of the container.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @var array
|
Chris@0
|
60 */
|
Chris@0
|
61 protected $aliases = [];
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * The service definitions of the container.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @var array
|
Chris@0
|
67 */
|
Chris@0
|
68 protected $serviceDefinitions = [];
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * The instantiated services.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @var array
|
Chris@0
|
74 */
|
Chris@0
|
75 protected $services = [];
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * The instantiated private services.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @var array
|
Chris@0
|
81 */
|
Chris@0
|
82 protected $privateServices = [];
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * The currently loading services.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @var array
|
Chris@0
|
88 */
|
Chris@0
|
89 protected $loading = [];
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Whether the container parameters can still be changed.
|
Chris@0
|
93 *
|
Chris@0
|
94 * For testing purposes the container needs to be changed.
|
Chris@0
|
95 *
|
Chris@0
|
96 * @var bool
|
Chris@0
|
97 */
|
Chris@0
|
98 protected $frozen = TRUE;
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Constructs a new Container instance.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @param array $container_definition
|
Chris@0
|
104 * An array containing the following keys:
|
Chris@0
|
105 * - aliases: The aliases of the container.
|
Chris@0
|
106 * - parameters: The parameters of the container.
|
Chris@0
|
107 * - services: The service definitions of the container.
|
Chris@0
|
108 * - frozen: Whether the container definition came from a frozen
|
Chris@0
|
109 * container builder or not.
|
Chris@0
|
110 * - machine_format: Whether this container definition uses the optimized
|
Chris@0
|
111 * machine-readable container format.
|
Chris@0
|
112 */
|
Chris@0
|
113 public function __construct(array $container_definition = []) {
|
Chris@0
|
114 if (!empty($container_definition) && (!isset($container_definition['machine_format']) || $container_definition['machine_format'] !== TRUE)) {
|
Chris@0
|
115 throw new InvalidArgumentException('The non-optimized format is not supported by this class. Use an optimized machine-readable format instead, e.g. as produced by \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper.');
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 $this->aliases = isset($container_definition['aliases']) ? $container_definition['aliases'] : [];
|
Chris@0
|
119 $this->parameters = isset($container_definition['parameters']) ? $container_definition['parameters'] : [];
|
Chris@0
|
120 $this->serviceDefinitions = isset($container_definition['services']) ? $container_definition['services'] : [];
|
Chris@0
|
121 $this->frozen = isset($container_definition['frozen']) ? $container_definition['frozen'] : FALSE;
|
Chris@0
|
122
|
Chris@0
|
123 // Register the service_container with itself.
|
Chris@0
|
124 $this->services['service_container'] = $this;
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 /**
|
Chris@0
|
128 * {@inheritdoc}
|
Chris@0
|
129 */
|
Chris@0
|
130 public function get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
|
Chris@0
|
131 if (isset($this->aliases[$id])) {
|
Chris@0
|
132 $id = $this->aliases[$id];
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 // Re-use shared service instance if it exists.
|
Chris@0
|
136 if (isset($this->services[$id]) || ($invalid_behavior === ContainerInterface::NULL_ON_INVALID_REFERENCE && array_key_exists($id, $this->services))) {
|
Chris@0
|
137 return $this->services[$id];
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 if (isset($this->loading[$id])) {
|
Chris@0
|
141 throw new ServiceCircularReferenceException($id, array_keys($this->loading));
|
Chris@0
|
142 }
|
Chris@0
|
143
|
Chris@0
|
144 $definition = isset($this->serviceDefinitions[$id]) ? $this->serviceDefinitions[$id] : NULL;
|
Chris@0
|
145
|
Chris@0
|
146 if (!$definition && $invalid_behavior === ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
|
Chris@0
|
147 if (!$id) {
|
Chris@0
|
148 throw new ServiceNotFoundException($id);
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 throw new ServiceNotFoundException($id, NULL, NULL, $this->getServiceAlternatives($id));
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 // In case something else than ContainerInterface::NULL_ON_INVALID_REFERENCE
|
Chris@0
|
155 // is used, the actual wanted behavior is to re-try getting the service at a
|
Chris@0
|
156 // later point.
|
Chris@0
|
157 if (!$definition) {
|
Chris@0
|
158 return;
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 // Definition is a keyed array, so [0] is only defined when it is a
|
Chris@0
|
162 // serialized string.
|
Chris@0
|
163 if (isset($definition[0])) {
|
Chris@0
|
164 $definition = unserialize($definition);
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 // Now create the service.
|
Chris@0
|
168 $this->loading[$id] = TRUE;
|
Chris@0
|
169
|
Chris@0
|
170 try {
|
Chris@0
|
171 $service = $this->createService($definition, $id);
|
Chris@0
|
172 }
|
Chris@0
|
173 catch (\Exception $e) {
|
Chris@0
|
174 unset($this->loading[$id]);
|
Chris@0
|
175 unset($this->services[$id]);
|
Chris@0
|
176
|
Chris@0
|
177 if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalid_behavior) {
|
Chris@0
|
178 return;
|
Chris@0
|
179 }
|
Chris@0
|
180
|
Chris@0
|
181 throw $e;
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 unset($this->loading[$id]);
|
Chris@0
|
185
|
Chris@0
|
186 return $service;
|
Chris@0
|
187 }
|
Chris@0
|
188
|
Chris@0
|
189 /**
|
Chris@0
|
190 * {@inheritdoc}
|
Chris@0
|
191 */
|
Chris@0
|
192 public function reset() {
|
Chris@0
|
193 if (!empty($this->scopedServices)) {
|
Chris@0
|
194 throw new LogicException('Resetting the container is not allowed when a scope is active.');
|
Chris@0
|
195 }
|
Chris@0
|
196
|
Chris@0
|
197 $this->services = [];
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Creates a service from a service definition.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @param array $definition
|
Chris@0
|
204 * The service definition to create a service from.
|
Chris@0
|
205 * @param string $id
|
Chris@0
|
206 * The service identifier, necessary so it can be shared if its public.
|
Chris@0
|
207 *
|
Chris@0
|
208 * @return object
|
Chris@0
|
209 * The service described by the service definition.
|
Chris@0
|
210 *
|
Chris@0
|
211 * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
Chris@0
|
212 * Thrown when the service is a synthetic service.
|
Chris@0
|
213 * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
|
Chris@0
|
214 * Thrown when the configurator callable in $definition['configurator'] is
|
Chris@0
|
215 * not actually a callable.
|
Chris@0
|
216 * @throws \ReflectionException
|
Chris@0
|
217 * Thrown when the service class takes more than 10 parameters to construct,
|
Chris@0
|
218 * and cannot be instantiated.
|
Chris@0
|
219 */
|
Chris@0
|
220 protected function createService(array $definition, $id) {
|
Chris@0
|
221 if (isset($definition['synthetic']) && $definition['synthetic'] === TRUE) {
|
Chris@0
|
222 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
|
223 }
|
Chris@0
|
224
|
Chris@0
|
225 $arguments = [];
|
Chris@0
|
226 if (isset($definition['arguments'])) {
|
Chris@0
|
227 $arguments = $definition['arguments'];
|
Chris@0
|
228
|
Chris@0
|
229 if ($arguments instanceof \stdClass) {
|
Chris@0
|
230 $arguments = $this->resolveServicesAndParameters($arguments);
|
Chris@0
|
231 }
|
Chris@0
|
232 }
|
Chris@0
|
233
|
Chris@0
|
234 if (isset($definition['file'])) {
|
Chris@0
|
235 $file = $this->frozen ? $definition['file'] : current($this->resolveServicesAndParameters([$definition['file']]));
|
Chris@0
|
236 require_once $file;
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 if (isset($definition['factory'])) {
|
Chris@0
|
240 $factory = $definition['factory'];
|
Chris@0
|
241 if (is_array($factory)) {
|
Chris@0
|
242 $factory = $this->resolveServicesAndParameters([$factory[0], $factory[1]]);
|
Chris@0
|
243 }
|
Chris@0
|
244 elseif (!is_string($factory)) {
|
Chris@0
|
245 throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
|
Chris@0
|
246 }
|
Chris@0
|
247
|
Chris@0
|
248 $service = call_user_func_array($factory, $arguments);
|
Chris@0
|
249 }
|
Chris@0
|
250 else {
|
Chris@0
|
251 $class = $this->frozen ? $definition['class'] : current($this->resolveServicesAndParameters([$definition['class']]));
|
Chris@0
|
252 $length = isset($definition['arguments_count']) ? $definition['arguments_count'] : count($arguments);
|
Chris@0
|
253
|
Chris@0
|
254 // Optimize class instantiation for services with up to 10 parameters as
|
Chris@0
|
255 // ReflectionClass is noticeably slow.
|
Chris@0
|
256 switch ($length) {
|
Chris@0
|
257 case 0:
|
Chris@0
|
258 $service = new $class();
|
Chris@0
|
259 break;
|
Chris@0
|
260
|
Chris@0
|
261 case 1:
|
Chris@0
|
262 $service = new $class($arguments[0]);
|
Chris@0
|
263 break;
|
Chris@0
|
264
|
Chris@0
|
265 case 2:
|
Chris@0
|
266 $service = new $class($arguments[0], $arguments[1]);
|
Chris@0
|
267 break;
|
Chris@0
|
268
|
Chris@0
|
269 case 3:
|
Chris@0
|
270 $service = new $class($arguments[0], $arguments[1], $arguments[2]);
|
Chris@0
|
271 break;
|
Chris@0
|
272
|
Chris@0
|
273 case 4:
|
Chris@0
|
274 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3]);
|
Chris@0
|
275 break;
|
Chris@0
|
276
|
Chris@0
|
277 case 5:
|
Chris@0
|
278 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4]);
|
Chris@0
|
279 break;
|
Chris@0
|
280
|
Chris@0
|
281 case 6:
|
Chris@0
|
282 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5]);
|
Chris@0
|
283 break;
|
Chris@0
|
284
|
Chris@0
|
285 case 7:
|
Chris@0
|
286 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6]);
|
Chris@0
|
287 break;
|
Chris@0
|
288
|
Chris@0
|
289 case 8:
|
Chris@0
|
290 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7]);
|
Chris@0
|
291 break;
|
Chris@0
|
292
|
Chris@0
|
293 case 9:
|
Chris@0
|
294 $service = new $class($arguments[0], $arguments[1], $arguments[2], $arguments[3], $arguments[4], $arguments[5], $arguments[6], $arguments[7], $arguments[8]);
|
Chris@0
|
295 break;
|
Chris@0
|
296
|
Chris@0
|
297 case 10:
|
Chris@0
|
298 $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
|
299 break;
|
Chris@0
|
300
|
Chris@0
|
301 default:
|
Chris@0
|
302 $r = new \ReflectionClass($class);
|
Chris@0
|
303 $service = $r->newInstanceArgs($arguments);
|
Chris@0
|
304 break;
|
Chris@0
|
305 }
|
Chris@0
|
306 }
|
Chris@0
|
307
|
Chris@0
|
308 if (!isset($definition['shared']) || $definition['shared'] !== FALSE) {
|
Chris@0
|
309 $this->services[$id] = $service;
|
Chris@0
|
310 }
|
Chris@0
|
311
|
Chris@0
|
312 if (isset($definition['calls'])) {
|
Chris@0
|
313 foreach ($definition['calls'] as $call) {
|
Chris@0
|
314 $method = $call[0];
|
Chris@0
|
315 $arguments = [];
|
Chris@0
|
316 if (!empty($call[1])) {
|
Chris@0
|
317 $arguments = $call[1];
|
Chris@0
|
318 if ($arguments instanceof \stdClass) {
|
Chris@0
|
319 $arguments = $this->resolveServicesAndParameters($arguments);
|
Chris@0
|
320 }
|
Chris@0
|
321 }
|
Chris@0
|
322 call_user_func_array([$service, $method], $arguments);
|
Chris@0
|
323 }
|
Chris@0
|
324 }
|
Chris@0
|
325
|
Chris@0
|
326 if (isset($definition['properties'])) {
|
Chris@0
|
327 if ($definition['properties'] instanceof \stdClass) {
|
Chris@0
|
328 $definition['properties'] = $this->resolveServicesAndParameters($definition['properties']);
|
Chris@0
|
329 }
|
Chris@0
|
330 foreach ($definition['properties'] as $key => $value) {
|
Chris@0
|
331 $service->{$key} = $value;
|
Chris@0
|
332 }
|
Chris@0
|
333 }
|
Chris@0
|
334
|
Chris@0
|
335 if (isset($definition['configurator'])) {
|
Chris@0
|
336 $callable = $definition['configurator'];
|
Chris@0
|
337 if (is_array($callable)) {
|
Chris@0
|
338 $callable = $this->resolveServicesAndParameters($callable);
|
Chris@0
|
339 }
|
Chris@0
|
340
|
Chris@0
|
341 if (!is_callable($callable)) {
|
Chris@0
|
342 throw new InvalidArgumentException(sprintf('The configurator for class "%s" is not a callable.', get_class($service)));
|
Chris@0
|
343 }
|
Chris@0
|
344
|
Chris@0
|
345 call_user_func($callable, $service);
|
Chris@0
|
346 }
|
Chris@0
|
347
|
Chris@0
|
348 return $service;
|
Chris@0
|
349 }
|
Chris@0
|
350
|
Chris@0
|
351 /**
|
Chris@0
|
352 * {@inheritdoc}
|
Chris@0
|
353 */
|
Chris@0
|
354 public function set($id, $service) {
|
Chris@0
|
355 $this->services[$id] = $service;
|
Chris@0
|
356 }
|
Chris@0
|
357
|
Chris@0
|
358 /**
|
Chris@0
|
359 * {@inheritdoc}
|
Chris@0
|
360 */
|
Chris@0
|
361 public function has($id) {
|
Chris@0
|
362 return isset($this->aliases[$id]) || isset($this->services[$id]) || isset($this->serviceDefinitions[$id]);
|
Chris@0
|
363 }
|
Chris@0
|
364
|
Chris@0
|
365 /**
|
Chris@0
|
366 * {@inheritdoc}
|
Chris@0
|
367 */
|
Chris@0
|
368 public function getParameter($name) {
|
Chris@0
|
369 if (!(isset($this->parameters[$name]) || array_key_exists($name, $this->parameters))) {
|
Chris@0
|
370 if (!$name) {
|
Chris@0
|
371 throw new ParameterNotFoundException($name);
|
Chris@0
|
372 }
|
Chris@0
|
373
|
Chris@0
|
374 throw new ParameterNotFoundException($name, NULL, NULL, NULL, $this->getParameterAlternatives($name));
|
Chris@0
|
375 }
|
Chris@0
|
376
|
Chris@0
|
377 return $this->parameters[$name];
|
Chris@0
|
378 }
|
Chris@0
|
379
|
Chris@0
|
380 /**
|
Chris@0
|
381 * {@inheritdoc}
|
Chris@0
|
382 */
|
Chris@0
|
383 public function hasParameter($name) {
|
Chris@0
|
384 return isset($this->parameters[$name]) || array_key_exists($name, $this->parameters);
|
Chris@0
|
385 }
|
Chris@0
|
386
|
Chris@0
|
387 /**
|
Chris@0
|
388 * {@inheritdoc}
|
Chris@0
|
389 */
|
Chris@0
|
390 public function setParameter($name, $value) {
|
Chris@0
|
391 if ($this->frozen) {
|
Chris@0
|
392 throw new LogicException('Impossible to call set() on a frozen ParameterBag.');
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 $this->parameters[$name] = $value;
|
Chris@0
|
396 }
|
Chris@0
|
397
|
Chris@0
|
398 /**
|
Chris@0
|
399 * {@inheritdoc}
|
Chris@0
|
400 */
|
Chris@0
|
401 public function initialized($id) {
|
Chris@0
|
402 if (isset($this->aliases[$id])) {
|
Chris@0
|
403 $id = $this->aliases[$id];
|
Chris@0
|
404 }
|
Chris@0
|
405
|
Chris@0
|
406 return isset($this->services[$id]) || array_key_exists($id, $this->services);
|
Chris@0
|
407 }
|
Chris@0
|
408
|
Chris@0
|
409 /**
|
Chris@0
|
410 * Resolves arguments that represent services or variables to the real values.
|
Chris@0
|
411 *
|
Chris@0
|
412 * @param array|\stdClass $arguments
|
Chris@0
|
413 * The arguments to resolve.
|
Chris@0
|
414 *
|
Chris@0
|
415 * @return array
|
Chris@0
|
416 * The resolved arguments.
|
Chris@0
|
417 *
|
Chris@0
|
418 * @throws \Symfony\Component\DependencyInjection\Exception\RuntimeException
|
Chris@0
|
419 * If a parameter/service could not be resolved.
|
Chris@0
|
420 * @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
|
Chris@0
|
421 * If an unknown type is met while resolving parameters and services.
|
Chris@0
|
422 */
|
Chris@0
|
423 protected function resolveServicesAndParameters($arguments) {
|
Chris@0
|
424 // Check if this collection needs to be resolved.
|
Chris@0
|
425 if ($arguments instanceof \stdClass) {
|
Chris@0
|
426 if ($arguments->type !== 'collection') {
|
Chris@0
|
427 throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $arguments->type));
|
Chris@0
|
428 }
|
Chris@0
|
429 // In case there is nothing to resolve, we are done here.
|
Chris@0
|
430 if (!$arguments->resolve) {
|
Chris@0
|
431 return $arguments->value;
|
Chris@0
|
432 }
|
Chris@0
|
433 $arguments = $arguments->value;
|
Chris@0
|
434 }
|
Chris@0
|
435
|
Chris@0
|
436 // Process the arguments.
|
Chris@0
|
437 foreach ($arguments as $key => $argument) {
|
Chris@0
|
438 // For this machine-optimized format, only \stdClass arguments are
|
Chris@0
|
439 // processed and resolved. All other values are kept as is.
|
Chris@0
|
440 if ($argument instanceof \stdClass) {
|
Chris@0
|
441 $type = $argument->type;
|
Chris@0
|
442
|
Chris@0
|
443 // Check for parameter.
|
Chris@0
|
444 if ($type == 'parameter') {
|
Chris@0
|
445 $name = $argument->name;
|
Chris@0
|
446 if (!isset($this->parameters[$name])) {
|
Chris@0
|
447 $arguments[$key] = $this->getParameter($name);
|
Chris@0
|
448 // This can never be reached as getParameter() throws an Exception,
|
Chris@0
|
449 // because we already checked that the parameter is not set above.
|
Chris@0
|
450 }
|
Chris@0
|
451
|
Chris@0
|
452 // Update argument.
|
Chris@0
|
453 $argument = $arguments[$key] = $this->parameters[$name];
|
Chris@0
|
454
|
Chris@0
|
455 // In case there is not a machine readable value (e.g. a service)
|
Chris@0
|
456 // behind this resolved parameter, continue.
|
Chris@0
|
457 if (!($argument instanceof \stdClass)) {
|
Chris@0
|
458 continue;
|
Chris@0
|
459 }
|
Chris@0
|
460
|
Chris@0
|
461 // Fall through.
|
Chris@0
|
462 $type = $argument->type;
|
Chris@0
|
463 }
|
Chris@0
|
464
|
Chris@0
|
465 // Create a service.
|
Chris@0
|
466 if ($type == 'service') {
|
Chris@0
|
467 $id = $argument->id;
|
Chris@0
|
468
|
Chris@0
|
469 // Does the service already exist?
|
Chris@0
|
470 if (isset($this->aliases[$id])) {
|
Chris@0
|
471 $id = $this->aliases[$id];
|
Chris@0
|
472 }
|
Chris@0
|
473
|
Chris@0
|
474 if (isset($this->services[$id])) {
|
Chris@0
|
475 $arguments[$key] = $this->services[$id];
|
Chris@0
|
476 continue;
|
Chris@0
|
477 }
|
Chris@0
|
478
|
Chris@0
|
479 // Return the service.
|
Chris@0
|
480 $arguments[$key] = $this->get($id, $argument->invalidBehavior);
|
Chris@0
|
481
|
Chris@0
|
482 continue;
|
Chris@0
|
483 }
|
Chris@0
|
484 // Create private service.
|
Chris@0
|
485 elseif ($type == 'private_service') {
|
Chris@0
|
486 $id = $argument->id;
|
Chris@0
|
487
|
Chris@0
|
488 // Does the private service already exist.
|
Chris@0
|
489 if (isset($this->privateServices[$id])) {
|
Chris@0
|
490 $arguments[$key] = $this->privateServices[$id];
|
Chris@0
|
491 continue;
|
Chris@0
|
492 }
|
Chris@0
|
493
|
Chris@0
|
494 // Create the private service.
|
Chris@0
|
495 $arguments[$key] = $this->createService($argument->value, $id);
|
Chris@0
|
496 if ($argument->shared) {
|
Chris@0
|
497 $this->privateServices[$id] = $arguments[$key];
|
Chris@0
|
498 }
|
Chris@0
|
499
|
Chris@0
|
500 continue;
|
Chris@0
|
501 }
|
Chris@0
|
502 // Check for collection.
|
Chris@0
|
503 elseif ($type == 'collection') {
|
Chris@0
|
504 $value = $argument->value;
|
Chris@0
|
505
|
Chris@0
|
506 // Does this collection need resolving?
|
Chris@0
|
507 if ($argument->resolve) {
|
Chris@0
|
508 $arguments[$key] = $this->resolveServicesAndParameters($value);
|
Chris@0
|
509 }
|
Chris@0
|
510 else {
|
Chris@0
|
511 $arguments[$key] = $value;
|
Chris@0
|
512 }
|
Chris@0
|
513
|
Chris@0
|
514 continue;
|
Chris@0
|
515 }
|
Chris@0
|
516
|
Chris@0
|
517 if ($type !== NULL) {
|
Chris@0
|
518 throw new InvalidArgumentException(sprintf('Undefined type "%s" while resolving parameters and services.', $type));
|
Chris@0
|
519 }
|
Chris@0
|
520 }
|
Chris@0
|
521 }
|
Chris@0
|
522
|
Chris@0
|
523 return $arguments;
|
Chris@0
|
524 }
|
Chris@0
|
525
|
Chris@0
|
526 /**
|
Chris@0
|
527 * Provides alternatives for a given array and key.
|
Chris@0
|
528 *
|
Chris@0
|
529 * @param string $search_key
|
Chris@0
|
530 * The search key to get alternatives for.
|
Chris@0
|
531 * @param array $keys
|
Chris@0
|
532 * The search space to search for alternatives in.
|
Chris@0
|
533 *
|
Chris@0
|
534 * @return string[]
|
Chris@0
|
535 * An array of strings with suitable alternatives.
|
Chris@0
|
536 */
|
Chris@0
|
537 protected function getAlternatives($search_key, array $keys) {
|
Chris@0
|
538 $alternatives = [];
|
Chris@0
|
539 foreach ($keys as $key) {
|
Chris@0
|
540 $lev = levenshtein($search_key, $key);
|
Chris@0
|
541 if ($lev <= strlen($search_key) / 3 || strpos($key, $search_key) !== FALSE) {
|
Chris@0
|
542 $alternatives[] = $key;
|
Chris@0
|
543 }
|
Chris@0
|
544 }
|
Chris@0
|
545
|
Chris@0
|
546 return $alternatives;
|
Chris@0
|
547 }
|
Chris@0
|
548
|
Chris@0
|
549 /**
|
Chris@0
|
550 * Provides alternatives in case a service was not found.
|
Chris@0
|
551 *
|
Chris@0
|
552 * @param string $id
|
Chris@0
|
553 * The service to get alternatives for.
|
Chris@0
|
554 *
|
Chris@0
|
555 * @return string[]
|
Chris@0
|
556 * An array of strings with suitable alternatives.
|
Chris@0
|
557 */
|
Chris@0
|
558 protected function getServiceAlternatives($id) {
|
Chris@0
|
559 $all_service_keys = array_unique(array_merge(array_keys($this->services), array_keys($this->serviceDefinitions)));
|
Chris@0
|
560 return $this->getAlternatives($id, $all_service_keys);
|
Chris@0
|
561 }
|
Chris@0
|
562
|
Chris@0
|
563 /**
|
Chris@0
|
564 * Provides alternatives in case a parameter was not found.
|
Chris@0
|
565 *
|
Chris@0
|
566 * @param string $name
|
Chris@0
|
567 * The parameter to get alternatives for.
|
Chris@0
|
568 *
|
Chris@0
|
569 * @return string[]
|
Chris@0
|
570 * An array of strings with suitable alternatives.
|
Chris@0
|
571 */
|
Chris@0
|
572 protected function getParameterAlternatives($name) {
|
Chris@0
|
573 return $this->getAlternatives($name, array_keys($this->parameters));
|
Chris@0
|
574 }
|
Chris@0
|
575
|
Chris@0
|
576 /**
|
Chris@0
|
577 * Gets all defined service IDs.
|
Chris@0
|
578 *
|
Chris@0
|
579 * @return array
|
Chris@0
|
580 * An array of all defined service IDs.
|
Chris@0
|
581 */
|
Chris@0
|
582 public function getServiceIds() {
|
Chris@0
|
583 return array_keys($this->serviceDefinitions + $this->services);
|
Chris@0
|
584 }
|
Chris@0
|
585
|
Chris@0
|
586 /**
|
Chris@0
|
587 * Ensure that cloning doesn't work.
|
Chris@0
|
588 */
|
Chris@0
|
589 private function __clone()
|
Chris@0
|
590 {
|
Chris@0
|
591 }
|
Chris@0
|
592
|
Chris@0
|
593 }
|