forked from slimphp/Slim
/
AdvancedCallableResolver.php
157 lines (130 loc) · 4.48 KB
/
AdvancedCallableResolver.php
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
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
*/
declare(strict_types=1);
namespace Slim;
use Closure;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use RuntimeException;
use Slim\Interfaces\AdvancedCallableResolverInterface;
final class AdvancedCallableResolver implements AdvancedCallableResolverInterface
{
/**
* @var ContainerInterface|null
*/
private $container;
/**
* @param ContainerInterface|null $container
*/
public function __construct(?ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* {@inheritdoc}
*/
public function resolve($toResolve): callable
{
$resolved = $toResolve;
if (!is_callable($toResolve) && is_string($toResolve)) {
$resolved = $this->resolveInstanceAndMethod($toResolve);
if ($resolved[1] === null) {
$resolved[1] = '__invoke';
}
}
return $this->checkResolvedAndBindContainerIfClosure($resolved, $toResolve);
}
/**
* {@inheritdoc}
*/
public function resolveRoute($toResolve): callable
{
$resolved = $toResolve;
if (!is_callable($toResolve) && is_string($toResolve)) {
[$instance, $method] = $this->resolveInstanceAndMethod($toResolve);
// For a class that implements RequestHandlerInterface, we will call handle()
// if no method has been specified explicitly
if ($instance instanceof RequestHandlerInterface && $method === null) {
$method = 'handle';
}
$resolved = [$instance, $method ?? '__invoke'];
}
if ($resolved instanceof RequestHandlerInterface) {
$resolved = [$resolved, 'handle'];
}
return $this->checkResolvedAndBindContainerIfClosure($resolved, $toResolve);
}
/**
* {@inheritdoc}
*/
public function resolveMiddleware($toResolve): callable
{
$resolved = $toResolve;
if (!is_callable($toResolve) && is_string($toResolve)) {
[$instance, $method] = $this->resolveInstanceAndMethod($toResolve);
// For a class that implements MiddlewareInterface, we will call process()
// if no method has been specified explicitly
if ($instance instanceof MiddlewareInterface && $method === null) {
$method = 'process';
}
$resolved = [$instance, $method ?? '__invoke'];
}
if ($resolved instanceof MiddlewareInterface) {
$resolved = [$resolved, 'process'];
}
return $this->checkResolvedAndBindContainerIfClosure($resolved, $toResolve);
}
/**
* Resolves the given param and if successful returns an instance as well
* as a method name.
*
* @param string $toResolve
*
* @return array [Instance, Method Name]
*/
private function resolveInstanceAndMethod(string $toResolve): array
{
$class = $toResolve;
$instance = null;
$method = null;
// Check for Slim callable as `class:method`
if (preg_match(CallableResolver::$callablePattern, $toResolve, $matches)) {
$class = $matches[1];
$method = $matches[2];
}
if ($this->container && $this->container->has($class)) {
$instance = $this->container->get($class);
} else {
if (!class_exists($class)) {
throw new RuntimeException(sprintf('Callable %s does not exist', $class));
}
$instance = new $class($this->container);
}
return [$instance, $method];
}
/**
* @param mixed $resolved
* @param string|object|array|callable $toResolve
*
* @return callable
*/
private function checkResolvedAndBindContainerIfClosure($resolved, $toResolve): callable
{
if (!is_callable($resolved)) {
throw new RuntimeException(sprintf(
'%s is not resolvable',
is_callable($toResolve) || is_object($toResolve) || is_array($toResolve) ?
json_encode($toResolve) : $toResolve
));
}
if ($this->container && $resolved instanceof Closure) {
$resolved = $resolved->bindTo($this->container);
}
return $resolved;
}
}