PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.32
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.32
2.1.0 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 All 42 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 1.32, at vendor/wpfluent/framework/src/WPFluent/Http/Router.php

412 lines 9.8 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 class Router
6 {
7 /**
8 * Application Instance
9 * @var \FluentBoards\Framework\Foundation\Application
10 */
11 protected $app = null;
12
13 /**
14 * Prefix for the route
15 * @var array
16 */
17 protected $prefix = [];
18
19 /**
20 * Controller/Handler namespace
21 * @var array
22 */
23 protected $namespace = [];
24
25 /**
26 * Registered routes collection
27 * @var array
28 */
29 protected $routes = [];
30
31 /**
32 * Route policy handler to pass to the route
33 * @var array
34 */
35 protected $policyHandler = [];
36
37 /**
38 * Route middleware to pass to the route
39 * @var array
40 */
41 protected $middleware = [
42 'before' => [],
43 'after' => []
44 ];
45
46 /**
47 * Keep the track of number of group calls
48 * @var integer
49 */
50 protected $groupCount = 0;
51
52 /**
53 * Construct the routet instance
54 * @param \FluentBoards\Framework\Foundation\Application $app
55 */
56 public function __construct($app)
57 {
58 $this->app = $app;
59 }
60
61 /**
62 * Create a route group
63 * @param array $attributes
64 * @param \Closure|null $callback
65 * @return null
66 */
67 public function group($attributes = [], \Closure $callback = null)
68 {
69 $this->groupCount += 1;
70
71 if ($attributes instanceof \Closure) {
72 $callback = $attributes;
73 $attributes = [];
74 }
75
76 if (isset($attributes['prefix'])) {
77 $this->prefix($attributes['prefix']);
78 }
79
80 if (isset($attributes['namespace'])) {
81 $this->namespace($attributes['namespace']);
82 }
83
84 if (isset($attributes['policy'])) {
85 $this->withPolicy($attributes['policy']);
86 }
87
88 if (isset($attributes['middleware'])) {
89 $middleware = $attributes['middleware'];
90 if (isset($middleware['before'])) {
91 $this->middleware('before', $middleware['before']);
92 } elseif ($middleware['after']) {
93 $this->middleware('after', $middleware['after']);
94 }
95 }
96
97 // If the current group doesn't have a policy handler
98 // but the parent group has then bring it in this group.
99 if (!isset($this->policyHandler[$this->groupCount])) {
100 if (isset($this->policyHandler[$this->groupCount - 1])) {
101 if ($policyHandler = $this->policyHandler[$this->groupCount - 1]) {
102 $this->policyHandler[] = $policyHandler;
103 }
104 }
105 }
106
107 // If the current group doesn't have a before middleware
108 // but the parent group has then bring it in this group.
109 if (!isset($this->middleware['before'][$this->groupCount])) {
110 if (isset($this->middleware['before'][$this->groupCount - 1])) {
111 if ($beforeMiddleware = $this->middleware['before'][$this->groupCount - 1]) {
112 $this->middleware['before'][] = $beforeMiddleware;
113 }
114 }
115 }
116
117 // If the current group doesn't have an after middleware
118 // but the parent group has then bring it in this group.
119 if (!isset($this->middleware['after'][$this->groupCount])) {
120 if (isset($this->middleware['after'][$this->groupCount - 1])) {
121 if ($afterMiddleware = $this->middleware['after'][$this->groupCount - 1]) {
122 $this->middleware['after'][] = $afterMiddleware;
123 }
124 }
125 }
126
127 return new Group($this, $callback);
128 }
129
130 /**
131 * Set the route prefix
132 *
133 * @param string $prefix
134 * @return self
135 */
136 public function prefix($prefix)
137 {
138 $this->prefix[] = $prefix;
139
140 return $this;
141 }
142
143 /**
144 * Set the namespace for the action/controller
145 *
146 * @param string $namespace
147 * @return self
148 */
149 public function namespace($ns)
150 {
151 $this->namespace[] = $ns;
152
153 return $this;
154 }
155
156 /**
157 * Set the route policy
158 *
159 * @param mixed $handler
160 * @param string|null $method
161 * @return self
162 */
163 public function withPolicy($handler, $method = null)
164 {
165 if (is_array($handler = $method ? func_get_args() : $handler)) {
166 $handler = implode('@', $handler);
167 }
168
169 $this->policyHandler[] = $handler;
170
171 return $this;
172 }
173
174 /**
175 * Set the route before middleware
176 *
177 * @param array|string $middleware
178 * @return self
179 */
180 public function before(...$middleware)
181 {
182 return $this->middleware('before', ...$middleware);
183 }
184
185 /**
186 * Set the route after middleware
187 *
188 * @param array|string $middleware
189 * @return self
190 */
191 public function after(...$middleware)
192 {
193 return $this->middleware('after', ...$middleware);
194 }
195
196 /**
197 * Set the route middleware
198 *
199 * @param array|string $middleware
200 * @return self
201 */
202 public function middleware($type = 'before', ...$middleware)
203 {
204 if (is_array($middleware[0])) {
205 $middleware = reset($middleware);
206 }
207
208 $this->middleware[$type] = array_merge(
209 $this->middleware[$type], $middleware
210 );
211
212 return $this;
213 }
214
215 /**
216 * Execute the route group callback
217 *
218 * @param Closure $callback
219 * @return null
220 */
221 public function executeGroupCallback($callback)
222 {
223 $callback($this);
224 $this->groupCount -= 1;
225 array_pop($this->prefix);
226 array_pop($this->namespace);
227 array_pop($this->middleware['before']);
228 array_pop($this->middleware['after']);
229 array_pop($this->policyHandler);
230 }
231
232 /**
233 * Declare a GET route endpoint
234 * @param string $uri
235 * @param array|string|Closure $handler
236 * @return \FluentBoards\Framework\Http\Route
237 */
238 public function get($uri, $handler)
239 {
240 $this->routes[] = $route = $this->newRoute(
241 $uri, $handler, 'GET'
242 );
243
244 return $route;
245 }
246
247 /**
248 * Declare a POST route endpoint
249 * @param string $uri
250 * @param array|string|Closure $handler
251 * @return \FluentBoards\Framework\Http\Route
252 */
253 public function post($uri, $handler)
254 {
255 $this->routes[] = $route = $this->newRoute(
256 $uri, $handler, 'POST'
257 );
258
259 return $route;
260 }
261
262 /**
263 * Declare a PUT route endpoint
264 * @param string $uri
265 * @param array|string|Closure $handler
266 * @return \FluentBoards\Framework\Http\Route
267 */
268 public function put($uri, $handler)
269 {
270 $this->routes[] = $route = $this->newRoute(
271 $uri, $handler, 'PUT'
272 );
273
274 return $route;
275 }
276
277 /**
278 * Declare a PATCH route endpoint
279 * @param string $uri
280 * @param array|string|Closure $handler
281 * @return \FluentBoards\Framework\Http\Route
282 */
283 public function patch($uri, $handler)
284 {
285 $this->routes[] = $route = $this->newRoute(
286 $uri, $handler, 'PATCH'
287 );
288
289 return $route;
290 }
291
292 /**
293 * Declare a DELETE route endpoint
294 * @param string $uri
295 * @param array|string|Closure $handler
296 * @return \FluentBoards\Framework\Http\Route
297 */
298 public function delete($uri, $handler)
299 {
300 $this->routes[] = $route = $this->newRoute(
301 $uri, $handler, 'DELETE'
302 );
303
304 return $route;
305 }
306
307 /**
308 * Declare a route endpoint that matches any HTTP Verb/Method
309 * @param string $uri
310 * @param array|string|Closure $handler
311 * @return \FluentBoards\Framework\Http\Route
312 */
313 public function any($uri, $handler)
314 {
315 $this->routes[] = $route = $this->newRoute(
316 $uri, $handler, \WP_REST_Server::ALLMETHODS
317 );
318
319 return $route;
320 }
321
322 /**
323 * Create a new route instance
324 * @param string $uri
325 * @param string|Closure $handler
326 * @param string $method HTTP Method
327 * @return \FluentBoards\Framework\Http\Route
328 */
329 protected function newRoute($uri, $handler, $method)
330 {
331 $route = Route::create(
332 $this->app,
333 $this->getRestNamespace(),
334 $this->buildUriWithPrefix($uri),
335 $handler,
336 $method
337 );
338
339 if ($this->namespace) {
340 $route->withNamespace($this->namespace);
341 }
342
343 if ($this->policyHandler) {
344 $route->withPolicy(end($this->policyHandler));
345 }
346
347 if ($this->middleware['before']) {
348 $route->before($this->middleware['before']);
349 }
350
351 if ($this->middleware['after']) {
352 $route->after($this->middleware['after']);
353 }
354
355 return $route;
356 }
357
358 /**
359 * Resolve the rest namespace for the plugin
360 *
361 * @return string
362 */
363 protected function getRestNamespace()
364 {
365 $version = $this->app->config->get('app.rest_version');
366
367 $namespace = trim(
368 $this->app->config->get('app.rest_namespace', ''), '/'
369 );
370
371 return "{$namespace}/{$version}";
372 }
373
374 /**
375 * Build the URI with the prefix
376 *
377 * @param string $uri
378 * @return string The URI
379 */
380 protected function buildUriWithPrefix($uri)
381 {
382 $uri = trim($uri, '/');
383
384 $prefix = array_map(function($prefix) {
385 return trim($prefix, '/');
386 }, $this->prefix);
387
388 $prefix = implode('/', $prefix);
389
390 return trim($prefix, '/') . '/' . trim($uri, '/');
391 }
392
393 /**
394 * Register all the routse in WordPress Rest Engine
395 *
396 * @return null
397 */
398 public function registerRoutes()
399 {
400 foreach ($this->routes as $route) $route->register();
401 }
402
403 /**
404 * Get all ther registered routes
405 * @return array
406 */
407 public function getRoutes()
408 {
409 return $this->routes;
410 }
411 }
412