| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Contracts\Service; |
| 12 |
|
| 13 |
use WindPressDeps\Psr\Container\ContainerExceptionInterface; |
| 14 |
use WindPressDeps\Psr\Container\NotFoundExceptionInterface; |
| 15 |
// Help opcache.preload discover always-needed symbols |
| 16 |
class_exists(ContainerExceptionInterface::class); |
| 17 |
class_exists(NotFoundExceptionInterface::class); |
| 18 |
/** |
| 19 |
* A trait to help implement ServiceProviderInterface. |
| 20 |
* |
| 21 |
* @author Robin Chalas <robin.chalas@gmail.com> |
| 22 |
* @author Nicolas Grekas <p@tchwork.com> |
| 23 |
*/ |
| 24 |
trait ServiceLocatorTrait |
| 25 |
{ |
| 26 |
private $factories; |
| 27 |
private $loading = []; |
| 28 |
private $providedTypes; |
| 29 |
/** |
| 30 |
* @param callable[] $factories |
| 31 |
*/ |
| 32 |
public function __construct(array $factories) |
| 33 |
{ |
| 34 |
$this->factories = $factories; |
| 35 |
} |
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function has(string $id) |
| 42 |
{ |
| 43 |
return isset($this->factories[$id]); |
| 44 |
} |
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
* |
| 48 |
* @return mixed |
| 49 |
*/ |
| 50 |
public function get(string $id) |
| 51 |
{ |
| 52 |
if (!isset($this->factories[$id])) { |
| 53 |
throw $this->createNotFoundException($id); |
| 54 |
} |
| 55 |
if (isset($this->loading[$id])) { |
| 56 |
$ids = array_values($this->loading); |
| 57 |
$ids = \array_slice($this->loading, array_search($id, $ids)); |
| 58 |
$ids[] = $id; |
| 59 |
throw $this->createCircularReferenceException($id, $ids); |
| 60 |
} |
| 61 |
$this->loading[$id] = $id; |
| 62 |
try { |
| 63 |
return $this->factories[$id]($this); |
| 64 |
} finally { |
| 65 |
unset($this->loading[$id]); |
| 66 |
} |
| 67 |
} |
| 68 |
/** |
| 69 |
* {@inheritdoc} |
| 70 |
*/ |
| 71 |
public function getProvidedServices(): array |
| 72 |
{ |
| 73 |
if (null === $this->providedTypes) { |
| 74 |
$this->providedTypes = []; |
| 75 |
foreach ($this->factories as $name => $factory) { |
| 76 |
if (!\is_callable($factory)) { |
| 77 |
$this->providedTypes[$name] = '?'; |
| 78 |
} else { |
| 79 |
$type = (new \ReflectionFunction($factory))->getReturnType(); |
| 80 |
$this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?'; |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
return $this->providedTypes; |
| 85 |
} |
| 86 |
private function createNotFoundException(string $id): NotFoundExceptionInterface |
| 87 |
{ |
| 88 |
if (!$alternatives = array_keys($this->factories)) { |
| 89 |
$message = 'is empty...'; |
| 90 |
} else { |
| 91 |
$last = array_pop($alternatives); |
| 92 |
if ($alternatives) { |
| 93 |
$message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last); |
| 94 |
} else { |
| 95 |
$message = sprintf('only knows about the "%s" service.', $last); |
| 96 |
} |
| 97 |
} |
| 98 |
if ($this->loading) { |
| 99 |
$message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message); |
| 100 |
} else { |
| 101 |
$message = sprintf('Service "%s" not found: the current service locator %s', $id, $message); |
| 102 |
} |
| 103 |
return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface |
| 104 |
{ |
| 105 |
}; |
| 106 |
} |
| 107 |
private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface |
| 108 |
{ |
| 109 |
return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface |
| 110 |
{ |
| 111 |
}; |
| 112 |
} |
| 113 |
} |
| 114 |
|