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