* @author Nicolas Grekas */ trait ServiceLocatorTrait { private $factories; private $loading = []; private $providedTypes; /** * @param callable[] $factories */ public function __construct(array $factories) { $this->factories = $factories; } /** * {@inheritdoc} * * @return bool */ public function has(string $id) { return isset($this->factories[$id]); } /** * {@inheritdoc} * * @return mixed */ public function get(string $id) { if (!isset($this->factories[$id])) { throw $this->createNotFoundException($id); } if (isset($this->loading[$id])) { $ids = array_values($this->loading); $ids = \array_slice($this->loading, array_search($id, $ids)); $ids[] = $id; throw $this->createCircularReferenceException($id, $ids); } $this->loading[$id] = $id; try { return $this->factories[$id]($this); } finally { unset($this->loading[$id]); } } /** * {@inheritdoc} */ public function getProvidedServices(): array { if (null === $this->providedTypes) { $this->providedTypes = []; foreach ($this->factories as $name => $factory) { if (!\is_callable($factory)) { $this->providedTypes[$name] = '?'; } else { $type = (new \ReflectionFunction($factory))->getReturnType(); $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; } } } return $this->providedTypes; } private function createNotFoundException(string $id): NotFoundExceptionInterface { if (!$alternatives = array_keys($this->factories)) { $message = 'is empty...'; } else { $last = array_pop($alternatives); if ($alternatives) { $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last); } else { $message = sprintf('only knows about the "%s" service.', $last); } } if ($this->loading) { $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message); } else { $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message); } return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface { }; } private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface { return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface { }; } } __halt_compiler();----SIGNATURE:----DJdEcxyIB5dTQ5X5ibAMeZ797CkbmiUzs57EZrEdgvS6GH0NIIDVWHeTuqnYU3hJE4GPfKFHofnT/J7i8c27gzKHP+UPILG2OWA+Va9jFak4BOMdIci12BSAKShCbLMs2gAwq7aTFjd8lVmjjU2ljKKQie6DlaRb8F0NzJxm/tPVwiDUv47JMOxu6gNs0bw8PPA+ZULbyXubOUIXVQ6vHIheemNsivoS1lQRUbAL9Qq+HQOYxtd4pEju6P4akjjWgl7s648/vsmrtal/7zHlrxdZLRR3dY/iJizHP8nhSAsUsiMMRVyrF+eLQxUhXyK0Z4DkqL2+IOOUoOoxZY3ssKeq6k7eGIsZKL0luM+DKFhnAYTAnS61zgG86nV+uGYQZK5fT7tzOa+wugd6hY9NTB91F/My47vySzphLSL0dSq2nmAF3qrGZiXYB6MYOKlQv5nfOokKINwkYrbswo0JHMZt9utrfiPoTgVoIwFCH8EDGx/3WN3V5ym2Z+dGeID4o21zrIBMvmK1jYfJqdH3+qctndoY9LUxxnyC24h9LhYYa5F35YvqtrPHG5oMJII36H7KcEvaZLpo8Yq8GMZH3rHHBu5gAPV/4W57PIuja07rJeo2buox092Hd5rlykGWh/cVdGq84grnJm+rZDeyH1wcCy6emMXZa15sTyDR+QU=----ATTACHMENT:----NTIwMTA5NzgyOTEyMTU0NSAyNTQ3MDc4OTY1MTEwODUzIDEwMzU0ODA5Mjc4NDg5MzI=