@@ -6,73 +6,73 @@ discard block |
||
6 | 6 | |
7 | 7 | class Route |
8 | 8 | { |
9 | - const NEXT = '_NEXT_'; |
|
9 | + const NEXT = '_NEXT_'; |
|
10 | 10 | |
11 | - private $app; |
|
11 | + private $app; |
|
12 | 12 | |
13 | - private $routes = []; |
|
13 | + private $routes = []; |
|
14 | 14 | |
15 | - public $current = []; |
|
15 | + public $current = []; |
|
16 | 16 | |
17 | - private $prefix; |
|
17 | + private $prefix; |
|
18 | 18 | |
19 | - private $basController; |
|
19 | + private $basController; |
|
20 | 20 | |
21 | - private $middlewareFrom; |
|
21 | + private $middlewareFrom; |
|
22 | 22 | |
23 | - private $groupMiddleware = []; |
|
23 | + private $groupMiddleware = []; |
|
24 | 24 | |
25 | - public function __construct(Application $app) |
|
26 | - { |
|
25 | + public function __construct(Application $app) |
|
26 | + { |
|
27 | 27 | $this->app = $app; |
28 | - } |
|
28 | + } |
|
29 | 29 | |
30 | - public function add($url, $action, $requestMethos = 'GET', $middleware = []) |
|
31 | - { |
|
30 | + public function add($url, $action, $requestMethos = 'GET', $middleware = []) |
|
31 | + { |
|
32 | 32 | |
33 | 33 | if ($this->prefix) { |
34 | 34 | |
35 | - if ($this->prefix !== '/') { |
|
35 | + if ($this->prefix !== '/') { |
|
36 | 36 | |
37 | 37 | $url = $this->prefix . $url; |
38 | 38 | |
39 | 39 | $url = rtrim($url, '/'); |
40 | - } |
|
40 | + } |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | if ($this->basController) { |
44 | - $action = $this->basController . '/' . $action; |
|
44 | + $action = $this->basController . '/' . $action; |
|
45 | 45 | } |
46 | 46 | |
47 | 47 | if ($this->groupMiddleware) { |
48 | 48 | |
49 | - if (is_array($middleware)) { |
|
49 | + if (is_array($middleware)) { |
|
50 | 50 | |
51 | 51 | $middleware = array_merge($this->groupMiddleware[0], $middleware); |
52 | 52 | |
53 | - } else { |
|
53 | + } else { |
|
54 | 54 | |
55 | 55 | array_push($this->groupMiddleware[0], $middleware); |
56 | 56 | |
57 | 57 | $middleware = $this->groupMiddleware[0]; |
58 | - } |
|
58 | + } |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | $routes = [ |
62 | - 'url' => $url, |
|
63 | - 'pattern' => $this->generatePattern($url), |
|
64 | - 'action' => $this->getAction($action), |
|
65 | - 'method' => $requestMethos, |
|
66 | - 'middleware' => $middleware |
|
62 | + 'url' => $url, |
|
63 | + 'pattern' => $this->generatePattern($url), |
|
64 | + 'action' => $this->getAction($action), |
|
65 | + 'method' => $requestMethos, |
|
66 | + 'middleware' => $middleware |
|
67 | 67 | ]; |
68 | 68 | |
69 | 69 | $this->routes[] = $routes; |
70 | 70 | |
71 | 71 | return $this; |
72 | - } |
|
72 | + } |
|
73 | 73 | |
74 | - public function group($groupOptions, callable $callback) |
|
75 | - { |
|
74 | + public function group($groupOptions, callable $callback) |
|
75 | + { |
|
76 | 76 | $prefix = $groupOptions['prefix']; |
77 | 77 | $controller = $groupOptions['controller']; |
78 | 78 | $middlewareFrom = array_shift($groupOptions['middleware']); |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | $check = '/' . explode('/', $url)[0]; |
83 | 83 | |
84 | 84 | if ($check !== $prefix) { |
85 | - return $this; |
|
85 | + return $this; |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | $this->prefix = $prefix; |
@@ -93,10 +93,10 @@ discard block |
||
93 | 93 | $callback($this); |
94 | 94 | |
95 | 95 | return $this; |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | - public function package($url, $controller) |
|
99 | - { |
|
98 | + public function package($url, $controller) |
|
99 | + { |
|
100 | 100 | $this->add("$url", "$controller"); |
101 | 101 | $this->add("$url/add", "$controller@add", "POST"); |
102 | 102 | $this->add("$url/submit", "$controller@submit", "POST"); |
@@ -105,19 +105,19 @@ discard block |
||
105 | 105 | $this->add("$url/delete/:id", "$controller@delete", "POST"); |
106 | 106 | |
107 | 107 | return $this; |
108 | - } |
|
108 | + } |
|
109 | 109 | |
110 | - public function generatePattern($url) |
|
111 | - { |
|
110 | + public function generatePattern($url) |
|
111 | + { |
|
112 | 112 | $pattern = '#^'; |
113 | 113 | $pattern .= str_replace([':text', ':id'], ['([a-zA-Z0-9-]+)', '(\d+)'], $url); |
114 | 114 | $pattern .= '$#'; |
115 | 115 | |
116 | 116 | return $pattern; |
117 | - } |
|
117 | + } |
|
118 | 118 | |
119 | - public function getAction($action) |
|
120 | - { |
|
119 | + public function getAction($action) |
|
120 | + { |
|
121 | 121 | $action = str_replace('/', '\\', $action); |
122 | 122 | |
123 | 123 | $action = (strpos($action, '@') != 0) ? $action : $action . '@index'; |
@@ -125,13 +125,13 @@ discard block |
||
125 | 125 | $action = explode('@', $action); |
126 | 126 | |
127 | 127 | return $action; |
128 | - } |
|
128 | + } |
|
129 | 129 | |
130 | - public function getProperRoute() |
|
131 | - { |
|
130 | + public function getProperRoute() |
|
131 | + { |
|
132 | 132 | foreach ($this->routes as $route) { |
133 | 133 | |
134 | - if ($this->isMatching($route['pattern']) && $this->isMatchingRequestMethod($route['method'])) { |
|
134 | + if ($this->isMatching($route['pattern']) && $this->isMatchingRequestMethod($route['method'])) { |
|
135 | 135 | |
136 | 136 | $this->current = $route; |
137 | 137 | |
@@ -139,87 +139,87 @@ discard block |
||
139 | 139 | |
140 | 140 | if ($route['middleware']) { |
141 | 141 | |
142 | - if (is_array($route['middleware'])) { |
|
142 | + if (is_array($route['middleware'])) { |
|
143 | 143 | |
144 | 144 | foreach ($route['middleware'] as $middleware) { |
145 | 145 | |
146 | - $output = $this->middleware($middleware); |
|
146 | + $output = $this->middleware($middleware); |
|
147 | 147 | |
148 | - if ($output != '') { |
|
148 | + if ($output != '') { |
|
149 | 149 | break; |
150 | - } |
|
150 | + } |
|
151 | 151 | } |
152 | 152 | |
153 | - } else { |
|
153 | + } else { |
|
154 | 154 | |
155 | 155 | $output = $this->middleware($route['middleware']); |
156 | 156 | |
157 | 157 | if ($output != '') { |
158 | - break; |
|
158 | + break; |
|
159 | + } |
|
159 | 160 | } |
160 | - } |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | if ($output == '') { |
164 | 164 | |
165 | - list($controller, $method) = $route['action']; |
|
165 | + list($controller, $method) = $route['action']; |
|
166 | 166 | |
167 | - $arguments = $this->getArgumentsFor($route['pattern']); |
|
167 | + $arguments = $this->getArgumentsFor($route['pattern']); |
|
168 | 168 | |
169 | - $output = (string) $this->app->load->action($controller, $method, $arguments); |
|
169 | + $output = (string) $this->app->load->action($controller, $method, $arguments); |
|
170 | 170 | |
171 | - return $output; |
|
171 | + return $output; |
|
172 | 172 | |
173 | 173 | } |
174 | 174 | break; |
175 | - } |
|
175 | + } |
|
176 | 176 | } |
177 | 177 | |
178 | 178 | $output = (string) $this->app->load->action('notFound', 'index', []); |
179 | 179 | |
180 | 180 | return $output; |
181 | - } |
|
181 | + } |
|
182 | 182 | |
183 | - public function isMatching($pattern) |
|
184 | - { |
|
183 | + public function isMatching($pattern) |
|
184 | + { |
|
185 | 185 | $url = $this->app->request->url(); |
186 | 186 | |
187 | 187 | $url = strtolower($url); |
188 | 188 | |
189 | 189 | return preg_match($pattern, $url); |
190 | - } |
|
190 | + } |
|
191 | 191 | |
192 | - private function isMatchingRequestMethod($method) |
|
193 | - { |
|
192 | + private function isMatchingRequestMethod($method) |
|
193 | + { |
|
194 | 194 | $methods = ['GET', 'POST']; |
195 | 195 | |
196 | 196 | if (($method == 'both') && in_array($this->app->request->method(), $methods)) { |
197 | - return true; |
|
197 | + return true; |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | if (is_array($method)) { |
201 | 201 | |
202 | - if (count($method) == 1) { |
|
202 | + if (count($method) == 1) { |
|
203 | 203 | |
204 | 204 | $method = $method[0]; |
205 | 205 | |
206 | - } else if (count($method) == 2) { |
|
206 | + } else if (count($method) == 2) { |
|
207 | 207 | |
208 | 208 | if (in_array($method[0], $methods) && in_array($method[1], $methods)) { |
209 | - return true; |
|
209 | + return true; |
|
210 | 210 | } |
211 | 211 | |
212 | - } else { |
|
212 | + } else { |
|
213 | 213 | |
214 | 214 | return false; |
215 | - } |
|
215 | + } |
|
216 | 216 | } |
217 | 217 | |
218 | 218 | return $this->app->request->method() == $method; |
219 | - } |
|
219 | + } |
|
220 | 220 | |
221 | - public function getArgumentsFor($pattern) |
|
222 | - { |
|
221 | + public function getArgumentsFor($pattern) |
|
222 | + { |
|
223 | 223 | $url = $this->app->request->url(); |
224 | 224 | |
225 | 225 | preg_match($pattern, $url, $matches); |
@@ -227,15 +227,15 @@ discard block |
||
227 | 227 | array_shift($matches); |
228 | 228 | |
229 | 229 | return $matches; |
230 | - } |
|
230 | + } |
|
231 | 231 | |
232 | - public function getCurrent($key) |
|
233 | - { |
|
232 | + public function getCurrent($key) |
|
233 | + { |
|
234 | 234 | return $this->current[$key]; |
235 | - } |
|
235 | + } |
|
236 | 236 | |
237 | - private function middleware($middleware, $from = 'admin') |
|
238 | - { |
|
237 | + private function middleware($middleware, $from = 'admin') |
|
238 | + { |
|
239 | 239 | $middlewareInterface = 'App\\Middleware\\MiddlewaresInterface'; |
240 | 240 | |
241 | 241 | $middlewares = $this->app->alias['middlewares'][$from]; |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | $middlewareClass = $middlewares[$middleware]; |
244 | 244 | |
245 | 245 | if (!in_array($middlewareInterface, class_implements($middlewareClass))) { |
246 | - throw new Exception("$middlewareClass not Implement"); |
|
246 | + throw new Exception("$middlewareClass not Implement"); |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | $middlewareObject = new $middlewareClass; |
@@ -251,9 +251,9 @@ discard block |
||
251 | 251 | $output = $middlewareObject->handle($this->app, static::NEXT); |
252 | 252 | |
253 | 253 | if ($output && $output === static::NEXT) { |
254 | - $output = ''; |
|
254 | + $output = ''; |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | return $output; |
258 | - } |
|
258 | + } |
|
259 | 259 | } |
260 | 260 | \ No newline at end of file |