1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Slim Framework (http://slimframework.com) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/slimphp/Slim |
6
|
|
|
* @copyright Copyright (c) 2011-2016 Josh Lockhart |
7
|
|
|
* @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License) |
8
|
|
|
*/ |
9
|
|
|
namespace Orx0r\Slim\Controller; |
10
|
|
|
|
11
|
|
|
use RuntimeException; |
12
|
|
|
use Interop\Container\ContainerInterface; |
13
|
|
|
use Slim\Interfaces\CallableResolverInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* This class resolves a string of the format 'class:method' into a closure |
17
|
|
|
* that can be dispatched. |
18
|
|
|
* |
19
|
|
|
* Class CallableResolver |
20
|
|
|
* @package Orx0r\Slim\Controller |
21
|
|
|
*/ |
22
|
|
|
final class CallableResolver implements CallableResolverInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContainerInterface |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $controllerNamespace; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ContainerInterface $container |
37
|
|
|
*/ |
38
|
13 |
|
public function __construct(ContainerInterface $container, $controllerNamespace = null) |
39
|
|
|
{ |
40
|
13 |
|
$this->container = $container; |
41
|
13 |
|
$this->controllerNamespace = $controllerNamespace; |
42
|
13 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Resolve toResolve into a closure that that the router can dispatch. |
46
|
|
|
* |
47
|
|
|
* If toResolve is of the format 'class:method', then try to extract 'class' |
48
|
|
|
* from the container otherwise instantiate it and then dispatch 'method'. |
49
|
|
|
* |
50
|
|
|
* @param mixed $toResolve |
51
|
|
|
* |
52
|
|
|
* @return callable |
53
|
|
|
* |
54
|
|
|
* @throws RuntimeException if the callable does not exist |
55
|
|
|
* @throws RuntimeException if the callable is not resolvable |
56
|
|
|
*/ |
57
|
13 |
|
public function resolve($toResolve) |
58
|
|
|
{ |
59
|
13 |
|
$resolved = $toResolve; |
60
|
|
|
|
61
|
13 |
|
if (!is_callable($toResolve) && is_string($toResolve)) { |
62
|
|
|
// check for slim callable as "class:method" |
63
|
10 |
|
$callablePattern = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!'; |
64
|
10 |
|
if (preg_match($callablePattern, $toResolve, $matches)) { |
65
|
7 |
|
$class = $matches[1]; |
66
|
7 |
|
$method = $matches[2]; |
67
|
|
|
|
68
|
7 |
|
if ($this->container->has($class)) { |
69
|
2 |
|
$resolved = [$this->container->get($class), $method]; |
70
|
7 |
|
} elseif (class_exists($class)) { |
71
|
2 |
|
$resolved = [new $class($this->container), $method]; |
72
|
2 |
|
} else { |
73
|
3 |
|
if (!$this->controllerNamespace) { |
74
|
1 |
|
throw new RuntimeException(sprintf('Callable %s does not exist', $class)); |
75
|
|
|
} |
76
|
2 |
|
$class = "$this->controllerNamespace\\$class"; |
77
|
2 |
|
if (!class_exists($class)) { |
78
|
1 |
|
throw new RuntimeException(sprintf('Callable %s does not exist', $class)); |
79
|
|
|
} |
80
|
1 |
|
$resolved = [new $class($this->container), $method]; |
81
|
|
|
} |
82
|
5 |
|
} else { |
83
|
|
|
// check if string is something in the DIC that's callable or is a class name which |
84
|
|
|
// has an __invoke() method |
85
|
3 |
|
$class = $toResolve; |
86
|
3 |
|
if ($this->container->has($class)) { |
87
|
1 |
|
$resolved = $this->container->get($class); |
88
|
1 |
|
} else { |
89
|
2 |
|
if (!class_exists($class)) { |
90
|
1 |
|
throw new RuntimeException(sprintf('Callable %s does not exist', $class)); |
91
|
|
|
} |
92
|
1 |
|
$resolved = new $class($this->container); |
93
|
|
|
} |
94
|
|
|
} |
95
|
7 |
|
} |
96
|
|
|
|
97
|
10 |
|
if (!is_callable($resolved)) { |
98
|
1 |
|
throw new RuntimeException(sprintf('%s is not resolvable', $toResolve)); |
99
|
|
|
} |
100
|
|
|
|
101
|
9 |
|
return $resolved; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|