1 | <?php |
||||
2 | |||||
3 | /** |
||||
4 | * |
||||
5 | * KNUT7 K7F (https://marciozebedeu.com/) |
||||
6 | * KNUT7 K7F (tm) : Rapid Development Framework (https://marciozebedeu.com/) |
||||
7 | * |
||||
8 | * Licensed under The MIT License |
||||
9 | * For full copyright and license information, please see the LICENSE.txt |
||||
10 | * Redistributions of files must retain the above copyright notice. |
||||
11 | * |
||||
12 | * @link https://github.com/knut7/framework/ for the canonical source repository |
||||
13 | * @copyright (c) 2015. KNUT7 Software Technologies AO Inc. (https://marciozebedeu.com/) |
||||
14 | * @license https://marciozebedeu.com/license/new-bsd New BSD License |
||||
15 | * @author Marcio Zebedeu - [email protected] |
||||
16 | * @version 1.0.14 |
||||
17 | * |
||||
18 | * |
||||
19 | */ |
||||
20 | |||||
21 | namespace Ballybran\Routing\Router; |
||||
22 | |||||
23 | class RouteMiddleware { |
||||
24 | |||||
25 | /** |
||||
26 | * @var array $middlewares General middlewares for per request |
||||
27 | */ |
||||
28 | protected $middlewares = []; |
||||
29 | |||||
30 | /** |
||||
31 | * @var array $routeMiddlewares Route middlewares |
||||
32 | */ |
||||
33 | protected $routeMiddlewares = []; |
||||
34 | |||||
35 | /** |
||||
36 | * @var array $middlewareGroups Middleware Groups |
||||
37 | */ |
||||
38 | protected $middlewareGroups = []; |
||||
39 | |||||
40 | /** |
||||
41 | * [TODO] This method implementation not completed yet. |
||||
42 | * |
||||
43 | * Set route middleware |
||||
44 | * |
||||
45 | * @param string|array $middleware |
||||
46 | * @param string $type |
||||
47 | * |
||||
48 | * @return $this |
||||
49 | */ |
||||
50 | public function middleware($middleware, $type = 'before') |
||||
51 | { |
||||
52 | if (!is_array($middleware) && !is_string($middleware)) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
53 | return $this; |
||||
54 | } |
||||
55 | |||||
56 | $currentRoute = end($this->routes); |
||||
57 | $currentRoute[$type] = $middleware; |
||||
58 | array_pop($this->routes); |
||||
59 | array_push($this->routes, $currentRoute); |
||||
60 | |||||
61 | return $this; |
||||
62 | } |
||||
63 | |||||
64 | /** |
||||
65 | * [TODO] This method implementation not completed yet. |
||||
66 | * |
||||
67 | * @param string|array $middleware |
||||
68 | * |
||||
69 | * @return $this |
||||
70 | */ |
||||
71 | public function middlewareBefore($middleware) |
||||
72 | { |
||||
73 | $this->middleware($middleware, 'before'); |
||||
74 | |||||
75 | return $this; |
||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * [TODO] This method implementation not completed yet. |
||||
80 | * |
||||
81 | * @param string|array $middleware |
||||
82 | * |
||||
83 | * @return $this |
||||
84 | */ |
||||
85 | public function middlewareAfter($middleware) |
||||
86 | { |
||||
87 | $this->middleware($middleware, 'after'); |
||||
88 | |||||
89 | return $this; |
||||
90 | } |
||||
91 | |||||
92 | /** |
||||
93 | * [TODO] This method implementation not completed yet. |
||||
94 | * |
||||
95 | * Set general middlewares |
||||
96 | * |
||||
97 | * @param array $middlewares |
||||
98 | * |
||||
99 | * @return void |
||||
100 | */ |
||||
101 | public function setMiddleware(array $middlewares) |
||||
102 | { |
||||
103 | $this->middlewares = $middlewares; |
||||
104 | } |
||||
105 | |||||
106 | /** |
||||
107 | * [TODO] This method implementation not completed yet. |
||||
108 | * |
||||
109 | * Set Route middlewares |
||||
110 | * |
||||
111 | * @param array $middlewares |
||||
112 | * |
||||
113 | * @return void |
||||
114 | */ |
||||
115 | public function setRouteMiddleware(array $middlewares) |
||||
116 | { |
||||
117 | $this->routeMiddlewares = $middlewares; |
||||
118 | } |
||||
119 | |||||
120 | /** |
||||
121 | * [TODO] This method implementation not completed yet. |
||||
122 | * |
||||
123 | * Set middleware groups |
||||
124 | * |
||||
125 | * @param array $middlewareGroup |
||||
126 | * |
||||
127 | * @return void |
||||
128 | */ |
||||
129 | public function setMiddlewareGroup(array $middlewareGroup) |
||||
130 | { |
||||
131 | $this->middlewareGroups = $middlewareGroup; |
||||
132 | } |
||||
133 | |||||
134 | |||||
135 | /** |
||||
136 | * Set paths and namespaces for Controllers and Middlewares. |
||||
137 | * |
||||
138 | * @param array $params |
||||
139 | * |
||||
140 | * @return void |
||||
141 | */ |
||||
142 | protected function setPaths($params) |
||||
143 | { |
||||
144 | if (empty($params)) { |
||||
145 | return; |
||||
146 | } |
||||
147 | |||||
148 | if (isset($params['paths']) && $paths = $params['paths']) { |
||||
149 | $this->paths['controllers'] = isset($paths['controllers']) |
||||
0 ignored issues
–
show
|
|||||
150 | ? trim($paths['controllers'], '/') |
||||
151 | : $this->paths['controllers']; |
||||
152 | |||||
153 | $this->paths['middlewares'] = isset($paths['middlewares']) |
||||
154 | ? trim($paths['middlewares'], '/') |
||||
155 | : $this->paths['middlewares']; |
||||
156 | } |
||||
157 | |||||
158 | if (isset($params['namespaces']) && $namespaces = $params['namespaces']) { |
||||
159 | $this->namespaces['controllers'] = isset($namespaces['controllers']) |
||||
0 ignored issues
–
show
|
|||||
160 | ? trim($namespaces['controllers'], '\\') . '\\' |
||||
161 | : ''; |
||||
162 | |||||
163 | $this->namespaces['middlewares'] = isset($namespaces['middlewares']) |
||||
164 | ? trim($namespaces['middlewares'], '\\') . '\\' |
||||
165 | : ''; |
||||
166 | } |
||||
167 | |||||
168 | if (isset($params['base_folder'])) { |
||||
169 | $this->baseFolder = rtrim($params['base_folder'], '/'); |
||||
0 ignored issues
–
show
|
|||||
170 | } |
||||
171 | |||||
172 | if (isset($params['main_method'])) { |
||||
173 | $this->mainMethod = $params['main_method']; |
||||
0 ignored issues
–
show
|
|||||
174 | } |
||||
175 | |||||
176 | $this->cacheFile = isset($params['cache']) ? $params['cache'] : realpath(__DIR__ . '/../cache.php'); |
||||
0 ignored issues
–
show
|
|||||
177 | } |
||||
178 | |||||
179 | /** |
||||
180 | * @param $controller |
||||
181 | * |
||||
182 | * @return RouterException|mixed |
||||
183 | */ |
||||
184 | protected function resolveClass($controller) |
||||
185 | { |
||||
186 | $controller = str_replace(['\\', '.'], '/', $controller); |
||||
187 | $controller = trim( |
||||
188 | preg_replace( |
||||
189 | '/' . str_replace('/', '\\/', $this->paths['controllers']) . '/i', |
||||
190 | '', $controller, |
||||
191 | 1 |
||||
192 | ), |
||||
193 | '/' |
||||
194 | ); |
||||
195 | $file = realpath(rtrim($this->paths['controllers'], '/') . '/' . $controller . '.php'); |
||||
196 | |||||
197 | if (!file_exists($file)) { |
||||
198 | return $this->exception($controller . ' class is not found!'); |
||||
199 | } |
||||
200 | |||||
201 | $controller = $this->namespaces['controllers'] . str_replace('/', '\\', $controller); |
||||
202 | if (!class_exists($controller)) { |
||||
203 | require $file; |
||||
204 | } |
||||
205 | |||||
206 | return $controller; |
||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * Routes error function. (Closure) |
||||
211 | * |
||||
212 | * @param $callback |
||||
213 | * |
||||
214 | * @return void |
||||
215 | */ |
||||
216 | public function error($callback) |
||||
217 | { |
||||
218 | $this->errorCallback = $callback; |
||||
0 ignored issues
–
show
|
|||||
219 | } |
||||
220 | |||||
221 | /** |
||||
222 | * Display all Routes. |
||||
223 | * |
||||
224 | * @return void |
||||
225 | */ |
||||
226 | public function getList() |
||||
227 | { |
||||
228 | echo '<pre>'; |
||||
229 | var_dump($this->getRoutes()); |
||||
0 ignored issues
–
show
|
|||||
230 | echo '</pre>'; |
||||
231 | die; |
||||
0 ignored issues
–
show
|
|||||
232 | } |
||||
233 | |||||
234 | /** |
||||
235 | * Get all Routes |
||||
236 | * |
||||
237 | * @return mixed |
||||
238 | */ |
||||
239 | public function getRoutes() |
||||
240 | { |
||||
241 | return $this->routes; |
||||
242 | } |
||||
243 | |||||
244 | /** |
||||
245 | * Throw new Exception for Router Error |
||||
246 | * |
||||
247 | * @param $message |
||||
248 | * |
||||
249 | * @return RouterException |
||||
250 | * @throws |
||||
251 | */ |
||||
252 | public function exception($message = '') |
||||
253 | { |
||||
254 | return new RouterException($message); |
||||
255 | } |
||||
256 | |||||
257 | /** |
||||
258 | * RouterCommand class |
||||
259 | * |
||||
260 | * @return RouterCommand |
||||
261 | */ |
||||
262 | public function routerCommand() |
||||
263 | { |
||||
264 | return RouterCommand::getInstance($this->baseFolder, $this->paths, $this->namespaces); |
||||
0 ignored issues
–
show
The call to
Ballybran\Routing\Router...rCommand::getInstance() has too few arguments starting with request .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||
265 | } |
||||
266 | |||||
267 | /** |
||||
268 | * Cache all routes |
||||
269 | * |
||||
270 | * @return bool |
||||
271 | * @throws Exception |
||||
272 | */ |
||||
273 | public function cache() |
||||
274 | { |
||||
275 | foreach ($this->getRoutes() as $key => $r) { |
||||
276 | if (!is_string($r['callback'])) { |
||||
277 | throw new \Exception(sprintf('Routes cannot contain a Closure/Function callback while caching.')); |
||||
278 | } |
||||
279 | } |
||||
280 | |||||
281 | $cacheContent = '<?php return ' . var_export($this->getRoutes(), true) . ';' . PHP_EOL; |
||||
282 | if (false === file_put_contents($this->cacheFile, $cacheContent)) { |
||||
283 | throw new \Exception(sprintf('Routes cache file could not be written.')); |
||||
284 | } |
||||
285 | |||||
286 | return true; |
||||
287 | } |
||||
288 | |||||
289 | /** |
||||
290 | * Load Cache file |
||||
291 | * |
||||
292 | * @return bool |
||||
293 | */ |
||||
294 | protected function loadCache() |
||||
295 | { |
||||
296 | if (file_exists($this->cacheFile)) { |
||||
297 | $this->routes = require $this->cacheFile; |
||||
0 ignored issues
–
show
|
|||||
298 | $this->cacheLoaded = true; |
||||
0 ignored issues
–
show
|
|||||
299 | return true; |
||||
300 | } |
||||
301 | |||||
302 | return false; |
||||
303 | } |
||||
304 | |||||
305 | |||||
306 | } |