PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 1.5.4
Fluent Support – Helpdesk & Customer Support Ticket System v1.5.4
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Http / Router.php

Router.php in Fluent Support – Helpdesk & Customer Support Ticket System 1.5.4, at vendor/wpfluent/framework/src/WPFluent/Http/Router.php

175 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentSupport\Framework\Http;
4
5 use FluentSupport\Framework\Support\Arr;
6
7 class Router
8 {
9 protected $app = null;
10
11 protected $name = [];
12
13 protected $prefix = [];
14
15 protected $routes = [];
16
17 protected $routeGroups = [];
18
19 protected $groupStack = [];
20
21 protected $policyHandler = null;
22
23 public function __construct($app)
24 {
25 $this->app = $app;
26 }
27
28 public function prefix($prefix)
29 {
30 $this->prefix[] = $prefix;
31
32 return $this;
33 }
34
35 public function name($name)
36 {
37 $this->name[] = $name;
38
39 return $this;
40 }
41
42 public function group($attributes = [], \Closure $callback = null)
43 {
44 if ($attributes instanceof \Closure) {
45 $callback = $attributes;
46 $attributes = [];
47 }
48
49 if (isset($attributes['name'])) {
50 $this->name($attributes['name']);
51 }
52
53 if (isset($attributes['prefix'])) {
54 $this->prefix($attributes['prefix']);
55 }
56
57 call_user_func($callback, $this);
58 array_pop($this->prefix);
59 array_pop($this->name);
60
61 return $this;
62 }
63
64 public function withPolicy($handler)
65 {
66 $this->policyHandler = $handler;
67
68 return $this;
69 }
70
71 public function get($uri, $handler)
72 {
73 $this->routes[] = $route = $this->newRoute(
74 $uri, $handler, \WP_REST_Server::READABLE
75 );
76
77 return $route;
78 }
79
80 public function post($uri, $handler)
81 {
82 $this->routes[] = $route = $this->newRoute(
83 $uri, $handler, \WP_REST_Server::CREATABLE
84 );
85
86 return $route;
87 }
88
89 public function put($uri, $handler)
90 {
91 $this->routes[] = $route = $this->newRoute(
92 $uri, $handler, \WP_REST_Server::EDITABLE
93 );
94
95 return $route;
96 }
97
98 public function patch($uri, $handler)
99 {
100 $this->routes[] = $route = $this->newRoute(
101 $uri, $handler, \WP_REST_Server::EDITABLE
102 );
103
104 return $route;
105 }
106
107 public function delete($uri, $handler)
108 {
109 $this->routes[] = $route = $this->newRoute(
110 $uri, $handler, \WP_REST_Server::DELETABLE
111 );
112
113 return $route;
114 }
115
116 public function any($uri, $handler)
117 {
118 $this->routes[] = $route = $this->newRoute(
119 $uri, $handler, \WP_REST_Server::ALLMETHODS
120 );
121
122 return $route;
123 }
124
125 protected function newRoute($uri, $handler, $method)
126 {
127 $route = Route::create(
128 $this->app,
129 $this->getRestNamespace(),
130 $this->buildUriWithPrefix($uri),
131 $handler,
132 $method,
133 implode('', $this->name)
134 );
135
136 if ($this->policyHandler) {
137 $route->withPolicy($this->policyHandler);
138 }
139
140 return $route;
141 }
142
143 protected function getRestNamespace()
144 {
145 $version = $this->app->config->get('app.rest_version');
146
147 $namespace = trim($this->app->config->get('app.rest_namespace'), '/');
148
149 return "{$namespace}/{$version}";
150 }
151
152 protected function buildUriWithPrefix($uri)
153 {
154 $uri = trim($uri, '/');
155
156 $prefix = array_map(function($prefix) {
157 return trim($prefix, '/');
158 }, $this->prefix);
159
160 $prefix = implode('/', $prefix);
161
162 return trim($prefix, '/') . '/' . trim($uri, '/');
163 }
164
165 public function registerRoutes()
166 {
167 foreach ($this->routes as $route) $route->register();
168 }
169
170 public function getRoutes()
171 {
172 return $this->routes;
173 }
174 }
175