PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.19
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.19
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / vendor / wpfluent / framework / src / WPFluent / Http / Router.php

Router.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.19, at vendor/wpfluent/framework/src/WPFluent/Http/Router.php

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