-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflectionContainer.php
More file actions
164 lines (136 loc) · 4.73 KB
/
Copy pathReflectionContainer.php
File metadata and controls
164 lines (136 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Cake\Container;
use Cake\Container\Argument\ArgumentResolverInterface;
use Cake\Container\Argument\ArgumentResolverTrait;
use Cake\Container\Exception\ContainerException;
use Cake\Container\Exception\NotFoundException;
use Psr\Container\ContainerInterface;
use ReflectionClass;
use ReflectionFunction;
use ReflectionMethod;
class ReflectionContainer implements ArgumentResolverInterface, ContainerInterface
{
use ArgumentResolverTrait;
use ContainerAwareTrait;
/**
* @var bool
*/
protected bool $cacheResolutions;
/**
* @var array
*/
protected array $cache = [];
/**
* @param bool $cacheResolutions
*/
public function __construct(bool $cacheResolutions = false)
{
$this->cacheResolutions = $cacheResolutions;
}
/**
* @inheritDoc
*/
public function get(string $id, array $args = [])
{
// Only use cache when no custom args are provided
if ($this->cacheResolutions && $args === [] && array_key_exists($id, $this->cache)) {
return $this->cache[$id];
}
if (!$this->has($id)) {
throw new NotFoundException(
sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id),
);
}
/** @var class-string $id */
$reflector = new ReflectionClass($id);
$construct = $reflector->getConstructor();
if ($construct && !$construct->isPublic()) {
throw new NotFoundException(
sprintf('Alias (%s) has a non-public constructor and therefore cannot be instantiated', $id),
);
}
$resolution = $construct === null
? new $id()
: $reflector->newInstanceArgs($this->reflectArguments($construct, $args));
// Only cache when no custom args are provided
if ($this->cacheResolutions && $args === []) {
$this->cache[$id] = $resolution;
}
return $resolution;
}
/**
* @inheritDoc
*/
public function has($id): bool
{
return class_exists($id);
}
/**
* Get a new instance, bypassing the cache.
*
* @param string $id
* @param array<string, mixed> $args
* @return mixed
*/
public function getNew(string $id, array $args = []): mixed
{
if (!$this->has($id)) {
throw new NotFoundException(
sprintf('Alias (%s) is not an existing class and therefore cannot be resolved', $id),
);
}
/** @var class-string $id */
$reflector = new ReflectionClass($id);
$construct = $reflector->getConstructor();
if ($construct && !$construct->isPublic()) {
throw new NotFoundException(
sprintf('Alias (%s) has a non-public constructor and therefore cannot be instantiated', $id),
);
}
return $construct === null
? new $id()
: $reflector->newInstanceArgs($this->reflectArguments($construct, $args));
}
/**
* @param callable|string $callable
* @param array $args
* @return mixed
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \ReflectionException
*/
public function call(callable|string $callable, array $args = []): mixed
{
if (is_string($callable) && str_contains($callable, '::')) {
$callable = explode('::', $callable);
}
if (is_array($callable)) {
if (is_string($callable[0])) {
// if we have a definition container, try that first, otherwise, reflect
try {
$callable[0] = $this->getContainer()->get($callable[0]);
} catch (ContainerException) {
$callable[0] = $this->get($callable[0]);
}
}
$reflection = new ReflectionMethod($callable[0], $callable[1]);
if ($reflection->isStatic()) {
$callable[0] = null;
}
return $reflection->invokeArgs($callable[0], $this->reflectArguments($reflection, $args));
}
if (is_object($callable)) {
$reflection = new ReflectionMethod($callable, '__invoke');
return $reflection->invokeArgs($callable, $this->reflectArguments($reflection, $args));
}
if (is_callable($callable)) {
$reflection = new ReflectionFunction($callable(...));
return $reflection->invokeArgs($this->reflectArguments($reflection, $args));
}
throw new NotFoundException(sprintf(
'Callable (%s) is not a valid callable',
$callable,
));
}
}