Argument
3 years ago
Definition
3 years ago
Exception
3 years ago
Inflector
5 years ago
ServiceProvider
3 years ago
Container.php
3 years ago
ContainerAwareInterface.php
3 years ago
ContainerAwareTrait.php
3 years ago
ReflectionContainer.php
3 years ago
ReflectionContainer.php
126 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Vendor\League\Container; |
| 4 | |
| 5 | use Automattic\WooCommerce\Vendor\League\Container\Argument\{ArgumentResolverInterface, ArgumentResolverTrait}; |
| 6 | use Automattic\WooCommerce\Vendor\League\Container\Exception\NotFoundException; |
| 7 | use Automattic\WooCommerce\Vendor\Psr\Container\ContainerInterface; |
| 8 | use ReflectionClass; |
| 9 | use ReflectionException; |
| 10 | use ReflectionFunction; |
| 11 | use ReflectionMethod; |
| 12 | |
| 13 | class ReflectionContainer implements ArgumentResolverInterface, ContainerInterface |
| 14 | { |
| 15 | use ArgumentResolverTrait; |
| 16 | use ContainerAwareTrait; |
| 17 | |
| 18 | /** |
| 19 | * @var boolean |
| 20 | */ |
| 21 | protected $cacheResolutions = false; |
| 22 | |
| 23 | /** |
| 24 | * Cache of resolutions. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected $cache = []; |
| 29 | |
| 30 | /** |
| 31 | * {@inheritdoc} |
| 32 | * |
| 33 | * @throws ReflectionException |
| 34 | */ |
| 35 | public function get($id, array $args = []) |
| 36 | { |
| 37 | if ($this->cacheResolutions === true && array_key_exists($id, $this->cache)) { |
| 38 | return $this->cache[$id]; |
| 39 | } |
| 40 | |
| 41 | if (! $this->has($id)) { |
| 42 | throw new NotFoundException( |
| 43 | sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id) |
| 44 | ); |
| 45 | } |
| 46 | |
| 47 | $reflector = new ReflectionClass($id); |
| 48 | $construct = $reflector->getConstructor(); |
| 49 | |
| 50 | $resolution = $construct === null |
| 51 | ? new $id |
| 52 | : $resolution = $reflector->newInstanceArgs($this->reflectArguments($construct, $args)) |
| 53 | ; |
| 54 | |
| 55 | if ($this->cacheResolutions === true) { |
| 56 | $this->cache[$id] = $resolution; |
| 57 | } |
| 58 | |
| 59 | return $resolution; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * {@inheritdoc} |
| 64 | */ |
| 65 | public function has($id) : bool |
| 66 | { |
| 67 | return class_exists($id); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Invoke a callable via the container. |
| 72 | * |
| 73 | * @param callable $callable |
| 74 | * @param array $args |
| 75 | * |
| 76 | * @return mixed |
| 77 | * |
| 78 | * @throws ReflectionException |
| 79 | */ |
| 80 | public function call(callable $callable, array $args = []) |
| 81 | { |
| 82 | if (is_string($callable) && strpos($callable, '::') !== false) { |
| 83 | $callable = explode('::', $callable); |
| 84 | } |
| 85 | |
| 86 | if (is_array($callable)) { |
| 87 | if (is_string($callable[0])) { |
| 88 | $callable[0] = $this->getContainer()->get($callable[0]); |
| 89 | } |
| 90 | |
| 91 | $reflection = new ReflectionMethod($callable[0], $callable[1]); |
| 92 | |
| 93 | if ($reflection->isStatic()) { |
| 94 | $callable[0] = null; |
| 95 | } |
| 96 | |
| 97 | return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args)); |
| 98 | } |
| 99 | |
| 100 | if (is_object($callable)) { |
| 101 | $reflection = new ReflectionMethod($callable, '__invoke'); |
| 102 | |
| 103 | return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args)); |
| 104 | } |
| 105 | |
| 106 | $reflection = new ReflectionFunction(\Closure::fromCallable($callable)); |
| 107 | |
| 108 | return $reflection->invokeArgs($this->reflectArguments($reflection, $args)); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Whether the container should default to caching resolutions and returning |
| 113 | * the cache on following calls. |
| 114 | * |
| 115 | * @param boolean $option |
| 116 | * |
| 117 | * @return self |
| 118 | */ |
| 119 | public function cacheResolutions(bool $option = true) : ContainerInterface |
| 120 | { |
| 121 | $this->cacheResolutions = $option; |
| 122 | |
| 123 | return $this; |
| 124 | } |
| 125 | } |
| 126 |