@@ -43,691 +43,691 @@ |
||
43 | 43 | */ |
44 | 44 | class Router implements Routable |
45 | 45 | { |
46 | - use Concerns\RouteMap, |
|
47 | - Macroable { |
|
48 | - __call as macroCall; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * The registered route value binders. |
|
53 | - * |
|
54 | - * @var array $binders |
|
55 | - */ |
|
56 | - protected $binders = []; |
|
57 | - |
|
58 | - /** |
|
59 | - * The container instance used by the router. |
|
60 | - * |
|
61 | - * @var \Syscodes\Components\Contracts\Container\Container $container |
|
62 | - */ |
|
63 | - protected $container; |
|
64 | - |
|
65 | - /** |
|
66 | - * Variable of group route. |
|
67 | - * |
|
68 | - * @var array $groupStack |
|
69 | - */ |
|
70 | - protected $groupStack = []; |
|
71 | - |
|
72 | - /** |
|
73 | - * Middleware for function of filters |
|
74 | - * |
|
75 | - * @var array $middleware |
|
76 | - */ |
|
77 | - protected $middleware = []; |
|
46 | + use Concerns\RouteMap, |
|
47 | + Macroable { |
|
48 | + __call as macroCall; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * The registered route value binders. |
|
53 | + * |
|
54 | + * @var array $binders |
|
55 | + */ |
|
56 | + protected $binders = []; |
|
57 | + |
|
58 | + /** |
|
59 | + * The container instance used by the router. |
|
60 | + * |
|
61 | + * @var \Syscodes\Components\Contracts\Container\Container $container |
|
62 | + */ |
|
63 | + protected $container; |
|
64 | + |
|
65 | + /** |
|
66 | + * Variable of group route. |
|
67 | + * |
|
68 | + * @var array $groupStack |
|
69 | + */ |
|
70 | + protected $groupStack = []; |
|
71 | + |
|
72 | + /** |
|
73 | + * Middleware for function of filters |
|
74 | + * |
|
75 | + * @var array $middleware |
|
76 | + */ |
|
77 | + protected $middleware = []; |
|
78 | 78 | |
79 | - /** |
|
80 | - * All of the middleware groups. |
|
81 | - * |
|
82 | - * @var array $middlewareGroups |
|
83 | - */ |
|
84 | - protected $middlewareGroups = []; |
|
79 | + /** |
|
80 | + * All of the middleware groups. |
|
81 | + * |
|
82 | + * @var array $middlewareGroups |
|
83 | + */ |
|
84 | + protected $middlewareGroups = []; |
|
85 | 85 | |
86 | - /** |
|
87 | - * The priority-sorted list of middleware. |
|
88 | - * |
|
89 | - * @var array $middlewarePriority |
|
90 | - */ |
|
91 | - public $middlewarePriority = []; |
|
86 | + /** |
|
87 | + * The priority-sorted list of middleware. |
|
88 | + * |
|
89 | + * @var array $middlewarePriority |
|
90 | + */ |
|
91 | + public $middlewarePriority = []; |
|
92 | 92 | |
93 | - /** |
|
94 | - * The globally available parameter patterns. |
|
95 | - * |
|
96 | - * @var array $patterns |
|
97 | - */ |
|
98 | - protected $patterns = []; |
|
99 | - |
|
100 | - /** |
|
101 | - * Resolve the given route. |
|
102 | - * |
|
103 | - * @var \Syscodes\Components\Routing\Resolver\RouteResolver $resolves |
|
104 | - */ |
|
105 | - protected $resolves; |
|
106 | - |
|
107 | - /** |
|
108 | - * The Resource instance. |
|
109 | - * |
|
110 | - * @var \Syscodes\Components\Routing\Resources\ResourceRegister $resources |
|
111 | - */ |
|
112 | - protected $resources; |
|
113 | - |
|
114 | - /** |
|
115 | - * The route collection instance. |
|
116 | - * |
|
117 | - * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
118 | - */ |
|
119 | - protected $routes; |
|
120 | - |
|
121 | - /** |
|
122 | - * Constructor. Create a new Router instance. |
|
123 | - * |
|
124 | - * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
125 | - * |
|
126 | - * @return void |
|
127 | - */ |
|
128 | - public function __construct(Container $container = null) |
|
129 | - { |
|
130 | - $this->routes = new RouteCollection; |
|
131 | - $this->container = $container ?: new Container; |
|
132 | - $this->resolves = new RouteResolver($this, $this->routes, $this->container); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Get the prefix from the group on the stack. |
|
137 | - * |
|
138 | - * @return string |
|
139 | - */ |
|
140 | - public function getGroupPrefix(): string |
|
141 | - { |
|
142 | - if ($this->hasGroupStack()) { |
|
143 | - $last = end($this->groupStack); |
|
144 | - |
|
145 | - return $last['prefix'] ?? ''; |
|
146 | - } |
|
147 | - |
|
148 | - return ''; |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Group a series of routes under a single URL segment. This is handy |
|
153 | - * for grouping items into an admin area, like: |
|
154 | - * |
|
155 | - * Example: |
|
156 | - * // Creates route: /admin show the word 'User' |
|
157 | - * Route::group(['prefix' => 'admin'], function() { |
|
158 | - * |
|
159 | - * Route::get('/user', function() { |
|
160 | - * echo 'Hello world..!'; |
|
161 | - * }); |
|
162 | - * |
|
163 | - * }); /admin/user |
|
164 | - * |
|
165 | - * @param array $attributes |
|
166 | - * @param \Closure|array|string $callback |
|
167 | - * |
|
168 | - * @return void |
|
169 | - */ |
|
170 | - public function group(array $attributes, $routes): void |
|
171 | - { |
|
172 | - foreach (Arr::wrap($routes) as $groupRoutes) { |
|
173 | - $this->updateGroupStack($attributes); |
|
93 | + /** |
|
94 | + * The globally available parameter patterns. |
|
95 | + * |
|
96 | + * @var array $patterns |
|
97 | + */ |
|
98 | + protected $patterns = []; |
|
99 | + |
|
100 | + /** |
|
101 | + * Resolve the given route. |
|
102 | + * |
|
103 | + * @var \Syscodes\Components\Routing\Resolver\RouteResolver $resolves |
|
104 | + */ |
|
105 | + protected $resolves; |
|
106 | + |
|
107 | + /** |
|
108 | + * The Resource instance. |
|
109 | + * |
|
110 | + * @var \Syscodes\Components\Routing\Resources\ResourceRegister $resources |
|
111 | + */ |
|
112 | + protected $resources; |
|
113 | + |
|
114 | + /** |
|
115 | + * The route collection instance. |
|
116 | + * |
|
117 | + * @var \Syscodes\Components\Routing\Collections\RouteCollection $routes |
|
118 | + */ |
|
119 | + protected $routes; |
|
120 | + |
|
121 | + /** |
|
122 | + * Constructor. Create a new Router instance. |
|
123 | + * |
|
124 | + * @param \Syscodes\Components\Contracts\Container\Container|null $container |
|
125 | + * |
|
126 | + * @return void |
|
127 | + */ |
|
128 | + public function __construct(Container $container = null) |
|
129 | + { |
|
130 | + $this->routes = new RouteCollection; |
|
131 | + $this->container = $container ?: new Container; |
|
132 | + $this->resolves = new RouteResolver($this, $this->routes, $this->container); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Get the prefix from the group on the stack. |
|
137 | + * |
|
138 | + * @return string |
|
139 | + */ |
|
140 | + public function getGroupPrefix(): string |
|
141 | + { |
|
142 | + if ($this->hasGroupStack()) { |
|
143 | + $last = end($this->groupStack); |
|
144 | + |
|
145 | + return $last['prefix'] ?? ''; |
|
146 | + } |
|
147 | + |
|
148 | + return ''; |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Group a series of routes under a single URL segment. This is handy |
|
153 | + * for grouping items into an admin area, like: |
|
154 | + * |
|
155 | + * Example: |
|
156 | + * // Creates route: /admin show the word 'User' |
|
157 | + * Route::group(['prefix' => 'admin'], function() { |
|
158 | + * |
|
159 | + * Route::get('/user', function() { |
|
160 | + * echo 'Hello world..!'; |
|
161 | + * }); |
|
162 | + * |
|
163 | + * }); /admin/user |
|
164 | + * |
|
165 | + * @param array $attributes |
|
166 | + * @param \Closure|array|string $callback |
|
167 | + * |
|
168 | + * @return void |
|
169 | + */ |
|
170 | + public function group(array $attributes, $routes): void |
|
171 | + { |
|
172 | + foreach (Arr::wrap($routes) as $groupRoutes) { |
|
173 | + $this->updateGroupStack($attributes); |
|
174 | 174 | |
175 | - $this->loadRoutes($groupRoutes); |
|
175 | + $this->loadRoutes($groupRoutes); |
|
176 | 176 | |
177 | - array_pop($this->groupStack); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Update the group stack with the given attributes. |
|
183 | - * |
|
184 | - * @param array $attributes |
|
185 | - * |
|
186 | - * @return void |
|
187 | - */ |
|
188 | - protected function updateGroupStack(array $attributes): void |
|
189 | - { |
|
190 | - if ($this->hasGroupStack()) { |
|
191 | - $attributes = $this->mergeLastGroup($attributes); |
|
192 | - } |
|
193 | - |
|
194 | - $this->groupStack[] = $attributes; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Merge the given group attributes. |
|
199 | - * |
|
200 | - * @param array $new |
|
201 | - * @param bool $existsPrefix |
|
202 | - * |
|
203 | - * @return array |
|
204 | - */ |
|
205 | - public function mergeLastGroup($new, bool $existsPrefix = true): array |
|
206 | - { |
|
207 | - return RouteGroup::mergeGroup($new, end($this->groupStack), $existsPrefix); |
|
208 | - } |
|
177 | + array_pop($this->groupStack); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Update the group stack with the given attributes. |
|
183 | + * |
|
184 | + * @param array $attributes |
|
185 | + * |
|
186 | + * @return void |
|
187 | + */ |
|
188 | + protected function updateGroupStack(array $attributes): void |
|
189 | + { |
|
190 | + if ($this->hasGroupStack()) { |
|
191 | + $attributes = $this->mergeLastGroup($attributes); |
|
192 | + } |
|
193 | + |
|
194 | + $this->groupStack[] = $attributes; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Merge the given group attributes. |
|
199 | + * |
|
200 | + * @param array $new |
|
201 | + * @param bool $existsPrefix |
|
202 | + * |
|
203 | + * @return array |
|
204 | + */ |
|
205 | + public function mergeLastGroup($new, bool $existsPrefix = true): array |
|
206 | + { |
|
207 | + return RouteGroup::mergeGroup($new, end($this->groupStack), $existsPrefix); |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * Load the provided routes. |
|
212 | - * |
|
213 | - * @param \Closure|string $callback |
|
214 | - * |
|
215 | - * @return void |
|
216 | - */ |
|
217 | - protected function loadRoutes($callback): void |
|
218 | - { |
|
219 | - if ($callback instanceof Closure) { |
|
220 | - $callback($this); |
|
221 | - } else { |
|
222 | - (new RouteFileRegister($this))->register($callback); |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Add a route to the underlying route collection. |
|
228 | - * |
|
229 | - * @param array|string $method |
|
230 | - * @param string $route |
|
231 | - * @param mixed $action |
|
232 | - * |
|
233 | - * @return \Syscodes\Components\Routing\Route |
|
234 | - */ |
|
235 | - public function addRoute($method, $route, $action) |
|
236 | - { |
|
237 | - return $this->routes->add($this->map($method, $route, $action)); |
|
238 | - } |
|
210 | + /** |
|
211 | + * Load the provided routes. |
|
212 | + * |
|
213 | + * @param \Closure|string $callback |
|
214 | + * |
|
215 | + * @return void |
|
216 | + */ |
|
217 | + protected function loadRoutes($callback): void |
|
218 | + { |
|
219 | + if ($callback instanceof Closure) { |
|
220 | + $callback($this); |
|
221 | + } else { |
|
222 | + (new RouteFileRegister($this))->register($callback); |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Add a route to the underlying route collection. |
|
228 | + * |
|
229 | + * @param array|string $method |
|
230 | + * @param string $route |
|
231 | + * @param mixed $action |
|
232 | + * |
|
233 | + * @return \Syscodes\Components\Routing\Route |
|
234 | + */ |
|
235 | + public function addRoute($method, $route, $action) |
|
236 | + { |
|
237 | + return $this->routes->add($this->map($method, $route, $action)); |
|
238 | + } |
|
239 | 239 | |
240 | - /** |
|
241 | - * Register a new fallback route with the router. |
|
242 | - * |
|
243 | - * @param array|string|callable|null $action |
|
244 | - * |
|
245 | - * @return \Syscodes\Components\Routing\Route |
|
246 | - */ |
|
247 | - public function fallback($action) |
|
248 | - { |
|
249 | - $placeholder = 'fallbackPlaceholder'; |
|
240 | + /** |
|
241 | + * Register a new fallback route with the router. |
|
242 | + * |
|
243 | + * @param array|string|callable|null $action |
|
244 | + * |
|
245 | + * @return \Syscodes\Components\Routing\Route |
|
246 | + */ |
|
247 | + public function fallback($action) |
|
248 | + { |
|
249 | + $placeholder = 'fallbackPlaceholder'; |
|
250 | 250 | |
251 | - return $this->addRoute('GET', "{{$placeholder}}", $action) |
|
252 | - ->where($placeholder, '.*') |
|
253 | - ->fallback(); |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Create a redirect from one URI to another. |
|
258 | - * |
|
259 | - * @param string $uri |
|
260 | - * @param string $destination |
|
261 | - * @param int $status |
|
262 | - * |
|
263 | - * @return \Syscodes\Components\Routing\Route |
|
264 | - */ |
|
265 | - public function redirect($uri, $destination, $status = 302) |
|
266 | - { |
|
267 | - return $this->any($uri, '\Syscodes\Components\Routing\Controllers\RedirectController') |
|
268 | - ->defaults('destination', $destination) |
|
269 | - ->defaults('status', $status); |
|
270 | - } |
|
251 | + return $this->addRoute('GET', "{{$placeholder}}", $action) |
|
252 | + ->where($placeholder, '.*') |
|
253 | + ->fallback(); |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Create a redirect from one URI to another. |
|
258 | + * |
|
259 | + * @param string $uri |
|
260 | + * @param string $destination |
|
261 | + * @param int $status |
|
262 | + * |
|
263 | + * @return \Syscodes\Components\Routing\Route |
|
264 | + */ |
|
265 | + public function redirect($uri, $destination, $status = 302) |
|
266 | + { |
|
267 | + return $this->any($uri, '\Syscodes\Components\Routing\Controllers\RedirectController') |
|
268 | + ->defaults('destination', $destination) |
|
269 | + ->defaults('status', $status); |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * Create a permanent redirect from one URI to another. |
|
274 | - * |
|
275 | - * @param string $uri |
|
276 | - * @param string $destination |
|
277 | - * |
|
278 | - * @return \Syscodes\Components\Routing\Route |
|
279 | - */ |
|
280 | - public function permanentRedirect($uri, $destination) |
|
281 | - { |
|
282 | - return $this->redirect($uri, $destination, 301); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Register a new route that returns a view. |
|
287 | - * |
|
288 | - * @param string $uri |
|
289 | - * @param string $view |
|
290 | - * @param array $data |
|
291 | - * @param int|array $status |
|
292 | - * @param array $headers |
|
293 | - * |
|
294 | - * @return \Syscodes\Components\Routing\Route |
|
295 | - */ |
|
296 | - public function view($uri, $view, $data = [], $status = 200, array $headers = []) |
|
297 | - { |
|
298 | - return $this->match(['GET', 'HEAD'], $uri, '\Syscodes\Components\Routing\Controllers\ViewController') |
|
299 | - ->setDefaults([ |
|
300 | - 'view' => $view, |
|
301 | - 'data' => $data, |
|
302 | - 'status' => is_array($status) ? 200 : $status, |
|
303 | - 'headers' => is_array($status) ? $status : $headers, |
|
304 | - ]); |
|
305 | - } |
|
306 | - |
|
307 | - /** |
|
308 | - * Add new route to routes array. |
|
309 | - * |
|
310 | - * @param array|string $method |
|
311 | - * @param string $route |
|
312 | - * @param mixed $action |
|
313 | - * |
|
314 | - * @return \Syscodes\Components\Routing\Route |
|
315 | - * |
|
316 | - * @throws \InvalidArgumentException |
|
317 | - */ |
|
318 | - public function map($method, $route, $action): Route |
|
319 | - { |
|
320 | - if ($this->actionReferencesController($action)) { |
|
321 | - $action = $this->convertToControllerAction($action); |
|
322 | - } |
|
323 | - |
|
324 | - $route = $this->newRoute( |
|
325 | - $method, |
|
326 | - $this->prefix($route), |
|
327 | - $action |
|
328 | - ); |
|
329 | - |
|
330 | - if ($this->hasGroupStack()) { |
|
331 | - $this->mergeGroupAttributesIntoRoute($route); |
|
332 | - } |
|
333 | - |
|
334 | - $this->addWhereClausesToRoute($route); |
|
272 | + /** |
|
273 | + * Create a permanent redirect from one URI to another. |
|
274 | + * |
|
275 | + * @param string $uri |
|
276 | + * @param string $destination |
|
277 | + * |
|
278 | + * @return \Syscodes\Components\Routing\Route |
|
279 | + */ |
|
280 | + public function permanentRedirect($uri, $destination) |
|
281 | + { |
|
282 | + return $this->redirect($uri, $destination, 301); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Register a new route that returns a view. |
|
287 | + * |
|
288 | + * @param string $uri |
|
289 | + * @param string $view |
|
290 | + * @param array $data |
|
291 | + * @param int|array $status |
|
292 | + * @param array $headers |
|
293 | + * |
|
294 | + * @return \Syscodes\Components\Routing\Route |
|
295 | + */ |
|
296 | + public function view($uri, $view, $data = [], $status = 200, array $headers = []) |
|
297 | + { |
|
298 | + return $this->match(['GET', 'HEAD'], $uri, '\Syscodes\Components\Routing\Controllers\ViewController') |
|
299 | + ->setDefaults([ |
|
300 | + 'view' => $view, |
|
301 | + 'data' => $data, |
|
302 | + 'status' => is_array($status) ? 200 : $status, |
|
303 | + 'headers' => is_array($status) ? $status : $headers, |
|
304 | + ]); |
|
305 | + } |
|
306 | + |
|
307 | + /** |
|
308 | + * Add new route to routes array. |
|
309 | + * |
|
310 | + * @param array|string $method |
|
311 | + * @param string $route |
|
312 | + * @param mixed $action |
|
313 | + * |
|
314 | + * @return \Syscodes\Components\Routing\Route |
|
315 | + * |
|
316 | + * @throws \InvalidArgumentException |
|
317 | + */ |
|
318 | + public function map($method, $route, $action): Route |
|
319 | + { |
|
320 | + if ($this->actionReferencesController($action)) { |
|
321 | + $action = $this->convertToControllerAction($action); |
|
322 | + } |
|
323 | + |
|
324 | + $route = $this->newRoute( |
|
325 | + $method, |
|
326 | + $this->prefix($route), |
|
327 | + $action |
|
328 | + ); |
|
329 | + |
|
330 | + if ($this->hasGroupStack()) { |
|
331 | + $this->mergeGroupAttributesIntoRoute($route); |
|
332 | + } |
|
333 | + |
|
334 | + $this->addWhereClausesToRoute($route); |
|
335 | 335 | |
336 | - return $route; |
|
337 | - } |
|
336 | + return $route; |
|
337 | + } |
|
338 | 338 | |
339 | - /** |
|
340 | - * Determine if the action is routing to a controller. |
|
341 | - * |
|
342 | - * @param array $action |
|
343 | - * |
|
344 | - * @return bool |
|
345 | - */ |
|
346 | - protected function actionReferencesController($action): bool |
|
347 | - { |
|
348 | - if ( ! $action instanceof Closure) { |
|
349 | - return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
350 | - } |
|
339 | + /** |
|
340 | + * Determine if the action is routing to a controller. |
|
341 | + * |
|
342 | + * @param array $action |
|
343 | + * |
|
344 | + * @return bool |
|
345 | + */ |
|
346 | + protected function actionReferencesController($action): bool |
|
347 | + { |
|
348 | + if ( ! $action instanceof Closure) { |
|
349 | + return is_string($action) || (isset($action['uses']) && is_string($action['uses'])); |
|
350 | + } |
|
351 | 351 | |
352 | - return false; |
|
353 | - } |
|
352 | + return false; |
|
353 | + } |
|
354 | 354 | |
355 | - /** |
|
356 | - * Add a controller based route action to the action array. |
|
357 | - * |
|
358 | - * @param array|string $action |
|
359 | - * |
|
360 | - * @return array |
|
361 | - */ |
|
362 | - protected function convertToControllerAction($action): array |
|
363 | - { |
|
364 | - if (is_string($action)) { |
|
365 | - $action = ['uses' => $action]; |
|
366 | - } |
|
355 | + /** |
|
356 | + * Add a controller based route action to the action array. |
|
357 | + * |
|
358 | + * @param array|string $action |
|
359 | + * |
|
360 | + * @return array |
|
361 | + */ |
|
362 | + protected function convertToControllerAction($action): array |
|
363 | + { |
|
364 | + if (is_string($action)) { |
|
365 | + $action = ['uses' => $action]; |
|
366 | + } |
|
367 | 367 | |
368 | - if ($this->hasGroupStack()) { |
|
369 | - $action['uses'] = $this->prependGroupController($action['uses']); |
|
370 | - $action['uses'] = $this->prependGroupNamespace($action['uses']); |
|
371 | - } |
|
368 | + if ($this->hasGroupStack()) { |
|
369 | + $action['uses'] = $this->prependGroupController($action['uses']); |
|
370 | + $action['uses'] = $this->prependGroupNamespace($action['uses']); |
|
371 | + } |
|
372 | 372 | |
373 | - $action['controller'] = $action['uses']; |
|
373 | + $action['controller'] = $action['uses']; |
|
374 | 374 | |
375 | - return $action; |
|
376 | - } |
|
375 | + return $action; |
|
376 | + } |
|
377 | 377 | |
378 | - /** |
|
379 | - * Prepend the last group namespaces onto the use clause. |
|
380 | - * |
|
381 | - * @param string $class |
|
382 | - * |
|
383 | - * @return string |
|
384 | - */ |
|
385 | - protected function prependGroupNamespace($class): string |
|
386 | - { |
|
387 | - $group = end($this->groupStack); |
|
378 | + /** |
|
379 | + * Prepend the last group namespaces onto the use clause. |
|
380 | + * |
|
381 | + * @param string $class |
|
382 | + * |
|
383 | + * @return string |
|
384 | + */ |
|
385 | + protected function prependGroupNamespace($class): string |
|
386 | + { |
|
387 | + $group = end($this->groupStack); |
|
388 | 388 | |
389 | - return isset($group['namespace']) && ! Str::startsWith($class, '\\') && ! Str::startsWith($class, $group['namespace']) |
|
390 | - ? $group['namespace'].'\\'.$class |
|
391 | - : $class; |
|
392 | - } |
|
389 | + return isset($group['namespace']) && ! Str::startsWith($class, '\\') && ! Str::startsWith($class, $group['namespace']) |
|
390 | + ? $group['namespace'].'\\'.$class |
|
391 | + : $class; |
|
392 | + } |
|
393 | 393 | |
394 | - /** |
|
395 | - * Prepend the last group controller onto the use clause. |
|
396 | - * |
|
397 | - * @param string $class |
|
398 | - * |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - protected function prependGroupController($class): string |
|
402 | - { |
|
403 | - $group = end($this->groupStack); |
|
394 | + /** |
|
395 | + * Prepend the last group controller onto the use clause. |
|
396 | + * |
|
397 | + * @param string $class |
|
398 | + * |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + protected function prependGroupController($class): string |
|
402 | + { |
|
403 | + $group = end($this->groupStack); |
|
404 | 404 | |
405 | - if ( ! isset($group['controller'])) { |
|
406 | - return $class; |
|
407 | - } |
|
405 | + if ( ! isset($group['controller'])) { |
|
406 | + return $class; |
|
407 | + } |
|
408 | 408 | |
409 | - if (class_exists($class)) { |
|
410 | - return $class; |
|
411 | - } |
|
409 | + if (class_exists($class)) { |
|
410 | + return $class; |
|
411 | + } |
|
412 | 412 | |
413 | - if (Str::contains($class, '@')) { |
|
414 | - return $class; |
|
415 | - } |
|
413 | + if (Str::contains($class, '@')) { |
|
414 | + return $class; |
|
415 | + } |
|
416 | 416 | |
417 | - return $group['controller'].'@'.$class; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Create a new Route object. |
|
422 | - * |
|
423 | - * @param array|string $method |
|
424 | - * @param string $uri |
|
425 | - * @param mixed $action |
|
426 | - * |
|
427 | - * @return \Syscodes\Components\Routing\Route |
|
428 | - */ |
|
429 | - public function newRoute($method, $uri, $action): route |
|
430 | - { |
|
431 | - return take(new Route($method, $uri, $action)) |
|
432 | - ->setContainer($this->container); |
|
433 | - } |
|
417 | + return $group['controller'].'@'.$class; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Create a new Route object. |
|
422 | + * |
|
423 | + * @param array|string $method |
|
424 | + * @param string $uri |
|
425 | + * @param mixed $action |
|
426 | + * |
|
427 | + * @return \Syscodes\Components\Routing\Route |
|
428 | + */ |
|
429 | + public function newRoute($method, $uri, $action): route |
|
430 | + { |
|
431 | + return take(new Route($method, $uri, $action)) |
|
432 | + ->setContainer($this->container); |
|
433 | + } |
|
434 | 434 | |
435 | - /** |
|
436 | - * Determine if the router currently has a group stack. |
|
437 | - * |
|
438 | - * @return bool |
|
439 | - */ |
|
440 | - public function hasGroupStack(): bool |
|
441 | - { |
|
442 | - return ! empty($this->groupStack); |
|
443 | - } |
|
435 | + /** |
|
436 | + * Determine if the router currently has a group stack. |
|
437 | + * |
|
438 | + * @return bool |
|
439 | + */ |
|
440 | + public function hasGroupStack(): bool |
|
441 | + { |
|
442 | + return ! empty($this->groupStack); |
|
443 | + } |
|
444 | 444 | |
445 | - /** |
|
446 | - * Merge the group stack with the controller action. |
|
447 | - * |
|
448 | - * @param \Syscpde\Routing\Route $route |
|
449 | - * |
|
450 | - * @return void |
|
451 | - */ |
|
452 | - protected function mergeGroupAttributesIntoRoute($route): void |
|
453 | - { |
|
454 | - $action = $this->mergeLastGroup( |
|
455 | - $route->getAction(), |
|
456 | - $existsPrefix = false |
|
457 | - ); |
|
445 | + /** |
|
446 | + * Merge the group stack with the controller action. |
|
447 | + * |
|
448 | + * @param \Syscpde\Routing\Route $route |
|
449 | + * |
|
450 | + * @return void |
|
451 | + */ |
|
452 | + protected function mergeGroupAttributesIntoRoute($route): void |
|
453 | + { |
|
454 | + $action = $this->mergeLastGroup( |
|
455 | + $route->getAction(), |
|
456 | + $existsPrefix = false |
|
457 | + ); |
|
458 | 458 | |
459 | - $route->setAction($action); |
|
460 | - } |
|
459 | + $route->setAction($action); |
|
460 | + } |
|
461 | 461 | |
462 | - /** |
|
463 | - * Add the necessary where clauses to the route based on its initial registration. |
|
464 | - * |
|
465 | - * @param \Syscodes\Components\Routing\Route $route |
|
466 | - * |
|
467 | - * @return \Syscodes\Components\Routing\Route |
|
468 | - */ |
|
469 | - protected function addWhereClausesToRoute($route): Route |
|
470 | - { |
|
471 | - $route->where(array_merge( |
|
472 | - $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
473 | - )); |
|
474 | - |
|
475 | - return $route; |
|
476 | - } |
|
477 | - |
|
478 | - /** |
|
479 | - * Add a prefix to the route URI. |
|
480 | - * |
|
481 | - * @param string $uri |
|
482 | - * |
|
483 | - * @return string |
|
484 | - */ |
|
485 | - protected function prefix($uri): string |
|
486 | - { |
|
487 | - return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
488 | - } |
|
489 | - |
|
490 | - /** |
|
491 | - * Set a global where pattern on all routes. |
|
492 | - * |
|
493 | - * @param string $name |
|
494 | - * @param string $pattern |
|
495 | - * |
|
496 | - * @return void |
|
497 | - */ |
|
498 | - public function pattern($name, $pattern): void |
|
499 | - { |
|
500 | - $this->patterns[$name] = $pattern; |
|
501 | - } |
|
502 | - |
|
503 | - /** |
|
504 | - * Set a group of global where patterns on all routes. |
|
505 | - * |
|
506 | - * @param array $patterns |
|
507 | - * |
|
508 | - * @return void |
|
509 | - */ |
|
510 | - public function patterns($patterns): void |
|
511 | - { |
|
512 | - foreach ($patterns as $key => $pattern) { |
|
513 | - $this->patterns[$key] = $pattern; |
|
514 | - } |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Get a Resource instance. |
|
519 | - * |
|
520 | - * @return \Syscodes\Components\Routing\ResourceRegister |
|
521 | - */ |
|
522 | - public function getResource() |
|
523 | - { |
|
524 | - if (isset($this->resources)) { |
|
525 | - return $this->resources; |
|
526 | - } |
|
527 | - |
|
528 | - return $this->resources = new ResourceRegister($this); |
|
529 | - } |
|
530 | - |
|
531 | - /** |
|
532 | - * Dispatches the given url and call the method that belongs to the route. |
|
533 | - * |
|
534 | - * @param \Syscodes\Components\Http\Request $request |
|
535 | - * |
|
536 | - * @return mixed |
|
537 | - */ |
|
538 | - public function dispatch(Request $request) |
|
539 | - { |
|
540 | - return $this->resolves->resolve($request); |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Gather the middleware for the given route. |
|
545 | - * |
|
546 | - * @param \Syscodes\Components\Routing\Route $route |
|
547 | - * |
|
548 | - * @return array |
|
549 | - */ |
|
550 | - public function gatherRouteMiddleware(Route $route): array |
|
551 | - { |
|
552 | - $middleware = array_map( |
|
553 | - fn ($name) => MiddlewareResolver::resolve($name, $this->middleware, $this->middlewareGroups), |
|
554 | - $route->gatherMiddleware() |
|
555 | - ); |
|
462 | + /** |
|
463 | + * Add the necessary where clauses to the route based on its initial registration. |
|
464 | + * |
|
465 | + * @param \Syscodes\Components\Routing\Route $route |
|
466 | + * |
|
467 | + * @return \Syscodes\Components\Routing\Route |
|
468 | + */ |
|
469 | + protected function addWhereClausesToRoute($route): Route |
|
470 | + { |
|
471 | + $route->where(array_merge( |
|
472 | + $this->patterns, Arr::get($route->getAction(), 'where', []) |
|
473 | + )); |
|
474 | + |
|
475 | + return $route; |
|
476 | + } |
|
477 | + |
|
478 | + /** |
|
479 | + * Add a prefix to the route URI. |
|
480 | + * |
|
481 | + * @param string $uri |
|
482 | + * |
|
483 | + * @return string |
|
484 | + */ |
|
485 | + protected function prefix($uri): string |
|
486 | + { |
|
487 | + return trim(trim($this->getGroupPrefix(), '/').'/'.trim($uri, '/'), '/') ?: '/'; |
|
488 | + } |
|
489 | + |
|
490 | + /** |
|
491 | + * Set a global where pattern on all routes. |
|
492 | + * |
|
493 | + * @param string $name |
|
494 | + * @param string $pattern |
|
495 | + * |
|
496 | + * @return void |
|
497 | + */ |
|
498 | + public function pattern($name, $pattern): void |
|
499 | + { |
|
500 | + $this->patterns[$name] = $pattern; |
|
501 | + } |
|
502 | + |
|
503 | + /** |
|
504 | + * Set a group of global where patterns on all routes. |
|
505 | + * |
|
506 | + * @param array $patterns |
|
507 | + * |
|
508 | + * @return void |
|
509 | + */ |
|
510 | + public function patterns($patterns): void |
|
511 | + { |
|
512 | + foreach ($patterns as $key => $pattern) { |
|
513 | + $this->patterns[$key] = $pattern; |
|
514 | + } |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Get a Resource instance. |
|
519 | + * |
|
520 | + * @return \Syscodes\Components\Routing\ResourceRegister |
|
521 | + */ |
|
522 | + public function getResource() |
|
523 | + { |
|
524 | + if (isset($this->resources)) { |
|
525 | + return $this->resources; |
|
526 | + } |
|
527 | + |
|
528 | + return $this->resources = new ResourceRegister($this); |
|
529 | + } |
|
530 | + |
|
531 | + /** |
|
532 | + * Dispatches the given url and call the method that belongs to the route. |
|
533 | + * |
|
534 | + * @param \Syscodes\Components\Http\Request $request |
|
535 | + * |
|
536 | + * @return mixed |
|
537 | + */ |
|
538 | + public function dispatch(Request $request) |
|
539 | + { |
|
540 | + return $this->resolves->resolve($request); |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Gather the middleware for the given route. |
|
545 | + * |
|
546 | + * @param \Syscodes\Components\Routing\Route $route |
|
547 | + * |
|
548 | + * @return array |
|
549 | + */ |
|
550 | + public function gatherRouteMiddleware(Route $route): array |
|
551 | + { |
|
552 | + $middleware = array_map( |
|
553 | + fn ($name) => MiddlewareResolver::resolve($name, $this->middleware, $this->middlewareGroups), |
|
554 | + $route->gatherMiddleware() |
|
555 | + ); |
|
556 | 556 | |
557 | - return Arr::flatten($middleware); |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Get all of the defined middleware |
|
562 | - * |
|
563 | - * @return array |
|
564 | - */ |
|
565 | - public function getMiddleware(): array |
|
566 | - { |
|
567 | - return $this->middleware; |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Register a short-hand name for a middleware. |
|
572 | - * |
|
573 | - * @param string $name |
|
574 | - * @param string $class |
|
575 | - * |
|
576 | - * @return static |
|
577 | - */ |
|
578 | - public function aliasMiddleware($name, $class): static |
|
579 | - { |
|
580 | - $this->middleware[$name] = $class; |
|
581 | - |
|
582 | - return $this; |
|
583 | - } |
|
584 | - |
|
585 | - /** |
|
586 | - * Register a group of middleware. |
|
587 | - * |
|
588 | - * @param string $name |
|
589 | - * @param array $middleware |
|
590 | - * |
|
591 | - * @return static |
|
592 | - */ |
|
593 | - public function middlewareGroup($name, array $middleware): static |
|
594 | - { |
|
595 | - $this->middlewareGroups[$name] = $middleware; |
|
596 | - |
|
597 | - return $this; |
|
598 | - } |
|
599 | - |
|
600 | - /** |
|
601 | - * Check if a route with the given name exists. |
|
602 | - * |
|
603 | - * @param string $name |
|
604 | - * |
|
605 | - * @return bool |
|
606 | - */ |
|
607 | - public function has($name): bool |
|
608 | - { |
|
609 | - $names = is_array($name) ? $name : func_get_args(); |
|
610 | - |
|
611 | - foreach ($names as $value) { |
|
612 | - if ( ! $this->routes->hasNamedRoute($value)) { |
|
613 | - return false; |
|
614 | - } |
|
615 | - } |
|
616 | - |
|
617 | - return true; |
|
618 | - } |
|
619 | - |
|
620 | - /** |
|
621 | - * Determine if the current route matches a pattern. |
|
622 | - * |
|
623 | - * @param mixed ...$patterns |
|
624 | - * |
|
625 | - * @return bool |
|
626 | - */ |
|
627 | - public function is(...$patterns): bool |
|
628 | - { |
|
629 | - return $this->currentRouteNamed(...$patterns); |
|
630 | - } |
|
631 | - |
|
632 | - /** |
|
633 | - * Determine if the current route matches a pattern. |
|
634 | - * |
|
635 | - * @param mixed ...$patterns |
|
636 | - * |
|
637 | - * @return bool |
|
638 | - */ |
|
639 | - public function currentRouteNamed(...$patterns): bool |
|
640 | - { |
|
641 | - return $this->resolves->current() && $this->resolves->current()->named(...$patterns); |
|
642 | - } |
|
643 | - |
|
644 | - /** |
|
645 | - * Register an array of resource controllers. |
|
646 | - * |
|
647 | - * @param array $resources |
|
648 | - * @param array $options |
|
649 | - * |
|
650 | - * @return void |
|
651 | - */ |
|
652 | - public function resources(array $resources, array $options = []): void |
|
653 | - { |
|
654 | - foreach ($resources as $name => $controller) { |
|
655 | - $this->resource($name, $controller, $options); |
|
656 | - } |
|
657 | - } |
|
658 | - |
|
659 | - /** |
|
660 | - * Route a resource to a controller. |
|
661 | - * |
|
662 | - * @param string $name |
|
663 | - * @param string $controller |
|
664 | - * @param array $options |
|
665 | - * |
|
666 | - * @return \Syscodes\Components\Routing\Resources\AwaitingResourceRegistration |
|
667 | - */ |
|
668 | - public function resource($name, $controller, array $options = []) |
|
669 | - { |
|
670 | - if ($this->container) { |
|
671 | - $register = $this->container->make(ResourceRegister::class); |
|
672 | - } else { |
|
673 | - $register = new ResourceRegister($this); |
|
674 | - } |
|
675 | - |
|
676 | - return new AwaitingResourceRegistration( |
|
677 | - $register, $name, $controller, $options |
|
678 | - ); |
|
679 | - } |
|
680 | - |
|
681 | - /** |
|
682 | - * Get the route collection. |
|
683 | - * |
|
684 | - * @return array |
|
685 | - */ |
|
686 | - public function getRoutes() |
|
687 | - { |
|
688 | - return $this->routes; |
|
689 | - } |
|
690 | - |
|
691 | - /** |
|
692 | - * Get or set the verbs used in the resource URIs. |
|
693 | - * |
|
694 | - * @param array $verbs |
|
695 | - * |
|
696 | - * @return array|null |
|
697 | - */ |
|
698 | - public function resourceVerbs(array $verbs = []) |
|
699 | - { |
|
700 | - ResourceRegister::verbs($verbs); |
|
701 | - } |
|
557 | + return Arr::flatten($middleware); |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Get all of the defined middleware |
|
562 | + * |
|
563 | + * @return array |
|
564 | + */ |
|
565 | + public function getMiddleware(): array |
|
566 | + { |
|
567 | + return $this->middleware; |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Register a short-hand name for a middleware. |
|
572 | + * |
|
573 | + * @param string $name |
|
574 | + * @param string $class |
|
575 | + * |
|
576 | + * @return static |
|
577 | + */ |
|
578 | + public function aliasMiddleware($name, $class): static |
|
579 | + { |
|
580 | + $this->middleware[$name] = $class; |
|
581 | + |
|
582 | + return $this; |
|
583 | + } |
|
584 | + |
|
585 | + /** |
|
586 | + * Register a group of middleware. |
|
587 | + * |
|
588 | + * @param string $name |
|
589 | + * @param array $middleware |
|
590 | + * |
|
591 | + * @return static |
|
592 | + */ |
|
593 | + public function middlewareGroup($name, array $middleware): static |
|
594 | + { |
|
595 | + $this->middlewareGroups[$name] = $middleware; |
|
596 | + |
|
597 | + return $this; |
|
598 | + } |
|
599 | + |
|
600 | + /** |
|
601 | + * Check if a route with the given name exists. |
|
602 | + * |
|
603 | + * @param string $name |
|
604 | + * |
|
605 | + * @return bool |
|
606 | + */ |
|
607 | + public function has($name): bool |
|
608 | + { |
|
609 | + $names = is_array($name) ? $name : func_get_args(); |
|
610 | + |
|
611 | + foreach ($names as $value) { |
|
612 | + if ( ! $this->routes->hasNamedRoute($value)) { |
|
613 | + return false; |
|
614 | + } |
|
615 | + } |
|
616 | + |
|
617 | + return true; |
|
618 | + } |
|
619 | + |
|
620 | + /** |
|
621 | + * Determine if the current route matches a pattern. |
|
622 | + * |
|
623 | + * @param mixed ...$patterns |
|
624 | + * |
|
625 | + * @return bool |
|
626 | + */ |
|
627 | + public function is(...$patterns): bool |
|
628 | + { |
|
629 | + return $this->currentRouteNamed(...$patterns); |
|
630 | + } |
|
631 | + |
|
632 | + /** |
|
633 | + * Determine if the current route matches a pattern. |
|
634 | + * |
|
635 | + * @param mixed ...$patterns |
|
636 | + * |
|
637 | + * @return bool |
|
638 | + */ |
|
639 | + public function currentRouteNamed(...$patterns): bool |
|
640 | + { |
|
641 | + return $this->resolves->current() && $this->resolves->current()->named(...$patterns); |
|
642 | + } |
|
643 | + |
|
644 | + /** |
|
645 | + * Register an array of resource controllers. |
|
646 | + * |
|
647 | + * @param array $resources |
|
648 | + * @param array $options |
|
649 | + * |
|
650 | + * @return void |
|
651 | + */ |
|
652 | + public function resources(array $resources, array $options = []): void |
|
653 | + { |
|
654 | + foreach ($resources as $name => $controller) { |
|
655 | + $this->resource($name, $controller, $options); |
|
656 | + } |
|
657 | + } |
|
658 | + |
|
659 | + /** |
|
660 | + * Route a resource to a controller. |
|
661 | + * |
|
662 | + * @param string $name |
|
663 | + * @param string $controller |
|
664 | + * @param array $options |
|
665 | + * |
|
666 | + * @return \Syscodes\Components\Routing\Resources\AwaitingResourceRegistration |
|
667 | + */ |
|
668 | + public function resource($name, $controller, array $options = []) |
|
669 | + { |
|
670 | + if ($this->container) { |
|
671 | + $register = $this->container->make(ResourceRegister::class); |
|
672 | + } else { |
|
673 | + $register = new ResourceRegister($this); |
|
674 | + } |
|
675 | + |
|
676 | + return new AwaitingResourceRegistration( |
|
677 | + $register, $name, $controller, $options |
|
678 | + ); |
|
679 | + } |
|
680 | + |
|
681 | + /** |
|
682 | + * Get the route collection. |
|
683 | + * |
|
684 | + * @return array |
|
685 | + */ |
|
686 | + public function getRoutes() |
|
687 | + { |
|
688 | + return $this->routes; |
|
689 | + } |
|
690 | + |
|
691 | + /** |
|
692 | + * Get or set the verbs used in the resource URIs. |
|
693 | + * |
|
694 | + * @param array $verbs |
|
695 | + * |
|
696 | + * @return array|null |
|
697 | + */ |
|
698 | + public function resourceVerbs(array $verbs = []) |
|
699 | + { |
|
700 | + ResourceRegister::verbs($verbs); |
|
701 | + } |
|
702 | 702 | |
703 | - /** |
|
704 | - * Remove any duplicate middleware from the given array. |
|
705 | - * |
|
706 | - * @param array $middleware |
|
707 | - * |
|
708 | - * @return array |
|
709 | - */ |
|
710 | - public static function uniqueMiddleware(array $middleware): array |
|
711 | - { |
|
712 | - return array_unique($middleware, SORT_REGULAR); |
|
713 | - } |
|
714 | - |
|
715 | - /** |
|
716 | - * Magic method. |
|
717 | - * |
|
718 | - * Dynamically handle calls into the router instance. |
|
719 | - * |
|
720 | - * @param string $method |
|
721 | - * @param array $parameters |
|
722 | - * |
|
723 | - * @return mixed |
|
724 | - */ |
|
725 | - public function __call($method, $parameters) |
|
726 | - { |
|
727 | - if (static::hasMacro($method)) { |
|
728 | - return $this->macroCall($method, $parameters); |
|
729 | - } |
|
703 | + /** |
|
704 | + * Remove any duplicate middleware from the given array. |
|
705 | + * |
|
706 | + * @param array $middleware |
|
707 | + * |
|
708 | + * @return array |
|
709 | + */ |
|
710 | + public static function uniqueMiddleware(array $middleware): array |
|
711 | + { |
|
712 | + return array_unique($middleware, SORT_REGULAR); |
|
713 | + } |
|
714 | + |
|
715 | + /** |
|
716 | + * Magic method. |
|
717 | + * |
|
718 | + * Dynamically handle calls into the router instance. |
|
719 | + * |
|
720 | + * @param string $method |
|
721 | + * @param array $parameters |
|
722 | + * |
|
723 | + * @return mixed |
|
724 | + */ |
|
725 | + public function __call($method, $parameters) |
|
726 | + { |
|
727 | + if (static::hasMacro($method)) { |
|
728 | + return $this->macroCall($method, $parameters); |
|
729 | + } |
|
730 | 730 | |
731 | - return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
732 | - } |
|
731 | + return (new RouteRegister($this))->attribute($method, $parameters[0]); |
|
732 | + } |
|
733 | 733 | } |
734 | 734 | \ No newline at end of file |
@@ -43,857 +43,857 @@ |
||
43 | 43 | */ |
44 | 44 | class Route |
45 | 45 | { |
46 | - use Concerns\RouteCondition, |
|
47 | - Concerns\RouteDependencyResolver; |
|
48 | - |
|
49 | - /** |
|
50 | - * The validators used by the routes. |
|
51 | - * |
|
52 | - * @var array $validators |
|
53 | - */ |
|
54 | - public static $validators; |
|
46 | + use Concerns\RouteCondition, |
|
47 | + Concerns\RouteDependencyResolver; |
|
48 | + |
|
49 | + /** |
|
50 | + * The validators used by the routes. |
|
51 | + * |
|
52 | + * @var array $validators |
|
53 | + */ |
|
54 | + public static $validators; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Action that the route will use when called. |
|
58 | - * |
|
59 | - * @var \Closure|string|array $action |
|
60 | - */ |
|
61 | - public $action; |
|
62 | - |
|
63 | - /** |
|
64 | - * The compiled version of the route. |
|
65 | - * |
|
66 | - * @var \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler|string $compiled |
|
67 | - */ |
|
68 | - public $compiled; |
|
69 | - |
|
70 | - /** |
|
71 | - * The computed gathered middleware. |
|
72 | - * |
|
73 | - * @var array|null $computedMiddleware |
|
74 | - */ |
|
75 | - public $computedMiddleware; |
|
76 | - |
|
77 | - /** |
|
78 | - * The container instance used by the route. |
|
79 | - * |
|
80 | - * @var \Syscodes\Components\Container\Container $container |
|
81 | - */ |
|
82 | - protected $container; |
|
83 | - |
|
84 | - /** |
|
85 | - * The controller instance. |
|
86 | - * |
|
87 | - * @var string $controller |
|
88 | - */ |
|
89 | - public $controller; |
|
90 | - |
|
91 | - /** |
|
92 | - * The default values for the route. |
|
93 | - * |
|
94 | - * @var array $defaults |
|
95 | - */ |
|
96 | - public $defaults = []; |
|
97 | - |
|
98 | - /** |
|
99 | - * Indicates whether the route is a fallback route. |
|
100 | - * |
|
101 | - * @var bool $fallback |
|
102 | - */ |
|
103 | - protected $fallback = false; |
|
104 | - |
|
105 | - /** |
|
106 | - * Variable of HTTP method. |
|
107 | - * |
|
108 | - * @var array|string $method |
|
109 | - */ |
|
110 | - public $method; |
|
111 | - |
|
112 | - /** |
|
113 | - * The array of matched parameters. |
|
114 | - * |
|
115 | - * @var array $parameters |
|
116 | - */ |
|
117 | - public $parameters = []; |
|
118 | - |
|
119 | - /** |
|
120 | - * The parameter names for the route. |
|
121 | - * |
|
122 | - * @var string|null $parameterNames |
|
123 | - */ |
|
124 | - public $parameterNames; |
|
125 | - |
|
126 | - /** |
|
127 | - * The URI pattern the route responds to. |
|
128 | - * |
|
129 | - * @var string $uri |
|
130 | - */ |
|
131 | - public $uri; |
|
132 | - |
|
133 | - /** |
|
134 | - * Contains the arguments of the current route. |
|
135 | - * |
|
136 | - * @var array $where |
|
137 | - */ |
|
138 | - public $wheres = []; |
|
139 | - |
|
140 | - /** |
|
141 | - * Constructor. Initialize route. |
|
142 | - * |
|
143 | - * @param array|string $method |
|
144 | - * @param string $uri |
|
145 | - * @param \Closure|array $action |
|
146 | - * |
|
147 | - * @return void |
|
148 | - */ |
|
149 | - public function __construct($method, $uri, $action) |
|
150 | - { |
|
151 | - $this->uri = $uri; |
|
152 | - |
|
153 | - // Set the method |
|
154 | - $this->method = $this->parseMethod($method); |
|
155 | - |
|
156 | - // Set the action |
|
157 | - $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
56 | + /** |
|
57 | + * Action that the route will use when called. |
|
58 | + * |
|
59 | + * @var \Closure|string|array $action |
|
60 | + */ |
|
61 | + public $action; |
|
62 | + |
|
63 | + /** |
|
64 | + * The compiled version of the route. |
|
65 | + * |
|
66 | + * @var \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler|string $compiled |
|
67 | + */ |
|
68 | + public $compiled; |
|
69 | + |
|
70 | + /** |
|
71 | + * The computed gathered middleware. |
|
72 | + * |
|
73 | + * @var array|null $computedMiddleware |
|
74 | + */ |
|
75 | + public $computedMiddleware; |
|
76 | + |
|
77 | + /** |
|
78 | + * The container instance used by the route. |
|
79 | + * |
|
80 | + * @var \Syscodes\Components\Container\Container $container |
|
81 | + */ |
|
82 | + protected $container; |
|
83 | + |
|
84 | + /** |
|
85 | + * The controller instance. |
|
86 | + * |
|
87 | + * @var string $controller |
|
88 | + */ |
|
89 | + public $controller; |
|
90 | + |
|
91 | + /** |
|
92 | + * The default values for the route. |
|
93 | + * |
|
94 | + * @var array $defaults |
|
95 | + */ |
|
96 | + public $defaults = []; |
|
97 | + |
|
98 | + /** |
|
99 | + * Indicates whether the route is a fallback route. |
|
100 | + * |
|
101 | + * @var bool $fallback |
|
102 | + */ |
|
103 | + protected $fallback = false; |
|
104 | + |
|
105 | + /** |
|
106 | + * Variable of HTTP method. |
|
107 | + * |
|
108 | + * @var array|string $method |
|
109 | + */ |
|
110 | + public $method; |
|
111 | + |
|
112 | + /** |
|
113 | + * The array of matched parameters. |
|
114 | + * |
|
115 | + * @var array $parameters |
|
116 | + */ |
|
117 | + public $parameters = []; |
|
118 | + |
|
119 | + /** |
|
120 | + * The parameter names for the route. |
|
121 | + * |
|
122 | + * @var string|null $parameterNames |
|
123 | + */ |
|
124 | + public $parameterNames; |
|
125 | + |
|
126 | + /** |
|
127 | + * The URI pattern the route responds to. |
|
128 | + * |
|
129 | + * @var string $uri |
|
130 | + */ |
|
131 | + public $uri; |
|
132 | + |
|
133 | + /** |
|
134 | + * Contains the arguments of the current route. |
|
135 | + * |
|
136 | + * @var array $where |
|
137 | + */ |
|
138 | + public $wheres = []; |
|
139 | + |
|
140 | + /** |
|
141 | + * Constructor. Initialize route. |
|
142 | + * |
|
143 | + * @param array|string $method |
|
144 | + * @param string $uri |
|
145 | + * @param \Closure|array $action |
|
146 | + * |
|
147 | + * @return void |
|
148 | + */ |
|
149 | + public function __construct($method, $uri, $action) |
|
150 | + { |
|
151 | + $this->uri = $uri; |
|
152 | + |
|
153 | + // Set the method |
|
154 | + $this->method = $this->parseMethod($method); |
|
155 | + |
|
156 | + // Set the action |
|
157 | + $this->action = Arr::except($this->parseAction($action), ['prefix']); |
|
158 | 158 | |
159 | - if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
160 | - $this->method[] = 'HEAD'; |
|
161 | - } |
|
162 | - |
|
163 | - $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Get the controller instance for the route. |
|
168 | - * |
|
169 | - * @return mixed |
|
170 | - */ |
|
171 | - public function getController() |
|
172 | - { |
|
173 | - if ( ! $this->controller) { |
|
174 | - $class = $this->parseControllerCallback()[0]; |
|
159 | + if (in_array('GET', $this->method) && ! in_array('HEAD', $this->method)) { |
|
160 | + $this->method[] = 'HEAD'; |
|
161 | + } |
|
162 | + |
|
163 | + $this->prefix(is_array($action) ? Arr::get($action, 'prefix') : ''); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Get the controller instance for the route. |
|
168 | + * |
|
169 | + * @return mixed |
|
170 | + */ |
|
171 | + public function getController() |
|
172 | + { |
|
173 | + if ( ! $this->controller) { |
|
174 | + $class = $this->parseControllerCallback()[0]; |
|
175 | 175 | |
176 | - $this->controller = $this->container->make(ltrim($class, '\\')); |
|
177 | - } |
|
178 | - |
|
179 | - return $this->controller; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Get the controller method used for the route. |
|
184 | - * |
|
185 | - * @return string |
|
186 | - */ |
|
187 | - public function getControllerMethod(): string |
|
188 | - { |
|
189 | - return $this->parseControllerCallback()[1]; |
|
190 | - } |
|
191 | - |
|
192 | - /** |
|
193 | - * Get or set the domain for the route. |
|
194 | - * |
|
195 | - * @param string|null $domain |
|
196 | - * |
|
197 | - * @return mixed |
|
198 | - */ |
|
199 | - public function domain($domain = null) |
|
200 | - { |
|
201 | - if (is_null($domain)) { |
|
202 | - return $this->getDomain(); |
|
203 | - } |
|
176 | + $this->controller = $this->container->make(ltrim($class, '\\')); |
|
177 | + } |
|
178 | + |
|
179 | + return $this->controller; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Get the controller method used for the route. |
|
184 | + * |
|
185 | + * @return string |
|
186 | + */ |
|
187 | + public function getControllerMethod(): string |
|
188 | + { |
|
189 | + return $this->parseControllerCallback()[1]; |
|
190 | + } |
|
191 | + |
|
192 | + /** |
|
193 | + * Get or set the domain for the route. |
|
194 | + * |
|
195 | + * @param string|null $domain |
|
196 | + * |
|
197 | + * @return mixed |
|
198 | + */ |
|
199 | + public function domain($domain = null) |
|
200 | + { |
|
201 | + if (is_null($domain)) { |
|
202 | + return $this->getDomain(); |
|
203 | + } |
|
204 | 204 | |
205 | - $parsed = RouteUri::parse($domain); |
|
206 | - |
|
207 | - $this->action['domain'] = $parsed->uri; |
|
208 | - |
|
209 | - return $this; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Get the domain defined for the route. |
|
214 | - * |
|
215 | - * @return string|null |
|
216 | - */ |
|
217 | - public function getDomain() |
|
218 | - { |
|
219 | - return isset($this->action['domain']) |
|
220 | - ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
221 | - : null; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Parse the controller. |
|
226 | - * |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - public function parseControllerCallback(): array |
|
230 | - { |
|
231 | - return Str::parseCallback($this->action['uses']); |
|
232 | - } |
|
205 | + $parsed = RouteUri::parse($domain); |
|
206 | + |
|
207 | + $this->action['domain'] = $parsed->uri; |
|
208 | + |
|
209 | + return $this; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Get the domain defined for the route. |
|
214 | + * |
|
215 | + * @return string|null |
|
216 | + */ |
|
217 | + public function getDomain() |
|
218 | + { |
|
219 | + return isset($this->action['domain']) |
|
220 | + ? str_replace(['http://', 'https://'], '', $this->action['domain']) |
|
221 | + : null; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Parse the controller. |
|
226 | + * |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + public function parseControllerCallback(): array |
|
230 | + { |
|
231 | + return Str::parseCallback($this->action['uses']); |
|
232 | + } |
|
233 | 233 | |
234 | - /** |
|
235 | - * Checks whether the route's action is a controller. |
|
236 | - * |
|
237 | - * @return bool |
|
238 | - */ |
|
239 | - public function isControllerAction(): bool |
|
240 | - { |
|
241 | - return is_string($this->action['uses']); |
|
242 | - } |
|
243 | - |
|
244 | - /** |
|
245 | - * Get the dispatcher for the route's controller. |
|
246 | - * |
|
247 | - * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
248 | - */ |
|
249 | - private function controllerDispatcher(): ControllerDispatcher |
|
250 | - { |
|
251 | - return new ControllerDispatcher($this->container); |
|
252 | - } |
|
234 | + /** |
|
235 | + * Checks whether the route's action is a controller. |
|
236 | + * |
|
237 | + * @return bool |
|
238 | + */ |
|
239 | + public function isControllerAction(): bool |
|
240 | + { |
|
241 | + return is_string($this->action['uses']); |
|
242 | + } |
|
243 | + |
|
244 | + /** |
|
245 | + * Get the dispatcher for the route's controller. |
|
246 | + * |
|
247 | + * @return \Syscodes\Components\Routing\ControllerDispatcher |
|
248 | + */ |
|
249 | + private function controllerDispatcher(): ControllerDispatcher |
|
250 | + { |
|
251 | + return new ControllerDispatcher($this->container); |
|
252 | + } |
|
253 | 253 | |
254 | - /** |
|
255 | - * Run the route action and return the response. |
|
256 | - * |
|
257 | - * @return mixed |
|
258 | - */ |
|
259 | - public function runResolver() |
|
260 | - { |
|
261 | - $this->container = $this->container ?: new Container; |
|
262 | - |
|
263 | - try { |
|
264 | - if ($this->isControllerAction()) { |
|
265 | - return $this->runResolverController(); |
|
266 | - } |
|
267 | - |
|
268 | - return $this->runResolverCallable(); |
|
269 | - } catch (HttpResponseException $e) { |
|
270 | - return $e->getResponse(); |
|
271 | - } |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Run the route action and return the response. |
|
276 | - * |
|
277 | - * @return mixed |
|
278 | - */ |
|
279 | - protected function runResolverCallable() |
|
280 | - { |
|
281 | - $callable = $this->action['uses']; |
|
282 | - |
|
283 | - return $callable(...array_values($this->resolveMethodDependencies( |
|
284 | - $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
285 | - ))); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Run the route action and return the response. |
|
290 | - * |
|
291 | - * @return mixed |
|
292 | - */ |
|
293 | - protected function runResolverController() |
|
294 | - { |
|
295 | - return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Determine if the route matches a given request. |
|
300 | - * |
|
301 | - * @param \Syscodes\Components\Http\Request $request |
|
302 | - * @param bool $method |
|
303 | - * |
|
304 | - * @return bool |
|
305 | - */ |
|
306 | - public function matches(Request $request, bool $method = true): bool |
|
307 | - { |
|
308 | - $this->compileRoute(); |
|
309 | - |
|
310 | - foreach (self::getValidators() as $validator) { |
|
311 | - if ($method && $validator instanceof MethodValidator) { |
|
312 | - continue; |
|
313 | - } |
|
314 | - |
|
315 | - if ( ! $validator->matches($this, $request)) { |
|
316 | - return false; |
|
317 | - } |
|
318 | - } |
|
319 | - |
|
320 | - return true; |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Get the route validators for the instance. |
|
325 | - * |
|
326 | - * @return array |
|
327 | - */ |
|
328 | - public function getValidators(): array |
|
329 | - { |
|
330 | - if (isset(static::$validators)) { |
|
331 | - return static::$validators; |
|
332 | - } |
|
333 | - |
|
334 | - return static::$validators = [ |
|
335 | - new HostValidator, new MethodValidator, |
|
336 | - new SchemeValidator, new UriValidator |
|
337 | - ]; |
|
338 | - } |
|
339 | - |
|
340 | - /** |
|
341 | - * Parse the route action. |
|
342 | - * |
|
343 | - * @param \callable|array|null $action |
|
344 | - * |
|
345 | - * @return array |
|
346 | - * |
|
347 | - * @throws \InvalidArgumentException |
|
348 | - */ |
|
349 | - protected function parseAction($action): array |
|
350 | - { |
|
351 | - if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
352 | - throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
353 | - } |
|
354 | - |
|
355 | - return RouteAction::parse($this->uri, $action); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * Set the method of the current route. |
|
360 | - * |
|
361 | - * @param array $method |
|
362 | - * |
|
363 | - * @return array |
|
364 | - * |
|
365 | - * @throws \InvalidArgumentException |
|
366 | - */ |
|
367 | - public function parseMethod($method): array |
|
368 | - { |
|
369 | - if ($method === null || empty($method)) { |
|
370 | - throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
371 | - } |
|
372 | - |
|
373 | - foreach ((array) $method as $httpMethod) { |
|
374 | - if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
375 | - throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
376 | - } |
|
377 | - } |
|
378 | - |
|
379 | - return array_map('strtoupper', (array) $method); |
|
380 | - } |
|
381 | - |
|
382 | - /** |
|
383 | - * Set the URI that the route responds to. |
|
384 | - * |
|
385 | - * @param string|null $uri |
|
386 | - * |
|
387 | - * @return static |
|
388 | - * |
|
389 | - * @throws \InvalidArgumentException |
|
390 | - */ |
|
391 | - public function setUri($uri): static |
|
392 | - { |
|
393 | - if ($uri === null) { |
|
394 | - throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
395 | - } |
|
396 | - |
|
397 | - $this->uri = $this->parseUri($uri); |
|
398 | - |
|
399 | - return $this; |
|
400 | - } |
|
254 | + /** |
|
255 | + * Run the route action and return the response. |
|
256 | + * |
|
257 | + * @return mixed |
|
258 | + */ |
|
259 | + public function runResolver() |
|
260 | + { |
|
261 | + $this->container = $this->container ?: new Container; |
|
262 | + |
|
263 | + try { |
|
264 | + if ($this->isControllerAction()) { |
|
265 | + return $this->runResolverController(); |
|
266 | + } |
|
267 | + |
|
268 | + return $this->runResolverCallable(); |
|
269 | + } catch (HttpResponseException $e) { |
|
270 | + return $e->getResponse(); |
|
271 | + } |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Run the route action and return the response. |
|
276 | + * |
|
277 | + * @return mixed |
|
278 | + */ |
|
279 | + protected function runResolverCallable() |
|
280 | + { |
|
281 | + $callable = $this->action['uses']; |
|
282 | + |
|
283 | + return $callable(...array_values($this->resolveMethodDependencies( |
|
284 | + $this->parametersWithouNulls(), new ReflectionFunction($callable) |
|
285 | + ))); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Run the route action and return the response. |
|
290 | + * |
|
291 | + * @return mixed |
|
292 | + */ |
|
293 | + protected function runResolverController() |
|
294 | + { |
|
295 | + return $this->controllerDispatcher()->dispatch($this, $this->getController(), $this->getControllerMethod()); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Determine if the route matches a given request. |
|
300 | + * |
|
301 | + * @param \Syscodes\Components\Http\Request $request |
|
302 | + * @param bool $method |
|
303 | + * |
|
304 | + * @return bool |
|
305 | + */ |
|
306 | + public function matches(Request $request, bool $method = true): bool |
|
307 | + { |
|
308 | + $this->compileRoute(); |
|
309 | + |
|
310 | + foreach (self::getValidators() as $validator) { |
|
311 | + if ($method && $validator instanceof MethodValidator) { |
|
312 | + continue; |
|
313 | + } |
|
314 | + |
|
315 | + if ( ! $validator->matches($this, $request)) { |
|
316 | + return false; |
|
317 | + } |
|
318 | + } |
|
319 | + |
|
320 | + return true; |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Get the route validators for the instance. |
|
325 | + * |
|
326 | + * @return array |
|
327 | + */ |
|
328 | + public function getValidators(): array |
|
329 | + { |
|
330 | + if (isset(static::$validators)) { |
|
331 | + return static::$validators; |
|
332 | + } |
|
333 | + |
|
334 | + return static::$validators = [ |
|
335 | + new HostValidator, new MethodValidator, |
|
336 | + new SchemeValidator, new UriValidator |
|
337 | + ]; |
|
338 | + } |
|
339 | + |
|
340 | + /** |
|
341 | + * Parse the route action. |
|
342 | + * |
|
343 | + * @param \callable|array|null $action |
|
344 | + * |
|
345 | + * @return array |
|
346 | + * |
|
347 | + * @throws \InvalidArgumentException |
|
348 | + */ |
|
349 | + protected function parseAction($action): array |
|
350 | + { |
|
351 | + if ( ! (is_object($action) && ($action instanceof Closure)) && ($action === null || $action === '')) { |
|
352 | + throw new InvalidArgumentException(__('route.actionClosureOrFunction')); |
|
353 | + } |
|
354 | + |
|
355 | + return RouteAction::parse($this->uri, $action); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * Set the method of the current route. |
|
360 | + * |
|
361 | + * @param array $method |
|
362 | + * |
|
363 | + * @return array |
|
364 | + * |
|
365 | + * @throws \InvalidArgumentException |
|
366 | + */ |
|
367 | + public function parseMethod($method): array |
|
368 | + { |
|
369 | + if ($method === null || empty($method)) { |
|
370 | + throw new InvalidArgumentException(__('route.methodNotProvided')); |
|
371 | + } |
|
372 | + |
|
373 | + foreach ((array) $method as $httpMethod) { |
|
374 | + if ( ! in_array($httpMethod, ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'OPTIONS', 'HEAD', 'ANY'])) { |
|
375 | + throw new InvalidArgumentException(__('route.methodNotAllowed')); |
|
376 | + } |
|
377 | + } |
|
378 | + |
|
379 | + return array_map('strtoupper', (array) $method); |
|
380 | + } |
|
381 | + |
|
382 | + /** |
|
383 | + * Set the URI that the route responds to. |
|
384 | + * |
|
385 | + * @param string|null $uri |
|
386 | + * |
|
387 | + * @return static |
|
388 | + * |
|
389 | + * @throws \InvalidArgumentException |
|
390 | + */ |
|
391 | + public function setUri($uri): static |
|
392 | + { |
|
393 | + if ($uri === null) { |
|
394 | + throw new InvalidArgumentException(__('route.uriNotProvided')); |
|
395 | + } |
|
396 | + |
|
397 | + $this->uri = $this->parseUri($uri); |
|
398 | + |
|
399 | + return $this; |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Parse the route URI and normalize. |
|
404 | - * |
|
405 | - * @param string $uri |
|
406 | - * |
|
407 | - * @return string |
|
408 | - */ |
|
409 | - protected function parseUri($uri): string |
|
410 | - { |
|
411 | - return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Add a prefix to the route URI. |
|
416 | - * |
|
417 | - * @param string $prefix |
|
418 | - * |
|
419 | - * @return static |
|
420 | - */ |
|
421 | - public function prefix($prefix): static |
|
422 | - { |
|
423 | - if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
424 | - $this->action['prefix'] = $newPrefix; |
|
425 | - } |
|
402 | + /** |
|
403 | + * Parse the route URI and normalize. |
|
404 | + * |
|
405 | + * @param string $uri |
|
406 | + * |
|
407 | + * @return string |
|
408 | + */ |
|
409 | + protected function parseUri($uri): string |
|
410 | + { |
|
411 | + return take(RouteUri::parse($uri), fn ($uri) => $uri)->uri; |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Add a prefix to the route URI. |
|
416 | + * |
|
417 | + * @param string $prefix |
|
418 | + * |
|
419 | + * @return static |
|
420 | + */ |
|
421 | + public function prefix($prefix): static |
|
422 | + { |
|
423 | + if ( ! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) { |
|
424 | + $this->action['prefix'] = $newPrefix; |
|
425 | + } |
|
426 | 426 | |
427 | - $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
427 | + $uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/'); |
|
428 | 428 | |
429 | - return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Set the action array for the route. |
|
434 | - * |
|
435 | - * @param array $action |
|
436 | - * |
|
437 | - * @return mixed |
|
438 | - */ |
|
439 | - public function setAction(array $action) |
|
440 | - { |
|
441 | - $this->action = $action; |
|
442 | - |
|
443 | - if (isset($this->action['domain'])) { |
|
444 | - $this->domain($this->action['domain']); |
|
445 | - } |
|
429 | + return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Set the action array for the route. |
|
434 | + * |
|
435 | + * @param array $action |
|
436 | + * |
|
437 | + * @return mixed |
|
438 | + */ |
|
439 | + public function setAction(array $action) |
|
440 | + { |
|
441 | + $this->action = $action; |
|
442 | + |
|
443 | + if (isset($this->action['domain'])) { |
|
444 | + $this->domain($this->action['domain']); |
|
445 | + } |
|
446 | 446 | |
447 | - return $this; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Set the name. |
|
452 | - * |
|
453 | - * @param string $name |
|
454 | - * |
|
455 | - * @return static |
|
456 | - */ |
|
457 | - public function name($name): static |
|
458 | - { |
|
459 | - $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
460 | - |
|
461 | - return $this; |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Determine whether the route's name matches the given patterns. |
|
466 | - * |
|
467 | - * @param mixed ...$patterns |
|
468 | - * |
|
469 | - * @return bool |
|
470 | - */ |
|
471 | - public function named(...$patterns): bool |
|
472 | - { |
|
473 | - if (is_null($routeName = $this->getName())) { |
|
474 | - return false; |
|
475 | - } |
|
476 | - |
|
477 | - foreach ($patterns as $pattern) { |
|
478 | - if (Str::is($pattern, $routeName)) { |
|
479 | - return true; |
|
480 | - } |
|
481 | - } |
|
482 | - |
|
483 | - return false; |
|
484 | - } |
|
485 | - |
|
486 | - /** |
|
487 | - * Set a default value for the route. |
|
488 | - * |
|
489 | - * @param string $key |
|
490 | - * @param mixed $value |
|
491 | - * |
|
492 | - * @return static |
|
493 | - */ |
|
494 | - public function defaults($key, $value): static |
|
495 | - { |
|
496 | - $this->defaults[$key] = $value; |
|
497 | - |
|
498 | - return $this; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Set a default values for the route. |
|
503 | - * |
|
504 | - * @param array $defaults |
|
505 | - * |
|
506 | - * @return static |
|
507 | - */ |
|
508 | - public function setDefaults(array $defaults): static |
|
509 | - { |
|
510 | - $this->defaults = $defaults; |
|
511 | - |
|
512 | - return $this; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Set the flag of fallback mode on the route. |
|
517 | - * |
|
518 | - * @return static |
|
519 | - */ |
|
520 | - public function fallback(): static |
|
521 | - { |
|
522 | - $this->fallback = true; |
|
523 | - |
|
524 | - return $this; |
|
525 | - } |
|
526 | - |
|
527 | - /** |
|
528 | - * Set the facllback value. |
|
529 | - * |
|
530 | - * @param bool $fallback |
|
531 | - * |
|
532 | - * @return static |
|
533 | - */ |
|
534 | - public function setFallback(bool $fallback): static |
|
535 | - { |
|
536 | - $this->fallback = $fallback; |
|
537 | - |
|
538 | - return $this; |
|
539 | - } |
|
540 | - |
|
541 | - /** |
|
542 | - * Set the where. |
|
543 | - * |
|
544 | - * @param array|string $name |
|
545 | - * @param string|null $expression |
|
546 | - * |
|
547 | - * @return static |
|
548 | - */ |
|
549 | - public function where($name, string $expression = null): static |
|
550 | - { |
|
551 | - $wheres = is_array($name) ? $name : [$name => $expression]; |
|
447 | + return $this; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Set the name. |
|
452 | + * |
|
453 | + * @param string $name |
|
454 | + * |
|
455 | + * @return static |
|
456 | + */ |
|
457 | + public function name($name): static |
|
458 | + { |
|
459 | + $this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name; |
|
460 | + |
|
461 | + return $this; |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Determine whether the route's name matches the given patterns. |
|
466 | + * |
|
467 | + * @param mixed ...$patterns |
|
468 | + * |
|
469 | + * @return bool |
|
470 | + */ |
|
471 | + public function named(...$patterns): bool |
|
472 | + { |
|
473 | + if (is_null($routeName = $this->getName())) { |
|
474 | + return false; |
|
475 | + } |
|
476 | + |
|
477 | + foreach ($patterns as $pattern) { |
|
478 | + if (Str::is($pattern, $routeName)) { |
|
479 | + return true; |
|
480 | + } |
|
481 | + } |
|
482 | + |
|
483 | + return false; |
|
484 | + } |
|
485 | + |
|
486 | + /** |
|
487 | + * Set a default value for the route. |
|
488 | + * |
|
489 | + * @param string $key |
|
490 | + * @param mixed $value |
|
491 | + * |
|
492 | + * @return static |
|
493 | + */ |
|
494 | + public function defaults($key, $value): static |
|
495 | + { |
|
496 | + $this->defaults[$key] = $value; |
|
497 | + |
|
498 | + return $this; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Set a default values for the route. |
|
503 | + * |
|
504 | + * @param array $defaults |
|
505 | + * |
|
506 | + * @return static |
|
507 | + */ |
|
508 | + public function setDefaults(array $defaults): static |
|
509 | + { |
|
510 | + $this->defaults = $defaults; |
|
511 | + |
|
512 | + return $this; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Set the flag of fallback mode on the route. |
|
517 | + * |
|
518 | + * @return static |
|
519 | + */ |
|
520 | + public function fallback(): static |
|
521 | + { |
|
522 | + $this->fallback = true; |
|
523 | + |
|
524 | + return $this; |
|
525 | + } |
|
526 | + |
|
527 | + /** |
|
528 | + * Set the facllback value. |
|
529 | + * |
|
530 | + * @param bool $fallback |
|
531 | + * |
|
532 | + * @return static |
|
533 | + */ |
|
534 | + public function setFallback(bool $fallback): static |
|
535 | + { |
|
536 | + $this->fallback = $fallback; |
|
537 | + |
|
538 | + return $this; |
|
539 | + } |
|
540 | + |
|
541 | + /** |
|
542 | + * Set the where. |
|
543 | + * |
|
544 | + * @param array|string $name |
|
545 | + * @param string|null $expression |
|
546 | + * |
|
547 | + * @return static |
|
548 | + */ |
|
549 | + public function where($name, string $expression = null): static |
|
550 | + { |
|
551 | + $wheres = is_array($name) ? $name : [$name => $expression]; |
|
552 | 552 | |
553 | - foreach ($wheres as $name => $expression) { |
|
554 | - $this->wheres[$name] = $expression; |
|
555 | - } |
|
556 | - |
|
557 | - return $this; |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Set the where when have a variable assign. |
|
562 | - * |
|
563 | - * @param string $key |
|
564 | - * |
|
565 | - * @return string|null |
|
566 | - */ |
|
567 | - public function setPattern(string $key): ?string |
|
568 | - { |
|
569 | - return $this->wheres[$key] ?? null; |
|
570 | - } |
|
571 | - |
|
572 | - /** |
|
573 | - * Bind the route to a given request for execution. |
|
574 | - * |
|
575 | - * @param \Syscodes\Components\Http\Request $request |
|
576 | - * |
|
577 | - * @return static |
|
578 | - */ |
|
579 | - public function bind(Request $request): static |
|
580 | - { |
|
581 | - $this->compileRoute(); |
|
553 | + foreach ($wheres as $name => $expression) { |
|
554 | + $this->wheres[$name] = $expression; |
|
555 | + } |
|
556 | + |
|
557 | + return $this; |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Set the where when have a variable assign. |
|
562 | + * |
|
563 | + * @param string $key |
|
564 | + * |
|
565 | + * @return string|null |
|
566 | + */ |
|
567 | + public function setPattern(string $key): ?string |
|
568 | + { |
|
569 | + return $this->wheres[$key] ?? null; |
|
570 | + } |
|
571 | + |
|
572 | + /** |
|
573 | + * Bind the route to a given request for execution. |
|
574 | + * |
|
575 | + * @param \Syscodes\Components\Http\Request $request |
|
576 | + * |
|
577 | + * @return static |
|
578 | + */ |
|
579 | + public function bind(Request $request): static |
|
580 | + { |
|
581 | + $this->compileRoute(); |
|
582 | 582 | |
583 | - $this->parameters = (new RouteParameter($this))->parameters($request); |
|
584 | - |
|
585 | - return $this; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Compile into a Route Compile instance. |
|
590 | - * |
|
591 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
592 | - */ |
|
593 | - protected function compileRoute() |
|
594 | - { |
|
595 | - if ( ! $this->compiled) { |
|
596 | - $this->compiled = $this->toBundleRoute()->compile(); |
|
597 | - } |
|
598 | - |
|
599 | - return $this->compiled; |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * Get all of the parameter names for the route. |
|
604 | - * |
|
605 | - * @return array |
|
606 | - */ |
|
607 | - public function parameterNames(): array |
|
608 | - { |
|
609 | - if (isset($this->parameterNames)) { |
|
610 | - return $this->parameterNames; |
|
611 | - } |
|
612 | - |
|
613 | - return $this->parameterNames = $this->compileParameterNames(); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Get the parameter names for the route. |
|
618 | - * |
|
619 | - * @return array |
|
620 | - */ |
|
621 | - protected function compileParameterNames(): array |
|
622 | - { |
|
623 | - preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
624 | - |
|
625 | - return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Get a given parameter from the route. |
|
630 | - * |
|
631 | - * @param string $name |
|
632 | - * @param mixed $default |
|
633 | - * |
|
634 | - * @return mixed |
|
635 | - */ |
|
636 | - public function parameter($name, $default = null) |
|
637 | - { |
|
638 | - return Arr::get($this->parameters(), $name, $default); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * Set a parameter to the given value. |
|
643 | - * |
|
644 | - * @param string $name |
|
645 | - * @param mixed $value |
|
646 | - * |
|
647 | - * @return void |
|
648 | - */ |
|
649 | - public function setParameter($name, $value): void |
|
650 | - { |
|
651 | - $this->parameters(); |
|
652 | - |
|
653 | - $this->parameters[$name] = $value; |
|
654 | - } |
|
655 | - |
|
656 | - /** |
|
657 | - * Get the key / value list of parameters without null values. |
|
658 | - * |
|
659 | - * @return array |
|
660 | - */ |
|
661 | - public function parametersWithouNulls(): array |
|
662 | - { |
|
663 | - return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
664 | - } |
|
665 | - |
|
666 | - /** |
|
667 | - * Get the key / value list of parameters for the route. |
|
668 | - * |
|
669 | - * @return array |
|
670 | - */ |
|
671 | - public function parameters(): array |
|
672 | - { |
|
673 | - if (isset($this->parameters)) { |
|
674 | - return $this->parameters; |
|
675 | - } |
|
676 | - |
|
677 | - throw new LogicException('The route is not bound'); |
|
678 | - } |
|
679 | - |
|
680 | - /** |
|
681 | - * Get all middleware, including the ones from the controller. |
|
682 | - * |
|
683 | - * @return array |
|
684 | - */ |
|
685 | - public function gatherMiddleware(): array |
|
686 | - { |
|
687 | - if ( ! is_null($this->computedMiddleware)) { |
|
688 | - return $this->computedMiddleware; |
|
689 | - } |
|
690 | - |
|
691 | - $this->computedMiddleware = []; |
|
692 | - |
|
693 | - return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
694 | - $this->middleware(), |
|
695 | - $this->controllerMiddleware() |
|
696 | - )); |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Get or set the middlewares attached to the route. |
|
701 | - * |
|
702 | - * @param array|string|null $middleware |
|
703 | - * |
|
704 | - * @return array|static |
|
705 | - */ |
|
706 | - public function middleware($middleware = null) |
|
707 | - { |
|
708 | - if (is_null($middleware)) { |
|
709 | - return $this->getMiddleware(); |
|
710 | - } |
|
711 | - |
|
712 | - if (is_string($middleware)) { |
|
713 | - $middleware = func_get_args(); |
|
714 | - } |
|
715 | - |
|
716 | - foreach ($middleware as $index => $value) { |
|
717 | - $middleware[$index] = (string) $value; |
|
718 | - } |
|
719 | - |
|
720 | - $this->action['middleware'] = array_merge( |
|
721 | - $this->getMiddleware(), |
|
722 | - $middleware |
|
723 | - ); |
|
724 | - |
|
725 | - return $this; |
|
726 | - } |
|
727 | - |
|
728 | - /** |
|
729 | - * Get the middlewares attached to the route. |
|
730 | - * |
|
731 | - * @return array |
|
732 | - */ |
|
733 | - protected function getMiddleware(): array |
|
734 | - { |
|
735 | - return (array) ($this->action['middleware'] ?? []); |
|
736 | - } |
|
737 | - |
|
738 | - /** |
|
739 | - * Get the middleware for the route's controller. |
|
740 | - * |
|
741 | - * @return array |
|
742 | - */ |
|
743 | - public function controllerMiddleware(): array |
|
744 | - { |
|
745 | - if ( ! $this->isControllerAction()) { |
|
746 | - return []; |
|
747 | - } |
|
748 | - |
|
749 | - return $this->controllerDispatcher()->getMiddleware( |
|
750 | - $this->getController(), |
|
751 | - $this->getControllerMethod() |
|
752 | - ); |
|
753 | - } |
|
754 | - |
|
755 | - /** |
|
756 | - * Determine if the route only responds to HTTP requests. |
|
757 | - * |
|
758 | - * @return bool |
|
759 | - */ |
|
760 | - public function httpOnly(): bool |
|
761 | - { |
|
762 | - return in_array('http', $this->action, true); |
|
763 | - } |
|
764 | - |
|
765 | - /** |
|
766 | - * Determine if the route only responds to HTTPS requests. |
|
767 | - * |
|
768 | - * @return bool |
|
769 | - */ |
|
770 | - public function httpsOnly(): bool |
|
771 | - { |
|
772 | - return $this->secure(); |
|
773 | - } |
|
774 | - |
|
775 | - /** |
|
776 | - * Determine if the route only responds to HTTPS requests. |
|
777 | - * |
|
778 | - * @return bool |
|
779 | - */ |
|
780 | - public function secure(): bool |
|
781 | - { |
|
782 | - return in_array('https', $this->action, true); |
|
783 | - } |
|
583 | + $this->parameters = (new RouteParameter($this))->parameters($request); |
|
584 | + |
|
585 | + return $this; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Compile into a Route Compile instance. |
|
590 | + * |
|
591 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\RouteCompiler |
|
592 | + */ |
|
593 | + protected function compileRoute() |
|
594 | + { |
|
595 | + if ( ! $this->compiled) { |
|
596 | + $this->compiled = $this->toBundleRoute()->compile(); |
|
597 | + } |
|
598 | + |
|
599 | + return $this->compiled; |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Get all of the parameter names for the route. |
|
604 | + * |
|
605 | + * @return array |
|
606 | + */ |
|
607 | + public function parameterNames(): array |
|
608 | + { |
|
609 | + if (isset($this->parameterNames)) { |
|
610 | + return $this->parameterNames; |
|
611 | + } |
|
612 | + |
|
613 | + return $this->parameterNames = $this->compileParameterNames(); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Get the parameter names for the route. |
|
618 | + * |
|
619 | + * @return array |
|
620 | + */ |
|
621 | + protected function compileParameterNames(): array |
|
622 | + { |
|
623 | + preg_match_all('~\{(.*?)\}~', $this->getDomain().$this->getUri(), $matches); |
|
624 | + |
|
625 | + return array_map(fn ($match) => trim($match, '?'), $matches[1]); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Get a given parameter from the route. |
|
630 | + * |
|
631 | + * @param string $name |
|
632 | + * @param mixed $default |
|
633 | + * |
|
634 | + * @return mixed |
|
635 | + */ |
|
636 | + public function parameter($name, $default = null) |
|
637 | + { |
|
638 | + return Arr::get($this->parameters(), $name, $default); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * Set a parameter to the given value. |
|
643 | + * |
|
644 | + * @param string $name |
|
645 | + * @param mixed $value |
|
646 | + * |
|
647 | + * @return void |
|
648 | + */ |
|
649 | + public function setParameter($name, $value): void |
|
650 | + { |
|
651 | + $this->parameters(); |
|
652 | + |
|
653 | + $this->parameters[$name] = $value; |
|
654 | + } |
|
655 | + |
|
656 | + /** |
|
657 | + * Get the key / value list of parameters without null values. |
|
658 | + * |
|
659 | + * @return array |
|
660 | + */ |
|
661 | + public function parametersWithouNulls(): array |
|
662 | + { |
|
663 | + return array_filter($this->parameters(), fn ($parameter) => ! is_null($parameter)); |
|
664 | + } |
|
665 | + |
|
666 | + /** |
|
667 | + * Get the key / value list of parameters for the route. |
|
668 | + * |
|
669 | + * @return array |
|
670 | + */ |
|
671 | + public function parameters(): array |
|
672 | + { |
|
673 | + if (isset($this->parameters)) { |
|
674 | + return $this->parameters; |
|
675 | + } |
|
676 | + |
|
677 | + throw new LogicException('The route is not bound'); |
|
678 | + } |
|
679 | + |
|
680 | + /** |
|
681 | + * Get all middleware, including the ones from the controller. |
|
682 | + * |
|
683 | + * @return array |
|
684 | + */ |
|
685 | + public function gatherMiddleware(): array |
|
686 | + { |
|
687 | + if ( ! is_null($this->computedMiddleware)) { |
|
688 | + return $this->computedMiddleware; |
|
689 | + } |
|
690 | + |
|
691 | + $this->computedMiddleware = []; |
|
692 | + |
|
693 | + return $this->computedMiddleware = Router::uniqueMiddleware(array_merge( |
|
694 | + $this->middleware(), |
|
695 | + $this->controllerMiddleware() |
|
696 | + )); |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Get or set the middlewares attached to the route. |
|
701 | + * |
|
702 | + * @param array|string|null $middleware |
|
703 | + * |
|
704 | + * @return array|static |
|
705 | + */ |
|
706 | + public function middleware($middleware = null) |
|
707 | + { |
|
708 | + if (is_null($middleware)) { |
|
709 | + return $this->getMiddleware(); |
|
710 | + } |
|
711 | + |
|
712 | + if (is_string($middleware)) { |
|
713 | + $middleware = func_get_args(); |
|
714 | + } |
|
715 | + |
|
716 | + foreach ($middleware as $index => $value) { |
|
717 | + $middleware[$index] = (string) $value; |
|
718 | + } |
|
719 | + |
|
720 | + $this->action['middleware'] = array_merge( |
|
721 | + $this->getMiddleware(), |
|
722 | + $middleware |
|
723 | + ); |
|
724 | + |
|
725 | + return $this; |
|
726 | + } |
|
727 | + |
|
728 | + /** |
|
729 | + * Get the middlewares attached to the route. |
|
730 | + * |
|
731 | + * @return array |
|
732 | + */ |
|
733 | + protected function getMiddleware(): array |
|
734 | + { |
|
735 | + return (array) ($this->action['middleware'] ?? []); |
|
736 | + } |
|
737 | + |
|
738 | + /** |
|
739 | + * Get the middleware for the route's controller. |
|
740 | + * |
|
741 | + * @return array |
|
742 | + */ |
|
743 | + public function controllerMiddleware(): array |
|
744 | + { |
|
745 | + if ( ! $this->isControllerAction()) { |
|
746 | + return []; |
|
747 | + } |
|
748 | + |
|
749 | + return $this->controllerDispatcher()->getMiddleware( |
|
750 | + $this->getController(), |
|
751 | + $this->getControllerMethod() |
|
752 | + ); |
|
753 | + } |
|
754 | + |
|
755 | + /** |
|
756 | + * Determine if the route only responds to HTTP requests. |
|
757 | + * |
|
758 | + * @return bool |
|
759 | + */ |
|
760 | + public function httpOnly(): bool |
|
761 | + { |
|
762 | + return in_array('http', $this->action, true); |
|
763 | + } |
|
764 | + |
|
765 | + /** |
|
766 | + * Determine if the route only responds to HTTPS requests. |
|
767 | + * |
|
768 | + * @return bool |
|
769 | + */ |
|
770 | + public function httpsOnly(): bool |
|
771 | + { |
|
772 | + return $this->secure(); |
|
773 | + } |
|
774 | + |
|
775 | + /** |
|
776 | + * Determine if the route only responds to HTTPS requests. |
|
777 | + * |
|
778 | + * @return bool |
|
779 | + */ |
|
780 | + public function secure(): bool |
|
781 | + { |
|
782 | + return in_array('https', $this->action, true); |
|
783 | + } |
|
784 | 784 | |
785 | - /** |
|
786 | - * Convert the route to a bundle route. |
|
787 | - * |
|
788 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
789 | - */ |
|
790 | - public function toBundleRoute() |
|
791 | - { |
|
792 | - return new BundleRoute( |
|
793 | - preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
794 | - $this->getOptionalParameterNames(), |
|
795 | - $this->getPatterns(), |
|
796 | - $this->getDomain() ?: '' |
|
797 | - ); |
|
798 | - } |
|
785 | + /** |
|
786 | + * Convert the route to a bundle route. |
|
787 | + * |
|
788 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\Route |
|
789 | + */ |
|
790 | + public function toBundleRoute() |
|
791 | + { |
|
792 | + return new BundleRoute( |
|
793 | + preg_replace('~\{(\w+?)\?\}~', '{$1}', $this->getUri()), |
|
794 | + $this->getOptionalParameterNames(), |
|
795 | + $this->getPatterns(), |
|
796 | + $this->getDomain() ?: '' |
|
797 | + ); |
|
798 | + } |
|
799 | 799 | |
800 | - /** |
|
801 | - * Get the optional parameter names for the route. |
|
802 | - * |
|
803 | - * @return array |
|
804 | - */ |
|
805 | - protected function getOptionalParameterNames(): array |
|
806 | - { |
|
807 | - preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
800 | + /** |
|
801 | + * Get the optional parameter names for the route. |
|
802 | + * |
|
803 | + * @return array |
|
804 | + */ |
|
805 | + protected function getOptionalParameterNames(): array |
|
806 | + { |
|
807 | + preg_match_all('~\{(\w+?)\?\}~', $this->getUri(), $matches); |
|
808 | 808 | |
809 | - return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
810 | - } |
|
811 | - |
|
812 | - /** |
|
813 | - * Get the action of the current route. |
|
814 | - * |
|
815 | - * @return \Closure|string|array |
|
816 | - */ |
|
817 | - public function getAction() |
|
818 | - { |
|
819 | - return $this->action; |
|
820 | - } |
|
809 | + return isset($matches[1]) ? array_fill_keys($matches[1], null) : []; |
|
810 | + } |
|
811 | + |
|
812 | + /** |
|
813 | + * Get the action of the current route. |
|
814 | + * |
|
815 | + * @return \Closure|string|array |
|
816 | + */ |
|
817 | + public function getAction() |
|
818 | + { |
|
819 | + return $this->action; |
|
820 | + } |
|
821 | 821 | |
822 | - /** |
|
823 | - * Get the compiled version of the route. |
|
824 | - * |
|
825 | - * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
826 | - */ |
|
827 | - public function getCompiled() |
|
828 | - { |
|
829 | - return $this->compiled; |
|
830 | - } |
|
831 | - |
|
832 | - /** |
|
833 | - * Get the URI associated with the route. |
|
834 | - * |
|
835 | - * @return string |
|
836 | - */ |
|
837 | - public function getUri(): string |
|
838 | - { |
|
839 | - return $this->uri; |
|
840 | - } |
|
841 | - |
|
842 | - /** |
|
843 | - * Get the patterns of the current route. |
|
844 | - * |
|
845 | - * @return array |
|
846 | - */ |
|
847 | - public function getPatterns(): array |
|
848 | - { |
|
849 | - return $this->wheres; |
|
850 | - } |
|
851 | - |
|
852 | - /** |
|
853 | - * Get the request method of the current route. |
|
854 | - * |
|
855 | - * @return array|string |
|
856 | - */ |
|
857 | - public function getMethod(): array|string |
|
858 | - { |
|
859 | - return $this->method; |
|
860 | - } |
|
861 | - |
|
862 | - /** |
|
863 | - * Get the url of the current route. |
|
864 | - * |
|
865 | - * @return string|null |
|
866 | - */ |
|
867 | - public function getName(): ?string |
|
868 | - { |
|
869 | - return $this->action['as'] ?? null; |
|
870 | - } |
|
871 | - |
|
872 | - /** |
|
873 | - * Set the container instance on the route. |
|
874 | - * |
|
875 | - * @param \Syscodes\Components\Container\Container $container |
|
876 | - * |
|
877 | - * @return static |
|
878 | - */ |
|
879 | - public function setContainer(Container $container): static |
|
880 | - { |
|
881 | - $this->container = $container; |
|
882 | - |
|
883 | - return $this; |
|
884 | - } |
|
885 | - |
|
886 | - /** |
|
887 | - * Magic method. |
|
888 | - * |
|
889 | - * Dynamically access route parameters. |
|
890 | - * |
|
891 | - * @param string $key |
|
892 | - * |
|
893 | - * @return mixed |
|
894 | - */ |
|
895 | - public function __get($key) |
|
896 | - { |
|
897 | - return $this->parameter($key); |
|
898 | - } |
|
822 | + /** |
|
823 | + * Get the compiled version of the route. |
|
824 | + * |
|
825 | + * @return \Syscodes\Bundles\ApplicationBundle\Routing\CompiledRoute |
|
826 | + */ |
|
827 | + public function getCompiled() |
|
828 | + { |
|
829 | + return $this->compiled; |
|
830 | + } |
|
831 | + |
|
832 | + /** |
|
833 | + * Get the URI associated with the route. |
|
834 | + * |
|
835 | + * @return string |
|
836 | + */ |
|
837 | + public function getUri(): string |
|
838 | + { |
|
839 | + return $this->uri; |
|
840 | + } |
|
841 | + |
|
842 | + /** |
|
843 | + * Get the patterns of the current route. |
|
844 | + * |
|
845 | + * @return array |
|
846 | + */ |
|
847 | + public function getPatterns(): array |
|
848 | + { |
|
849 | + return $this->wheres; |
|
850 | + } |
|
851 | + |
|
852 | + /** |
|
853 | + * Get the request method of the current route. |
|
854 | + * |
|
855 | + * @return array|string |
|
856 | + */ |
|
857 | + public function getMethod(): array|string |
|
858 | + { |
|
859 | + return $this->method; |
|
860 | + } |
|
861 | + |
|
862 | + /** |
|
863 | + * Get the url of the current route. |
|
864 | + * |
|
865 | + * @return string|null |
|
866 | + */ |
|
867 | + public function getName(): ?string |
|
868 | + { |
|
869 | + return $this->action['as'] ?? null; |
|
870 | + } |
|
871 | + |
|
872 | + /** |
|
873 | + * Set the container instance on the route. |
|
874 | + * |
|
875 | + * @param \Syscodes\Components\Container\Container $container |
|
876 | + * |
|
877 | + * @return static |
|
878 | + */ |
|
879 | + public function setContainer(Container $container): static |
|
880 | + { |
|
881 | + $this->container = $container; |
|
882 | + |
|
883 | + return $this; |
|
884 | + } |
|
885 | + |
|
886 | + /** |
|
887 | + * Magic method. |
|
888 | + * |
|
889 | + * Dynamically access route parameters. |
|
890 | + * |
|
891 | + * @param string $key |
|
892 | + * |
|
893 | + * @return mixed |
|
894 | + */ |
|
895 | + public function __get($key) |
|
896 | + { |
|
897 | + return $this->parameter($key); |
|
898 | + } |
|
899 | 899 | } |
900 | 900 | \ No newline at end of file |
@@ -34,160 +34,160 @@ |
||
34 | 34 | */ |
35 | 35 | class Parameters implements IteratorAggregate, Countable |
36 | 36 | { |
37 | - /** |
|
38 | - * Array parameters from the Server global. |
|
39 | - * |
|
40 | - * @var array $parameters |
|
41 | - */ |
|
42 | - protected $parameters; |
|
43 | - |
|
44 | - /** |
|
45 | - * Parameter Object Constructor. |
|
46 | - * |
|
47 | - * @param array $parameters |
|
48 | - * |
|
49 | - * @return array |
|
50 | - */ |
|
51 | - public function __construct(array $parameters = []) |
|
52 | - { |
|
53 | - $this->parameters = $parameters; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns the parameters. |
|
58 | - * |
|
59 | - * @param string|null $key |
|
60 | - * |
|
61 | - * @return array |
|
62 | - */ |
|
63 | - public function all(string $key = null): array |
|
64 | - { |
|
65 | - if (null === $key) { |
|
66 | - return $this->parameters; |
|
67 | - } |
|
68 | - |
|
69 | - if ( ! is_array($value = $this->parameters[$key] ?? [])) { |
|
70 | - throw new BadRequestException( |
|
71 | - sprintf('Unexpected value for parameter "%s", got "%s"', $key, get_debug_type($value)) |
|
72 | - ); |
|
73 | - } |
|
74 | - |
|
75 | - return $value; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Returns the parameter keys. |
|
80 | - * |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - public function keys(): array |
|
84 | - { |
|
85 | - return array_keys($this->parameters); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Replaces the current parameters. |
|
90 | - * |
|
91 | - * @param array $parameters |
|
92 | - * |
|
93 | - * @return void |
|
94 | - */ |
|
95 | - public function replace(array $parameters = []): void |
|
96 | - { |
|
97 | - $this->parameters = $parameters; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Adds parameters. |
|
102 | - * |
|
103 | - * @param array $parameters |
|
104 | - * |
|
105 | - * @return void |
|
106 | - */ |
|
107 | - public function add(array $parameters = []): void |
|
108 | - { |
|
109 | - $this->parameters = array_replace($this->parameters, $parameters); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Get a parameter array item. |
|
114 | - * |
|
115 | - * @param string $key |
|
116 | - * @param mixed $default |
|
117 | - * |
|
118 | - * @return mixed |
|
119 | - */ |
|
120 | - public function get(string $key, mixed $default = null): mixed |
|
121 | - { |
|
122 | - return $this->has($key) ? $this->parameters[$key] : $default; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Check if a parameter array item exists. |
|
127 | - * |
|
128 | - * @param string $key |
|
129 | - * |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - public function has(string $key): bool |
|
133 | - { |
|
134 | - return Arr::exists($this->parameters, $key); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Set a parameter array item. |
|
139 | - * |
|
140 | - * @param string $key |
|
141 | - * @param string $value |
|
142 | - * |
|
143 | - * @return void |
|
144 | - */ |
|
145 | - public function set(string $key, $value): void |
|
146 | - { |
|
147 | - $this->parameters[$key] = $value; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Remove a parameter array item. |
|
152 | - * |
|
153 | - * @param string $key |
|
154 | - * |
|
155 | - * @return void |
|
156 | - */ |
|
157 | - public function remove(string $key): void |
|
158 | - { |
|
159 | - unset($this->parameters[$key]); |
|
160 | - } |
|
161 | - |
|
162 | - /* |
|
37 | + /** |
|
38 | + * Array parameters from the Server global. |
|
39 | + * |
|
40 | + * @var array $parameters |
|
41 | + */ |
|
42 | + protected $parameters; |
|
43 | + |
|
44 | + /** |
|
45 | + * Parameter Object Constructor. |
|
46 | + * |
|
47 | + * @param array $parameters |
|
48 | + * |
|
49 | + * @return array |
|
50 | + */ |
|
51 | + public function __construct(array $parameters = []) |
|
52 | + { |
|
53 | + $this->parameters = $parameters; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns the parameters. |
|
58 | + * |
|
59 | + * @param string|null $key |
|
60 | + * |
|
61 | + * @return array |
|
62 | + */ |
|
63 | + public function all(string $key = null): array |
|
64 | + { |
|
65 | + if (null === $key) { |
|
66 | + return $this->parameters; |
|
67 | + } |
|
68 | + |
|
69 | + if ( ! is_array($value = $this->parameters[$key] ?? [])) { |
|
70 | + throw new BadRequestException( |
|
71 | + sprintf('Unexpected value for parameter "%s", got "%s"', $key, get_debug_type($value)) |
|
72 | + ); |
|
73 | + } |
|
74 | + |
|
75 | + return $value; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Returns the parameter keys. |
|
80 | + * |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + public function keys(): array |
|
84 | + { |
|
85 | + return array_keys($this->parameters); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Replaces the current parameters. |
|
90 | + * |
|
91 | + * @param array $parameters |
|
92 | + * |
|
93 | + * @return void |
|
94 | + */ |
|
95 | + public function replace(array $parameters = []): void |
|
96 | + { |
|
97 | + $this->parameters = $parameters; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Adds parameters. |
|
102 | + * |
|
103 | + * @param array $parameters |
|
104 | + * |
|
105 | + * @return void |
|
106 | + */ |
|
107 | + public function add(array $parameters = []): void |
|
108 | + { |
|
109 | + $this->parameters = array_replace($this->parameters, $parameters); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Get a parameter array item. |
|
114 | + * |
|
115 | + * @param string $key |
|
116 | + * @param mixed $default |
|
117 | + * |
|
118 | + * @return mixed |
|
119 | + */ |
|
120 | + public function get(string $key, mixed $default = null): mixed |
|
121 | + { |
|
122 | + return $this->has($key) ? $this->parameters[$key] : $default; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Check if a parameter array item exists. |
|
127 | + * |
|
128 | + * @param string $key |
|
129 | + * |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + public function has(string $key): bool |
|
133 | + { |
|
134 | + return Arr::exists($this->parameters, $key); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Set a parameter array item. |
|
139 | + * |
|
140 | + * @param string $key |
|
141 | + * @param string $value |
|
142 | + * |
|
143 | + * @return void |
|
144 | + */ |
|
145 | + public function set(string $key, $value): void |
|
146 | + { |
|
147 | + $this->parameters[$key] = $value; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Remove a parameter array item. |
|
152 | + * |
|
153 | + * @param string $key |
|
154 | + * |
|
155 | + * @return void |
|
156 | + */ |
|
157 | + public function remove(string $key): void |
|
158 | + { |
|
159 | + unset($this->parameters[$key]); |
|
160 | + } |
|
161 | + |
|
162 | + /* |
|
163 | 163 | |----------------------------------------------------------------- |
164 | 164 | | IteratorAggregate Method |
165 | 165 | |----------------------------------------------------------------- |
166 | 166 | */ |
167 | 167 | |
168 | - /** |
|
169 | - * Retrieve an external iterator. |
|
170 | - * |
|
171 | - * @return \ArrayIterator |
|
172 | - */ |
|
173 | - public function getIterator(): Traversable |
|
174 | - { |
|
175 | - return new ArrayIterator($this->parameters); |
|
176 | - } |
|
168 | + /** |
|
169 | + * Retrieve an external iterator. |
|
170 | + * |
|
171 | + * @return \ArrayIterator |
|
172 | + */ |
|
173 | + public function getIterator(): Traversable |
|
174 | + { |
|
175 | + return new ArrayIterator($this->parameters); |
|
176 | + } |
|
177 | 177 | |
178 | - /* |
|
178 | + /* |
|
179 | 179 | |----------------------------------------------------------------- |
180 | 180 | | Countable Method |
181 | 181 | |----------------------------------------------------------------- |
182 | 182 | */ |
183 | 183 | |
184 | - /** |
|
185 | - * Returns the number of parameters. |
|
186 | - * |
|
187 | - * @return int The number of parameters |
|
188 | - */ |
|
189 | - public function count(): int |
|
190 | - { |
|
191 | - return count($this->parameters); |
|
192 | - } |
|
184 | + /** |
|
185 | + * Returns the number of parameters. |
|
186 | + * |
|
187 | + * @return int The number of parameters |
|
188 | + */ |
|
189 | + public function count(): int |
|
190 | + { |
|
191 | + return count($this->parameters); |
|
192 | + } |
|
193 | 193 | } |
194 | 194 | \ No newline at end of file |
@@ -30,70 +30,70 @@ |
||
30 | 30 | */ |
31 | 31 | final class Inputs extends Parameters |
32 | 32 | { |
33 | - /** |
|
34 | - * Replaces the current parameters. |
|
35 | - * |
|
36 | - * @param array $parameters |
|
37 | - * |
|
38 | - * @return void |
|
39 | - */ |
|
40 | - public function replace(array $inputs = []): void |
|
41 | - { |
|
42 | - $this->parameters = []; |
|
43 | - $this->add($inputs); |
|
44 | - } |
|
33 | + /** |
|
34 | + * Replaces the current parameters. |
|
35 | + * |
|
36 | + * @param array $parameters |
|
37 | + * |
|
38 | + * @return void |
|
39 | + */ |
|
40 | + public function replace(array $inputs = []): void |
|
41 | + { |
|
42 | + $this->parameters = []; |
|
43 | + $this->add($inputs); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Adds parameters. |
|
48 | - * |
|
49 | - * @param array $parameters |
|
50 | - * |
|
51 | - * @return void |
|
52 | - */ |
|
53 | - public function add(array $inputs = []): void |
|
54 | - { |
|
55 | - foreach ($inputs as $input => $file) { |
|
56 | - $this->set($input, $file); |
|
57 | - } |
|
58 | - } |
|
46 | + /** |
|
47 | + * Adds parameters. |
|
48 | + * |
|
49 | + * @param array $parameters |
|
50 | + * |
|
51 | + * @return void |
|
52 | + */ |
|
53 | + public function add(array $inputs = []): void |
|
54 | + { |
|
55 | + foreach ($inputs as $input => $file) { |
|
56 | + $this->set($input, $file); |
|
57 | + } |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Gets a string input value by name. |
|
62 | - * |
|
63 | - * @param string $key |
|
64 | - * @param mixed $default |
|
65 | - * |
|
66 | - * @return string|int|float|bool|null |
|
67 | - */ |
|
68 | - public function get(string $key, mixed $default = null): string|int|float|bool|null |
|
69 | - { |
|
70 | - if (null !== $default && ! is_scalar($default) && ! method_exists($default, '__toString')) { |
|
71 | - throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
|
72 | - } |
|
60 | + /** |
|
61 | + * Gets a string input value by name. |
|
62 | + * |
|
63 | + * @param string $key |
|
64 | + * @param mixed $default |
|
65 | + * |
|
66 | + * @return string|int|float|bool|null |
|
67 | + */ |
|
68 | + public function get(string $key, mixed $default = null): string|int|float|bool|null |
|
69 | + { |
|
70 | + if (null !== $default && ! is_scalar($default) && ! method_exists($default, '__toString')) { |
|
71 | + throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
|
72 | + } |
|
73 | 73 | |
74 | - $value = parent::get($key, $this); |
|
74 | + $value = parent::get($key, $this); |
|
75 | 75 | |
76 | - if (null !== $value && $this !== $value && ! is_scalar($value) && ! method_exists($value, '__toString')) { |
|
77 | - throw new BadRequestHttpException(sprintf('Retrieving a non-string value from "%s()" is deprecated, and will throw a exception in Syscodes, use "%s::all($key)" instead', __METHOD__, __CLASS__)); |
|
78 | - } |
|
76 | + if (null !== $value && $this !== $value && ! is_scalar($value) && ! method_exists($value, '__toString')) { |
|
77 | + throw new BadRequestHttpException(sprintf('Retrieving a non-string value from "%s()" is deprecated, and will throw a exception in Syscodes, use "%s::all($key)" instead', __METHOD__, __CLASS__)); |
|
78 | + } |
|
79 | 79 | |
80 | - return $this === $value ? $default : $value; |
|
81 | - } |
|
80 | + return $this === $value ? $default : $value; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Sets an input by name. |
|
85 | - * |
|
86 | - * @param string $key |
|
87 | - * @param mixed $value |
|
88 | - * |
|
89 | - * @return void |
|
90 | - */ |
|
91 | - public function set(string $key, mixed $value): void |
|
92 | - { |
|
93 | - if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exists($value, '__toString')) { |
|
94 | - throw new BadRequestHttpException(sprintf('Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead', get_debug_type($value), __METHOD__)); |
|
95 | - } |
|
83 | + /** |
|
84 | + * Sets an input by name. |
|
85 | + * |
|
86 | + * @param string $key |
|
87 | + * @param mixed $value |
|
88 | + * |
|
89 | + * @return void |
|
90 | + */ |
|
91 | + public function set(string $key, mixed $value): void |
|
92 | + { |
|
93 | + if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exists($value, '__toString')) { |
|
94 | + throw new BadRequestHttpException(sprintf('Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead', get_debug_type($value), __METHOD__)); |
|
95 | + } |
|
96 | 96 | |
97 | - $this->parameters[$key] = $value; |
|
98 | - } |
|
97 | + $this->parameters[$key] = $value; |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | \ No newline at end of file |
@@ -65,7 +65,7 @@ |
||
65 | 65 | * |
66 | 66 | * @return string|int|float|bool|null |
67 | 67 | */ |
68 | - public function get(string $key, mixed $default = null): string|int|float|bool|null |
|
68 | + public function get(string $key, mixed $default = null): string | int | float | bool | null |
|
69 | 69 | { |
70 | 70 | if (null !== $default && ! is_scalar($default) && ! method_exists($default, '__toString')) { |
71 | 71 | throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
@@ -40,29 +40,29 @@ |
||
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
43 | - * Replaces the current parameters. |
|
44 | - * |
|
45 | - * @param array $parameters |
|
46 | - * |
|
47 | - * @return void |
|
48 | - */ |
|
49 | - public function replace(array $files = []): void |
|
50 | - { |
|
51 | - $this->parameters = []; |
|
43 | + * Replaces the current parameters. |
|
44 | + * |
|
45 | + * @param array $parameters |
|
46 | + * |
|
47 | + * @return void |
|
48 | + */ |
|
49 | + public function replace(array $files = []): void |
|
50 | + { |
|
51 | + $this->parameters = []; |
|
52 | 52 | $this->add($files); |
53 | - } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Adds parameters. |
|
57 | - * |
|
58 | - * @param array $parameters |
|
59 | - * |
|
60 | - * @return void |
|
61 | - */ |
|
62 | - public function add(array $files = []): void |
|
63 | - { |
|
55 | + /** |
|
56 | + * Adds parameters. |
|
57 | + * |
|
58 | + * @param array $parameters |
|
59 | + * |
|
60 | + * @return void |
|
61 | + */ |
|
62 | + public function add(array $files = []): void |
|
63 | + { |
|
64 | 64 | foreach ($files as $key => $file) { |
65 | 65 | $this->set($key, $file); |
66 | 66 | } |
67 | - } |
|
67 | + } |
|
68 | 68 | } |
69 | 69 | \ No newline at end of file |
@@ -35,266 +35,266 @@ |
||
35 | 35 | */ |
36 | 36 | class Headers implements IteratorAggregate, Countable |
37 | 37 | { |
38 | - protected const STRING_UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
39 | - protected const STRING_LOWER = '-abcdefghijklmnopqrstuvwxyz'; |
|
38 | + protected const STRING_UPPER = '_ABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
|
39 | + protected const STRING_LOWER = '-abcdefghijklmnopqrstuvwxyz'; |
|
40 | 40 | |
41 | - /** |
|
42 | - * An array of HTTP headers. |
|
43 | - * |
|
44 | - * @var array $herders |
|
45 | - */ |
|
46 | - protected $headers = []; |
|
41 | + /** |
|
42 | + * An array of HTTP headers. |
|
43 | + * |
|
44 | + * @var array $herders |
|
45 | + */ |
|
46 | + protected $headers = []; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Specifies the directives for the caching mechanisms in both |
|
50 | - * the requests and the responses. |
|
51 | - * |
|
52 | - * @var array $cacheControl |
|
53 | - */ |
|
54 | - protected $cacheControl = []; |
|
48 | + /** |
|
49 | + * Specifies the directives for the caching mechanisms in both |
|
50 | + * the requests and the responses. |
|
51 | + * |
|
52 | + * @var array $cacheControl |
|
53 | + */ |
|
54 | + protected $cacheControl = []; |
|
55 | 55 | |
56 | - /** |
|
57 | - * Constructor. The Headers class instance. |
|
58 | - * |
|
59 | - * @param array $headers |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - public function __construct(array $headers = []) |
|
64 | - { |
|
65 | - foreach ($headers as $key => $values) { |
|
66 | - $this->set($key, $values); |
|
67 | - } |
|
68 | - } |
|
56 | + /** |
|
57 | + * Constructor. The Headers class instance. |
|
58 | + * |
|
59 | + * @param array $headers |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + public function __construct(array $headers = []) |
|
64 | + { |
|
65 | + foreach ($headers as $key => $values) { |
|
66 | + $this->set($key, $values); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * Returns all the headers. |
|
72 | - * |
|
73 | - * @param string|null $key The name of the headers |
|
74 | - * |
|
75 | - * @return array |
|
76 | - */ |
|
77 | - public function all(string $key = null): array |
|
78 | - { |
|
79 | - if (null !== $key) { |
|
80 | - return $this->headers[strtr($key, self::STRING_UPPER, self::STRING_LOWER)] ?? []; |
|
81 | - } |
|
70 | + /** |
|
71 | + * Returns all the headers. |
|
72 | + * |
|
73 | + * @param string|null $key The name of the headers |
|
74 | + * |
|
75 | + * @return array |
|
76 | + */ |
|
77 | + public function all(string $key = null): array |
|
78 | + { |
|
79 | + if (null !== $key) { |
|
80 | + return $this->headers[strtr($key, self::STRING_UPPER, self::STRING_LOWER)] ?? []; |
|
81 | + } |
|
82 | 82 | |
83 | - return $this->headers; |
|
84 | - } |
|
83 | + return $this->headers; |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Returns the parameter keys. |
|
88 | - * |
|
89 | - * @return array An array of parameter keys |
|
90 | - */ |
|
91 | - public function keys(): array |
|
92 | - { |
|
93 | - return array_keys($this->all()); |
|
94 | - } |
|
86 | + /** |
|
87 | + * Returns the parameter keys. |
|
88 | + * |
|
89 | + * @return array An array of parameter keys |
|
90 | + */ |
|
91 | + public function keys(): array |
|
92 | + { |
|
93 | + return array_keys($this->all()); |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Replaces the current HTTP headers by a new set. |
|
98 | - * |
|
99 | - * @param array $headers |
|
100 | - * |
|
101 | - * @return void |
|
102 | - */ |
|
103 | - public function replace(array $headers = []): void |
|
104 | - { |
|
105 | - $this->headers = []; |
|
106 | - $this->add($headers); |
|
107 | - } |
|
96 | + /** |
|
97 | + * Replaces the current HTTP headers by a new set. |
|
98 | + * |
|
99 | + * @param array $headers |
|
100 | + * |
|
101 | + * @return void |
|
102 | + */ |
|
103 | + public function replace(array $headers = []): void |
|
104 | + { |
|
105 | + $this->headers = []; |
|
106 | + $this->add($headers); |
|
107 | + } |
|
108 | 108 | |
109 | - /** |
|
110 | - * Adds multiple header to the queue. |
|
111 | - * |
|
112 | - * @param array $headers The header name |
|
113 | - * |
|
114 | - * @return mixed |
|
115 | - */ |
|
116 | - public function add(array $headers = []) |
|
117 | - { |
|
118 | - foreach ($headers as $key => $values) { |
|
119 | - $this->set($key, $values); |
|
120 | - } |
|
121 | - } |
|
109 | + /** |
|
110 | + * Adds multiple header to the queue. |
|
111 | + * |
|
112 | + * @param array $headers The header name |
|
113 | + * |
|
114 | + * @return mixed |
|
115 | + */ |
|
116 | + public function add(array $headers = []) |
|
117 | + { |
|
118 | + foreach ($headers as $key => $values) { |
|
119 | + $this->set($key, $values); |
|
120 | + } |
|
121 | + } |
|
122 | 122 | |
123 | - /** |
|
124 | - * Gets a header value by name. |
|
125 | - * |
|
126 | - * @param string $key The header name, or null for all headers |
|
127 | - * @param string|null $default The default value |
|
128 | - * |
|
129 | - * @return mixed |
|
130 | - */ |
|
131 | - public function get(string $key, string $default = null): ?string |
|
132 | - { |
|
133 | - $headers = $this->all($key); |
|
123 | + /** |
|
124 | + * Gets a header value by name. |
|
125 | + * |
|
126 | + * @param string $key The header name, or null for all headers |
|
127 | + * @param string|null $default The default value |
|
128 | + * |
|
129 | + * @return mixed |
|
130 | + */ |
|
131 | + public function get(string $key, string $default = null): ?string |
|
132 | + { |
|
133 | + $headers = $this->all($key); |
|
134 | 134 | |
135 | - if ( ! $headers) { |
|
136 | - return $default; |
|
137 | - } |
|
135 | + if ( ! $headers) { |
|
136 | + return $default; |
|
137 | + } |
|
138 | 138 | |
139 | - if (null === $headers[0]) { |
|
140 | - return null; |
|
141 | - } |
|
139 | + if (null === $headers[0]) { |
|
140 | + return null; |
|
141 | + } |
|
142 | 142 | |
143 | - return (string) $headers[0]; |
|
144 | - } |
|
143 | + return (string) $headers[0]; |
|
144 | + } |
|
145 | 145 | |
146 | - /** |
|
147 | - * Sets a header by name. |
|
148 | - * |
|
149 | - * @param string $key The header name |
|
150 | - * @param string|string[]|null $values The value or an array of values |
|
151 | - * @param bool $replace If you want to replace the value exists by the header, |
|
152 | - * it is not overwritten / overwritten when it is false |
|
153 | - * |
|
154 | - * @return void |
|
155 | - */ |
|
156 | - public function set(string $key, $values, bool $replace = true): void |
|
157 | - { |
|
158 | - $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
146 | + /** |
|
147 | + * Sets a header by name. |
|
148 | + * |
|
149 | + * @param string $key The header name |
|
150 | + * @param string|string[]|null $values The value or an array of values |
|
151 | + * @param bool $replace If you want to replace the value exists by the header, |
|
152 | + * it is not overwritten / overwritten when it is false |
|
153 | + * |
|
154 | + * @return void |
|
155 | + */ |
|
156 | + public function set(string $key, $values, bool $replace = true): void |
|
157 | + { |
|
158 | + $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
159 | 159 | |
160 | - if (is_array($values)) { |
|
161 | - $values = array_values($values); |
|
160 | + if (is_array($values)) { |
|
161 | + $values = array_values($values); |
|
162 | 162 | |
163 | - if (true === $replace || ! isset($this->headers[$key])) { |
|
164 | - $this->headers[$key] = $values; |
|
165 | - } else { |
|
166 | - $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
167 | - } |
|
168 | - } else { |
|
169 | - if (true === $replace || ! isset($this->headers[$key])) { |
|
170 | - $this->headers[$key] = [$values]; |
|
171 | - } else { |
|
172 | - $this->headers[$key][] = $values; |
|
173 | - } |
|
174 | - } |
|
163 | + if (true === $replace || ! isset($this->headers[$key])) { |
|
164 | + $this->headers[$key] = $values; |
|
165 | + } else { |
|
166 | + $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
167 | + } |
|
168 | + } else { |
|
169 | + if (true === $replace || ! isset($this->headers[$key])) { |
|
170 | + $this->headers[$key] = [$values]; |
|
171 | + } else { |
|
172 | + $this->headers[$key][] = $values; |
|
173 | + } |
|
174 | + } |
|
175 | 175 | |
176 | - if ('cache-control' === $key) { |
|
177 | - $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key])); |
|
178 | - } |
|
179 | - } |
|
176 | + if ('cache-control' === $key) { |
|
177 | + $this->cacheControl = $this->parseCacheControl(implode(', ', $this->headers[$key])); |
|
178 | + } |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * Returns true if the HTTP header is defined. |
|
183 | - * |
|
184 | - * @param string $key The HTTP header |
|
185 | - * |
|
186 | - * @return bool true if the parameter exists, false otherwise |
|
187 | - */ |
|
188 | - public function has(string $key): bool |
|
189 | - { |
|
190 | - return array_key_exists(strtr($key, self::STRING_UPPER, self::STRING_LOWER), $this->all()); |
|
191 | - } |
|
181 | + /** |
|
182 | + * Returns true if the HTTP header is defined. |
|
183 | + * |
|
184 | + * @param string $key The HTTP header |
|
185 | + * |
|
186 | + * @return bool true if the parameter exists, false otherwise |
|
187 | + */ |
|
188 | + public function has(string $key): bool |
|
189 | + { |
|
190 | + return array_key_exists(strtr($key, self::STRING_UPPER, self::STRING_LOWER), $this->all()); |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Removes a header. |
|
195 | - * |
|
196 | - * @param string $name The header name |
|
197 | - * |
|
198 | - * @return mixed |
|
199 | - */ |
|
200 | - public function remove(string $key) |
|
201 | - { |
|
202 | - $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
193 | + /** |
|
194 | + * Removes a header. |
|
195 | + * |
|
196 | + * @param string $name The header name |
|
197 | + * |
|
198 | + * @return mixed |
|
199 | + */ |
|
200 | + public function remove(string $key) |
|
201 | + { |
|
202 | + $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
203 | 203 | |
204 | - unset($this->headers[$key]); |
|
204 | + unset($this->headers[$key]); |
|
205 | 205 | |
206 | - if ('cache-control' === $key) { |
|
207 | - $this->cacheControl = []; |
|
208 | - } |
|
209 | - } |
|
206 | + if ('cache-control' === $key) { |
|
207 | + $this->cacheControl = []; |
|
208 | + } |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
212 | - * Returns the HTTP header value converted to a date. |
|
213 | - * |
|
214 | - * @param string $key |
|
215 | - * @param DateTime|null $default |
|
216 | - * |
|
217 | - * @throws \RuntimeException When the HTTP header is not parseable |
|
218 | - */ |
|
219 | - public function getDate(string $key, DateTime $default = null): ?DateTimeInterface |
|
220 | - { |
|
221 | - if (null === $value = $this->get($key)) { |
|
222 | - return $default; |
|
223 | - } |
|
211 | + /** |
|
212 | + * Returns the HTTP header value converted to a date. |
|
213 | + * |
|
214 | + * @param string $key |
|
215 | + * @param DateTime|null $default |
|
216 | + * |
|
217 | + * @throws \RuntimeException When the HTTP header is not parseable |
|
218 | + */ |
|
219 | + public function getDate(string $key, DateTime $default = null): ?DateTimeInterface |
|
220 | + { |
|
221 | + if (null === $value = $this->get($key)) { |
|
222 | + return $default; |
|
223 | + } |
|
224 | 224 | |
225 | - if (false === $date = DateTime::createFromFormat(DATE_RFC2822, $value)) { |
|
226 | - throw new RuntimeException(sprintf('The "%s" HTTP header is not parseable (%s).', $key, $value)); |
|
227 | - } |
|
225 | + if (false === $date = DateTime::createFromFormat(DATE_RFC2822, $value)) { |
|
226 | + throw new RuntimeException(sprintf('The "%s" HTTP header is not parseable (%s).', $key, $value)); |
|
227 | + } |
|
228 | 228 | |
229 | - return $date; |
|
230 | - } |
|
229 | + return $date; |
|
230 | + } |
|
231 | 231 | |
232 | - /** |
|
233 | - * Returns an iterator for headers. |
|
234 | - * |
|
235 | - * @return \ArrayIterator An \ArrayIterator instance |
|
236 | - */ |
|
237 | - public function getIterator(): Traversable |
|
238 | - { |
|
239 | - return new ArrayIterator($this->headers); |
|
240 | - } |
|
232 | + /** |
|
233 | + * Returns an iterator for headers. |
|
234 | + * |
|
235 | + * @return \ArrayIterator An \ArrayIterator instance |
|
236 | + */ |
|
237 | + public function getIterator(): Traversable |
|
238 | + { |
|
239 | + return new ArrayIterator($this->headers); |
|
240 | + } |
|
241 | 241 | |
242 | - /** |
|
243 | - * Returns the number of headers. |
|
244 | - * |
|
245 | - * @return int The number of headers |
|
246 | - */ |
|
247 | - public function count(): int |
|
248 | - { |
|
249 | - return count($this->headers); |
|
250 | - } |
|
242 | + /** |
|
243 | + * Returns the number of headers. |
|
244 | + * |
|
245 | + * @return int The number of headers |
|
246 | + */ |
|
247 | + public function count(): int |
|
248 | + { |
|
249 | + return count($this->headers); |
|
250 | + } |
|
251 | 251 | |
252 | - /** |
|
253 | - * Parses a Cache-Control HTTP header. |
|
254 | - * |
|
255 | - * @param string $header The value of the Cache-Control HTTP header |
|
256 | - * |
|
257 | - * @return array An array representing the attribute values |
|
258 | - */ |
|
259 | - protected function parseCacheControl($header): array |
|
260 | - { |
|
261 | - $cacheControl = []; |
|
252 | + /** |
|
253 | + * Parses a Cache-Control HTTP header. |
|
254 | + * |
|
255 | + * @param string $header The value of the Cache-Control HTTP header |
|
256 | + * |
|
257 | + * @return array An array representing the attribute values |
|
258 | + */ |
|
259 | + protected function parseCacheControl($header): array |
|
260 | + { |
|
261 | + $cacheControl = []; |
|
262 | 262 | |
263 | - preg_match_all('~([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?~', $header, $matches, PREG_SET_ORDER); |
|
263 | + preg_match_all('~([a-zA-Z][a-zA-Z_-]*)\s*(?:=(?:"([^"]*)"|([^ \t",;]*)))?~', $header, $matches, PREG_SET_ORDER); |
|
264 | 264 | |
265 | - foreach ($matches as $match) { |
|
266 | - $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); |
|
267 | - } |
|
265 | + foreach ($matches as $match) { |
|
266 | + $cacheControl[strtolower($match[1])] = isset($match[3]) ? $match[3] : (isset($match[2]) ? $match[2] : true); |
|
267 | + } |
|
268 | 268 | |
269 | - return $cacheControl; |
|
270 | - } |
|
269 | + return $cacheControl; |
|
270 | + } |
|
271 | 271 | |
272 | - /** |
|
273 | - * Magic method. |
|
274 | - * |
|
275 | - * Returns the headers as a string. |
|
276 | - * |
|
277 | - * @return string The headers |
|
278 | - */ |
|
279 | - public function __toString(): string |
|
280 | - { |
|
281 | - if ( ! $headers = $this->all()) { |
|
282 | - return ''; |
|
283 | - } |
|
272 | + /** |
|
273 | + * Magic method. |
|
274 | + * |
|
275 | + * Returns the headers as a string. |
|
276 | + * |
|
277 | + * @return string The headers |
|
278 | + */ |
|
279 | + public function __toString(): string |
|
280 | + { |
|
281 | + if ( ! $headers = $this->all()) { |
|
282 | + return ''; |
|
283 | + } |
|
284 | 284 | |
285 | - ksort($headers); |
|
285 | + ksort($headers); |
|
286 | 286 | |
287 | - $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
288 | - $content = ''; |
|
287 | + $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
288 | + $content = ''; |
|
289 | 289 | |
290 | - foreach ($headers as $name => $values) { |
|
291 | - $name = ucwords($name, '-'); |
|
290 | + foreach ($headers as $name => $values) { |
|
291 | + $name = ucwords($name, '-'); |
|
292 | 292 | |
293 | - foreach ($values as $value) { |
|
294 | - $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
295 | - } |
|
296 | - } |
|
293 | + foreach ($values as $value) { |
|
294 | + $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
295 | + } |
|
296 | + } |
|
297 | 297 | |
298 | - return $content; |
|
299 | - } |
|
298 | + return $content; |
|
299 | + } |
|
300 | 300 | } |
301 | 301 | \ No newline at end of file |
@@ -34,199 +34,199 @@ |
||
34 | 34 | */ |
35 | 35 | class Response |
36 | 36 | { |
37 | - use HttpResponse, |
|
37 | + use HttpResponse, |
|
38 | 38 | HttpStatusCode; |
39 | 39 | |
40 | - /** |
|
41 | - * Sets up the response with a content and a status code. |
|
42 | - * |
|
43 | - * @param mixed $content The response content |
|
44 | - * @param int $status The response status |
|
45 | - * @param array $headers Array of HTTP headers for this response |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function __construct($content = '', int $status = 200, array $headers = []) |
|
50 | - { |
|
51 | - $this->headers = new ResponseHeaders($headers); |
|
40 | + /** |
|
41 | + * Sets up the response with a content and a status code. |
|
42 | + * |
|
43 | + * @param mixed $content The response content |
|
44 | + * @param int $status The response status |
|
45 | + * @param array $headers Array of HTTP headers for this response |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function __construct($content = '', int $status = 200, array $headers = []) |
|
50 | + { |
|
51 | + $this->headers = new ResponseHeaders($headers); |
|
52 | 52 | |
53 | - $this->setContent($content); |
|
54 | - $this->setStatusCode($status); |
|
55 | - $this->setProtocolVersion('1.0'); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Creates an instance of the same response class for rendering contents to the content, |
|
60 | - * status code and headers. |
|
61 | - * |
|
62 | - * @param mixed $content The response content |
|
63 | - * @param int $status The HTTP response status for this response |
|
64 | - * @param array $headers Array of HTTP headers for this response |
|
65 | - * |
|
66 | - * @return static |
|
67 | - */ |
|
68 | - public static function render($content = '', $status = 200, $headers = []): static |
|
69 | - { |
|
70 | - return new static($content, $status, $headers); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Gets the current response content. |
|
75 | - * |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getContent(): string |
|
79 | - { |
|
80 | - return $this->content; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Sends the headers if they haven't already been sent. |
|
85 | - * Returns whether they were sent or not. |
|
86 | - * |
|
87 | - * @return static |
|
88 | - */ |
|
89 | - public function sendHeaders(): static |
|
90 | - { |
|
91 | - // Have the headers already been sent? |
|
92 | - if (headers_sent()) { |
|
93 | - return $this; |
|
94 | - } |
|
95 | - |
|
96 | - // Headers |
|
97 | - foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { |
|
98 | - $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
99 | - |
|
100 | - foreach ($values as $value) { |
|
101 | - header($name.': '. $value, $replace, $this->statusCode); |
|
102 | - } |
|
103 | - } |
|
53 | + $this->setContent($content); |
|
54 | + $this->setStatusCode($status); |
|
55 | + $this->setProtocolVersion('1.0'); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Creates an instance of the same response class for rendering contents to the content, |
|
60 | + * status code and headers. |
|
61 | + * |
|
62 | + * @param mixed $content The response content |
|
63 | + * @param int $status The HTTP response status for this response |
|
64 | + * @param array $headers Array of HTTP headers for this response |
|
65 | + * |
|
66 | + * @return static |
|
67 | + */ |
|
68 | + public static function render($content = '', $status = 200, $headers = []): static |
|
69 | + { |
|
70 | + return new static($content, $status, $headers); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Gets the current response content. |
|
75 | + * |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getContent(): string |
|
79 | + { |
|
80 | + return $this->content; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Sends the headers if they haven't already been sent. |
|
85 | + * Returns whether they were sent or not. |
|
86 | + * |
|
87 | + * @return static |
|
88 | + */ |
|
89 | + public function sendHeaders(): static |
|
90 | + { |
|
91 | + // Have the headers already been sent? |
|
92 | + if (headers_sent()) { |
|
93 | + return $this; |
|
94 | + } |
|
95 | + |
|
96 | + // Headers |
|
97 | + foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { |
|
98 | + $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
99 | + |
|
100 | + foreach ($values as $value) { |
|
101 | + header($name.': '. $value, $replace, $this->statusCode); |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - // Cookies |
|
106 | - foreach ($this->headers->getCookies() as $cookie) { |
|
107 | - header('Set-Cookie: '.$cookie, false, $this->statusCode); |
|
108 | - } |
|
105 | + // Cookies |
|
106 | + foreach ($this->headers->getCookies() as $cookie) { |
|
107 | + header('Set-Cookie: '.$cookie, false, $this->statusCode); |
|
108 | + } |
|
109 | 109 | |
110 | - // Status |
|
111 | - if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
112 | - // Send the protocol/status line first, FCGI servers need different status header |
|
113 | - header(sprintf('Status: %s %s', $this->statusCode, $this->statusText)); |
|
114 | - } else { |
|
115 | - header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); |
|
116 | - } |
|
117 | - |
|
118 | - return $this; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Sends content for the current web response. |
|
123 | - * |
|
124 | - * @return static |
|
125 | - */ |
|
126 | - public function sendContent(): static |
|
127 | - { |
|
128 | - echo $this->content; |
|
129 | - |
|
130 | - return $this; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Sends the response to the output buffer. Optionally, headers will be sent. |
|
135 | - * |
|
136 | - * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
137 | - * |
|
138 | - * @return static |
|
139 | - */ |
|
140 | - public function send($sendHeader = false): static |
|
141 | - { |
|
142 | - if ($sendHeader) { |
|
143 | - $this->sendHeaders(); |
|
144 | - } |
|
145 | - |
|
146 | - if (null !== $this->content) { |
|
147 | - $this->sendContent(); |
|
148 | - } |
|
149 | - |
|
150 | - return $this; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sends the content of the message to the browser. |
|
155 | - * |
|
156 | - * @param mixed $content The response content |
|
157 | - * |
|
158 | - * @return static |
|
159 | - */ |
|
160 | - public function setContent($content): static |
|
161 | - { |
|
162 | - if (null !== $content && ! is_string($content) && ! is_numeric($content) && |
|
163 | - ! is_bool($content) && ! is_object($content) && ! is_callable([$content, '__toString'])) { |
|
164 | - throw new UnexpectedValueException( |
|
165 | - sprintf('The Response content must be a string or object implementing __toString(), "%s" given', gettype($content) |
|
166 | - )); |
|
167 | - } |
|
168 | - |
|
169 | - if ($content instanceof JsonSerializable || is_array($content)) { |
|
170 | - $this->header('Content-Type', 'application/json'); |
|
171 | - |
|
172 | - $content = json_encode($content); |
|
173 | - } elseif ($content instanceof Renderable) { |
|
174 | - $content = $content->render(); |
|
175 | - } |
|
110 | + // Status |
|
111 | + if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
112 | + // Send the protocol/status line first, FCGI servers need different status header |
|
113 | + header(sprintf('Status: %s %s', $this->statusCode, $this->statusText)); |
|
114 | + } else { |
|
115 | + header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); |
|
116 | + } |
|
117 | + |
|
118 | + return $this; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Sends content for the current web response. |
|
123 | + * |
|
124 | + * @return static |
|
125 | + */ |
|
126 | + public function sendContent(): static |
|
127 | + { |
|
128 | + echo $this->content; |
|
129 | + |
|
130 | + return $this; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Sends the response to the output buffer. Optionally, headers will be sent. |
|
135 | + * |
|
136 | + * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
137 | + * |
|
138 | + * @return static |
|
139 | + */ |
|
140 | + public function send($sendHeader = false): static |
|
141 | + { |
|
142 | + if ($sendHeader) { |
|
143 | + $this->sendHeaders(); |
|
144 | + } |
|
145 | + |
|
146 | + if (null !== $this->content) { |
|
147 | + $this->sendContent(); |
|
148 | + } |
|
149 | + |
|
150 | + return $this; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sends the content of the message to the browser. |
|
155 | + * |
|
156 | + * @param mixed $content The response content |
|
157 | + * |
|
158 | + * @return static |
|
159 | + */ |
|
160 | + public function setContent($content): static |
|
161 | + { |
|
162 | + if (null !== $content && ! is_string($content) && ! is_numeric($content) && |
|
163 | + ! is_bool($content) && ! is_object($content) && ! is_callable([$content, '__toString'])) { |
|
164 | + throw new UnexpectedValueException( |
|
165 | + sprintf('The Response content must be a string or object implementing __toString(), "%s" given', gettype($content) |
|
166 | + )); |
|
167 | + } |
|
168 | + |
|
169 | + if ($content instanceof JsonSerializable || is_array($content)) { |
|
170 | + $this->header('Content-Type', 'application/json'); |
|
171 | + |
|
172 | + $content = json_encode($content); |
|
173 | + } elseif ($content instanceof Renderable) { |
|
174 | + $content = $content->render(); |
|
175 | + } |
|
176 | 176 | |
177 | - $this->content = $content ?? ''; |
|
178 | - |
|
179 | - return $this; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Prepares the Response before it is sent to the client. |
|
184 | - * |
|
185 | - * @param \Syscodes\Components\Http\Request $request |
|
186 | - * |
|
187 | - * @return static |
|
188 | - */ |
|
189 | - public function prepare($request): static |
|
190 | - { |
|
191 | - $headers = $this->headers; |
|
192 | - |
|
193 | - if ($this->isInformational() || $this->isEmpty()) { |
|
194 | - $this->setContent(null); |
|
195 | - $headers->remove('Content-Type'); |
|
196 | - $headers->remove('Content-Length'); |
|
197 | - } |
|
177 | + $this->content = $content ?? ''; |
|
178 | + |
|
179 | + return $this; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Prepares the Response before it is sent to the client. |
|
184 | + * |
|
185 | + * @param \Syscodes\Components\Http\Request $request |
|
186 | + * |
|
187 | + * @return static |
|
188 | + */ |
|
189 | + public function prepare($request): static |
|
190 | + { |
|
191 | + $headers = $this->headers; |
|
192 | + |
|
193 | + if ($this->isInformational() || $this->isEmpty()) { |
|
194 | + $this->setContent(null); |
|
195 | + $headers->remove('Content-Type'); |
|
196 | + $headers->remove('Content-Length'); |
|
197 | + } |
|
198 | 198 | |
199 | - // Fix protocol |
|
200 | - if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) { |
|
201 | - $this->setProtocolVersion('1.1'); |
|
202 | - } |
|
199 | + // Fix protocol |
|
200 | + if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) { |
|
201 | + $this->setProtocolVersion('1.1'); |
|
202 | + } |
|
203 | 203 | |
204 | - return $this; |
|
205 | - } |
|
204 | + return $this; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Magic method. |
|
209 | - * |
|
210 | - * Returns the Response as an HTTP string. |
|
211 | - * |
|
212 | - * @return string |
|
213 | - */ |
|
214 | - public function __toString(): string |
|
215 | - { |
|
216 | - return sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". |
|
217 | - $this->headers."\r\n". |
|
218 | - $this->getContent(); |
|
219 | - } |
|
207 | + /** |
|
208 | + * Magic method. |
|
209 | + * |
|
210 | + * Returns the Response as an HTTP string. |
|
211 | + * |
|
212 | + * @return string |
|
213 | + */ |
|
214 | + public function __toString(): string |
|
215 | + { |
|
216 | + return sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". |
|
217 | + $this->headers."\r\n". |
|
218 | + $this->getContent(); |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Magic method. |
|
223 | - * |
|
224 | - * Clone the current Response instance. |
|
225 | - * |
|
226 | - * @return void |
|
227 | - */ |
|
228 | - public function __clone() |
|
229 | - { |
|
230 | - $this->headers = clone $this->headers; |
|
231 | - } |
|
221 | + /** |
|
222 | + * Magic method. |
|
223 | + * |
|
224 | + * Clone the current Response instance. |
|
225 | + * |
|
226 | + * @return void |
|
227 | + */ |
|
228 | + public function __clone() |
|
229 | + { |
|
230 | + $this->headers = clone $this->headers; |
|
231 | + } |
|
232 | 232 | } |
233 | 233 | \ No newline at end of file |
@@ -31,499 +31,499 @@ |
||
31 | 31 | */ |
32 | 32 | class URI |
33 | 33 | { |
34 | - /** |
|
35 | - * Returns default schemes/ports. |
|
36 | - * |
|
37 | - * @var array $defaultPorts |
|
38 | - */ |
|
39 | - protected $defaultPorts = [ |
|
40 | - 'http' => 80, |
|
41 | - 'https' => 443, |
|
42 | - 'ftp' => 21, |
|
43 | - 'sftp' => 22 |
|
44 | - ]; |
|
45 | - |
|
46 | - /** |
|
47 | - * The name of any fragment. |
|
48 | - * |
|
49 | - * @var string $fragment |
|
50 | - */ |
|
51 | - protected $fragment = ''; |
|
52 | - |
|
53 | - /** |
|
54 | - * The URI Host. |
|
55 | - * |
|
56 | - * @var string $host |
|
57 | - */ |
|
58 | - protected $host; |
|
34 | + /** |
|
35 | + * Returns default schemes/ports. |
|
36 | + * |
|
37 | + * @var array $defaultPorts |
|
38 | + */ |
|
39 | + protected $defaultPorts = [ |
|
40 | + 'http' => 80, |
|
41 | + 'https' => 443, |
|
42 | + 'ftp' => 21, |
|
43 | + 'sftp' => 22 |
|
44 | + ]; |
|
45 | + |
|
46 | + /** |
|
47 | + * The name of any fragment. |
|
48 | + * |
|
49 | + * @var string $fragment |
|
50 | + */ |
|
51 | + protected $fragment = ''; |
|
52 | + |
|
53 | + /** |
|
54 | + * The URI Host. |
|
55 | + * |
|
56 | + * @var string $host |
|
57 | + */ |
|
58 | + protected $host; |
|
59 | 59 | |
60 | - /** |
|
61 | - * The URI User Password. |
|
62 | - * |
|
63 | - * @var string $password |
|
64 | - */ |
|
65 | - protected $password; |
|
66 | - |
|
67 | - /** |
|
68 | - * The URI path. |
|
69 | - * |
|
70 | - * @var string $path |
|
71 | - */ |
|
72 | - protected $path; |
|
73 | - |
|
74 | - /** |
|
75 | - * The URI Port. |
|
76 | - * |
|
77 | - * @var int $port |
|
78 | - */ |
|
79 | - protected $port; |
|
80 | - |
|
81 | - /** |
|
82 | - * The query string. |
|
83 | - * |
|
84 | - * @var string $query |
|
85 | - */ |
|
86 | - protected $query; |
|
87 | - |
|
88 | - /** |
|
89 | - * The URI Scheme. |
|
90 | - * |
|
91 | - * @var string $scheme |
|
92 | - */ |
|
93 | - protected $scheme = 'http'; |
|
94 | - |
|
95 | - /** |
|
96 | - * Whether passwords should be shown in userInfo/authority calls. |
|
97 | - * |
|
98 | - * @var boolean $showPassword |
|
99 | - */ |
|
100 | - protected $showPassword = false; |
|
60 | + /** |
|
61 | + * The URI User Password. |
|
62 | + * |
|
63 | + * @var string $password |
|
64 | + */ |
|
65 | + protected $password; |
|
66 | + |
|
67 | + /** |
|
68 | + * The URI path. |
|
69 | + * |
|
70 | + * @var string $path |
|
71 | + */ |
|
72 | + protected $path; |
|
73 | + |
|
74 | + /** |
|
75 | + * The URI Port. |
|
76 | + * |
|
77 | + * @var int $port |
|
78 | + */ |
|
79 | + protected $port; |
|
80 | + |
|
81 | + /** |
|
82 | + * The query string. |
|
83 | + * |
|
84 | + * @var string $query |
|
85 | + */ |
|
86 | + protected $query; |
|
87 | + |
|
88 | + /** |
|
89 | + * The URI Scheme. |
|
90 | + * |
|
91 | + * @var string $scheme |
|
92 | + */ |
|
93 | + protected $scheme = 'http'; |
|
94 | + |
|
95 | + /** |
|
96 | + * Whether passwords should be shown in userInfo/authority calls. |
|
97 | + * |
|
98 | + * @var boolean $showPassword |
|
99 | + */ |
|
100 | + protected $showPassword = false; |
|
101 | 101 | |
102 | - /** |
|
103 | - * The URI User Info. |
|
104 | - * |
|
105 | - * @var string $user |
|
106 | - */ |
|
107 | - protected $user; |
|
108 | - |
|
109 | - /** |
|
110 | - * Constructor. The URI class instance. |
|
111 | - * |
|
112 | - * @param string|null $uri |
|
113 | - * |
|
114 | - * @return void |
|
115 | - * |
|
116 | - * @throws \Syscodes\Components\Http\Exceptions\HttpURIException |
|
117 | - */ |
|
118 | - public function __construct(string $uri = null) |
|
119 | - { |
|
120 | - if ( ! is_null($uri)) { |
|
121 | - $this->setUri($uri); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Sets and overwrites any current URI information. |
|
127 | - * |
|
128 | - * @param string|null $uri |
|
129 | - * |
|
130 | - * @return static |
|
131 | - * |
|
132 | - * @throws \Syscodes\Components\Http\Exceptions\HttpURIException |
|
133 | - */ |
|
134 | - public function setUri(string $uri = null): static |
|
135 | - { |
|
136 | - if ( ! is_null($uri)) { |
|
137 | - $parts = parse_url($uri); |
|
138 | - |
|
139 | - if ($parts === false) { |
|
140 | - throw HttpURIException::UnableToParseURI($uri); |
|
141 | - } |
|
142 | - |
|
143 | - $this->applyParts($parts); |
|
144 | - } |
|
145 | - |
|
146 | - return $this; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * Returns the full URI string. |
|
151 | - * |
|
152 | - * @return string The URI string |
|
153 | - */ |
|
154 | - public function get(): string |
|
155 | - { |
|
156 | - return '/'.ltrim($this->path, '/'); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Sets of URI string. |
|
161 | - * |
|
162 | - * @param string $uri |
|
163 | - * |
|
164 | - * @return static |
|
165 | - */ |
|
166 | - public function set(string $uri): static |
|
167 | - { |
|
168 | - $this->path = $uri; |
|
169 | - |
|
170 | - return $this; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Retrieve the path component of the URI. The path can either be empty or absolute |
|
175 | - * (starting with a slash) or rootless (not starting with a slash). |
|
176 | - * |
|
177 | - * @return string |
|
178 | - */ |
|
179 | - public function getPath(): string |
|
180 | - { |
|
181 | - return (is_null($this->path) ? '' : $this->path); |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Sets the path portion of the URI. |
|
186 | - * |
|
187 | - * @param string $uri |
|
188 | - * |
|
189 | - * @return array |
|
190 | - */ |
|
191 | - public function setPath(string $uri): array |
|
192 | - { |
|
193 | - $this->path = $this->filterPath($uri); |
|
194 | - |
|
195 | - $tempPath = trim($this->path, '/'); |
|
196 | - |
|
197 | - return $this->filterSegments($tempPath); |
|
198 | - } |
|
199 | - |
|
200 | - /** |
|
201 | - * Encodes any dangerous characters. |
|
202 | - * |
|
203 | - * @param string $uri |
|
204 | - * |
|
205 | - * @return string |
|
206 | - */ |
|
207 | - protected function filterPath(string $uri): string |
|
208 | - { |
|
209 | - return urldecode($uri); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Filter the segments of path. |
|
214 | - * |
|
215 | - * @param string $uri |
|
216 | - * |
|
217 | - * @return string[] |
|
218 | - */ |
|
219 | - protected function filterSegments(string $uri): array |
|
220 | - { |
|
221 | - return ($uri == '') ? [] : explode('/', $uri); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Get the specified URI segment, return default if it doesn't exist. |
|
226 | - * Segment index is 1 based, not 0 based. |
|
227 | - * |
|
228 | - * @param int $index The 1-based segment index |
|
229 | - * @param mixed $default The default value |
|
230 | - * |
|
231 | - * @return mixed |
|
232 | - */ |
|
233 | - public function getSegment(int $index, $default = null): mixed |
|
234 | - { |
|
235 | - return Arr::get($this->getSegments(), $index - 1, $default); |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Returns the segments of the path as an array. |
|
240 | - * |
|
241 | - * @return array The URI segments |
|
242 | - */ |
|
243 | - public function getSegments(): array |
|
244 | - { |
|
245 | - $segments = $this->setPath(Request::decodedPath()); |
|
246 | - |
|
247 | - return array_values(array_filter($segments, fn ($value) => $value != '')); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Returns the total number of segment. |
|
252 | - * |
|
253 | - * @return int |
|
254 | - */ |
|
255 | - public function getTotalSegments(): int |
|
256 | - { |
|
257 | - return count($this->getSegments()); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Retrieve the scheme component of the URI. |
|
262 | - * |
|
263 | - * @return string |
|
264 | - */ |
|
265 | - public function getScheme(): string |
|
266 | - { |
|
267 | - return $this->scheme; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Sets the scheme for this URI. |
|
272 | - * |
|
273 | - * @param string $str |
|
274 | - * |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public function setScheme(string $str): string |
|
278 | - { |
|
279 | - $str = preg_replace('~:(//)?$~', '', strtolower($str)); |
|
280 | - |
|
281 | - $this->scheme = $str; |
|
282 | - |
|
283 | - return $this->scheme; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Retrieve the user component of the URI. |
|
288 | - * |
|
289 | - * @return string|null |
|
290 | - */ |
|
291 | - public function getUserInfo() |
|
292 | - { |
|
293 | - $user = $this->user; |
|
294 | - $pass = $this->password; |
|
295 | - |
|
296 | - if ($this->showPassword === true && ! empty($pass)) { |
|
297 | - $user .= ":$pass"; |
|
298 | - } |
|
299 | - |
|
300 | - return $user; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * Sets the user portion of the URI. |
|
305 | - * |
|
306 | - * @param string $user |
|
307 | - * |
|
308 | - * @return string|null |
|
309 | - */ |
|
310 | - public function setUser($user): string |
|
311 | - { |
|
312 | - $this->user = trim($user); |
|
313 | - |
|
314 | - return $this->user; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Sets the password portion of the URI. |
|
319 | - * |
|
320 | - * @param string $password |
|
321 | - * |
|
322 | - * @return string|null |
|
323 | - */ |
|
324 | - public function setPassword($password): string |
|
325 | - { |
|
326 | - $this->password = trim($password); |
|
327 | - |
|
328 | - return $this->password; |
|
329 | - } |
|
330 | - |
|
331 | - /** |
|
332 | - * Temporarily sets the URI to show a password in userInfo. |
|
333 | - * |
|
334 | - * @param boolean $option |
|
335 | - * |
|
336 | - * @return static |
|
337 | - */ |
|
338 | - public function showPassword(bool $option = true): static |
|
339 | - { |
|
340 | - $this->showPassword = $option; |
|
341 | - |
|
342 | - return $this; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Retrieve the authority component of the URI. |
|
347 | - * |
|
348 | - * @param boolean $ignore |
|
349 | - * |
|
350 | - * @return string |
|
351 | - */ |
|
352 | - public function getAuthority(bool $ignore = false): string |
|
353 | - { |
|
354 | - if (empty($this->host)) { |
|
355 | - return ''; |
|
356 | - } |
|
357 | - |
|
358 | - $authority = $this->host; |
|
359 | - |
|
360 | - if ( ! empty($this->getUserInfo())) { |
|
361 | - $authority = $this->getUserInfo().'@'.$authority; |
|
362 | - } |
|
363 | - |
|
364 | - if ( ! empty($this->port) && ! $ignore) { |
|
365 | - if ($this->port !== $this->defaultPorts[$this->scheme]) { |
|
366 | - $authority .= ":$this->port"; |
|
367 | - } |
|
368 | - } |
|
369 | - |
|
370 | - $this->showPassword = false; |
|
371 | - |
|
372 | - return $authority; |
|
373 | - } |
|
374 | - |
|
375 | - /** |
|
376 | - * Parses the given string an saves the appropriate authority pieces. |
|
377 | - * |
|
378 | - * @param string $str |
|
379 | - * |
|
380 | - * @return static |
|
381 | - */ |
|
382 | - public function setAuthority(string $str): static |
|
383 | - { |
|
384 | - $parts = parse_url($str); |
|
385 | - |
|
386 | - if (empty($parts['host']) && ! empty($parts['path'])) { |
|
387 | - $parts['host'] = $parts['path']; |
|
388 | - unset($parts['path']); |
|
389 | - } |
|
390 | - |
|
391 | - $this->applyParts($parts); |
|
392 | - |
|
393 | - return $this; |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * Retrieve the host component of the URI. |
|
398 | - * |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - public function getHost(): string |
|
402 | - { |
|
403 | - return $this->host; |
|
404 | - } |
|
405 | - |
|
406 | - /** |
|
407 | - * Sets the host name to use. |
|
408 | - * |
|
409 | - * @param string $str |
|
410 | - * |
|
411 | - * @return string |
|
412 | - */ |
|
413 | - public function setHost(string $str): string |
|
414 | - { |
|
415 | - $this->host = trim($str); |
|
416 | - |
|
417 | - return $this->host; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Retrieve the port component of the URI. |
|
422 | - * |
|
423 | - * @return int|null |
|
424 | - */ |
|
425 | - public function getPort() |
|
426 | - { |
|
427 | - return $this->port; |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * Sets the port portion of the URI. |
|
432 | - * |
|
433 | - * @param int|null $port |
|
434 | - * |
|
435 | - * @return string |
|
436 | - */ |
|
437 | - public function setPort(int $port = null): string |
|
438 | - { |
|
439 | - if (is_null($port)) { |
|
440 | - return $this; |
|
441 | - } |
|
442 | - |
|
443 | - if ($port <= 0 || $port > 65355) { |
|
444 | - throw HttpURIException::invalidPort($port); |
|
445 | - } |
|
446 | - |
|
447 | - $this->port = $port; |
|
448 | - |
|
449 | - return $this->port; |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Retrieve a URI fragment. |
|
454 | - * |
|
455 | - * @return string |
|
456 | - */ |
|
457 | - public function getFragment(): string |
|
458 | - { |
|
459 | - return is_null($this->fragment) ? '' : $this->fragment; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Sets the fragment portion of the URI. |
|
464 | - * |
|
465 | - * @param string $str |
|
466 | - * |
|
467 | - * @return string |
|
468 | - */ |
|
469 | - public function setFragment(string $str): string |
|
470 | - { |
|
471 | - $this->fragment = trim($str, '# '); |
|
472 | - |
|
473 | - return $this->fragment; |
|
474 | - } |
|
475 | - |
|
476 | - /** |
|
477 | - * Saves our parts from a parse_url call. |
|
478 | - * |
|
479 | - * @param array $parts |
|
480 | - * |
|
481 | - * @return mixed |
|
482 | - */ |
|
483 | - public function applyParts(array $paths) |
|
484 | - { |
|
485 | - if (isset($parts['scheme'])) { |
|
486 | - $this->SetScheme(rtrim($parts['scheme'], ':/')); |
|
487 | - } else { |
|
488 | - $this->setScheme('http'); |
|
489 | - } |
|
490 | - |
|
491 | - if ( ! empty($parts['host'])) { |
|
492 | - $this->host = $parts['host']; |
|
493 | - } |
|
494 | - |
|
495 | - if (isset($parts['port'])) { |
|
496 | - if ( ! is_null($parts['port'])) { |
|
497 | - $this->port = $parts['port']; |
|
498 | - } |
|
499 | - } |
|
500 | - |
|
501 | - if ( ! empty($parts['user'])) { |
|
502 | - $this->user = $parts['user']; |
|
503 | - } |
|
504 | - |
|
505 | - if ( ! empty($parts['pass'])) { |
|
506 | - $this->password = $parts['pass']; |
|
507 | - } |
|
508 | - |
|
509 | - if ( ! empty($parts['path'])) { |
|
510 | - $this->path = $this->filterPath($parts['path']); |
|
511 | - } |
|
512 | - |
|
513 | - if ( ! empty($parts['fragment'])) { |
|
514 | - $this->fragment = $parts['fragment']; |
|
515 | - } |
|
516 | - } |
|
517 | - |
|
518 | - /** |
|
519 | - * Magic method. |
|
520 | - * |
|
521 | - * Returns the URI string. |
|
522 | - * |
|
523 | - * @return string |
|
524 | - */ |
|
525 | - public function __toString(): string |
|
526 | - { |
|
527 | - return (string) $this->getPath(); |
|
528 | - } |
|
102 | + /** |
|
103 | + * The URI User Info. |
|
104 | + * |
|
105 | + * @var string $user |
|
106 | + */ |
|
107 | + protected $user; |
|
108 | + |
|
109 | + /** |
|
110 | + * Constructor. The URI class instance. |
|
111 | + * |
|
112 | + * @param string|null $uri |
|
113 | + * |
|
114 | + * @return void |
|
115 | + * |
|
116 | + * @throws \Syscodes\Components\Http\Exceptions\HttpURIException |
|
117 | + */ |
|
118 | + public function __construct(string $uri = null) |
|
119 | + { |
|
120 | + if ( ! is_null($uri)) { |
|
121 | + $this->setUri($uri); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Sets and overwrites any current URI information. |
|
127 | + * |
|
128 | + * @param string|null $uri |
|
129 | + * |
|
130 | + * @return static |
|
131 | + * |
|
132 | + * @throws \Syscodes\Components\Http\Exceptions\HttpURIException |
|
133 | + */ |
|
134 | + public function setUri(string $uri = null): static |
|
135 | + { |
|
136 | + if ( ! is_null($uri)) { |
|
137 | + $parts = parse_url($uri); |
|
138 | + |
|
139 | + if ($parts === false) { |
|
140 | + throw HttpURIException::UnableToParseURI($uri); |
|
141 | + } |
|
142 | + |
|
143 | + $this->applyParts($parts); |
|
144 | + } |
|
145 | + |
|
146 | + return $this; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * Returns the full URI string. |
|
151 | + * |
|
152 | + * @return string The URI string |
|
153 | + */ |
|
154 | + public function get(): string |
|
155 | + { |
|
156 | + return '/'.ltrim($this->path, '/'); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Sets of URI string. |
|
161 | + * |
|
162 | + * @param string $uri |
|
163 | + * |
|
164 | + * @return static |
|
165 | + */ |
|
166 | + public function set(string $uri): static |
|
167 | + { |
|
168 | + $this->path = $uri; |
|
169 | + |
|
170 | + return $this; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Retrieve the path component of the URI. The path can either be empty or absolute |
|
175 | + * (starting with a slash) or rootless (not starting with a slash). |
|
176 | + * |
|
177 | + * @return string |
|
178 | + */ |
|
179 | + public function getPath(): string |
|
180 | + { |
|
181 | + return (is_null($this->path) ? '' : $this->path); |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Sets the path portion of the URI. |
|
186 | + * |
|
187 | + * @param string $uri |
|
188 | + * |
|
189 | + * @return array |
|
190 | + */ |
|
191 | + public function setPath(string $uri): array |
|
192 | + { |
|
193 | + $this->path = $this->filterPath($uri); |
|
194 | + |
|
195 | + $tempPath = trim($this->path, '/'); |
|
196 | + |
|
197 | + return $this->filterSegments($tempPath); |
|
198 | + } |
|
199 | + |
|
200 | + /** |
|
201 | + * Encodes any dangerous characters. |
|
202 | + * |
|
203 | + * @param string $uri |
|
204 | + * |
|
205 | + * @return string |
|
206 | + */ |
|
207 | + protected function filterPath(string $uri): string |
|
208 | + { |
|
209 | + return urldecode($uri); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Filter the segments of path. |
|
214 | + * |
|
215 | + * @param string $uri |
|
216 | + * |
|
217 | + * @return string[] |
|
218 | + */ |
|
219 | + protected function filterSegments(string $uri): array |
|
220 | + { |
|
221 | + return ($uri == '') ? [] : explode('/', $uri); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Get the specified URI segment, return default if it doesn't exist. |
|
226 | + * Segment index is 1 based, not 0 based. |
|
227 | + * |
|
228 | + * @param int $index The 1-based segment index |
|
229 | + * @param mixed $default The default value |
|
230 | + * |
|
231 | + * @return mixed |
|
232 | + */ |
|
233 | + public function getSegment(int $index, $default = null): mixed |
|
234 | + { |
|
235 | + return Arr::get($this->getSegments(), $index - 1, $default); |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Returns the segments of the path as an array. |
|
240 | + * |
|
241 | + * @return array The URI segments |
|
242 | + */ |
|
243 | + public function getSegments(): array |
|
244 | + { |
|
245 | + $segments = $this->setPath(Request::decodedPath()); |
|
246 | + |
|
247 | + return array_values(array_filter($segments, fn ($value) => $value != '')); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Returns the total number of segment. |
|
252 | + * |
|
253 | + * @return int |
|
254 | + */ |
|
255 | + public function getTotalSegments(): int |
|
256 | + { |
|
257 | + return count($this->getSegments()); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Retrieve the scheme component of the URI. |
|
262 | + * |
|
263 | + * @return string |
|
264 | + */ |
|
265 | + public function getScheme(): string |
|
266 | + { |
|
267 | + return $this->scheme; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Sets the scheme for this URI. |
|
272 | + * |
|
273 | + * @param string $str |
|
274 | + * |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public function setScheme(string $str): string |
|
278 | + { |
|
279 | + $str = preg_replace('~:(//)?$~', '', strtolower($str)); |
|
280 | + |
|
281 | + $this->scheme = $str; |
|
282 | + |
|
283 | + return $this->scheme; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Retrieve the user component of the URI. |
|
288 | + * |
|
289 | + * @return string|null |
|
290 | + */ |
|
291 | + public function getUserInfo() |
|
292 | + { |
|
293 | + $user = $this->user; |
|
294 | + $pass = $this->password; |
|
295 | + |
|
296 | + if ($this->showPassword === true && ! empty($pass)) { |
|
297 | + $user .= ":$pass"; |
|
298 | + } |
|
299 | + |
|
300 | + return $user; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * Sets the user portion of the URI. |
|
305 | + * |
|
306 | + * @param string $user |
|
307 | + * |
|
308 | + * @return string|null |
|
309 | + */ |
|
310 | + public function setUser($user): string |
|
311 | + { |
|
312 | + $this->user = trim($user); |
|
313 | + |
|
314 | + return $this->user; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Sets the password portion of the URI. |
|
319 | + * |
|
320 | + * @param string $password |
|
321 | + * |
|
322 | + * @return string|null |
|
323 | + */ |
|
324 | + public function setPassword($password): string |
|
325 | + { |
|
326 | + $this->password = trim($password); |
|
327 | + |
|
328 | + return $this->password; |
|
329 | + } |
|
330 | + |
|
331 | + /** |
|
332 | + * Temporarily sets the URI to show a password in userInfo. |
|
333 | + * |
|
334 | + * @param boolean $option |
|
335 | + * |
|
336 | + * @return static |
|
337 | + */ |
|
338 | + public function showPassword(bool $option = true): static |
|
339 | + { |
|
340 | + $this->showPassword = $option; |
|
341 | + |
|
342 | + return $this; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Retrieve the authority component of the URI. |
|
347 | + * |
|
348 | + * @param boolean $ignore |
|
349 | + * |
|
350 | + * @return string |
|
351 | + */ |
|
352 | + public function getAuthority(bool $ignore = false): string |
|
353 | + { |
|
354 | + if (empty($this->host)) { |
|
355 | + return ''; |
|
356 | + } |
|
357 | + |
|
358 | + $authority = $this->host; |
|
359 | + |
|
360 | + if ( ! empty($this->getUserInfo())) { |
|
361 | + $authority = $this->getUserInfo().'@'.$authority; |
|
362 | + } |
|
363 | + |
|
364 | + if ( ! empty($this->port) && ! $ignore) { |
|
365 | + if ($this->port !== $this->defaultPorts[$this->scheme]) { |
|
366 | + $authority .= ":$this->port"; |
|
367 | + } |
|
368 | + } |
|
369 | + |
|
370 | + $this->showPassword = false; |
|
371 | + |
|
372 | + return $authority; |
|
373 | + } |
|
374 | + |
|
375 | + /** |
|
376 | + * Parses the given string an saves the appropriate authority pieces. |
|
377 | + * |
|
378 | + * @param string $str |
|
379 | + * |
|
380 | + * @return static |
|
381 | + */ |
|
382 | + public function setAuthority(string $str): static |
|
383 | + { |
|
384 | + $parts = parse_url($str); |
|
385 | + |
|
386 | + if (empty($parts['host']) && ! empty($parts['path'])) { |
|
387 | + $parts['host'] = $parts['path']; |
|
388 | + unset($parts['path']); |
|
389 | + } |
|
390 | + |
|
391 | + $this->applyParts($parts); |
|
392 | + |
|
393 | + return $this; |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * Retrieve the host component of the URI. |
|
398 | + * |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + public function getHost(): string |
|
402 | + { |
|
403 | + return $this->host; |
|
404 | + } |
|
405 | + |
|
406 | + /** |
|
407 | + * Sets the host name to use. |
|
408 | + * |
|
409 | + * @param string $str |
|
410 | + * |
|
411 | + * @return string |
|
412 | + */ |
|
413 | + public function setHost(string $str): string |
|
414 | + { |
|
415 | + $this->host = trim($str); |
|
416 | + |
|
417 | + return $this->host; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Retrieve the port component of the URI. |
|
422 | + * |
|
423 | + * @return int|null |
|
424 | + */ |
|
425 | + public function getPort() |
|
426 | + { |
|
427 | + return $this->port; |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * Sets the port portion of the URI. |
|
432 | + * |
|
433 | + * @param int|null $port |
|
434 | + * |
|
435 | + * @return string |
|
436 | + */ |
|
437 | + public function setPort(int $port = null): string |
|
438 | + { |
|
439 | + if (is_null($port)) { |
|
440 | + return $this; |
|
441 | + } |
|
442 | + |
|
443 | + if ($port <= 0 || $port > 65355) { |
|
444 | + throw HttpURIException::invalidPort($port); |
|
445 | + } |
|
446 | + |
|
447 | + $this->port = $port; |
|
448 | + |
|
449 | + return $this->port; |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Retrieve a URI fragment. |
|
454 | + * |
|
455 | + * @return string |
|
456 | + */ |
|
457 | + public function getFragment(): string |
|
458 | + { |
|
459 | + return is_null($this->fragment) ? '' : $this->fragment; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Sets the fragment portion of the URI. |
|
464 | + * |
|
465 | + * @param string $str |
|
466 | + * |
|
467 | + * @return string |
|
468 | + */ |
|
469 | + public function setFragment(string $str): string |
|
470 | + { |
|
471 | + $this->fragment = trim($str, '# '); |
|
472 | + |
|
473 | + return $this->fragment; |
|
474 | + } |
|
475 | + |
|
476 | + /** |
|
477 | + * Saves our parts from a parse_url call. |
|
478 | + * |
|
479 | + * @param array $parts |
|
480 | + * |
|
481 | + * @return mixed |
|
482 | + */ |
|
483 | + public function applyParts(array $paths) |
|
484 | + { |
|
485 | + if (isset($parts['scheme'])) { |
|
486 | + $this->SetScheme(rtrim($parts['scheme'], ':/')); |
|
487 | + } else { |
|
488 | + $this->setScheme('http'); |
|
489 | + } |
|
490 | + |
|
491 | + if ( ! empty($parts['host'])) { |
|
492 | + $this->host = $parts['host']; |
|
493 | + } |
|
494 | + |
|
495 | + if (isset($parts['port'])) { |
|
496 | + if ( ! is_null($parts['port'])) { |
|
497 | + $this->port = $parts['port']; |
|
498 | + } |
|
499 | + } |
|
500 | + |
|
501 | + if ( ! empty($parts['user'])) { |
|
502 | + $this->user = $parts['user']; |
|
503 | + } |
|
504 | + |
|
505 | + if ( ! empty($parts['pass'])) { |
|
506 | + $this->password = $parts['pass']; |
|
507 | + } |
|
508 | + |
|
509 | + if ( ! empty($parts['path'])) { |
|
510 | + $this->path = $this->filterPath($parts['path']); |
|
511 | + } |
|
512 | + |
|
513 | + if ( ! empty($parts['fragment'])) { |
|
514 | + $this->fragment = $parts['fragment']; |
|
515 | + } |
|
516 | + } |
|
517 | + |
|
518 | + /** |
|
519 | + * Magic method. |
|
520 | + * |
|
521 | + * Returns the URI string. |
|
522 | + * |
|
523 | + * @return string |
|
524 | + */ |
|
525 | + public function __toString(): string |
|
526 | + { |
|
527 | + return (string) $this->getPath(); |
|
528 | + } |
|
529 | 529 | } |
530 | 530 | \ No newline at end of file |
@@ -48,560 +48,560 @@ discard block |
||
48 | 48 | */ |
49 | 49 | class Request |
50 | 50 | { |
51 | - use HttpRequest, |
|
52 | - HttpResources, |
|
53 | - CanBePrecognitive, |
|
54 | - InteractsWithInput, |
|
55 | - InteractsWithContentTypes; |
|
56 | - |
|
57 | - /** |
|
58 | - * Get the acceptable of content types. |
|
59 | - * |
|
60 | - * @var string[] $acceptableContenTypes |
|
61 | - */ |
|
62 | - protected $acceptableContentTypes; |
|
63 | - |
|
64 | - /** |
|
65 | - * Get the custom parameters. |
|
66 | - * |
|
67 | - * @var \Syscodes\Components\Http\Loaders\Parameters $attributes |
|
68 | - */ |
|
69 | - public $attributes; |
|
70 | - |
|
71 | - /** |
|
72 | - * The base URL. |
|
73 | - * |
|
74 | - * @var string $baseUrl |
|
75 | - */ |
|
76 | - protected $baseUrl; |
|
77 | - |
|
78 | - /** |
|
79 | - * Get the client ip. |
|
80 | - * |
|
81 | - * @var mixed $clientIp |
|
82 | - */ |
|
83 | - protected $clientIp; |
|
84 | - |
|
85 | - /** |
|
86 | - * Gets cookies ($_COOKIE). |
|
87 | - * |
|
88 | - * @var \Syscodes\Components\Http\Loaders\Inputs $cookies |
|
89 | - */ |
|
90 | - public $cookies; |
|
91 | - |
|
92 | - /** |
|
93 | - * Gets the string with format JSON. |
|
94 | - * |
|
95 | - * @var string|resource|object|null $content |
|
96 | - */ |
|
97 | - protected $content; |
|
98 | - |
|
99 | - /** |
|
100 | - * The default Locale this request. |
|
101 | - * |
|
102 | - * @var string $defaultLocale |
|
103 | - */ |
|
104 | - protected $defaultLocale = 'en'; |
|
51 | + use HttpRequest, |
|
52 | + HttpResources, |
|
53 | + CanBePrecognitive, |
|
54 | + InteractsWithInput, |
|
55 | + InteractsWithContentTypes; |
|
56 | + |
|
57 | + /** |
|
58 | + * Get the acceptable of content types. |
|
59 | + * |
|
60 | + * @var string[] $acceptableContenTypes |
|
61 | + */ |
|
62 | + protected $acceptableContentTypes; |
|
63 | + |
|
64 | + /** |
|
65 | + * Get the custom parameters. |
|
66 | + * |
|
67 | + * @var \Syscodes\Components\Http\Loaders\Parameters $attributes |
|
68 | + */ |
|
69 | + public $attributes; |
|
70 | + |
|
71 | + /** |
|
72 | + * The base URL. |
|
73 | + * |
|
74 | + * @var string $baseUrl |
|
75 | + */ |
|
76 | + protected $baseUrl; |
|
77 | + |
|
78 | + /** |
|
79 | + * Get the client ip. |
|
80 | + * |
|
81 | + * @var mixed $clientIp |
|
82 | + */ |
|
83 | + protected $clientIp; |
|
84 | + |
|
85 | + /** |
|
86 | + * Gets cookies ($_COOKIE). |
|
87 | + * |
|
88 | + * @var \Syscodes\Components\Http\Loaders\Inputs $cookies |
|
89 | + */ |
|
90 | + public $cookies; |
|
91 | + |
|
92 | + /** |
|
93 | + * Gets the string with format JSON. |
|
94 | + * |
|
95 | + * @var string|resource|object|null $content |
|
96 | + */ |
|
97 | + protected $content; |
|
98 | + |
|
99 | + /** |
|
100 | + * The default Locale this request. |
|
101 | + * |
|
102 | + * @var string $defaultLocale |
|
103 | + */ |
|
104 | + protected $defaultLocale = 'en'; |
|
105 | 105 | |
106 | - /** |
|
107 | - * Gets files request ($_FILES). |
|
108 | - * |
|
109 | - * @var \Syscodes\Components\Http\Loaders\Files $files |
|
110 | - */ |
|
111 | - public $files; |
|
106 | + /** |
|
107 | + * Gets files request ($_FILES). |
|
108 | + * |
|
109 | + * @var \Syscodes\Components\Http\Loaders\Files $files |
|
110 | + */ |
|
111 | + public $files; |
|
112 | 112 | |
113 | - /** |
|
114 | - * Get the headers request ($_SERVER). |
|
115 | - * |
|
116 | - * @var \Syscodes\Components\Http\Loaders\Headers $headers |
|
117 | - */ |
|
118 | - public $headers; |
|
119 | - |
|
120 | - /** |
|
121 | - * The decoded JSON content for the request. |
|
122 | - * |
|
123 | - * @var \Syscodes\Components\Http\Loaders\Parameters|null $json |
|
124 | - */ |
|
125 | - protected $json; |
|
126 | - |
|
127 | - /** |
|
128 | - * The current language of the application. |
|
129 | - * |
|
130 | - * @var string $languages |
|
131 | - */ |
|
132 | - protected $languages; |
|
113 | + /** |
|
114 | + * Get the headers request ($_SERVER). |
|
115 | + * |
|
116 | + * @var \Syscodes\Components\Http\Loaders\Headers $headers |
|
117 | + */ |
|
118 | + public $headers; |
|
119 | + |
|
120 | + /** |
|
121 | + * The decoded JSON content for the request. |
|
122 | + * |
|
123 | + * @var \Syscodes\Components\Http\Loaders\Parameters|null $json |
|
124 | + */ |
|
125 | + protected $json; |
|
126 | + |
|
127 | + /** |
|
128 | + * The current language of the application. |
|
129 | + * |
|
130 | + * @var string $languages |
|
131 | + */ |
|
132 | + protected $languages; |
|
133 | 133 | |
134 | - /** |
|
135 | - * Get the locale. |
|
136 | - * |
|
137 | - * @var string $locale |
|
138 | - */ |
|
139 | - protected $locale; |
|
134 | + /** |
|
135 | + * Get the locale. |
|
136 | + * |
|
137 | + * @var string $locale |
|
138 | + */ |
|
139 | + protected $locale; |
|
140 | 140 | |
141 | - /** |
|
142 | - * The method name. |
|
143 | - * |
|
144 | - * @var string $method |
|
145 | - */ |
|
146 | - protected $method; |
|
147 | - |
|
148 | - /** |
|
149 | - * The path info of URL. |
|
150 | - * |
|
151 | - * @var string $pathInfo |
|
152 | - */ |
|
153 | - protected $pathInfo; |
|
154 | - |
|
155 | - /** |
|
156 | - * Query string parameters ($_GET). |
|
157 | - * |
|
158 | - * @var \Syscodes\Components\Http\Loaders\Parameters $query |
|
159 | - */ |
|
160 | - public $query; |
|
161 | - |
|
162 | - /** |
|
163 | - * Request body parameters ($_POST). |
|
164 | - * |
|
165 | - * @var \Syscodes\Components\Http\Loaders\Parameters $request |
|
166 | - */ |
|
167 | - public $request; |
|
168 | - |
|
169 | - /** |
|
170 | - * Get request URI. |
|
171 | - * |
|
172 | - * @var string $requestToUri |
|
173 | - */ |
|
174 | - protected $requestToUri; |
|
175 | - |
|
176 | - /** |
|
177 | - * Get the route resolver callback. |
|
178 | - * |
|
179 | - * @var \Closure $routeResolver |
|
180 | - */ |
|
181 | - protected $routeResolver; |
|
182 | - |
|
183 | - /** |
|
184 | - * The Session implementation. |
|
185 | - * |
|
186 | - * @var \Syscodes\Components\Contracts\Session\Session $session |
|
187 | - */ |
|
188 | - protected $session; |
|
189 | - |
|
190 | - /** |
|
191 | - * The detected uri and server variables ($_SERVER). |
|
192 | - * |
|
193 | - * @var \Syscodes\Components\Http\Loaders\Server $server |
|
194 | - */ |
|
195 | - public $server; |
|
196 | - |
|
197 | - /** |
|
198 | - * List of routes uri. |
|
199 | - * |
|
200 | - * @var \Syscodes\Components\Http\URI $uri |
|
201 | - */ |
|
202 | - public $uri; |
|
203 | - |
|
204 | - /** |
|
205 | - * Stores the valid locale codes. |
|
206 | - * |
|
207 | - * @var array $validLocales |
|
208 | - */ |
|
209 | - protected $validLocales = []; |
|
210 | - |
|
211 | - /** |
|
212 | - * Constructor: Create new the Request class. |
|
213 | - * |
|
214 | - * @param array $query |
|
215 | - * @param array $request |
|
216 | - * @param array $attributes |
|
217 | - * @param array $cookies |
|
218 | - * @param array $files |
|
219 | - * @param array $server |
|
220 | - * @param string|resource|null $content |
|
221 | - * |
|
222 | - * @return void |
|
223 | - */ |
|
224 | - public function __construct( |
|
225 | - array $query = [], |
|
226 | - array $request = [], |
|
227 | - array $attributes = [], |
|
228 | - array $cookies = [], |
|
229 | - array $files = [], |
|
230 | - array $server = [], |
|
231 | - $content = null |
|
232 | - ) { |
|
233 | - $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content); |
|
141 | + /** |
|
142 | + * The method name. |
|
143 | + * |
|
144 | + * @var string $method |
|
145 | + */ |
|
146 | + protected $method; |
|
147 | + |
|
148 | + /** |
|
149 | + * The path info of URL. |
|
150 | + * |
|
151 | + * @var string $pathInfo |
|
152 | + */ |
|
153 | + protected $pathInfo; |
|
154 | + |
|
155 | + /** |
|
156 | + * Query string parameters ($_GET). |
|
157 | + * |
|
158 | + * @var \Syscodes\Components\Http\Loaders\Parameters $query |
|
159 | + */ |
|
160 | + public $query; |
|
161 | + |
|
162 | + /** |
|
163 | + * Request body parameters ($_POST). |
|
164 | + * |
|
165 | + * @var \Syscodes\Components\Http\Loaders\Parameters $request |
|
166 | + */ |
|
167 | + public $request; |
|
168 | + |
|
169 | + /** |
|
170 | + * Get request URI. |
|
171 | + * |
|
172 | + * @var string $requestToUri |
|
173 | + */ |
|
174 | + protected $requestToUri; |
|
175 | + |
|
176 | + /** |
|
177 | + * Get the route resolver callback. |
|
178 | + * |
|
179 | + * @var \Closure $routeResolver |
|
180 | + */ |
|
181 | + protected $routeResolver; |
|
182 | + |
|
183 | + /** |
|
184 | + * The Session implementation. |
|
185 | + * |
|
186 | + * @var \Syscodes\Components\Contracts\Session\Session $session |
|
187 | + */ |
|
188 | + protected $session; |
|
189 | + |
|
190 | + /** |
|
191 | + * The detected uri and server variables ($_SERVER). |
|
192 | + * |
|
193 | + * @var \Syscodes\Components\Http\Loaders\Server $server |
|
194 | + */ |
|
195 | + public $server; |
|
196 | + |
|
197 | + /** |
|
198 | + * List of routes uri. |
|
199 | + * |
|
200 | + * @var \Syscodes\Components\Http\URI $uri |
|
201 | + */ |
|
202 | + public $uri; |
|
203 | + |
|
204 | + /** |
|
205 | + * Stores the valid locale codes. |
|
206 | + * |
|
207 | + * @var array $validLocales |
|
208 | + */ |
|
209 | + protected $validLocales = []; |
|
210 | + |
|
211 | + /** |
|
212 | + * Constructor: Create new the Request class. |
|
213 | + * |
|
214 | + * @param array $query |
|
215 | + * @param array $request |
|
216 | + * @param array $attributes |
|
217 | + * @param array $cookies |
|
218 | + * @param array $files |
|
219 | + * @param array $server |
|
220 | + * @param string|resource|null $content |
|
221 | + * |
|
222 | + * @return void |
|
223 | + */ |
|
224 | + public function __construct( |
|
225 | + array $query = [], |
|
226 | + array $request = [], |
|
227 | + array $attributes = [], |
|
228 | + array $cookies = [], |
|
229 | + array $files = [], |
|
230 | + array $server = [], |
|
231 | + $content = null |
|
232 | + ) { |
|
233 | + $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content); |
|
234 | 234 | |
235 | - $this->detectLocale(); |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Sets the parameters for this request. |
|
240 | - * |
|
241 | - * @param array $query |
|
242 | - * @param array $request |
|
243 | - * @param array $attributes |
|
244 | - * @param array $cookies |
|
245 | - * @param array $files |
|
246 | - * @param array $server |
|
247 | - * |
|
248 | - * @return void |
|
249 | - */ |
|
250 | - public function initialize( |
|
251 | - array $query = [], |
|
252 | - array $request = [], |
|
253 | - array $attributes = [], |
|
254 | - array $cookies = [], |
|
255 | - array $files = [], |
|
256 | - array $server = [], |
|
257 | - $content = null |
|
258 | - ): void { |
|
259 | - $this->query = new Inputs($query); |
|
260 | - $this->request = new Inputs($request); |
|
261 | - $this->attributes = new Parameters($attributes); |
|
262 | - $this->cookies = new Inputs($cookies); |
|
263 | - $this->files = new Files($files); |
|
264 | - $this->server = new Server($server); |
|
265 | - $this->headers = new Headers($this->server->all()); |
|
266 | - |
|
267 | - // Variables initialized |
|
268 | - $this->uri = new URI; |
|
269 | - $this->method = null; |
|
270 | - $this->baseUrl = null; |
|
271 | - $this->content = $content; |
|
272 | - $this->pathInfo = null; |
|
273 | - $this->languages = null; |
|
274 | - $this->acceptableContentTypes = null; |
|
275 | - $this->validLocales = config('app.supportedLocales'); |
|
276 | - $this->clientIp = new RequestClientIP($this->server->all()); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Clones a request and overrides some of its parameters. |
|
281 | - * |
|
282 | - * @param array|null $query |
|
283 | - * @param array|null $request |
|
284 | - * @param array|null $attributes |
|
285 | - * @param array|null $cookies |
|
286 | - * @param array|null $files |
|
287 | - * @param array|null $server |
|
288 | - * |
|
289 | - * @return static |
|
290 | - */ |
|
291 | - public function duplicate( |
|
292 | - array $query = null, |
|
293 | - array $request = null, |
|
294 | - array $attributes = null, |
|
295 | - array $cookies = null, |
|
296 | - array $files = null, |
|
297 | - array $server = null |
|
298 | - ): static { |
|
299 | - $duplicate = clone $this; |
|
300 | - |
|
301 | - if (null !== $query) { |
|
302 | - $duplicate->query = new Inputs($query); |
|
303 | - } |
|
304 | - |
|
305 | - if (null !== $request) { |
|
306 | - $duplicate->request = new Inputs($request); |
|
307 | - } |
|
308 | - |
|
309 | - if (null !== $attributes) { |
|
310 | - $duplicate->attributes = new Parameters($attributes); |
|
311 | - } |
|
312 | - |
|
313 | - if (null !== $cookies) { |
|
314 | - $duplicate->cookies = new Inputs($cookies); |
|
315 | - } |
|
316 | - |
|
317 | - if (null !== $files) { |
|
318 | - $duplicate->files = new Files($files); |
|
319 | - } |
|
320 | - |
|
321 | - if (null !== $server) { |
|
322 | - $duplicate->server = new Server($server); |
|
323 | - $duplicate->headers = new Headers($duplicate->server->all()); |
|
324 | - } |
|
325 | - |
|
326 | - $duplicate->uri = new URI; |
|
327 | - $duplicate->locale = null; |
|
328 | - $duplicate->method = null; |
|
329 | - $duplicate->baseUrl = null; |
|
330 | - $duplicate->pathInfo = null; |
|
331 | - $duplicate->validLocales = config('app.supportedLocales'); |
|
332 | - $duplicate->clientIp = new RequestClientIP($duplicate->server->all()); |
|
333 | - |
|
334 | - return $duplicate; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Returns the desired segment, or $default if it does not exist. |
|
339 | - * |
|
340 | - * @param int $index The segment number (1-based index) |
|
341 | - * @param mixed $default Default value to return |
|
342 | - * |
|
343 | - * @return string |
|
344 | - */ |
|
345 | - public function segment($index, $default = null) |
|
346 | - { |
|
347 | - return $this->uri->getSegment($index, $default); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * Returns all segments in an array. For total of segments |
|
352 | - * used the function PHP count(). |
|
353 | - * |
|
354 | - * @return array|null |
|
355 | - */ |
|
356 | - public function segments() |
|
357 | - { |
|
358 | - return $this->uri->getSegments(); |
|
359 | - } |
|
360 | - |
|
361 | - /** |
|
362 | - * Returns the total number of segment. |
|
363 | - * |
|
364 | - * @return int|null |
|
365 | - */ |
|
366 | - public function totalSegments() |
|
367 | - { |
|
368 | - return $this->uri->getTotalSegments(); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Handles setting up the locale, auto-detecting of language. |
|
373 | - * |
|
374 | - * @return void |
|
375 | - */ |
|
376 | - public function detectLocale(): void |
|
377 | - { |
|
378 | - $this->languages = $this->defaultLocale = config('app.locale'); |
|
379 | - |
|
380 | - $this->setLocale($this->validLocales[0]); |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Returns the default locale as set. |
|
385 | - * |
|
386 | - * @return string |
|
387 | - */ |
|
388 | - public function getDefaultLocale(): string |
|
389 | - { |
|
390 | - return $this->defaultLocale; |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Gets the current locale, with a fallback to the default. |
|
395 | - * |
|
396 | - * @return string |
|
397 | - */ |
|
398 | - public function getLocale(): string |
|
399 | - { |
|
400 | - return $this->languages ?: $this->defaultLocale; |
|
401 | - } |
|
402 | - |
|
403 | - /** |
|
404 | - * Sets the locale string for this request. |
|
405 | - * |
|
406 | - * @param string $locale |
|
407 | - * |
|
408 | - * @return self |
|
409 | - */ |
|
410 | - public function setLocale(string $locale): self |
|
411 | - { |
|
412 | - if ( ! in_array($locale, $this->validLocales, true)) { |
|
413 | - $locale = $this->defaultLocale; |
|
414 | - } |
|
235 | + $this->detectLocale(); |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Sets the parameters for this request. |
|
240 | + * |
|
241 | + * @param array $query |
|
242 | + * @param array $request |
|
243 | + * @param array $attributes |
|
244 | + * @param array $cookies |
|
245 | + * @param array $files |
|
246 | + * @param array $server |
|
247 | + * |
|
248 | + * @return void |
|
249 | + */ |
|
250 | + public function initialize( |
|
251 | + array $query = [], |
|
252 | + array $request = [], |
|
253 | + array $attributes = [], |
|
254 | + array $cookies = [], |
|
255 | + array $files = [], |
|
256 | + array $server = [], |
|
257 | + $content = null |
|
258 | + ): void { |
|
259 | + $this->query = new Inputs($query); |
|
260 | + $this->request = new Inputs($request); |
|
261 | + $this->attributes = new Parameters($attributes); |
|
262 | + $this->cookies = new Inputs($cookies); |
|
263 | + $this->files = new Files($files); |
|
264 | + $this->server = new Server($server); |
|
265 | + $this->headers = new Headers($this->server->all()); |
|
266 | + |
|
267 | + // Variables initialized |
|
268 | + $this->uri = new URI; |
|
269 | + $this->method = null; |
|
270 | + $this->baseUrl = null; |
|
271 | + $this->content = $content; |
|
272 | + $this->pathInfo = null; |
|
273 | + $this->languages = null; |
|
274 | + $this->acceptableContentTypes = null; |
|
275 | + $this->validLocales = config('app.supportedLocales'); |
|
276 | + $this->clientIp = new RequestClientIP($this->server->all()); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Clones a request and overrides some of its parameters. |
|
281 | + * |
|
282 | + * @param array|null $query |
|
283 | + * @param array|null $request |
|
284 | + * @param array|null $attributes |
|
285 | + * @param array|null $cookies |
|
286 | + * @param array|null $files |
|
287 | + * @param array|null $server |
|
288 | + * |
|
289 | + * @return static |
|
290 | + */ |
|
291 | + public function duplicate( |
|
292 | + array $query = null, |
|
293 | + array $request = null, |
|
294 | + array $attributes = null, |
|
295 | + array $cookies = null, |
|
296 | + array $files = null, |
|
297 | + array $server = null |
|
298 | + ): static { |
|
299 | + $duplicate = clone $this; |
|
300 | + |
|
301 | + if (null !== $query) { |
|
302 | + $duplicate->query = new Inputs($query); |
|
303 | + } |
|
304 | + |
|
305 | + if (null !== $request) { |
|
306 | + $duplicate->request = new Inputs($request); |
|
307 | + } |
|
308 | + |
|
309 | + if (null !== $attributes) { |
|
310 | + $duplicate->attributes = new Parameters($attributes); |
|
311 | + } |
|
312 | + |
|
313 | + if (null !== $cookies) { |
|
314 | + $duplicate->cookies = new Inputs($cookies); |
|
315 | + } |
|
316 | + |
|
317 | + if (null !== $files) { |
|
318 | + $duplicate->files = new Files($files); |
|
319 | + } |
|
320 | + |
|
321 | + if (null !== $server) { |
|
322 | + $duplicate->server = new Server($server); |
|
323 | + $duplicate->headers = new Headers($duplicate->server->all()); |
|
324 | + } |
|
325 | + |
|
326 | + $duplicate->uri = new URI; |
|
327 | + $duplicate->locale = null; |
|
328 | + $duplicate->method = null; |
|
329 | + $duplicate->baseUrl = null; |
|
330 | + $duplicate->pathInfo = null; |
|
331 | + $duplicate->validLocales = config('app.supportedLocales'); |
|
332 | + $duplicate->clientIp = new RequestClientIP($duplicate->server->all()); |
|
333 | + |
|
334 | + return $duplicate; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Returns the desired segment, or $default if it does not exist. |
|
339 | + * |
|
340 | + * @param int $index The segment number (1-based index) |
|
341 | + * @param mixed $default Default value to return |
|
342 | + * |
|
343 | + * @return string |
|
344 | + */ |
|
345 | + public function segment($index, $default = null) |
|
346 | + { |
|
347 | + return $this->uri->getSegment($index, $default); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * Returns all segments in an array. For total of segments |
|
352 | + * used the function PHP count(). |
|
353 | + * |
|
354 | + * @return array|null |
|
355 | + */ |
|
356 | + public function segments() |
|
357 | + { |
|
358 | + return $this->uri->getSegments(); |
|
359 | + } |
|
360 | + |
|
361 | + /** |
|
362 | + * Returns the total number of segment. |
|
363 | + * |
|
364 | + * @return int|null |
|
365 | + */ |
|
366 | + public function totalSegments() |
|
367 | + { |
|
368 | + return $this->uri->getTotalSegments(); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Handles setting up the locale, auto-detecting of language. |
|
373 | + * |
|
374 | + * @return void |
|
375 | + */ |
|
376 | + public function detectLocale(): void |
|
377 | + { |
|
378 | + $this->languages = $this->defaultLocale = config('app.locale'); |
|
379 | + |
|
380 | + $this->setLocale($this->validLocales[0]); |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Returns the default locale as set. |
|
385 | + * |
|
386 | + * @return string |
|
387 | + */ |
|
388 | + public function getDefaultLocale(): string |
|
389 | + { |
|
390 | + return $this->defaultLocale; |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Gets the current locale, with a fallback to the default. |
|
395 | + * |
|
396 | + * @return string |
|
397 | + */ |
|
398 | + public function getLocale(): string |
|
399 | + { |
|
400 | + return $this->languages ?: $this->defaultLocale; |
|
401 | + } |
|
402 | + |
|
403 | + /** |
|
404 | + * Sets the locale string for this request. |
|
405 | + * |
|
406 | + * @param string $locale |
|
407 | + * |
|
408 | + * @return self |
|
409 | + */ |
|
410 | + public function setLocale(string $locale): self |
|
411 | + { |
|
412 | + if ( ! in_array($locale, $this->validLocales, true)) { |
|
413 | + $locale = $this->defaultLocale; |
|
414 | + } |
|
415 | 415 | |
416 | - $this->languages = $locale; |
|
416 | + $this->languages = $locale; |
|
417 | 417 | |
418 | - Locale::setDefault($locale); |
|
418 | + Locale::setDefault($locale); |
|
419 | 419 | |
420 | - return $this; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Returns the full request string. |
|
425 | - * |
|
426 | - * @param string $key |
|
427 | - * @param mixed $default |
|
428 | - * |
|
429 | - * @return mixed |
|
430 | - */ |
|
431 | - public function get(string $key, $default = null) |
|
432 | - { |
|
433 | - if ($this !== $result = $this->attributes->get($key, $this)) { |
|
434 | - return $result; |
|
435 | - } |
|
436 | - |
|
437 | - if ($this->query->has($key)) { |
|
438 | - return $this->query->all()[$key]; |
|
439 | - } |
|
420 | + return $this; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Returns the full request string. |
|
425 | + * |
|
426 | + * @param string $key |
|
427 | + * @param mixed $default |
|
428 | + * |
|
429 | + * @return mixed |
|
430 | + */ |
|
431 | + public function get(string $key, $default = null) |
|
432 | + { |
|
433 | + if ($this !== $result = $this->attributes->get($key, $this)) { |
|
434 | + return $result; |
|
435 | + } |
|
436 | + |
|
437 | + if ($this->query->has($key)) { |
|
438 | + return $this->query->all()[$key]; |
|
439 | + } |
|
440 | 440 | |
441 | - if ($this->request->has($key)) { |
|
442 | - return $this->request->all()[$key]; |
|
443 | - } |
|
441 | + if ($this->request->has($key)) { |
|
442 | + return $this->request->all()[$key]; |
|
443 | + } |
|
444 | 444 | |
445 | - return $default; |
|
446 | - } |
|
447 | - |
|
448 | - /** |
|
449 | - * Gets the Session. |
|
450 | - * |
|
451 | - * @return \Syscodes\Components\Http\Session\SessionInterface |
|
452 | - * |
|
453 | - * @throws \Syscodes\Components\Http\Exceptions\SessionNotFoundException |
|
454 | - */ |
|
455 | - public function getSession() |
|
456 | - { |
|
457 | - $this->hasSession() |
|
458 | - ? new SessionDecorator($this->session()) |
|
459 | - : throw new SessionNotFoundException; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Whether the request contains a Session object. |
|
464 | - * |
|
465 | - * @return bool |
|
466 | - */ |
|
467 | - public function hasSession(): bool |
|
468 | - { |
|
469 | - return ! is_null($this->session); |
|
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Get the session associated with the request. |
|
474 | - * |
|
475 | - * @return \Syscodes\Components\Contracts\Session\Session |
|
476 | - * |
|
477 | - * @throws RuntimeException |
|
478 | - */ |
|
479 | - public function session() |
|
480 | - { |
|
481 | - if ( ! $this->hasSession()) { |
|
482 | - throw new RuntimeException('Session store not set on request'); |
|
483 | - } |
|
445 | + return $default; |
|
446 | + } |
|
447 | + |
|
448 | + /** |
|
449 | + * Gets the Session. |
|
450 | + * |
|
451 | + * @return \Syscodes\Components\Http\Session\SessionInterface |
|
452 | + * |
|
453 | + * @throws \Syscodes\Components\Http\Exceptions\SessionNotFoundException |
|
454 | + */ |
|
455 | + public function getSession() |
|
456 | + { |
|
457 | + $this->hasSession() |
|
458 | + ? new SessionDecorator($this->session()) |
|
459 | + : throw new SessionNotFoundException; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Whether the request contains a Session object. |
|
464 | + * |
|
465 | + * @return bool |
|
466 | + */ |
|
467 | + public function hasSession(): bool |
|
468 | + { |
|
469 | + return ! is_null($this->session); |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Get the session associated with the request. |
|
474 | + * |
|
475 | + * @return \Syscodes\Components\Contracts\Session\Session |
|
476 | + * |
|
477 | + * @throws RuntimeException |
|
478 | + */ |
|
479 | + public function session() |
|
480 | + { |
|
481 | + if ( ! $this->hasSession()) { |
|
482 | + throw new RuntimeException('Session store not set on request'); |
|
483 | + } |
|
484 | 484 | |
485 | - return $this->session; |
|
486 | - } |
|
485 | + return $this->session; |
|
486 | + } |
|
487 | 487 | |
488 | - /** |
|
489 | - * Set the session instance on the request. |
|
490 | - * |
|
491 | - * @param \Syscodes\Components\Contracts\Session\Session $session |
|
492 | - * |
|
493 | - * @return void |
|
494 | - */ |
|
495 | - public function setSession($session): void |
|
496 | - { |
|
497 | - $this->session = $session; |
|
498 | - } |
|
499 | - |
|
500 | - /** |
|
501 | - * Get the JSON payload for the request. |
|
502 | - * |
|
503 | - * @param string|null $key |
|
504 | - * @param mixed $default |
|
505 | - * |
|
506 | - * @return \Syscodes\Components\Http\Utilities\Parameters|mixed |
|
507 | - */ |
|
508 | - public function json($key = null, $default = null) |
|
509 | - { |
|
510 | - if ( ! isset($this->json)) { |
|
511 | - $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
512 | - } |
|
513 | - |
|
514 | - if (is_null($key)) { |
|
515 | - return $this->json; |
|
516 | - } |
|
517 | - |
|
518 | - return Arr::get($this->json->all(), $key, $default); |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Set the JSON payload for the request. |
|
523 | - * |
|
524 | - * @param \Syscodes\Components\Http\Utilities\Parameters $json |
|
525 | - * |
|
526 | - * @return static |
|
527 | - */ |
|
528 | - public function setJson($json): static |
|
529 | - { |
|
530 | - $this->json = $json; |
|
531 | - |
|
532 | - return $this; |
|
533 | - } |
|
488 | + /** |
|
489 | + * Set the session instance on the request. |
|
490 | + * |
|
491 | + * @param \Syscodes\Components\Contracts\Session\Session $session |
|
492 | + * |
|
493 | + * @return void |
|
494 | + */ |
|
495 | + public function setSession($session): void |
|
496 | + { |
|
497 | + $this->session = $session; |
|
498 | + } |
|
499 | + |
|
500 | + /** |
|
501 | + * Get the JSON payload for the request. |
|
502 | + * |
|
503 | + * @param string|null $key |
|
504 | + * @param mixed $default |
|
505 | + * |
|
506 | + * @return \Syscodes\Components\Http\Utilities\Parameters|mixed |
|
507 | + */ |
|
508 | + public function json($key = null, $default = null) |
|
509 | + { |
|
510 | + if ( ! isset($this->json)) { |
|
511 | + $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
512 | + } |
|
513 | + |
|
514 | + if (is_null($key)) { |
|
515 | + return $this->json; |
|
516 | + } |
|
517 | + |
|
518 | + return Arr::get($this->json->all(), $key, $default); |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Set the JSON payload for the request. |
|
523 | + * |
|
524 | + * @param \Syscodes\Components\Http\Utilities\Parameters $json |
|
525 | + * |
|
526 | + * @return static |
|
527 | + */ |
|
528 | + public function setJson($json): static |
|
529 | + { |
|
530 | + $this->json = $json; |
|
531 | + |
|
532 | + return $this; |
|
533 | + } |
|
534 | 534 | |
535 | - /** |
|
536 | - * Gets a list of content types acceptable by the client browser in preferable order. |
|
537 | - * |
|
538 | - * @return string[] |
|
539 | - */ |
|
540 | - public function getAcceptableContentTypes(): array |
|
541 | - { |
|
542 | - if (null !== $this->acceptableContentTypes) { |
|
543 | - return $this->acceptableContentTypes; |
|
544 | - } |
|
535 | + /** |
|
536 | + * Gets a list of content types acceptable by the client browser in preferable order. |
|
537 | + * |
|
538 | + * @return string[] |
|
539 | + */ |
|
540 | + public function getAcceptableContentTypes(): array |
|
541 | + { |
|
542 | + if (null !== $this->acceptableContentTypes) { |
|
543 | + return $this->acceptableContentTypes; |
|
544 | + } |
|
545 | 545 | |
546 | - return $this->acceptableContentTypes = array_map('strval', [$this->headers->get('Accept')]); |
|
547 | - } |
|
548 | - |
|
549 | - /** |
|
550 | - * Returns whether this is an AJAX request or not. |
|
551 | - * Alias of isXmlHttpRequest(). |
|
552 | - * |
|
553 | - * @return bool |
|
554 | - */ |
|
555 | - public function ajax(): bool |
|
556 | - { |
|
557 | - return $this->isXmlHttpRequest(); |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Returns whether this is an AJAX request or not. |
|
562 | - * |
|
563 | - * @return bool |
|
564 | - */ |
|
565 | - public function isXmlHttpRequest(): bool |
|
566 | - { |
|
567 | - return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
568 | - strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
569 | - } |
|
546 | + return $this->acceptableContentTypes = array_map('strval', [$this->headers->get('Accept')]); |
|
547 | + } |
|
548 | + |
|
549 | + /** |
|
550 | + * Returns whether this is an AJAX request or not. |
|
551 | + * Alias of isXmlHttpRequest(). |
|
552 | + * |
|
553 | + * @return bool |
|
554 | + */ |
|
555 | + public function ajax(): bool |
|
556 | + { |
|
557 | + return $this->isXmlHttpRequest(); |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Returns whether this is an AJAX request or not. |
|
562 | + * |
|
563 | + * @return bool |
|
564 | + */ |
|
565 | + public function isXmlHttpRequest(): bool |
|
566 | + { |
|
567 | + return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
568 | + strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
569 | + } |
|
570 | 570 | |
571 | - /** |
|
572 | - * Determine if the request is the result of a PJAX call. |
|
573 | - * |
|
574 | - * @return bool |
|
575 | - */ |
|
576 | - public function pjax(): bool |
|
577 | - { |
|
578 | - return $this->headers->get('X-PJAX') == true; |
|
579 | - } |
|
571 | + /** |
|
572 | + * Determine if the request is the result of a PJAX call. |
|
573 | + * |
|
574 | + * @return bool |
|
575 | + */ |
|
576 | + public function pjax(): bool |
|
577 | + { |
|
578 | + return $this->headers->get('X-PJAX') == true; |
|
579 | + } |
|
580 | 580 | |
581 | - /** |
|
582 | - * Determine if the request is the result of a prefetch call. |
|
583 | - * |
|
584 | - * @return bool |
|
585 | - */ |
|
586 | - public function prefetch(): bool |
|
587 | - { |
|
588 | - return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 || |
|
589 | - strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0; |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * Checks if the method request is of specified type. |
|
594 | - * |
|
595 | - * @param string $method |
|
596 | - * |
|
597 | - * @return bool |
|
598 | - */ |
|
599 | - public function isMethod(string $method): bool |
|
600 | - { |
|
601 | - return $this->getMethod() === strtoupper($method); |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
581 | + /** |
|
582 | + * Determine if the request is the result of a prefetch call. |
|
583 | + * |
|
584 | + * @return bool |
|
585 | + */ |
|
586 | + public function prefetch(): bool |
|
587 | + { |
|
588 | + return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 || |
|
589 | + strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0; |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * Checks if the method request is of specified type. |
|
594 | + * |
|
595 | + * @param string $method |
|
596 | + * |
|
597 | + * @return bool |
|
598 | + */ |
|
599 | + public function isMethod(string $method): bool |
|
600 | + { |
|
601 | + return $this->getMethod() === strtoupper($method); |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | 605 | * Alias of the request method. |
606 | 606 | * |
607 | 607 | * @return string |
@@ -611,509 +611,509 @@ discard block |
||
611 | 611 | return $this->getMethod(); |
612 | 612 | } |
613 | 613 | |
614 | - /** |
|
615 | - * Returns the input method used (GET, POST, DELETE, etc.). |
|
616 | - * |
|
617 | - * @return string |
|
618 | - * |
|
619 | - * @throws \LogicException |
|
620 | - */ |
|
621 | - public function getmethod(): string |
|
622 | - { |
|
623 | - if (null !== $this->method) { |
|
624 | - return $this->method; |
|
625 | - } |
|
614 | + /** |
|
615 | + * Returns the input method used (GET, POST, DELETE, etc.). |
|
616 | + * |
|
617 | + * @return string |
|
618 | + * |
|
619 | + * @throws \LogicException |
|
620 | + */ |
|
621 | + public function getmethod(): string |
|
622 | + { |
|
623 | + if (null !== $this->method) { |
|
624 | + return $this->method; |
|
625 | + } |
|
626 | 626 | |
627 | - $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
627 | + $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
628 | 628 | |
629 | - if ('POST' !== $this->method) { |
|
630 | - return $this->method; |
|
631 | - } |
|
629 | + if ('POST' !== $this->method) { |
|
630 | + return $this->method; |
|
631 | + } |
|
632 | 632 | |
633 | - $method = $this->headers->get('X-HTTP-METHOD-OVERRIDE'); |
|
633 | + $method = $this->headers->get('X-HTTP-METHOD-OVERRIDE'); |
|
634 | 634 | |
635 | - if ( ! $method && self::$httpMethodParameterOverride) { |
|
636 | - $method = $this->request->get('_method', $this->query->get('_method', 'POST')); |
|
637 | - } |
|
635 | + if ( ! $method && self::$httpMethodParameterOverride) { |
|
636 | + $method = $this->request->get('_method', $this->query->get('_method', 'POST')); |
|
637 | + } |
|
638 | 638 | |
639 | - if ( ! is_string($method)) { |
|
640 | - return $this->method; |
|
641 | - } |
|
639 | + if ( ! is_string($method)) { |
|
640 | + return $this->method; |
|
641 | + } |
|
642 | 642 | |
643 | - $method = strtoupper($method); |
|
643 | + $method = strtoupper($method); |
|
644 | 644 | |
645 | - if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
646 | - return $this->method = $method; |
|
647 | - } |
|
645 | + if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
646 | + return $this->method = $method; |
|
647 | + } |
|
648 | 648 | |
649 | - if ( ! preg_match('/^[A-Z]++$/D', $method)) { |
|
650 | - throw new LogicException(sprintf('Invalid method override "%s".', $method)); |
|
651 | - } |
|
649 | + if ( ! preg_match('/^[A-Z]++$/D', $method)) { |
|
650 | + throw new LogicException(sprintf('Invalid method override "%s".', $method)); |
|
651 | + } |
|
652 | 652 | |
653 | - return $this->method = $method; |
|
654 | - } |
|
655 | - |
|
656 | - /** |
|
657 | - * Sets the request method. |
|
658 | - * |
|
659 | - * @param string $method |
|
660 | - * |
|
661 | - * @return void |
|
662 | - */ |
|
663 | - public function setMethod(string $method): void |
|
664 | - { |
|
665 | - $this->method = null; |
|
666 | - |
|
667 | - $this->server->set('REQUEST_METHOD', $method); |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Get the input source for the request. |
|
672 | - * |
|
673 | - * @return \Syscodes\Components\Http\Utilities\Parameters |
|
674 | - */ |
|
675 | - public function getInputSource() |
|
676 | - { |
|
677 | - if ($this->isJson()) { |
|
678 | - return $this->json(); |
|
679 | - } |
|
680 | - |
|
681 | - return in_array($this->getMethod(), ['GET', 'HEAD']) ? $this->query : $this->request; |
|
682 | - } |
|
653 | + return $this->method = $method; |
|
654 | + } |
|
655 | + |
|
656 | + /** |
|
657 | + * Sets the request method. |
|
658 | + * |
|
659 | + * @param string $method |
|
660 | + * |
|
661 | + * @return void |
|
662 | + */ |
|
663 | + public function setMethod(string $method): void |
|
664 | + { |
|
665 | + $this->method = null; |
|
666 | + |
|
667 | + $this->server->set('REQUEST_METHOD', $method); |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Get the input source for the request. |
|
672 | + * |
|
673 | + * @return \Syscodes\Components\Http\Utilities\Parameters |
|
674 | + */ |
|
675 | + public function getInputSource() |
|
676 | + { |
|
677 | + if ($this->isJson()) { |
|
678 | + return $this->json(); |
|
679 | + } |
|
680 | + |
|
681 | + return in_array($this->getMethod(), ['GET', 'HEAD']) ? $this->query : $this->request; |
|
682 | + } |
|
683 | 683 | |
684 | - /** |
|
685 | - * Determine if the current request URI matches a pattern. |
|
686 | - * |
|
687 | - * @param mixed ...$patterns |
|
688 | - * |
|
689 | - * @return bool |
|
690 | - */ |
|
691 | - public function is(...$patterns): bool |
|
692 | - { |
|
693 | - $path = $this->decodedPath(); |
|
684 | + /** |
|
685 | + * Determine if the current request URI matches a pattern. |
|
686 | + * |
|
687 | + * @param mixed ...$patterns |
|
688 | + * |
|
689 | + * @return bool |
|
690 | + */ |
|
691 | + public function is(...$patterns): bool |
|
692 | + { |
|
693 | + $path = $this->decodedPath(); |
|
694 | 694 | |
695 | - foreach ($patterns as $pattern) { |
|
696 | - if (Str::is($pattern, $path)) { |
|
697 | - return true; |
|
698 | - } |
|
699 | - } |
|
700 | - |
|
701 | - return false; |
|
702 | - } |
|
703 | - |
|
704 | - /** |
|
705 | - * Determine if the route name matches a given pattern. |
|
706 | - * |
|
707 | - * @param mixed ...$patterns |
|
708 | - * |
|
709 | - * @return bool |
|
710 | - */ |
|
711 | - public function routeIs(...$patterns): bool |
|
712 | - { |
|
713 | - return $this->route() && $this->route()->is(...$patterns); |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Get the route handling the request. |
|
718 | - * |
|
719 | - * @param string|null $param |
|
720 | - * @param mixed $default |
|
721 | - * |
|
722 | - * @return \Syscodes\Components\Routing\Route|object|string|null |
|
723 | - */ |
|
724 | - public function route($param = null, $default = null) |
|
725 | - { |
|
726 | - $route = call_user_func($this->getRouteResolver()); |
|
727 | - |
|
728 | - if (is_null($route) || is_null($param)) { |
|
729 | - return $route; |
|
730 | - } |
|
731 | - |
|
732 | - return $route->parameter($param, $default); |
|
733 | - } |
|
734 | - |
|
735 | - /** |
|
736 | - * Get the current decoded path info for the request. |
|
737 | - * |
|
738 | - * @return string |
|
739 | - */ |
|
740 | - public function decodedPath(): string |
|
741 | - { |
|
742 | - return rawurldecode($this->path()); |
|
743 | - } |
|
744 | - |
|
745 | - /** |
|
746 | - * Get the current path info for the request. |
|
747 | - * |
|
748 | - * @return string |
|
749 | - */ |
|
750 | - public function path(): string |
|
751 | - { |
|
752 | - $path = trim($this->getPathInfo(), '/'); |
|
753 | - |
|
754 | - return $path == '' ? '/' : $path; |
|
755 | - } |
|
756 | - |
|
757 | - /** |
|
758 | - * Get the full URL for the request. |
|
759 | - * |
|
760 | - * @return string |
|
761 | - */ |
|
762 | - public function fullUrl(): string |
|
763 | - { |
|
764 | - $query = $this->getQueryString(); |
|
695 | + foreach ($patterns as $pattern) { |
|
696 | + if (Str::is($pattern, $path)) { |
|
697 | + return true; |
|
698 | + } |
|
699 | + } |
|
700 | + |
|
701 | + return false; |
|
702 | + } |
|
703 | + |
|
704 | + /** |
|
705 | + * Determine if the route name matches a given pattern. |
|
706 | + * |
|
707 | + * @param mixed ...$patterns |
|
708 | + * |
|
709 | + * @return bool |
|
710 | + */ |
|
711 | + public function routeIs(...$patterns): bool |
|
712 | + { |
|
713 | + return $this->route() && $this->route()->is(...$patterns); |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Get the route handling the request. |
|
718 | + * |
|
719 | + * @param string|null $param |
|
720 | + * @param mixed $default |
|
721 | + * |
|
722 | + * @return \Syscodes\Components\Routing\Route|object|string|null |
|
723 | + */ |
|
724 | + public function route($param = null, $default = null) |
|
725 | + { |
|
726 | + $route = call_user_func($this->getRouteResolver()); |
|
727 | + |
|
728 | + if (is_null($route) || is_null($param)) { |
|
729 | + return $route; |
|
730 | + } |
|
731 | + |
|
732 | + return $route->parameter($param, $default); |
|
733 | + } |
|
734 | + |
|
735 | + /** |
|
736 | + * Get the current decoded path info for the request. |
|
737 | + * |
|
738 | + * @return string |
|
739 | + */ |
|
740 | + public function decodedPath(): string |
|
741 | + { |
|
742 | + return rawurldecode($this->path()); |
|
743 | + } |
|
744 | + |
|
745 | + /** |
|
746 | + * Get the current path info for the request. |
|
747 | + * |
|
748 | + * @return string |
|
749 | + */ |
|
750 | + public function path(): string |
|
751 | + { |
|
752 | + $path = trim($this->getPathInfo(), '/'); |
|
753 | + |
|
754 | + return $path == '' ? '/' : $path; |
|
755 | + } |
|
756 | + |
|
757 | + /** |
|
758 | + * Get the full URL for the request. |
|
759 | + * |
|
760 | + * @return string |
|
761 | + */ |
|
762 | + public function fullUrl(): string |
|
763 | + { |
|
764 | + $query = $this->getQueryString(); |
|
765 | 765 | |
766 | - $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; |
|
766 | + $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; |
|
767 | 767 | |
768 | - return $query ? $this->url().$question.$query : $this->url(); |
|
769 | - } |
|
768 | + return $query ? $this->url().$question.$query : $this->url(); |
|
769 | + } |
|
770 | 770 | |
771 | - /** |
|
772 | - * Generates the normalized query string for the Request. |
|
773 | - * |
|
774 | - * @return string |
|
775 | - */ |
|
776 | - public function getQueryString(): ?string |
|
777 | - { |
|
778 | - $queryString = RequestUtils::normalizedQueryString($this->server->get('QUERY_STRING')); |
|
771 | + /** |
|
772 | + * Generates the normalized query string for the Request. |
|
773 | + * |
|
774 | + * @return string |
|
775 | + */ |
|
776 | + public function getQueryString(): ?string |
|
777 | + { |
|
778 | + $queryString = RequestUtils::normalizedQueryString($this->server->get('QUERY_STRING')); |
|
779 | 779 | |
780 | - return '' === $queryString ? null : $queryString; |
|
781 | - } |
|
782 | - |
|
783 | - /** |
|
784 | - * Retunrs the request body content. |
|
785 | - * |
|
786 | - * @return string |
|
787 | - */ |
|
788 | - public function getContent(): string |
|
789 | - { |
|
790 | - if (null === $this->content || false === $this->content) { |
|
791 | - $this->content = file_get_contents('php://input'); |
|
792 | - } |
|
793 | - |
|
794 | - return $this->content; |
|
795 | - } |
|
796 | - |
|
797 | - /** |
|
798 | - * Returns the path being requested relative to the executed script. |
|
799 | - * |
|
800 | - * @return string |
|
801 | - */ |
|
802 | - public function getPathInfo(): string |
|
803 | - { |
|
804 | - if (null === $this->pathInfo) { |
|
805 | - $this->pathInfo = $this->parsePathInfo(); |
|
806 | - } |
|
807 | - |
|
808 | - return $this->pathInfo; |
|
809 | - } |
|
810 | - |
|
811 | - /** |
|
812 | - * Returns the root URL from which this request is executed. |
|
813 | - * |
|
814 | - * @return string |
|
815 | - */ |
|
816 | - public function getBaseUrl(): string |
|
817 | - { |
|
818 | - if (null === $this->baseUrl) { |
|
819 | - $this->baseUrl = $this->parseBaseUrl(); |
|
820 | - } |
|
821 | - |
|
822 | - return $this->baseUrl; |
|
823 | - } |
|
824 | - |
|
825 | - /** |
|
826 | - * Returns the requested URI. |
|
827 | - * |
|
828 | - * @return string |
|
829 | - */ |
|
830 | - public function getRequestUri(): string |
|
831 | - { |
|
832 | - if (null === $this->requestToUri) { |
|
833 | - $this->requestToUri = $this->parseRequestUri(); |
|
834 | - } |
|
835 | - |
|
836 | - return $this->requestToUri; |
|
837 | - } |
|
780 | + return '' === $queryString ? null : $queryString; |
|
781 | + } |
|
782 | + |
|
783 | + /** |
|
784 | + * Retunrs the request body content. |
|
785 | + * |
|
786 | + * @return string |
|
787 | + */ |
|
788 | + public function getContent(): string |
|
789 | + { |
|
790 | + if (null === $this->content || false === $this->content) { |
|
791 | + $this->content = file_get_contents('php://input'); |
|
792 | + } |
|
793 | + |
|
794 | + return $this->content; |
|
795 | + } |
|
796 | + |
|
797 | + /** |
|
798 | + * Returns the path being requested relative to the executed script. |
|
799 | + * |
|
800 | + * @return string |
|
801 | + */ |
|
802 | + public function getPathInfo(): string |
|
803 | + { |
|
804 | + if (null === $this->pathInfo) { |
|
805 | + $this->pathInfo = $this->parsePathInfo(); |
|
806 | + } |
|
807 | + |
|
808 | + return $this->pathInfo; |
|
809 | + } |
|
810 | + |
|
811 | + /** |
|
812 | + * Returns the root URL from which this request is executed. |
|
813 | + * |
|
814 | + * @return string |
|
815 | + */ |
|
816 | + public function getBaseUrl(): string |
|
817 | + { |
|
818 | + if (null === $this->baseUrl) { |
|
819 | + $this->baseUrl = $this->parseBaseUrl(); |
|
820 | + } |
|
821 | + |
|
822 | + return $this->baseUrl; |
|
823 | + } |
|
824 | + |
|
825 | + /** |
|
826 | + * Returns the requested URI. |
|
827 | + * |
|
828 | + * @return string |
|
829 | + */ |
|
830 | + public function getRequestUri(): string |
|
831 | + { |
|
832 | + if (null === $this->requestToUri) { |
|
833 | + $this->requestToUri = $this->parseRequestUri(); |
|
834 | + } |
|
835 | + |
|
836 | + return $this->requestToUri; |
|
837 | + } |
|
838 | 838 | |
839 | - /** |
|
840 | - * Generates a normalized URI (URL) for the Request. |
|
841 | - * |
|
842 | - * @return string |
|
843 | - */ |
|
844 | - public function getUri(): string |
|
845 | - { |
|
846 | - if (null !== $query = $this->getQueryString()) { |
|
847 | - $query = '?'.$query; |
|
848 | - } |
|
839 | + /** |
|
840 | + * Generates a normalized URI (URL) for the Request. |
|
841 | + * |
|
842 | + * @return string |
|
843 | + */ |
|
844 | + public function getUri(): string |
|
845 | + { |
|
846 | + if (null !== $query = $this->getQueryString()) { |
|
847 | + $query = '?'.$query; |
|
848 | + } |
|
849 | 849 | |
850 | - return $this->getSchemeWithHttpHost().$this->getBaseUrl().$this->getPathInfo().$query; |
|
851 | - } |
|
850 | + return $this->getSchemeWithHttpHost().$this->getBaseUrl().$this->getPathInfo().$query; |
|
851 | + } |
|
852 | 852 | |
853 | - /** |
|
854 | - * Gets the request's scheme. |
|
855 | - * |
|
856 | - * @return string |
|
857 | - */ |
|
858 | - public function getScheme(): string |
|
859 | - { |
|
860 | - return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
861 | - } |
|
862 | - |
|
863 | - /** |
|
864 | - * Returns the host name. |
|
865 | - * |
|
866 | - * @return string |
|
867 | - */ |
|
868 | - public function getHost(): string |
|
869 | - { |
|
870 | - if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
871 | - $host = $forwardedHost[0]; |
|
872 | - } elseif ( ! $host = $this->headers->get('HOST')) { |
|
873 | - if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
874 | - $host = $this->server->get('REMOTE_ADDR', ''); |
|
875 | - } |
|
876 | - } |
|
877 | - |
|
878 | - $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
853 | + /** |
|
854 | + * Gets the request's scheme. |
|
855 | + * |
|
856 | + * @return string |
|
857 | + */ |
|
858 | + public function getScheme(): string |
|
859 | + { |
|
860 | + return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
861 | + } |
|
862 | + |
|
863 | + /** |
|
864 | + * Returns the host name. |
|
865 | + * |
|
866 | + * @return string |
|
867 | + */ |
|
868 | + public function getHost(): string |
|
869 | + { |
|
870 | + if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
871 | + $host = $forwardedHost[0]; |
|
872 | + } elseif ( ! $host = $this->headers->get('HOST')) { |
|
873 | + if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
874 | + $host = $this->server->get('REMOTE_ADDR', ''); |
|
875 | + } |
|
876 | + } |
|
877 | + |
|
878 | + $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
879 | 879 | |
880 | - return $this->uri->setHost($host); |
|
881 | - } |
|
882 | - |
|
883 | - /** |
|
884 | - * Returns the port on which the request is made. |
|
885 | - * |
|
886 | - * @return int |
|
887 | - */ |
|
888 | - public function getPort(): int |
|
889 | - { |
|
890 | - if ( ! $this->server->get('HTTP_HOST')) { |
|
891 | - return $this->server->get('SERVER_PORT'); |
|
892 | - } |
|
880 | + return $this->uri->setHost($host); |
|
881 | + } |
|
882 | + |
|
883 | + /** |
|
884 | + * Returns the port on which the request is made. |
|
885 | + * |
|
886 | + * @return int |
|
887 | + */ |
|
888 | + public function getPort(): int |
|
889 | + { |
|
890 | + if ( ! $this->server->get('HTTP_HOST')) { |
|
891 | + return $this->server->get('SERVER_PORT'); |
|
892 | + } |
|
893 | 893 | |
894 | - return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
895 | - } |
|
896 | - |
|
897 | - /** |
|
898 | - * Get the user. |
|
899 | - * |
|
900 | - * @return string|null |
|
901 | - */ |
|
902 | - public function getUser(): ?string |
|
903 | - { |
|
904 | - $user = $this->uri->setUser( |
|
905 | - $this->headers->get('PHP_AUTH_USER') |
|
906 | - ); |
|
907 | - |
|
908 | - return $user; |
|
909 | - } |
|
910 | - |
|
911 | - /** |
|
912 | - * Get the password. |
|
913 | - * |
|
914 | - * @return string|null |
|
915 | - */ |
|
916 | - public function getPassword(): ?string |
|
917 | - { |
|
918 | - $password = $this->uri->setPassword( |
|
919 | - $this->headers->get('PHP_AUTH_PW') |
|
920 | - ); |
|
921 | - |
|
922 | - return $password; |
|
923 | - } |
|
924 | - |
|
925 | - /** |
|
926 | - * Gets the user info. |
|
927 | - * |
|
928 | - * @return string|null |
|
929 | - */ |
|
930 | - public function getUserInfo(): ?string |
|
931 | - { |
|
932 | - return $this->uri->getUserInfo(); |
|
933 | - } |
|
934 | - |
|
935 | - /** |
|
936 | - * Returns the HTTP host being requested. |
|
937 | - * |
|
938 | - * @return string |
|
939 | - */ |
|
940 | - public function getHttpHost(): string |
|
941 | - { |
|
942 | - $scheme = $this->getScheme(); |
|
943 | - $port = $this->getPort(); |
|
944 | - |
|
945 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
946 | - return $this->getHost(); |
|
947 | - } |
|
948 | - |
|
949 | - return $this->getHost().':'.$port; |
|
950 | - } |
|
951 | - |
|
952 | - /** |
|
953 | - * Gets the scheme and HTTP host. |
|
954 | - * |
|
955 | - * @return string |
|
956 | - */ |
|
957 | - public function getSchemeWithHttpHost(): string |
|
958 | - { |
|
959 | - return $this->getScheme().'://'.$this->getHttpHost(); |
|
960 | - } |
|
961 | - |
|
962 | - /** |
|
963 | - * Get the root URL for the application. |
|
964 | - * |
|
965 | - * @return string |
|
966 | - */ |
|
967 | - public function root(): string |
|
968 | - { |
|
969 | - return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
970 | - } |
|
971 | - |
|
972 | - /** |
|
973 | - * Get the URL for the request. |
|
974 | - * |
|
975 | - * @return string |
|
976 | - */ |
|
977 | - public function url(): string |
|
978 | - { |
|
979 | - // Changed $this->path() for $this->getUri() |
|
980 | - return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); |
|
981 | - } |
|
982 | - |
|
983 | - /** |
|
984 | - * Returns the referer. |
|
985 | - * |
|
986 | - * @param string $default |
|
987 | - * |
|
988 | - * @return string |
|
989 | - */ |
|
990 | - public function referer(string $default = ''): string |
|
991 | - { |
|
992 | - return $this->server->get('HTTP_REFERER', $default); |
|
993 | - } |
|
894 | + return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
895 | + } |
|
896 | + |
|
897 | + /** |
|
898 | + * Get the user. |
|
899 | + * |
|
900 | + * @return string|null |
|
901 | + */ |
|
902 | + public function getUser(): ?string |
|
903 | + { |
|
904 | + $user = $this->uri->setUser( |
|
905 | + $this->headers->get('PHP_AUTH_USER') |
|
906 | + ); |
|
907 | + |
|
908 | + return $user; |
|
909 | + } |
|
910 | + |
|
911 | + /** |
|
912 | + * Get the password. |
|
913 | + * |
|
914 | + * @return string|null |
|
915 | + */ |
|
916 | + public function getPassword(): ?string |
|
917 | + { |
|
918 | + $password = $this->uri->setPassword( |
|
919 | + $this->headers->get('PHP_AUTH_PW') |
|
920 | + ); |
|
921 | + |
|
922 | + return $password; |
|
923 | + } |
|
924 | + |
|
925 | + /** |
|
926 | + * Gets the user info. |
|
927 | + * |
|
928 | + * @return string|null |
|
929 | + */ |
|
930 | + public function getUserInfo(): ?string |
|
931 | + { |
|
932 | + return $this->uri->getUserInfo(); |
|
933 | + } |
|
934 | + |
|
935 | + /** |
|
936 | + * Returns the HTTP host being requested. |
|
937 | + * |
|
938 | + * @return string |
|
939 | + */ |
|
940 | + public function getHttpHost(): string |
|
941 | + { |
|
942 | + $scheme = $this->getScheme(); |
|
943 | + $port = $this->getPort(); |
|
944 | + |
|
945 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
946 | + return $this->getHost(); |
|
947 | + } |
|
948 | + |
|
949 | + return $this->getHost().':'.$port; |
|
950 | + } |
|
951 | + |
|
952 | + /** |
|
953 | + * Gets the scheme and HTTP host. |
|
954 | + * |
|
955 | + * @return string |
|
956 | + */ |
|
957 | + public function getSchemeWithHttpHost(): string |
|
958 | + { |
|
959 | + return $this->getScheme().'://'.$this->getHttpHost(); |
|
960 | + } |
|
961 | + |
|
962 | + /** |
|
963 | + * Get the root URL for the application. |
|
964 | + * |
|
965 | + * @return string |
|
966 | + */ |
|
967 | + public function root(): string |
|
968 | + { |
|
969 | + return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
970 | + } |
|
971 | + |
|
972 | + /** |
|
973 | + * Get the URL for the request. |
|
974 | + * |
|
975 | + * @return string |
|
976 | + */ |
|
977 | + public function url(): string |
|
978 | + { |
|
979 | + // Changed $this->path() for $this->getUri() |
|
980 | + return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); |
|
981 | + } |
|
982 | + |
|
983 | + /** |
|
984 | + * Returns the referer. |
|
985 | + * |
|
986 | + * @param string $default |
|
987 | + * |
|
988 | + * @return string |
|
989 | + */ |
|
990 | + public function referer(string $default = ''): string |
|
991 | + { |
|
992 | + return $this->server->get('HTTP_REFERER', $default); |
|
993 | + } |
|
994 | 994 | |
995 | - /** |
|
996 | - * Attempts to detect if the current connection is secure through |
|
997 | - * over HTTPS protocol. |
|
998 | - * |
|
999 | - * @return bool |
|
1000 | - */ |
|
1001 | - public function secure(): bool |
|
1002 | - { |
|
1003 | - if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
1004 | - return true; |
|
1005 | - } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
1006 | - return true; |
|
1007 | - } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
1008 | - return true; |
|
1009 | - } |
|
1010 | - |
|
1011 | - return false; |
|
1012 | - } |
|
1013 | - |
|
1014 | - /** |
|
1015 | - * Returns the user agent. |
|
1016 | - * |
|
1017 | - * @param string|null $default |
|
1018 | - * |
|
1019 | - * @return string |
|
1020 | - */ |
|
1021 | - public function userAgent(string $default = null): string |
|
1022 | - { |
|
1023 | - return $this->server->get('HTTP_USER_AGENT', $default); |
|
1024 | - } |
|
995 | + /** |
|
996 | + * Attempts to detect if the current connection is secure through |
|
997 | + * over HTTPS protocol. |
|
998 | + * |
|
999 | + * @return bool |
|
1000 | + */ |
|
1001 | + public function secure(): bool |
|
1002 | + { |
|
1003 | + if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
1004 | + return true; |
|
1005 | + } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
1006 | + return true; |
|
1007 | + } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
1008 | + return true; |
|
1009 | + } |
|
1010 | + |
|
1011 | + return false; |
|
1012 | + } |
|
1013 | + |
|
1014 | + /** |
|
1015 | + * Returns the user agent. |
|
1016 | + * |
|
1017 | + * @param string|null $default |
|
1018 | + * |
|
1019 | + * @return string |
|
1020 | + */ |
|
1021 | + public function userAgent(string $default = null): string |
|
1022 | + { |
|
1023 | + return $this->server->get('HTTP_USER_AGENT', $default); |
|
1024 | + } |
|
1025 | 1025 | |
1026 | - /** |
|
1027 | - * Get the client IP address. |
|
1028 | - * |
|
1029 | - * @return string|null |
|
1030 | - */ |
|
1031 | - public function ip(): ?string |
|
1032 | - { |
|
1033 | - return $this->clientIp->getClientIp(); |
|
1034 | - } |
|
1035 | - |
|
1036 | - /** |
|
1037 | - * Get the route resolver callback. |
|
1038 | - * |
|
1039 | - * @return \Closure |
|
1040 | - */ |
|
1041 | - public function getRouteResolver(): Closure |
|
1042 | - { |
|
1043 | - return $this->routeResolver ?: function () { |
|
1044 | - // |
|
1045 | - }; |
|
1046 | - } |
|
1047 | - |
|
1048 | - /** |
|
1049 | - * Set the route resolver callback. |
|
1050 | - * |
|
1051 | - * @param \Closure $callback |
|
1052 | - * |
|
1053 | - * @return static |
|
1054 | - */ |
|
1055 | - public function setRouteResolver(Closure $callback): static |
|
1056 | - { |
|
1057 | - $this->routeResolver = $callback; |
|
1058 | - |
|
1059 | - return $this; |
|
1060 | - } |
|
1061 | - |
|
1062 | - /** |
|
1063 | - * Magic method. |
|
1064 | - * |
|
1065 | - * Get an element from the request. |
|
1066 | - * |
|
1067 | - * @return string[] |
|
1068 | - */ |
|
1069 | - public function __get($key) |
|
1070 | - { |
|
1071 | - return Arr::get($this->all(), $key, fn () => $this->route($key)); |
|
1072 | - } |
|
1073 | - |
|
1074 | - /** |
|
1075 | - * Magic method. |
|
1076 | - * |
|
1077 | - * Returns the Request as an HTTP string. |
|
1078 | - * |
|
1079 | - * @return string |
|
1080 | - */ |
|
1081 | - public function __toString(): string |
|
1082 | - { |
|
1083 | - $content = $this->getContent(); |
|
1084 | - |
|
1085 | - $cookieHeader = ''; |
|
1086 | - $cookies = []; |
|
1087 | - |
|
1088 | - foreach ($this->cookies as $key => $value) { |
|
1089 | - $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
1090 | - } |
|
1091 | - |
|
1092 | - if ($cookies) { |
|
1093 | - $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
1094 | - } |
|
1026 | + /** |
|
1027 | + * Get the client IP address. |
|
1028 | + * |
|
1029 | + * @return string|null |
|
1030 | + */ |
|
1031 | + public function ip(): ?string |
|
1032 | + { |
|
1033 | + return $this->clientIp->getClientIp(); |
|
1034 | + } |
|
1035 | + |
|
1036 | + /** |
|
1037 | + * Get the route resolver callback. |
|
1038 | + * |
|
1039 | + * @return \Closure |
|
1040 | + */ |
|
1041 | + public function getRouteResolver(): Closure |
|
1042 | + { |
|
1043 | + return $this->routeResolver ?: function () { |
|
1044 | + // |
|
1045 | + }; |
|
1046 | + } |
|
1047 | + |
|
1048 | + /** |
|
1049 | + * Set the route resolver callback. |
|
1050 | + * |
|
1051 | + * @param \Closure $callback |
|
1052 | + * |
|
1053 | + * @return static |
|
1054 | + */ |
|
1055 | + public function setRouteResolver(Closure $callback): static |
|
1056 | + { |
|
1057 | + $this->routeResolver = $callback; |
|
1058 | + |
|
1059 | + return $this; |
|
1060 | + } |
|
1061 | + |
|
1062 | + /** |
|
1063 | + * Magic method. |
|
1064 | + * |
|
1065 | + * Get an element from the request. |
|
1066 | + * |
|
1067 | + * @return string[] |
|
1068 | + */ |
|
1069 | + public function __get($key) |
|
1070 | + { |
|
1071 | + return Arr::get($this->all(), $key, fn () => $this->route($key)); |
|
1072 | + } |
|
1073 | + |
|
1074 | + /** |
|
1075 | + * Magic method. |
|
1076 | + * |
|
1077 | + * Returns the Request as an HTTP string. |
|
1078 | + * |
|
1079 | + * @return string |
|
1080 | + */ |
|
1081 | + public function __toString(): string |
|
1082 | + { |
|
1083 | + $content = $this->getContent(); |
|
1084 | + |
|
1085 | + $cookieHeader = ''; |
|
1086 | + $cookies = []; |
|
1087 | + |
|
1088 | + foreach ($this->cookies as $key => $value) { |
|
1089 | + $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
1090 | + } |
|
1091 | + |
|
1092 | + if ($cookies) { |
|
1093 | + $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
1094 | + } |
|
1095 | 1095 | |
1096 | - return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
1097 | - $this->headers. |
|
1098 | - $cookieHeader."\r\n". |
|
1099 | - $content; |
|
1100 | - } |
|
1101 | - |
|
1102 | - /** |
|
1103 | - * Magic method. |
|
1104 | - * |
|
1105 | - * Clones the current request. |
|
1106 | - * |
|
1107 | - * @return void |
|
1108 | - */ |
|
1109 | - public function __clone() |
|
1110 | - { |
|
1111 | - $this->query = clone $this->query; |
|
1112 | - $this->request = clone $this->request; |
|
1113 | - $this->attributes = clone $this->attributes; |
|
1114 | - $this->cookies = clone $this->cookies; |
|
1115 | - $this->files = clone $this->files; |
|
1116 | - $this->server = clone $this->server; |
|
1117 | - $this->headers = clone $this->headers; |
|
1118 | - } |
|
1096 | + return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
1097 | + $this->headers. |
|
1098 | + $cookieHeader."\r\n". |
|
1099 | + $content; |
|
1100 | + } |
|
1101 | + |
|
1102 | + /** |
|
1103 | + * Magic method. |
|
1104 | + * |
|
1105 | + * Clones the current request. |
|
1106 | + * |
|
1107 | + * @return void |
|
1108 | + */ |
|
1109 | + public function __clone() |
|
1110 | + { |
|
1111 | + $this->query = clone $this->query; |
|
1112 | + $this->request = clone $this->request; |
|
1113 | + $this->attributes = clone $this->attributes; |
|
1114 | + $this->cookies = clone $this->cookies; |
|
1115 | + $this->files = clone $this->files; |
|
1116 | + $this->server = clone $this->server; |
|
1117 | + $this->headers = clone $this->headers; |
|
1118 | + } |
|
1119 | 1119 | } |
1120 | 1120 | \ No newline at end of file |
@@ -942,7 +942,7 @@ discard block |
||
942 | 942 | $scheme = $this->getScheme(); |
943 | 943 | $port = $this->getPort(); |
944 | 944 | |
945 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
945 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
946 | 946 | return $this->getHost(); |
947 | 947 | } |
948 | 948 | |
@@ -1040,7 +1040,7 @@ discard block |
||
1040 | 1040 | */ |
1041 | 1041 | public function getRouteResolver(): Closure |
1042 | 1042 | { |
1043 | - return $this->routeResolver ?: function () { |
|
1043 | + return $this->routeResolver ?: function() { |
|
1044 | 1044 | // |
1045 | 1045 | }; |
1046 | 1046 | } |
@@ -1086,7 +1086,7 @@ discard block |
||
1086 | 1086 | $cookies = []; |
1087 | 1087 | |
1088 | 1088 | foreach ($this->cookies as $key => $value) { |
1089 | - $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
1089 | + $cookies[] = is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
1090 | 1090 | } |
1091 | 1091 | |
1092 | 1092 | if ($cookies) { |