1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JetFire\Routing; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Router |
7
|
|
|
* @package JetFire\Routing |
8
|
|
|
*/ |
9
|
|
|
class Router |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var Route |
14
|
|
|
*/ |
15
|
|
|
public $route; |
16
|
|
|
/** |
17
|
|
|
* @var RouteCollection |
18
|
|
|
*/ |
19
|
|
|
public $collection; |
20
|
|
|
/** |
21
|
|
|
* @var ResponseInterface |
22
|
|
|
*/ |
23
|
|
|
public $response; |
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public $middlewareCollection = []; |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
public $matcher = []; |
32
|
|
|
/** |
33
|
|
|
* @var |
34
|
|
|
*/ |
35
|
|
|
public $dispatcher; |
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $config = [ |
40
|
|
|
'templateExtension' => ['.html', '.php', '.json', '.xml'], |
41
|
|
|
'templateCallback' => [], |
42
|
|
|
'di' => '', |
43
|
|
|
'generateRoutesPath' => false, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param RouteCollection $collection |
48
|
|
|
* @param ResponseInterface $response |
49
|
|
|
* @param Route $route |
50
|
|
|
*/ |
51
|
|
|
public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null) |
52
|
|
|
{ |
53
|
|
|
$this->collection = $collection; |
54
|
|
|
$this->response = is_null($response) ? new Response() : $response; |
55
|
|
|
$this->route = is_null($route) ? new Route() : $route; |
56
|
|
|
$this->config['di'] = function ($class) { |
57
|
|
|
return new $class; |
58
|
|
|
}; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param array $config |
63
|
|
|
*/ |
64
|
|
|
public function setConfig($config) |
65
|
|
|
{ |
66
|
|
|
$this->config = array_merge($this->config, $config); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getConfig() |
73
|
|
|
{ |
74
|
|
|
return $this->config; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param object|array $middleware |
79
|
|
|
*/ |
80
|
|
|
public function setMiddleware($middleware) |
81
|
|
|
{ |
82
|
|
|
$this->middlewareCollection = is_array($middleware) |
83
|
|
|
? $middleware |
84
|
|
|
: [$middleware]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param MiddlewareInterface $middleware |
89
|
|
|
*/ |
90
|
|
|
public function addMiddleware(MiddlewareInterface $middleware) |
91
|
|
|
{ |
92
|
|
|
$this->middlewareCollection[] = $middleware; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param object|array $matcher |
97
|
|
|
*/ |
98
|
|
|
public function setMatcher($matcher) |
99
|
|
|
{ |
100
|
|
|
$this->matcher = is_array($matcher) |
101
|
|
|
? $matcher |
102
|
|
|
: [$matcher]; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $matcher |
107
|
|
|
*/ |
108
|
|
|
public function addMatcher($matcher) |
109
|
|
|
{ |
110
|
|
|
$this->matcher[] = $matcher; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @description main function |
115
|
|
|
*/ |
116
|
|
|
public function run() |
117
|
|
|
{ |
118
|
|
|
$this->setUrl(); |
119
|
|
|
if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
120
|
|
|
if ($this->match() === true) { |
121
|
|
|
$this->callMiddleware('before'); |
122
|
|
|
if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) { |
123
|
|
|
$this->callTarget(); |
124
|
|
|
} |
125
|
|
|
}else{ |
126
|
|
|
$this->response->setStatusCode(404); |
127
|
|
|
} |
128
|
|
|
$this->callMiddleware('after'); |
129
|
|
|
return $this->response->send(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @description call the middleware before and after the target |
134
|
|
|
* @param $action |
135
|
|
|
*/ |
136
|
|
|
public function callMiddleware($action) |
137
|
|
|
{ |
138
|
|
|
foreach ($this->middlewareCollection as $middleware) { |
139
|
|
|
if ($middleware instanceof MiddlewareInterface) { |
140
|
|
|
foreach ($middleware->getCallbacks($action) as $callback) { |
141
|
|
|
if (method_exists($middleware, $callback)) { |
142
|
|
|
call_user_func_array([$middleware, $callback], [$action]); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param null $url |
151
|
|
|
*/ |
152
|
|
|
public function setUrl($url = null) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
if (is_null($url)) |
155
|
|
|
$url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
156
|
|
|
$this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public function match() |
163
|
|
|
{ |
164
|
|
|
foreach ($this->matcher as $key => $matcher) { |
165
|
|
|
if (call_user_func([$this->matcher[$key], 'match'])) return true; |
166
|
|
|
} |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @description call the target for the request uri |
172
|
|
|
*/ |
173
|
|
|
public function callTarget() |
174
|
|
|
{ |
175
|
|
|
$target = is_array($this->route->getTarget('dispatcher')) ? $this->route->getTarget('dispatcher') : [$this->route->getTarget('dispatcher')]; |
176
|
|
|
if (!empty($target)) { |
177
|
|
|
foreach ($target as $call) { |
178
|
|
|
$this->dispatcher = new $call($this); |
179
|
|
|
call_user_func([$this->dispatcher, 'call']); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: