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
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $matcher = ['matchController','matchTemplate']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $dispatcher = [ |
29
|
|
|
'matchTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
30
|
|
|
'matchController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Router $router |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Router $router) |
37
|
|
|
{ |
38
|
|
|
$this->router = $router; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $matcher |
43
|
|
|
*/ |
44
|
|
|
public function addMatcher($matcher){ |
45
|
|
|
$this->matcher[] = $matcher; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
|
|
public function getMatcher() |
52
|
|
|
{ |
53
|
|
|
return $this->matcher; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $dispatcher |
58
|
|
|
*/ |
59
|
|
|
public function setDispatcher($dispatcher = []) |
60
|
|
|
{ |
61
|
|
|
$this->dispatcher = $dispatcher; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $method |
66
|
|
|
* @param $class |
67
|
|
|
* @return mixed|void |
68
|
|
|
*/ |
69
|
|
|
public function addDispatcher($method,$class){ |
70
|
|
|
$this->dispatcher[$method] = $class; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function match() |
77
|
|
|
{ |
78
|
|
|
foreach($this->matcher as $matcher){ |
79
|
|
|
if(call_user_func([$this,$matcher])) { |
80
|
|
|
$this->router->response->setStatusCode(202); |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function matchTemplate() |
91
|
|
|
{ |
92
|
|
|
foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
93
|
|
|
for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
94
|
|
|
$url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl())); |
95
|
|
|
$end = array_pop($url); |
96
|
|
|
$url = implode('/', array_map('ucwords', $url)).'/'.$end; |
97
|
|
|
if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) { |
98
|
|
|
$block = $this->router->collection->getRoutes('block_'.$i); |
99
|
|
|
$viewDir = $this->router->collection->getRoutes('view_dir_'.$i); |
100
|
|
|
$this->router->route->setTarget([ |
101
|
|
|
'dispatcher' => $this->dispatcher['matchTemplate'], |
102
|
|
|
'block' => $block, |
103
|
|
|
'view_dir' => $viewDir, |
104
|
|
|
'template' => $template, |
105
|
|
|
'extension' => str_replace('.', '', $extension), |
106
|
|
|
'callback' => $this->router->getConfig()['templateCallback'] |
107
|
|
|
]); |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
public function matchController() |
119
|
|
|
{ |
120
|
|
|
$routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
121
|
|
|
$i = 0; |
122
|
|
|
do{ |
123
|
|
|
$route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
124
|
|
|
if (isset($route[0])) { |
125
|
|
|
$class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller')) |
126
|
|
|
? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller' |
127
|
|
|
: ucfirst($route[0]) . 'Controller'; |
128
|
|
|
if (isset($route[1]) && method_exists($class, $route[1])) { |
129
|
|
|
$block = $this->router->collection->getRoutes('block_'.$i); |
130
|
|
|
$viewDir = $this->router->collection->getRoutes('view_dir_'.$i); |
131
|
|
|
$this->router->route->setTarget([ |
132
|
|
|
'dispatcher' => $this->dispatcher['matchController'], |
133
|
|
|
'block' => $block, |
134
|
|
|
'view_dir' => $viewDir, |
135
|
|
|
'di' => $this->router->getConfig()['di'], |
136
|
|
|
'controller' => $class, |
137
|
|
|
'action' => $route[1] |
138
|
|
|
]); |
139
|
|
|
$this->router->route->addDetail('parameters', array_slice($route, 2)); |
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
++$i; |
144
|
|
|
}while($i < $this->router->collection->countRoutes); |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|