|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JetFire\Routing\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use JetFire\Routing\Dispatcher\ControllerDispatcher; |
|
6
|
|
|
use JetFire\Routing\Dispatcher\TemplateDispatcher; |
|
7
|
|
|
use JetFire\Routing\Router; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class SmartMatch |
|
11
|
|
|
* @package JetFire\Routing\Match |
|
12
|
|
|
*/ |
|
13
|
|
|
class UriMatcher implements MatcherInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var |
|
18
|
|
|
*/ |
|
19
|
|
|
private $router; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private $request = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $resolver = ['isControllerAndTemplate']; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private $dispatcher = [ |
|
35
|
|
|
'isTemplate' => TemplateDispatcher::class, |
|
36
|
|
|
'isController' => ControllerDispatcher::class, |
|
37
|
|
|
'isControllerAndTemplate' => [ControllerDispatcher::class, TemplateDispatcher::class], |
|
38
|
|
|
]; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param Router $router |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct(Router $router) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->router = $router; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $resolver |
|
50
|
|
|
*/ |
|
51
|
|
|
public function setResolver($resolver = []) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->resolver = $resolver; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $resolver |
|
58
|
|
|
*/ |
|
59
|
|
|
public function addResolver($resolver) |
|
60
|
|
|
{ |
|
61
|
|
|
$this->resolver[] = $resolver; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getResolver() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->resolver; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $dispatcher |
|
74
|
|
|
*/ |
|
75
|
|
|
public function setDispatcher($dispatcher = []) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->dispatcher = $dispatcher; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $method |
|
82
|
|
|
* @param $class |
|
83
|
|
|
*/ |
|
84
|
|
|
public function addDispatcher($method, $class) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->dispatcher[$method] = $class; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function match() |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($this->resolver as $resolver) { |
|
95
|
|
|
if (is_array($target = call_user_func([$this, $resolver]))) { |
|
96
|
|
|
$this->setTarget($target); |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $target |
|
105
|
|
|
*/ |
|
106
|
|
|
public function setTarget($target = []) |
|
107
|
|
|
{ |
|
108
|
|
|
$index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
|
109
|
|
|
$this->router->route->setDetail($this->request); |
|
110
|
|
|
$this->router->route->setTarget($target); |
|
111
|
|
|
$this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
112
|
|
|
$this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
113
|
|
|
$this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return array|bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function isControllerAndTemplate() |
|
120
|
|
|
{ |
|
121
|
|
|
if (is_array($ctrl = $this->isController())) { |
|
122
|
|
|
if (is_array($tpl = $this->isTemplate())) { |
|
123
|
|
|
return array_merge(array_merge($ctrl, $tpl), [ |
|
124
|
|
|
'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
|
125
|
|
|
]); |
|
126
|
|
|
} |
|
127
|
|
|
return $ctrl; |
|
128
|
|
|
} |
|
129
|
|
|
return $this->isTemplate(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return bool|array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function isTemplate() |
|
136
|
|
|
{ |
|
137
|
|
|
foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
|
138
|
|
|
for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
|
139
|
|
|
$url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '', $this->router->route->getUrl())); |
|
140
|
|
|
$end = array_pop($url); |
|
141
|
|
|
$url = implode('/', array_map('ucwords', $url)) . '/' . $end; |
|
142
|
|
|
$viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_' . $i)) ? $viewDir : [$viewDir]; |
|
143
|
|
|
foreach ($viewDir as $dir) { |
|
144
|
|
|
if (is_file(($template = rtrim($dir, '/') . $url . $extension))) { |
|
145
|
|
|
$this->request['collection_index'] = $i; |
|
146
|
|
|
return [ |
|
147
|
|
|
'dispatcher' => $this->dispatcher['isTemplate'], |
|
148
|
|
|
'template' => $template, |
|
149
|
|
|
'extension' => substr(strrchr($extension, "."), 1), |
|
150
|
|
|
'callback' => $this->router->getConfig()['templateCallback'] |
|
151
|
|
|
]; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
return false; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return bool|array |
|
161
|
|
|
*/ |
|
162
|
|
|
public function isController() |
|
163
|
|
|
{ |
|
164
|
|
|
$routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
|
165
|
|
|
$i = 0; |
|
166
|
|
|
do { |
|
167
|
|
|
$route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
168
|
|
|
if (isset($route[0])) { |
|
169
|
|
|
$class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller')) |
|
170
|
|
|
? $this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller' |
|
171
|
|
|
: ucfirst($route[0]) . 'Controller'; |
|
172
|
|
|
$route[1] = isset($route[1]) ? $route[1] : 'index'; |
|
173
|
|
|
if (method_exists($class, $route[1])) { |
|
174
|
|
|
$this->request['parameters'] = array_slice($route, 2); |
|
175
|
|
|
$this->request['collection_index'] = $i; |
|
176
|
|
|
return [ |
|
177
|
|
|
'dispatcher' => $this->dispatcher['isController'], |
|
178
|
|
|
'di' => $this->router->getConfig()['di'], |
|
179
|
|
|
'controller' => $class, |
|
180
|
|
|
'action' => $route[1] |
|
181
|
|
|
]; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
++$i; |
|
185
|
|
|
} while ($i < $this->router->collection->countRoutes); |
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|