annotate core/lib/Drupal/Component/DependencyInjection/Container.php @ 19:fa3358dc1485 tip

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