1 | <?php |
||
15 | class ReflectionContainer implements ArgumentResolverInterface, ContainerInterface |
||
16 | { |
||
17 | use ArgumentResolverTrait; |
||
18 | use ContainerAwareTrait; |
||
19 | |||
20 | /** |
||
21 | * @var boolean |
||
22 | */ |
||
23 | protected $cacheResolutions; |
||
24 | |||
25 | /** |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $cache = []; |
||
29 | |||
30 | 48 | public function __construct(bool $cacheResolutions = false) |
|
31 | { |
||
32 | 48 | $this->cacheResolutions = $cacheResolutions; |
|
33 | 48 | } |
|
34 | |||
35 | 39 | public function get($id, array $args = []) |
|
36 | { |
||
37 | 39 | if ($this->cacheResolutions === true && array_key_exists($id, $this->cache)) { |
|
38 | 6 | return $this->cache[$id]; |
|
39 | } |
||
40 | |||
41 | 39 | if (! $this->has($id)) { |
|
42 | 3 | throw new NotFoundException( |
|
43 | 3 | sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id) |
|
44 | ); |
||
45 | } |
||
46 | |||
47 | 36 | $reflector = new ReflectionClass($id); |
|
48 | 36 | $construct = $reflector->getConstructor(); |
|
49 | |||
50 | 36 | $resolution = $construct === null |
|
51 | 30 | ? new $id() |
|
52 | 36 | : $resolution = $reflector->newInstanceArgs($this->reflectArguments($construct, $args)) |
|
53 | ; |
||
54 | |||
55 | 36 | if ($this->cacheResolutions === true) { |
|
56 | 6 | $this->cache[$id] = $resolution; |
|
57 | } |
||
58 | |||
59 | 36 | return $resolution; |
|
60 | } |
||
61 | |||
62 | 45 | public function has($id): bool |
|
66 | |||
67 | 18 | public function call(callable $callable, array $args = []) |
|
68 | { |
||
101 | } |
||
102 |