|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Phoole (PHP7.2+) |
|
5
|
|
|
* |
|
6
|
|
|
* @category Library |
|
7
|
|
|
* @package Phoole\Route |
|
8
|
|
|
* @copyright Copyright (c) 2019 Hong Zhang |
|
9
|
|
|
*/ |
|
10
|
|
|
declare(strict_types = 1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Phoole\Route\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Phoole\Base\Reference\ReferenceTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* DefaultResolver |
|
18
|
|
|
* |
|
19
|
|
|
* Resolving [controllerName, methodName]into a request handler callable |
|
20
|
|
|
* |
|
21
|
|
|
* @package Phoole\Route |
|
22
|
|
|
*/ |
|
23
|
|
|
class DefaultResolver implements ResolverInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use ReferenceTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $namespace; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $controllerSuffix; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $actionSuffix; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* used for resolving '${controller}' etc. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $params; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* set the namespace to search thru |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $namespace |
|
53
|
|
|
* @param string $controllerSuffix |
|
54
|
|
|
* @param string $actionSuffix |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
string $namespace = '', |
|
58
|
|
|
string $controllerSuffix = 'Controller', |
|
59
|
|
|
string $actionSuffix = 'Action' |
|
60
|
|
|
) { |
|
61
|
|
|
$this->namespace = $namespace; |
|
62
|
|
|
$this->controllerSuffix = $controllerSuffix; |
|
63
|
|
|
$this->actionSuffix = $actionSuffix; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Resolve [controller, action] to a callable |
|
68
|
|
|
* |
|
69
|
|
|
* {@inheritDoc} |
|
70
|
|
|
*/ |
|
71
|
|
|
public function resolve($notCallable, array $params = []): callable |
|
72
|
|
|
{ |
|
73
|
|
|
try { |
|
74
|
|
|
/** |
|
75
|
|
|
* dereference if any parameters used in callable definition |
|
76
|
|
|
* such as ['${controller}', '${action}'] |
|
77
|
|
|
*/ |
|
78
|
|
|
$this->params = $params; |
|
79
|
|
|
$this->deReference($notCallable); |
|
80
|
|
|
|
|
81
|
|
|
return $this->appendSuffix($notCallable); |
|
82
|
|
|
} catch (\Throwable $e) { |
|
83
|
|
|
throw new \InvalidArgumentException($e->getMessage()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param array $notCallable |
|
89
|
|
|
* @return callable |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function appendSuffix(array $notCallable): callable |
|
92
|
|
|
{ |
|
93
|
|
|
$controller = $this->namespace . '\\' . $notCallable[0] . $this->controllerSuffix; |
|
94
|
|
|
$action = $notCallable[1] . $this->actionSuffix; |
|
95
|
|
|
return [new $controller(), $action]; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* resolve parameters |
|
100
|
|
|
* |
|
101
|
|
|
* {@inheritDoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getReference(string $name) |
|
104
|
|
|
{ |
|
105
|
|
|
if (isset($this->params[$name])) { |
|
106
|
|
|
return $this->params[$name]; |
|
107
|
|
|
} else { |
|
108
|
|
|
return NULL; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |