PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / trunk
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration vtrunk
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Http / Router.php

Router.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration trunk, at vendor/wpfluent/framework/src/WPFluent/Http/Router.php

507 lines 11.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Http;
4
5 use Closure;
6
7 class Router
8 {
9 /**
10 * Application Instance
11 * @var \FluentBoards\Framework\Foundation\Application
12 */
13 protected $app = null;
14
15 /**
16 * Name for the route.
17 * @var array
18 */
19 protected $name = [];
20
21 /**
22 * Mapping of named routes.
23 * @var array
24 */
25 protected $namedRoutes = [];
26
27 /**
28 * Prefix for the route
29 * @var array
30 */
31 protected $prefix = [];
32
33 /**
34 * Controller/Handler namespace
35 * @var array
36 */
37 protected $namespace = [];
38
39 /**
40 * Registered routes collection
41 * @var array
42 */
43 protected $routes = [];
44
45 /**
46 * Route policy handler to pass to the route
47 * @var array
48 */
49 protected $policyHandler = [];
50
51 /**
52 * Route middleware to pass to the route
53 * @var array
54 */
55 protected $middleware = [
56 'before' => [],
57 'after' => []
58 ];
59
60 /**
61 * Whether routes created by this router should override existing ones.
62 * @var bool
63 */
64 protected $shouldOverride = false;
65
66 /**
67 * Keep the track of number of group calls
68 * @var integer
69 */
70 protected $groupCount = 0;
71
72 /**
73 * Construct the routet instance
74 * @param \FluentBoards\Framework\Foundation\Application $app
75 */
76 public function __construct($app)
77 {
78 $this->app = $app;
79 }
80
81 /**
82 * Create a route group
83 * @param array $attributes
84 * @param Closure|null $callback
85 * @return null
86 */
87 public function group($attributes = [], ?Closure $callback = null)
88 {
89 $this->groupCount += 1;
90
91 if ($attributes instanceof Closure) {
92 $callback = $attributes;
93 $attributes = [];
94 }
95
96 if (isset($attributes['name'])) {
97 $this->name($attributes['name']);
98 }
99
100 if (isset($attributes['prefix'])) {
101 $this->prefix($attributes['prefix']);
102 }
103
104 if (isset($attributes['namespace'])) {
105 $this->namespace($attributes['namespace']);
106 }
107
108 if (isset($attributes['policy'])) {
109 $this->withPolicy($attributes['policy']);
110 }
111
112 if (isset($attributes['middleware'])) {
113 $middleware = $attributes['middleware'];
114 if (isset($middleware['before'])) {
115 $this->middleware('before', $middleware['before']);
116 } elseif ($middleware['after']) {
117 $this->middleware('after', $middleware['after']);
118 }
119 }
120
121 // If the current group doesn't have a policy handler
122 // but the parent group has then bring it in this group.
123 if (!isset($this->policyHandler[$this->groupCount])) {
124 if (isset($this->policyHandler[$this->groupCount - 1])) {
125 if ($policyHandler = $this->policyHandler[$this->groupCount - 1]) {
126 $this->policyHandler[] = $policyHandler;
127 }
128 }
129 }
130
131 // If the current group doesn't have a before middleware
132 // but the parent group has then bring it in this group.
133 if (!isset($this->middleware['before'][$this->groupCount])) {
134 if (isset($this->middleware['before'][$this->groupCount - 1])) {
135 if ($beforeMiddleware = $this->middleware['before'][$this->groupCount - 1]) {
136 $this->middleware['before'][] = $beforeMiddleware;
137 }
138 }
139 }
140
141 // If the current group doesn't have an after middleware
142 // but the parent group has then bring it in this group.
143 if (!isset($this->middleware['after'][$this->groupCount])) {
144 if (isset($this->middleware['after'][$this->groupCount - 1])) {
145 if ($afterMiddleware = $this->middleware['after'][$this->groupCount - 1]) {
146 $this->middleware['after'][] = $afterMiddleware;
147 }
148 }
149 }
150
151 return new Group($this, $callback);
152 }
153
154 /**
155 * Set the route name
156 *
157 * @param string $name
158 * @return self
159 */
160 public function name($name)
161 {
162 $this->name[] = $name;
163
164 return $this;
165 }
166
167 /**
168 * Set the route prefix
169 *
170 * @param string $prefix
171 * @return self
172 */
173 public function prefix($prefix)
174 {
175 $this->prefix[] = $prefix;
176
177 return $this;
178 }
179
180 /**
181 * Set the namespace for the action/controller
182 *
183 * @param string $ns
184 * @return self
185 */
186 public function namespace($ns)
187 {
188 $this->namespace[] = $ns;
189
190 return $this;
191 }
192
193 /**
194 * Set the default route policy.
195 *
196 * @return self
197 */
198 public function withDefaultPolicy()
199 {
200 return $this->withPolicy(
201 // @phpstan-ignore-next-line
202 $this->app->__namespace__.'\\App\\Http\\Policies\\Policy'
203 );
204 }
205
206 /**
207 * Set the route policy
208 *
209 * @param mixed $handler
210 * @param string|null $method
211 * @return self
212 */
213 public function withPolicy($handler, $method = null)
214 {
215 if (is_array($handler = $method ? func_get_args() : $handler)) {
216 $handler = implode('@', $handler);
217 }
218
219 $this->policyHandler[] = $handler;
220
221 return $this;
222 }
223
224 /**
225 * Set the route before middleware
226 *
227 * @param array|string $middleware
228 * @return self
229 */
230 public function before(...$middleware)
231 {
232 return $this->middleware('before', ...$middleware);
233 }
234
235 /**
236 * Set the route after middleware
237 *
238 * @param array|string $middleware
239 * @return self
240 */
241 public function after(...$middleware)
242 {
243 return $this->middleware('after', ...$middleware);
244 }
245
246 /**
247 * Set the route middleware
248 *
249 * @param array|string $middleware
250 * @return self
251 */
252 public function middleware($type = 'before', ...$middleware)
253 {
254 if (is_array($middleware[0])) {
255 $middleware = reset($middleware);
256 }
257
258 $this->middleware[$type] = array_merge(
259 $this->middleware[$type], $middleware
260 );
261
262 return $this;
263 }
264
265 /**
266 * Execute the route group callback
267 *
268 * @param Closure $callback
269 * @return null
270 */
271 public function executeGroupCallback($callback)
272 {
273 $callback($this);
274 $this->groupCount -= 1;
275 array_pop($this->name);
276 array_pop($this->prefix);
277 array_pop($this->namespace);
278 array_pop($this->middleware['before']);
279 array_pop($this->middleware['after']);
280 array_pop($this->policyHandler);
281 }
282
283 /**
284 * Declare a GET route endpoint
285 * @param string $uri
286 * @param array|string|Closure $handler
287 * @return \FluentBoards\Framework\Http\Route
288 */
289 public function get($uri, $handler)
290 {
291 $this->routes[] = $route = $this->newRoute(
292 $uri, $handler, 'GET'
293 );
294
295 return $route;
296 }
297
298 /**
299 * Declare a POST route endpoint
300 * @param string $uri
301 * @param array|string|Closure $handler
302 * @return \FluentBoards\Framework\Http\Route
303 */
304 public function post($uri, $handler)
305 {
306 $this->routes[] = $route = $this->newRoute(
307 $uri, $handler, 'POST'
308 );
309
310 return $route;
311 }
312
313 /**
314 * Declare a PUT route endpoint
315 * @param string $uri
316 * @param array|string|Closure $handler
317 * @return \FluentBoards\Framework\Http\Route
318 */
319 public function put($uri, $handler)
320 {
321 $this->routes[] = $route = $this->newRoute(
322 $uri, $handler, 'PUT'
323 );
324
325 return $route;
326 }
327
328 /**
329 * Declare a PATCH route endpoint
330 * @param string $uri
331 * @param array|string|Closure $handler
332 * @return \FluentBoards\Framework\Http\Route
333 */
334 public function patch($uri, $handler)
335 {
336 $this->routes[] = $route = $this->newRoute(
337 $uri, $handler, 'PATCH'
338 );
339
340 return $route;
341 }
342
343 /**
344 * Declare a DELETE route endpoint
345 * @param string $uri
346 * @param array|string|Closure $handler
347 * @return \FluentBoards\Framework\Http\Route
348 */
349 public function delete($uri, $handler)
350 {
351 $this->routes[] = $route = $this->newRoute(
352 $uri, $handler, 'DELETE'
353 );
354
355 return $route;
356 }
357
358 /**
359 * Declare a route endpoint that matches any HTTP Verb/Method
360 * @param string $uri
361 * @param array|string|Closure $handler
362 * @return \FluentBoards\Framework\Http\Route
363 */
364 public function any($uri, $handler)
365 {
366 $this->routes[] = $route = $this->newRoute(
367 $uri, $handler, \WP_REST_Server::ALLMETHODS
368 );
369
370 return $route;
371 }
372
373 /**
374 * Create a new route instance
375 * @param string $uri
376 * @param string|Closure $handler
377 * @param string $method HTTP Method
378 * @return \FluentBoards\Framework\Http\Route
379 */
380 protected function newRoute($uri, $handler, $method)
381 {
382 $route = Route::create(
383 $this->app,
384 $this->getRestNamespace(),
385 $this->buildUriWithPrefix($uri),
386 $handler,
387 $method
388 );
389
390 if ($this->name) {
391 $route->withName($this->name);
392 }
393
394 if ($this->namespace) {
395 $route->withNamespace($this->namespace);
396 }
397
398 if ($this->policyHandler) {
399 $route->withPolicy(end($this->policyHandler));
400 }
401
402 if ($this->middleware['before']) {
403 $route->before($this->middleware['before']);
404 }
405
406 if ($this->middleware['after']) {
407 $route->after($this->middleware['after']);
408 }
409
410 if ($this->shouldOverride) {
411 $route->override();
412 }
413
414 return $route->preparefrontendHandlers();
415 }
416
417 /**
418 * Resolve the rest namespace for the plugin
419 *
420 * @return string
421 */
422 protected function getRestNamespace()
423 {
424 $version = $this->app->config->get('app.rest_version');
425
426 $namespace = trim(
427 $this->app->config->get('app.rest_namespace', ''), '/'
428 );
429
430 return "{$namespace}/{$version}";
431 }
432
433 /**
434 * Build the URI with the prefix
435 *
436 * @param string $uri
437 * @return string The URI
438 */
439 protected function buildUriWithPrefix($uri)
440 {
441 $uri = trim($uri, '/');
442
443 $prefix = array_map(function($prefix) {
444 return trim($prefix, '/');
445 }, $this->prefix);
446
447 $prefix = implode('/', $prefix);
448
449 return trim($prefix, '/') . '/' . trim($uri, '/');
450 }
451
452 /**
453 * Mark all routes created by this router to override existing ones.
454 *
455 * @return $this
456 */
457 public function overrideExisting()
458 {
459 $this->shouldOverride = true;
460
461 return $this;
462 }
463
464 /**
465 * Register all the routse in WordPress Rest Engine
466 *
467 * @return null
468 */
469 public function registerRoutes()
470 {
471 foreach ($this->routes as $route) $route->register();
472 }
473
474 /**
475 * Get all ther registered routes
476 * @return array
477 */
478 public function getRoutes()
479 {
480 return $this->routes;
481 }
482
483 /**
484 * Set a named route in the router.
485 *
486 * @param string $name
487 * @param Route $route
488 */
489 public function setNamedRoute($name, Route $route)
490 {
491 $this->namedRoutes[$name] = $route;
492
493 return $route;
494 }
495
496 /**
497 * Get a route by name.
498 *
499 * @param string $name
500 * @return Route|null
501 */
502 public function getByName($name)
503 {
504 return $this->namedRoutes[$name] ?? null;
505 }
506 }
507