1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim Framework (https://slimframework.com) |
4
|
|
|
* |
5
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Slim; |
9
|
|
|
|
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use RuntimeException; |
12
|
|
|
use Slim\Interfaces\CallableResolverInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class resolves a string of the format 'class:method' into a closure |
16
|
|
|
* that can be dispatched. |
17
|
|
|
*/ |
18
|
|
|
final class CallableResolver implements CallableResolverInterface |
19
|
|
|
{ |
20
|
|
|
const CALLABLE_PATTERN = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface |
24
|
|
|
*/ |
25
|
|
|
private $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Resolve toResolve into a closure so that the router can dispatch. |
37
|
|
|
* |
38
|
|
|
* If toResolve is of the format 'class:method', then try to extract 'class' |
39
|
|
|
* from the container otherwise instantiate it and then dispatch 'method'. |
40
|
|
|
* |
41
|
|
|
* @param callable|string $toResolve |
42
|
|
|
* |
43
|
|
|
* @return callable |
44
|
|
|
* |
45
|
|
|
* @throws RuntimeException If the callable does not exist |
46
|
|
|
* @throws RuntimeException If the callable is not resolvable |
47
|
|
|
*/ |
48
|
|
|
public function resolve($toResolve) |
49
|
|
|
{ |
50
|
|
|
if (is_callable($toResolve)) { |
51
|
|
|
return $toResolve; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$resolved = $toResolve; |
55
|
|
|
|
56
|
|
|
if (is_string($toResolve)) { |
57
|
|
|
list($class, $method) = $this->parseCallable($toResolve); |
58
|
|
|
$resolved = $this->resolveCallable($class, $method); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$this->assertCallable($resolved); |
62
|
|
|
return $resolved; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Extract class and method from toResolve |
67
|
|
|
* |
68
|
|
|
* @param string $toResolve |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
protected function parseCallable($toResolve) |
73
|
|
|
{ |
74
|
|
|
if (preg_match(self::CALLABLE_PATTERN, $toResolve, $matches)) { |
75
|
|
|
return [$matches[1], $matches[2]]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return [$toResolve, '__invoke']; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if string is something in the DIC |
83
|
|
|
* that's callable or is a class name which has an __invoke() method. |
84
|
|
|
* |
85
|
|
|
* @param string $class |
86
|
|
|
* @param string $method |
87
|
|
|
* |
88
|
|
|
* @return callable |
89
|
|
|
* |
90
|
|
|
* @throws RuntimeException if the callable does not exist |
91
|
|
|
*/ |
92
|
|
|
protected function resolveCallable($class, $method) |
93
|
|
|
{ |
94
|
|
|
if ($this->container->has($class)) { |
95
|
|
|
return [$this->container->get($class), $method]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!class_exists($class)) { |
99
|
|
|
throw new RuntimeException(sprintf('Callable %s does not exist', $class)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return [new $class($this->container), $method]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param Callable $callable |
107
|
|
|
* |
108
|
|
|
* @throws RuntimeException if the callable is not resolvable |
109
|
|
|
*/ |
110
|
|
|
protected function assertCallable($callable) |
111
|
|
|
{ |
112
|
|
|
if (!is_callable($callable)) { |
113
|
|
|
throw new RuntimeException(sprintf( |
114
|
|
|
'%s is not resolvable', |
115
|
|
|
is_array($callable) || is_object($callable) ? json_encode($callable) : $callable |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|