@@ -40,661 +40,661 @@ |
||
40 | 40 | */ |
41 | 41 | class Route |
42 | 42 | { |
43 | - use Concerns\RouteCondition, |
|
44 | - Concerns\RouteDependencyResolver; |
|
43 | + use Concerns\RouteCondition, |
|
44 | + Concerns\RouteDependencyResolver; |
|
45 | 45 | |
46 | - /** |
|
47 | - * Action that the route will use when called. |
|
48 | - * |
|
49 | - * @var \Closure|string|array $action |
|
50 | - */ |
|
51 | - public $action; |
|
52 | - |
|
53 | - /** |
|
54 | - * The container instance used by the route. |
|
55 | - * |
|
56 | - * @var \Syscodes\Container\Container $container |
|
57 | - */ |
|
58 | - protected $container; |
|
59 | - |
|
60 | - /** |
|
61 | - * The controller instance. |
|
62 | - * |
|
63 | - * @var string $controller |
|
64 | - */ |
|
65 | - public $controller; |
|
66 | - |
|
67 | - /** |
|
68 | - * The default values for the route. |
|
69 | - * |
|
70 | - * @var array $defaults |
|
71 | - */ |
|
72 | - public $defaults = []; |
|
73 | - |
|
74 | - /** |
|
75 | - * Variable of HTTP method. |
|
76 | - * |
|
77 | - * @var array|string $method |
|
78 | - */ |
|
79 | - public $method; |
|
80 | - |
|
81 | - /** |
|
82 | - * The array of matched parameters. |
|
83 | - * |
|
84 | - * @var array $parameters |
|
85 | - */ |
|
86 | - public $parameters = []; |
|
87 | - |
|
88 | - /** |
|
89 | - * The parameter names for the route. |
|
90 | - * |
|
91 | - * @var string|null $parameterNames |
|
92 | - */ |
|
93 | - public $parameterNames; |
|
94 | - |
|
95 | - /** |
|
96 | - * Patterns that should be replaced. |
|
97 | - * |
|
98 | - * @var array $patterns |
|
99 | - */ |
|
100 | - public $patterns = [ |
|
101 | - '~/~' => '\/', // Slash |
|
102 | - '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
103 | - '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
104 | - '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
105 | - '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
106 | - '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
107 | - '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
108 | - '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
109 | - ]; |
|
110 | - |
|
111 | - /** |
|
112 | - * The URI pattern the route responds to. |
|
113 | - * |
|
114 | - * @var array $uri |
|
115 | - */ |
|
116 | - public $uri = []; |
|
117 | - |
|
118 | - /** |
|
119 | - * Contains the arguments of the current route. |
|
120 | - * |
|
121 | - * @var array $where |
|
122 | - */ |
|
123 | - public $wheres = []; |
|
124 | - |
|
125 | - /** |
|
126 | - * Constructor. Initialize route. |
|
127 | - * |
|
128 | - * @param array|string|null $method (null by default) |
|
129 | - * @param string|null $uri (null by default) |
|
130 | - * @param \Closure|string|null $action (null by default) |
|
131 | - * |
|
132 | - * @return void |
|
133 | - */ |
|
134 | - public function __construct($method = null, $uri = null, $action = null) |
|
135 | - { |
|
136 | - $this->uri = $uri; |
|
137 | - |
|
138 | - // Set the method |
|
139 | - $this->parseMethod($method); |
|
140 | - |
|
141 | - // Set the action |
|
142 | - $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
143 | - |
|
144 | - if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
145 | - $this->prefix($prefix); |
|
146 | - } |
|
147 | - } |
|
148 | - |
|
149 | - // Getters |
|
150 | - |
|
151 | - /** |
|
152 | - * Get the action of the current route. |
|
153 | - * |
|
154 | - * @return \Closure|string|array |
|
155 | - */ |
|
156 | - public function getAction() |
|
157 | - { |
|
158 | - return $this->action; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the arguments of the current route. |
|
163 | - * |
|
164 | - * @return array |
|
165 | - */ |
|
166 | - public function getArguments() |
|
167 | - { |
|
168 | - return $this->wheres; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * Get the controller instance for the route. |
|
173 | - * |
|
174 | - * @return mixed |
|
175 | - */ |
|
176 | - public function getController() |
|
177 | - { |
|
178 | - if ( ! $this->controller) { |
|
179 | - $class = $this->parseControllerCallback()[0]; |
|
46 | + /** |
|
47 | + * Action that the route will use when called. |
|
48 | + * |
|
49 | + * @var \Closure|string|array $action |
|
50 | + */ |
|
51 | + public $action; |
|
52 | + |
|
53 | + /** |
|
54 | + * The container instance used by the route. |
|
55 | + * |
|
56 | + * @var \Syscodes\Container\Container $container |
|
57 | + */ |
|
58 | + protected $container; |
|
59 | + |
|
60 | + /** |
|
61 | + * The controller instance. |
|
62 | + * |
|
63 | + * @var string $controller |
|
64 | + */ |
|
65 | + public $controller; |
|
66 | + |
|
67 | + /** |
|
68 | + * The default values for the route. |
|
69 | + * |
|
70 | + * @var array $defaults |
|
71 | + */ |
|
72 | + public $defaults = []; |
|
73 | + |
|
74 | + /** |
|
75 | + * Variable of HTTP method. |
|
76 | + * |
|
77 | + * @var array|string $method |
|
78 | + */ |
|
79 | + public $method; |
|
80 | + |
|
81 | + /** |
|
82 | + * The array of matched parameters. |
|
83 | + * |
|
84 | + * @var array $parameters |
|
85 | + */ |
|
86 | + public $parameters = []; |
|
87 | + |
|
88 | + /** |
|
89 | + * The parameter names for the route. |
|
90 | + * |
|
91 | + * @var string|null $parameterNames |
|
92 | + */ |
|
93 | + public $parameterNames; |
|
94 | + |
|
95 | + /** |
|
96 | + * Patterns that should be replaced. |
|
97 | + * |
|
98 | + * @var array $patterns |
|
99 | + */ |
|
100 | + public $patterns = [ |
|
101 | + '~/~' => '\/', // Slash |
|
102 | + '~{an:[^\/{}]+}~' => '([0-9a-zA-Z]++)', // Placeholder accepts alphabetic and numeric chars |
|
103 | + '~{n:[^\/{}]+}~' => '([0-9]++)', // Placeholder accepts only numeric |
|
104 | + '~{a:[^\/{}]+}~' => '([a-zA-Z]++)', // Placeholder accepts only alphabetic chars |
|
105 | + '~{w:[^\/{}]+}~' => '([0-9a-zA-Z-_]++)', // Placeholder accepts alphanumeric and underscore |
|
106 | + '~{\*:[^\/{}]+}~' => '(.++)', // Placeholder match rest of url |
|
107 | + '~(\\\/)?{\?:[^\/{}]+}~' => '\/?([^\/]*)', // Optional placeholder |
|
108 | + '~{[^\/{}]+}~' => '([^\/]++)' // Normal placeholder |
|
109 | + ]; |
|
110 | + |
|
111 | + /** |
|
112 | + * The URI pattern the route responds to. |
|
113 | + * |
|
114 | + * @var array $uri |
|
115 | + */ |
|
116 | + public $uri = []; |
|
117 | + |
|
118 | + /** |
|
119 | + * Contains the arguments of the current route. |
|
120 | + * |
|
121 | + * @var array $where |
|
122 | + */ |
|
123 | + public $wheres = []; |
|
124 | + |
|
125 | + /** |
|
126 | + * Constructor. Initialize route. |
|
127 | + * |
|
128 | + * @param array|string|null $method (null by default) |
|
129 | + * @param string|null $uri (null by default) |
|
130 | + * @param \Closure|string|null $action (null by default) |
|
131 | + * |
|
132 | + * @return void |
|
133 | + */ |
|
134 | + public function __construct($method = null, $uri = null, $action = null) |
|
135 | + { |
|
136 | + $this->uri = $uri; |
|
137 | + |
|
138 | + // Set the method |
|
139 | + $this->parseMethod($method); |
|
140 | + |
|
141 | + // Set the action |
|
142 | + $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
143 | + |
|
144 | + if (is_null($prefix = Arr::get($this->action, 'prefix'))) { |
|
145 | + $this->prefix($prefix); |
|
146 | + } |
|
147 | + } |
|
148 | + |
|
149 | + // Getters |
|
150 | + |
|
151 | + /** |
|
152 | + * Get the action of the current route. |
|
153 | + * |
|
154 | + * @return \Closure|string|array |
|
155 | + */ |
|
156 | + public function getAction() |
|
157 | + { |
|
158 | + return $this->action; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the arguments of the current route. |
|
163 | + * |
|
164 | + * @return array |
|
165 | + */ |
|
166 | + public function getArguments() |
|
167 | + { |
|
168 | + return $this->wheres; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * Get the controller instance for the route. |
|
173 | + * |
|
174 | + * @return mixed |
|
175 | + */ |
|
176 | + public function getController() |
|
177 | + { |
|
178 | + if ( ! $this->controller) { |
|
179 | + $class = $this->parseControllerCallback()[0]; |
|
180 | 180 | |
181 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
182 | - } |
|
183 | - |
|
184 | - return $this->controller; |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Get the controller method used for the route. |
|
189 | - * |
|
190 | - * @return string |
|
191 | - */ |
|
192 | - public function getControllerMethod() |
|
193 | - { |
|
194 | - return $this->parseControllerCallback()[1]; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Get the request method of the current route. |
|
199 | - * |
|
200 | - * @return array|string |
|
201 | - */ |
|
202 | - public function getMethod() |
|
203 | - { |
|
204 | - return $this->method; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * Get the url of the current route. |
|
209 | - * |
|
210 | - * @return string |
|
211 | - */ |
|
212 | - public function getName() |
|
213 | - { |
|
214 | - return $this->action['as'] ?? null; |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Get the url of the current route. |
|
219 | - * |
|
220 | - * @return array |
|
221 | - */ |
|
222 | - public function getRoute() |
|
223 | - { |
|
224 | - return $this->uri; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Get or set the domain for the route. |
|
229 | - * |
|
230 | - * @param string|null $domain (null by default) |
|
231 | - * |
|
232 | - * @return $this |
|
233 | - */ |
|
234 | - public function domain($domain = null) |
|
235 | - { |
|
236 | - if (is_null($domain)) { |
|
237 | - return $this->getDomain(); |
|
238 | - } |
|
239 | - |
|
240 | - $this->action['domain'] = $this->parseRoute($domain); |
|
241 | - |
|
242 | - return $this; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Get the domain defined for the route. |
|
247 | - * |
|
248 | - * @return string|null |
|
249 | - */ |
|
250 | - public function getDomain() |
|
251 | - { |
|
252 | - return isset($this->action['domain']) |
|
253 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
254 | - : null; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Parse the controller. |
|
259 | - * |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - public function parseControllerCallback() |
|
263 | - { |
|
264 | - return Str::parseCallback($this->action['uses']); |
|
265 | - } |
|
181 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
182 | + } |
|
183 | + |
|
184 | + return $this->controller; |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Get the controller method used for the route. |
|
189 | + * |
|
190 | + * @return string |
|
191 | + */ |
|
192 | + public function getControllerMethod() |
|
193 | + { |
|
194 | + return $this->parseControllerCallback()[1]; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Get the request method of the current route. |
|
199 | + * |
|
200 | + * @return array|string |
|
201 | + */ |
|
202 | + public function getMethod() |
|
203 | + { |
|
204 | + return $this->method; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * Get the url of the current route. |
|
209 | + * |
|
210 | + * @return string |
|
211 | + */ |
|
212 | + public function getName() |
|
213 | + { |
|
214 | + return $this->action['as'] ?? null; |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Get the url of the current route. |
|
219 | + * |
|
220 | + * @return array |
|
221 | + */ |
|
222 | + public function getRoute() |
|
223 | + { |
|
224 | + return $this->uri; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Get or set the domain for the route. |
|
229 | + * |
|
230 | + * @param string|null $domain (null by default) |
|
231 | + * |
|
232 | + * @return $this |
|
233 | + */ |
|
234 | + public function domain($domain = null) |
|
235 | + { |
|
236 | + if (is_null($domain)) { |
|
237 | + return $this->getDomain(); |
|
238 | + } |
|
239 | + |
|
240 | + $this->action['domain'] = $this->parseRoute($domain); |
|
241 | + |
|
242 | + return $this; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Get the domain defined for the route. |
|
247 | + * |
|
248 | + * @return string|null |
|
249 | + */ |
|
250 | + public function getDomain() |
|
251 | + { |
|
252 | + return isset($this->action['domain']) |
|
253 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
254 | + : null; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Parse the controller. |
|
259 | + * |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + public function parseControllerCallback() |
|
263 | + { |
|
264 | + return Str::parseCallback($this->action['uses']); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Checks whether the route's action is a controller. |
|
269 | - * |
|
270 | - * @return bool |
|
271 | - */ |
|
272 | - public function isControllerAction() |
|
273 | - { |
|
274 | - return is_string($this->action['uses']); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Get the dispatcher for the route's controller. |
|
279 | - * |
|
280 | - * @return \Syscodes\Controller\ControllerDispatcher |
|
281 | - */ |
|
282 | - private function controllerDispatcher() |
|
283 | - { |
|
284 | - return new ControllerDispatcher($this->container); |
|
285 | - } |
|
286 | - |
|
287 | - // Setters |
|
267 | + /** |
|
268 | + * Checks whether the route's action is a controller. |
|
269 | + * |
|
270 | + * @return bool |
|
271 | + */ |
|
272 | + public function isControllerAction() |
|
273 | + { |
|
274 | + return is_string($this->action['uses']); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Get the dispatcher for the route's controller. |
|
279 | + * |
|
280 | + * @return \Syscodes\Controller\ControllerDispatcher |
|
281 | + */ |
|
282 | + private function controllerDispatcher() |
|
283 | + { |
|
284 | + return new ControllerDispatcher($this->container); |
|
285 | + } |
|
286 | + |
|
287 | + // Setters |
|
288 | 288 | |
289 | - /** |
|
290 | - * Run the route action and return the response. |
|
291 | - * |
|
292 | - * @return mixed |
|
293 | - */ |
|
294 | - public function runResolver() |
|
295 | - { |
|
296 | - $this->container = $this->container ?: new Container; |
|
297 | - |
|
298 | - try { |
|
299 | - if ($this->isControllerAction()) { |
|
300 | - return $this->runResolverController(); |
|
301 | - } |
|
302 | - |
|
303 | - return $this->runResolverCallable(); |
|
304 | - } catch (HttpResponseException $e) { |
|
305 | - return $e->getResponse(); |
|
306 | - } |
|
307 | - } |
|
308 | - |
|
309 | - /** |
|
310 | - * Run the route action and return the response. |
|
311 | - * |
|
312 | - * @return mixed |
|
313 | - */ |
|
314 | - protected function runResolverCallable() |
|
315 | - { |
|
316 | - $callable = $this->action['uses']; |
|
317 | - |
|
318 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
319 | - $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
320 | - ))); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Run the route action and return the response. |
|
325 | - * |
|
326 | - * @return mixed |
|
327 | - */ |
|
328 | - protected function runResolverController() |
|
329 | - { |
|
330 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Set the action. |
|
335 | - * |
|
336 | - * @param \Closure|string $action |
|
337 | - * |
|
338 | - * @return $this |
|
339 | - * |
|
340 | - * @throws \InvalidArgumentException |
|
341 | - */ |
|
342 | - public function parseAction($action) |
|
343 | - { |
|
344 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
345 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
346 | - } |
|
347 | - |
|
348 | - return RouteAction::parse($this->uri, $action); |
|
349 | - } |
|
350 | - |
|
351 | - /** |
|
352 | - * Set the method of the current route. |
|
353 | - * |
|
354 | - * @param array $method |
|
355 | - * |
|
356 | - * @return string $this |
|
357 | - * |
|
358 | - * @throws \InvalidArgumentException |
|
359 | - */ |
|
360 | - public function parseMethod($method) |
|
361 | - { |
|
362 | - if ($method === null || empty($method)) { |
|
363 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
289 | + /** |
|
290 | + * Run the route action and return the response. |
|
291 | + * |
|
292 | + * @return mixed |
|
293 | + */ |
|
294 | + public function runResolver() |
|
295 | + { |
|
296 | + $this->container = $this->container ?: new Container; |
|
297 | + |
|
298 | + try { |
|
299 | + if ($this->isControllerAction()) { |
|
300 | + return $this->runResolverController(); |
|
301 | + } |
|
302 | + |
|
303 | + return $this->runResolverCallable(); |
|
304 | + } catch (HttpResponseException $e) { |
|
305 | + return $e->getResponse(); |
|
306 | + } |
|
307 | + } |
|
308 | + |
|
309 | + /** |
|
310 | + * Run the route action and return the response. |
|
311 | + * |
|
312 | + * @return mixed |
|
313 | + */ |
|
314 | + protected function runResolverCallable() |
|
315 | + { |
|
316 | + $callable = $this->action['uses']; |
|
317 | + |
|
318 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
319 | + $this->parametersWithouNulls(), new ReflectionFunction($this->action['uses']) |
|
320 | + ))); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Run the route action and return the response. |
|
325 | + * |
|
326 | + * @return mixed |
|
327 | + */ |
|
328 | + protected function runResolverController() |
|
329 | + { |
|
330 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Set the action. |
|
335 | + * |
|
336 | + * @param \Closure|string $action |
|
337 | + * |
|
338 | + * @return $this |
|
339 | + * |
|
340 | + * @throws \InvalidArgumentException |
|
341 | + */ |
|
342 | + public function parseAction($action) |
|
343 | + { |
|
344 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
345 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
346 | + } |
|
347 | + |
|
348 | + return RouteAction::parse($this->uri, $action); |
|
349 | + } |
|
350 | + |
|
351 | + /** |
|
352 | + * Set the method of the current route. |
|
353 | + * |
|
354 | + * @param array $method |
|
355 | + * |
|
356 | + * @return string $this |
|
357 | + * |
|
358 | + * @throws \InvalidArgumentException |
|
359 | + */ |
|
360 | + public function parseMethod($method) |
|
361 | + { |
|
362 | + if ($method === null || empty($method)) { |
|
363 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
364 | 364 | |
365 | - } |
|
366 | - |
|
367 | - foreach ($method as $httpMethod) { |
|
368 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
369 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - $this->method = $method; |
|
374 | - |
|
375 | - return $this; |
|
376 | - } |
|
377 | - |
|
378 | - /** |
|
379 | - * Set the route. |
|
380 | - * |
|
381 | - * @param string|array|null $uri |
|
382 | - * |
|
383 | - * @return string |
|
384 | - * |
|
385 | - * @throws \InvalidArgumentException |
|
386 | - */ |
|
387 | - public function parseRoute($uri) |
|
388 | - { |
|
389 | - if ($uri === null) { |
|
390 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
391 | - } |
|
392 | - |
|
393 | - $this->uri = $this->parseRoutePath($uri); |
|
394 | - |
|
395 | - return $this; |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Replace word patterns with regex in route uri. |
|
400 | - * |
|
401 | - * @param string $uri |
|
402 | - * |
|
403 | - * @return string |
|
404 | - */ |
|
405 | - protected function parseRoutePath($uri) |
|
406 | - { |
|
407 | - $uri = trim($uri, '\/?'); |
|
408 | - $uri = trim($uri, '\/'); |
|
365 | + } |
|
366 | + |
|
367 | + foreach ($method as $httpMethod) { |
|
368 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
369 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + $this->method = $method; |
|
374 | + |
|
375 | + return $this; |
|
376 | + } |
|
377 | + |
|
378 | + /** |
|
379 | + * Set the route. |
|
380 | + * |
|
381 | + * @param string|array|null $uri |
|
382 | + * |
|
383 | + * @return string |
|
384 | + * |
|
385 | + * @throws \InvalidArgumentException |
|
386 | + */ |
|
387 | + public function parseRoute($uri) |
|
388 | + { |
|
389 | + if ($uri === null) { |
|
390 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
391 | + } |
|
392 | + |
|
393 | + $this->uri = $this->parseRoutePath($uri); |
|
394 | + |
|
395 | + return $this; |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Replace word patterns with regex in route uri. |
|
400 | + * |
|
401 | + * @param string $uri |
|
402 | + * |
|
403 | + * @return string |
|
404 | + */ |
|
405 | + protected function parseRoutePath($uri) |
|
406 | + { |
|
407 | + $uri = trim($uri, '\/?'); |
|
408 | + $uri = trim($uri, '\/'); |
|
409 | 409 | |
410 | - preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
410 | + preg_match_all('/\{([\w\:]+?)\??\}/', $uri, $matches); |
|
411 | 411 | |
412 | - foreach ($matches[1] as $match) { |
|
413 | - if (strpos($match, ':') === false) { |
|
414 | - continue; |
|
415 | - } |
|
412 | + foreach ($matches[1] as $match) { |
|
413 | + if (strpos($match, ':') === false) { |
|
414 | + continue; |
|
415 | + } |
|
416 | 416 | |
417 | - $pattern = array_keys($this->patterns); |
|
418 | - $replace = array_values($this->patterns); |
|
419 | - $segments = explode(':', trim($match, '{}?')); |
|
417 | + $pattern = array_keys($this->patterns); |
|
418 | + $replace = array_values($this->patterns); |
|
419 | + $segments = explode(':', trim($match, '{}?')); |
|
420 | 420 | |
421 | - $uri = strpos($match, ':') !== false |
|
422 | - ? preg_replace($pattern, $replace, $uri) |
|
423 | - : str_replace($match, '{'.$segments[0].'}', $uri); |
|
424 | - } |
|
421 | + $uri = strpos($match, ':') !== false |
|
422 | + ? preg_replace($pattern, $replace, $uri) |
|
423 | + : str_replace($match, '{'.$segments[0].'}', $uri); |
|
424 | + } |
|
425 | 425 | |
426 | - return $uri; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Add a prefix to the route URI. |
|
431 | - * |
|
432 | - * @param string $prefix |
|
433 | - * |
|
434 | - * @return $this |
|
435 | - */ |
|
436 | - public function prefix($prefix) |
|
437 | - { |
|
438 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
439 | - $this->action['prefix'] = $newPrefix; |
|
440 | - } |
|
426 | + return $uri; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Add a prefix to the route URI. |
|
431 | + * |
|
432 | + * @param string $prefix |
|
433 | + * |
|
434 | + * @return $this |
|
435 | + */ |
|
436 | + public function prefix($prefix) |
|
437 | + { |
|
438 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
439 | + $this->action['prefix'] = $newPrefix; |
|
440 | + } |
|
441 | 441 | |
442 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
442 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
443 | 443 | |
444 | - return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Set the action array for the route. |
|
449 | - * |
|
450 | - * @param array $action |
|
451 | - * |
|
452 | - * @return $this |
|
453 | - */ |
|
454 | - public function setAction(array $action) |
|
455 | - { |
|
456 | - $this->action = $action; |
|
457 | - |
|
458 | - if (isset($this->action['domain'])) { |
|
459 | - $this->domain($this->action['domain']); |
|
460 | - } |
|
444 | + return $this->parseRoute($uri !== '/' ? trim($uri, '/') : $uri); |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * Set the action array for the route. |
|
449 | + * |
|
450 | + * @param array $action |
|
451 | + * |
|
452 | + * @return $this |
|
453 | + */ |
|
454 | + public function setAction(array $action) |
|
455 | + { |
|
456 | + $this->action = $action; |
|
457 | + |
|
458 | + if (isset($this->action['domain'])) { |
|
459 | + $this->domain($this->action['domain']); |
|
460 | + } |
|
461 | 461 | |
462 | - return $this; |
|
463 | - } |
|
464 | - |
|
465 | - /** |
|
466 | - * Set the name. |
|
467 | - * |
|
468 | - * @param string $name |
|
469 | - * |
|
470 | - * @return $this |
|
471 | - */ |
|
472 | - public function name($name) |
|
473 | - { |
|
474 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
475 | - |
|
476 | - return $this; |
|
477 | - } |
|
478 | - |
|
479 | - /** |
|
480 | - * Determine whether the route's name matches the given patterns. |
|
481 | - * |
|
482 | - * @param mixed ...$patterns |
|
483 | - * |
|
484 | - * @return bool |
|
485 | - */ |
|
486 | - public function named(...$patterns) |
|
487 | - { |
|
488 | - if (is_null($routeName = $this->getName())) { |
|
489 | - return false; |
|
490 | - } |
|
491 | - |
|
492 | - foreach ($patterns as $pattern) { |
|
493 | - if (Str::is($pattern, $routeName)) { |
|
494 | - return true; |
|
495 | - } |
|
496 | - } |
|
497 | - |
|
498 | - return false; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Set a default value for the route. |
|
503 | - * |
|
504 | - * @param string $key |
|
505 | - * @param mixed $value |
|
506 | - * |
|
507 | - * @return $this |
|
508 | - */ |
|
509 | - public function defaults($key, $value) |
|
510 | - { |
|
511 | - $this->defaults[$key] = $value; |
|
512 | - |
|
513 | - return $this; |
|
514 | - } |
|
515 | - |
|
516 | - /** |
|
517 | - * Set a default values for the route. |
|
518 | - * |
|
519 | - * @param string $defaults |
|
520 | - * |
|
521 | - * @return $this |
|
522 | - */ |
|
523 | - public function setDefaults(array $defaults) |
|
524 | - { |
|
525 | - $this->defaults = $defaults; |
|
526 | - |
|
527 | - return $this; |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Set the where. |
|
532 | - * |
|
533 | - * @param array|string $name |
|
534 | - * @param string|null $expression (null by default) |
|
535 | - * |
|
536 | - * @return $this |
|
537 | - */ |
|
538 | - public function where($name, string $expression = null) |
|
539 | - { |
|
540 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
462 | + return $this; |
|
463 | + } |
|
464 | + |
|
465 | + /** |
|
466 | + * Set the name. |
|
467 | + * |
|
468 | + * @param string $name |
|
469 | + * |
|
470 | + * @return $this |
|
471 | + */ |
|
472 | + public function name($name) |
|
473 | + { |
|
474 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
475 | + |
|
476 | + return $this; |
|
477 | + } |
|
478 | + |
|
479 | + /** |
|
480 | + * Determine whether the route's name matches the given patterns. |
|
481 | + * |
|
482 | + * @param mixed ...$patterns |
|
483 | + * |
|
484 | + * @return bool |
|
485 | + */ |
|
486 | + public function named(...$patterns) |
|
487 | + { |
|
488 | + if (is_null($routeName = $this->getName())) { |
|
489 | + return false; |
|
490 | + } |
|
491 | + |
|
492 | + foreach ($patterns as $pattern) { |
|
493 | + if (Str::is($pattern, $routeName)) { |
|
494 | + return true; |
|
495 | + } |
|
496 | + } |
|
497 | + |
|
498 | + return false; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Set a default value for the route. |
|
503 | + * |
|
504 | + * @param string $key |
|
505 | + * @param mixed $value |
|
506 | + * |
|
507 | + * @return $this |
|
508 | + */ |
|
509 | + public function defaults($key, $value) |
|
510 | + { |
|
511 | + $this->defaults[$key] = $value; |
|
512 | + |
|
513 | + return $this; |
|
514 | + } |
|
515 | + |
|
516 | + /** |
|
517 | + * Set a default values for the route. |
|
518 | + * |
|
519 | + * @param string $defaults |
|
520 | + * |
|
521 | + * @return $this |
|
522 | + */ |
|
523 | + public function setDefaults(array $defaults) |
|
524 | + { |
|
525 | + $this->defaults = $defaults; |
|
526 | + |
|
527 | + return $this; |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Set the where. |
|
532 | + * |
|
533 | + * @param array|string $name |
|
534 | + * @param string|null $expression (null by default) |
|
535 | + * |
|
536 | + * @return $this |
|
537 | + */ |
|
538 | + public function where($name, string $expression = null) |
|
539 | + { |
|
540 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
541 | 541 | |
542 | - foreach ($wheres as $name => $expression) { |
|
543 | - $this->wheres[$name] = $expression; |
|
544 | - } |
|
545 | - |
|
546 | - return $this; |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Bind the route to a given request for execution. |
|
551 | - * |
|
552 | - * @param \Syscodes\Http\Request $request |
|
553 | - * |
|
554 | - * @return $this |
|
555 | - */ |
|
556 | - public function bind(Request $request) |
|
557 | - { |
|
558 | - $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
559 | - |
|
560 | - return $this; |
|
561 | - } |
|
562 | - |
|
563 | - /** |
|
564 | - * Get all of the parameter names for the route. |
|
565 | - * |
|
566 | - * @return array |
|
567 | - */ |
|
568 | - public function parameterNames() |
|
569 | - { |
|
570 | - if (isset($this->parameterNames)) { |
|
571 | - return $this->parameterNames; |
|
572 | - } |
|
573 | - |
|
574 | - return $this->parameterNames = $this->compileParamNames(); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Get the parameter names for the route. |
|
579 | - * |
|
580 | - * @return array |
|
581 | - */ |
|
582 | - protected function compileParamNames() |
|
583 | - { |
|
584 | - preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
585 | - |
|
586 | - return array_map(function ($match) { |
|
587 | - return trim($match, '?'); |
|
588 | - }, $matches[0]); |
|
589 | - } |
|
590 | - |
|
591 | - /** |
|
592 | - * Get a given parameter from the route. |
|
593 | - * |
|
594 | - * @param string $name |
|
595 | - * @param mixed $default (null by default) |
|
596 | - * |
|
597 | - * @return array |
|
598 | - */ |
|
599 | - public function parameter($name, $default = null) |
|
600 | - { |
|
601 | - return Arr::get($this->parameters(), $name, $default); |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
605 | - * Set a parameter to the given value. |
|
606 | - * |
|
607 | - * @param string $name |
|
608 | - * @param mixed $value |
|
609 | - * |
|
610 | - * @return array |
|
611 | - */ |
|
612 | - public function setParameter($name, $value) |
|
613 | - { |
|
614 | - $this->parameters(); |
|
615 | - |
|
616 | - $this->parameters[$name] = $value; |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * Get the key / value list of parameters without null values. |
|
621 | - * |
|
622 | - * @return array |
|
623 | - */ |
|
624 | - public function parametersWithouNulls() |
|
625 | - { |
|
626 | - return array_filter($this->parameters(), function ($parameter) { |
|
627 | - return ! is_null($parameter); |
|
628 | - }); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Get the key / value list of parameters for the route. |
|
633 | - * |
|
634 | - * @return array |
|
635 | - */ |
|
636 | - public function parameters() |
|
637 | - { |
|
638 | - if (isset($this->parameters)) { |
|
639 | - return $this->parameters; |
|
640 | - } |
|
641 | - |
|
642 | - throw new LogicException('The route is not bound.'); |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * Determine if the route only responds to HTTP requests. |
|
647 | - * |
|
648 | - * @return bool |
|
649 | - */ |
|
650 | - public function httpOnly() |
|
651 | - { |
|
652 | - return in_array('http', $this->action, true); |
|
653 | - } |
|
654 | - |
|
655 | - /** |
|
656 | - * Determine if the route only responds to HTTPS requests. |
|
657 | - * |
|
658 | - * @return bool |
|
659 | - */ |
|
660 | - public function httpsOnly() |
|
661 | - { |
|
662 | - return $this->secure(); |
|
663 | - } |
|
664 | - |
|
665 | - /** |
|
666 | - * Determine if the route only responds to HTTPS requests. |
|
667 | - * |
|
668 | - * @return bool |
|
669 | - */ |
|
670 | - public function secure() |
|
671 | - { |
|
672 | - return in_array('https', $this->action, true); |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Set the container instance on the route. |
|
677 | - * |
|
678 | - * @param \Syscodes\Container\Container $container |
|
679 | - * |
|
680 | - * @return $this |
|
681 | - */ |
|
682 | - public function setContainer(Container $container) |
|
683 | - { |
|
684 | - $this->container = $container; |
|
685 | - |
|
686 | - return $this; |
|
687 | - } |
|
688 | - |
|
689 | - /** |
|
690 | - * Dynamically access route parameters. |
|
691 | - * |
|
692 | - * @param string $key |
|
693 | - * |
|
694 | - * @return mixed |
|
695 | - */ |
|
696 | - public function __get($key) |
|
697 | - { |
|
698 | - return $this->parameter($key); |
|
699 | - } |
|
542 | + foreach ($wheres as $name => $expression) { |
|
543 | + $this->wheres[$name] = $expression; |
|
544 | + } |
|
545 | + |
|
546 | + return $this; |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Bind the route to a given request for execution. |
|
551 | + * |
|
552 | + * @param \Syscodes\Http\Request $request |
|
553 | + * |
|
554 | + * @return $this |
|
555 | + */ |
|
556 | + public function bind(Request $request) |
|
557 | + { |
|
558 | + $this->parameters = (new RouteParamBinding($this))->parameters($request); |
|
559 | + |
|
560 | + return $this; |
|
561 | + } |
|
562 | + |
|
563 | + /** |
|
564 | + * Get all of the parameter names for the route. |
|
565 | + * |
|
566 | + * @return array |
|
567 | + */ |
|
568 | + public function parameterNames() |
|
569 | + { |
|
570 | + if (isset($this->parameterNames)) { |
|
571 | + return $this->parameterNames; |
|
572 | + } |
|
573 | + |
|
574 | + return $this->parameterNames = $this->compileParamNames(); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Get the parameter names for the route. |
|
579 | + * |
|
580 | + * @return array |
|
581 | + */ |
|
582 | + protected function compileParamNames() |
|
583 | + { |
|
584 | + preg_match_all('~[^\/\{(.*?)\}]~', $this->domain().$this->uri, $matches); |
|
585 | + |
|
586 | + return array_map(function ($match) { |
|
587 | + return trim($match, '?'); |
|
588 | + }, $matches[0]); |
|
589 | + } |
|
590 | + |
|
591 | + /** |
|
592 | + * Get a given parameter from the route. |
|
593 | + * |
|
594 | + * @param string $name |
|
595 | + * @param mixed $default (null by default) |
|
596 | + * |
|
597 | + * @return array |
|
598 | + */ |
|
599 | + public function parameter($name, $default = null) |
|
600 | + { |
|
601 | + return Arr::get($this->parameters(), $name, $default); |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | + * Set a parameter to the given value. |
|
606 | + * |
|
607 | + * @param string $name |
|
608 | + * @param mixed $value |
|
609 | + * |
|
610 | + * @return array |
|
611 | + */ |
|
612 | + public function setParameter($name, $value) |
|
613 | + { |
|
614 | + $this->parameters(); |
|
615 | + |
|
616 | + $this->parameters[$name] = $value; |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * Get the key / value list of parameters without null values. |
|
621 | + * |
|
622 | + * @return array |
|
623 | + */ |
|
624 | + public function parametersWithouNulls() |
|
625 | + { |
|
626 | + return array_filter($this->parameters(), function ($parameter) { |
|
627 | + return ! is_null($parameter); |
|
628 | + }); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Get the key / value list of parameters for the route. |
|
633 | + * |
|
634 | + * @return array |
|
635 | + */ |
|
636 | + public function parameters() |
|
637 | + { |
|
638 | + if (isset($this->parameters)) { |
|
639 | + return $this->parameters; |
|
640 | + } |
|
641 | + |
|
642 | + throw new LogicException('The route is not bound.'); |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * Determine if the route only responds to HTTP requests. |
|
647 | + * |
|
648 | + * @return bool |
|
649 | + */ |
|
650 | + public function httpOnly() |
|
651 | + { |
|
652 | + return in_array('http', $this->action, true); |
|
653 | + } |
|
654 | + |
|
655 | + /** |
|
656 | + * Determine if the route only responds to HTTPS requests. |
|
657 | + * |
|
658 | + * @return bool |
|
659 | + */ |
|
660 | + public function httpsOnly() |
|
661 | + { |
|
662 | + return $this->secure(); |
|
663 | + } |
|
664 | + |
|
665 | + /** |
|
666 | + * Determine if the route only responds to HTTPS requests. |
|
667 | + * |
|
668 | + * @return bool |
|
669 | + */ |
|
670 | + public function secure() |
|
671 | + { |
|
672 | + return in_array('https', $this->action, true); |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Set the container instance on the route. |
|
677 | + * |
|
678 | + * @param \Syscodes\Container\Container $container |
|
679 | + * |
|
680 | + * @return $this |
|
681 | + */ |
|
682 | + public function setContainer(Container $container) |
|
683 | + { |
|
684 | + $this->container = $container; |
|
685 | + |
|
686 | + return $this; |
|
687 | + } |
|
688 | + |
|
689 | + /** |
|
690 | + * Dynamically access route parameters. |
|
691 | + * |
|
692 | + * @param string $key |
|
693 | + * |
|
694 | + * @return mixed |
|
695 | + */ |
|
696 | + public function __get($key) |
|
697 | + { |
|
698 | + return $this->parameter($key); |
|
699 | + } |
|
700 | 700 | } |
701 | 701 | \ No newline at end of file |