-
-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathCallableFactory.php
More file actions
135 lines (111 loc) · 3.97 KB
/
Copy pathCallableFactory.php
File metadata and controls
135 lines (111 loc) · 3.97 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
<?php
declare(strict_types=1);
namespace Yiisoft\Queue\Middleware;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use ReflectionException;
use ReflectionMethod;
use Yiisoft\Injector\Injector;
use function is_array;
use function is_callable;
use function is_object;
use function is_string;
/**
* @internal Create real callable listener from configuration, ready to be invoked with dependency injection.
*/
final class CallableFactory
{
private readonly Injector $injector;
public function __construct(
private readonly ContainerInterface $container,
?ContainerInterface $dependencyContainer = null,
) {
$this->injector = new Injector($dependencyContainer ?? $container);
}
/**
* Create a real callable listener from definition. Calling the returned callable invokes the resolved listener,
* injecting its dependencies by type hinting.
*
* @param mixed $definition Definition to create listener from.
*
* @throws InvalidCallableConfigurationException Failed to create listener.
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*
* @psalm-return callable(mixed...): mixed
*/
public function create(mixed $definition): callable
{
$callable = $this->resolve($definition);
return fn(mixed ...$params): mixed => $this->injector->invoke($callable, $params);
}
/**
* @throws InvalidCallableConfigurationException Failed to resolve listener.
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
private function resolve(mixed $definition): callable
{
if ($definition === null) {
throw new InvalidCallableConfigurationException();
}
if (is_callable($definition)) {
return $definition;
}
if (is_string($definition) && $this->container->has($definition)) {
$result = $this->container->get($definition);
if (is_callable($result)) {
return $result;
}
throw new InvalidCallableConfigurationException();
}
if (is_array($definition)
&& array_keys($definition) === [0, 1]
&& is_string($definition[1])
) {
/** @psalm-var array{0:mixed,1:string} $definition */
if (is_object($definition[0])) {
$callable = $this->fromObjectDefinition($definition[0], $definition[1]);
if ($callable !== null) {
return $callable;
}
}
if (is_string($definition[0])) {
$callable = $this->fromDefinition($definition[0], $definition[1]);
if ($callable !== null) {
return $callable;
}
}
}
throw new InvalidCallableConfigurationException();
}
/**
* @throws ContainerExceptionInterface Error while retrieving the entry from container.
*/
private function fromDefinition(string $className, string $methodName): ?callable
{
$result = null;
if (class_exists($className)) {
try {
$reflection = new ReflectionMethod($className, $methodName);
if ($reflection->isStatic()) {
$result = [$className, $methodName];
}
} catch (ReflectionException) {
}
}
if ($result === null && $this->container->has($className)) {
$result = [
$this->container->get($className),
$methodName,
];
}
return is_callable($result) ? $result : null;
}
private function fromObjectDefinition(object $object, string $methodName): ?callable
{
if (!method_exists($object, $methodName)) {
return null;
}
$result = [$object, $methodName];
return is_callable($result) ? $result : null;
}
}