@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | */ |
152 | 152 | protected function call() |
153 | 153 | { |
154 | - return function ($stack, $pipe) { |
|
155 | - return function ($passable) use ($stack, $pipe) { |
|
154 | + return function($stack, $pipe) { |
|
155 | + return function($passable) use ($stack, $pipe) { |
|
156 | 156 | try { |
157 | 157 | if (is_callable($pipe)) { |
158 | 158 | return $pipe($passable, $stack); |
@@ -205,10 +205,10 @@ discard block |
||
205 | 205 | */ |
206 | 206 | protected function prepareDestination(Closure $destination) |
207 | 207 | { |
208 | - return function ($passable) use ($destination) { |
|
208 | + return function($passable) use ($destination) { |
|
209 | 209 | try { |
210 | 210 | return $destination($passable); |
211 | - } catch(Throwable $e) { |
|
211 | + } catch (Throwable $e) { |
|
212 | 212 | return $this->handleException($passable, $e); |
213 | 213 | } |
214 | 214 | }; |
@@ -99,13 +99,13 @@ |
||
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
102 | - * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
103 | - * Can redirect via a Location header. |
|
104 | - * |
|
105 | - * @param string $url The url |
|
106 | - * |
|
107 | - * @return $this |
|
108 | - */ |
|
102 | + * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
103 | + * Can redirect via a Location header. |
|
104 | + * |
|
105 | + * @param string $url The url |
|
106 | + * |
|
107 | + * @return $this |
|
108 | + */ |
|
109 | 109 | public function setTargetUrl($url) |
110 | 110 | { |
111 | 111 | if ('' === ($url ?? '')) { |
@@ -39,297 +39,297 @@ |
||
39 | 39 | */ |
40 | 40 | class Response extends Status |
41 | 41 | { |
42 | - use ResponseTrait; |
|
43 | - |
|
44 | - /** |
|
45 | - * Sets up the response with a content and a status code. |
|
46 | - * |
|
47 | - * @param mixed $content The response content |
|
48 | - * @param int $status The response status (200 by default) |
|
49 | - * @param array $headers Array of HTTP headers for this response |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function __construct($content = '', int $status = 200, array $headers = []) |
|
54 | - { |
|
55 | - $this->setContent($content); |
|
56 | - $this->setStatusCode($status); |
|
42 | + use ResponseTrait; |
|
43 | + |
|
44 | + /** |
|
45 | + * Sets up the response with a content and a status code. |
|
46 | + * |
|
47 | + * @param mixed $content The response content |
|
48 | + * @param int $status The response status (200 by default) |
|
49 | + * @param array $headers Array of HTTP headers for this response |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function __construct($content = '', int $status = 200, array $headers = []) |
|
54 | + { |
|
55 | + $this->setContent($content); |
|
56 | + $this->setStatusCode($status); |
|
57 | 57 | |
58 | - $this->server = new Server($_SERVER); |
|
59 | - $this->headers = new Headers($headers); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Creates an instance of the same response class for rendering contents to the content, |
|
64 | - * status code and headers. |
|
65 | - * |
|
66 | - * @param mixed $content The response content |
|
67 | - * @param int $status The HTTP response status for this response (200 by default) |
|
68 | - * @param array $headers Array of HTTP headers for this response |
|
69 | - * |
|
70 | - * @return static |
|
71 | - */ |
|
72 | - public static function render($content = '', $status = 200, $headers = []) |
|
73 | - { |
|
74 | - return new static($content, $status, $headers); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Gets the current response content. |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getContent() |
|
83 | - { |
|
84 | - return $this->content; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Gets the response status code. |
|
89 | - * |
|
90 | - * The status code is a 3-digit code to specify server response results to the browser. |
|
91 | - * |
|
92 | - * @return int |
|
93 | - * |
|
94 | - * @throws \BadMethodCallException |
|
95 | - */ |
|
96 | - public function getStatusCode() |
|
97 | - { |
|
98 | - if (empty($this->status)) { |
|
99 | - throw new BadMethodCallException('HTTP Response is missing a status code.'); |
|
100 | - } |
|
101 | - |
|
102 | - return $this->status; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Sends the headers if they haven't already been sent. |
|
107 | - * Returns whether they were sent or not. |
|
108 | - * |
|
109 | - * @return bool |
|
110 | - * |
|
111 | - * @uses \Syscodes\Http\Http |
|
112 | - */ |
|
113 | - public function sendHeaders() |
|
114 | - { |
|
115 | - // Have the headers already been sent? |
|
116 | - if (headers_sent()) { |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - // Headers |
|
121 | - foreach ($this->headers->allPreserveCase() as $name => $values) { |
|
122 | - $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
123 | - |
|
124 | - foreach ($values as $value) { |
|
125 | - header($name.': '. $value, $replace, $this->status); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - // Status |
|
130 | - if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
131 | - // Send the protocol/status line first, FCGI servers need different status header |
|
132 | - header(sprintf('Status: %s %s', $this->status, $this->statusText)); |
|
133 | - } else { |
|
134 | - $this->protocol = (string) $this->server->get('SERVER_PROTOCOL') ?: 'HTTP/1.1'; |
|
135 | - header(sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText), true, $this->status); |
|
136 | - } |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Sends content for the current web response. |
|
143 | - * |
|
144 | - * @return $this |
|
145 | - */ |
|
146 | - public function sendContent() |
|
147 | - { |
|
148 | - echo $this->content; |
|
149 | - |
|
150 | - return $this; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sends the response to the output buffer. Optionally, headers will be sent. |
|
155 | - * |
|
156 | - * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
157 | - * |
|
158 | - * @return $this |
|
159 | - */ |
|
160 | - public function send($sendHeader = false) |
|
161 | - { |
|
162 | - if ($sendHeader) { |
|
163 | - $this->sendHeaders(); |
|
164 | - } |
|
165 | - |
|
166 | - if (null !== $this->content) { |
|
167 | - $this->sendContent(); |
|
168 | - } |
|
169 | - |
|
170 | - return $this; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Sends the content of the message to the browser. |
|
175 | - * |
|
176 | - * @param mixed $content The response content |
|
177 | - * |
|
178 | - * @return $this |
|
179 | - */ |
|
180 | - public function setContent($content) |
|
181 | - { |
|
182 | - if ($content !== null && ! is_string($content) && ! is_numeric($content) && ! is_callable([$content, '__toString'])) { |
|
183 | - throw new UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content))); |
|
184 | - } |
|
185 | - |
|
186 | - if ($content instanceof JsonSerializable || is_array($content)) { |
|
187 | - $this->header('Content-Type', 'application/json'); |
|
188 | - |
|
189 | - $content = json_encode($content); |
|
190 | - } elseif ($content instanceof Renderable) { |
|
191 | - $content = $content->render(); |
|
192 | - } |
|
58 | + $this->server = new Server($_SERVER); |
|
59 | + $this->headers = new Headers($headers); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Creates an instance of the same response class for rendering contents to the content, |
|
64 | + * status code and headers. |
|
65 | + * |
|
66 | + * @param mixed $content The response content |
|
67 | + * @param int $status The HTTP response status for this response (200 by default) |
|
68 | + * @param array $headers Array of HTTP headers for this response |
|
69 | + * |
|
70 | + * @return static |
|
71 | + */ |
|
72 | + public static function render($content = '', $status = 200, $headers = []) |
|
73 | + { |
|
74 | + return new static($content, $status, $headers); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Gets the current response content. |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getContent() |
|
83 | + { |
|
84 | + return $this->content; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Gets the response status code. |
|
89 | + * |
|
90 | + * The status code is a 3-digit code to specify server response results to the browser. |
|
91 | + * |
|
92 | + * @return int |
|
93 | + * |
|
94 | + * @throws \BadMethodCallException |
|
95 | + */ |
|
96 | + public function getStatusCode() |
|
97 | + { |
|
98 | + if (empty($this->status)) { |
|
99 | + throw new BadMethodCallException('HTTP Response is missing a status code.'); |
|
100 | + } |
|
101 | + |
|
102 | + return $this->status; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Sends the headers if they haven't already been sent. |
|
107 | + * Returns whether they were sent or not. |
|
108 | + * |
|
109 | + * @return bool |
|
110 | + * |
|
111 | + * @uses \Syscodes\Http\Http |
|
112 | + */ |
|
113 | + public function sendHeaders() |
|
114 | + { |
|
115 | + // Have the headers already been sent? |
|
116 | + if (headers_sent()) { |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + // Headers |
|
121 | + foreach ($this->headers->allPreserveCase() as $name => $values) { |
|
122 | + $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
123 | + |
|
124 | + foreach ($values as $value) { |
|
125 | + header($name.': '. $value, $replace, $this->status); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + // Status |
|
130 | + if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
131 | + // Send the protocol/status line first, FCGI servers need different status header |
|
132 | + header(sprintf('Status: %s %s', $this->status, $this->statusText)); |
|
133 | + } else { |
|
134 | + $this->protocol = (string) $this->server->get('SERVER_PROTOCOL') ?: 'HTTP/1.1'; |
|
135 | + header(sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText), true, $this->status); |
|
136 | + } |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Sends content for the current web response. |
|
143 | + * |
|
144 | + * @return $this |
|
145 | + */ |
|
146 | + public function sendContent() |
|
147 | + { |
|
148 | + echo $this->content; |
|
149 | + |
|
150 | + return $this; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sends the response to the output buffer. Optionally, headers will be sent. |
|
155 | + * |
|
156 | + * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
157 | + * |
|
158 | + * @return $this |
|
159 | + */ |
|
160 | + public function send($sendHeader = false) |
|
161 | + { |
|
162 | + if ($sendHeader) { |
|
163 | + $this->sendHeaders(); |
|
164 | + } |
|
165 | + |
|
166 | + if (null !== $this->content) { |
|
167 | + $this->sendContent(); |
|
168 | + } |
|
169 | + |
|
170 | + return $this; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Sends the content of the message to the browser. |
|
175 | + * |
|
176 | + * @param mixed $content The response content |
|
177 | + * |
|
178 | + * @return $this |
|
179 | + */ |
|
180 | + public function setContent($content) |
|
181 | + { |
|
182 | + if ($content !== null && ! is_string($content) && ! is_numeric($content) && ! is_callable([$content, '__toString'])) { |
|
183 | + throw new UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content))); |
|
184 | + } |
|
185 | + |
|
186 | + if ($content instanceof JsonSerializable || is_array($content)) { |
|
187 | + $this->header('Content-Type', 'application/json'); |
|
188 | + |
|
189 | + $content = json_encode($content); |
|
190 | + } elseif ($content instanceof Renderable) { |
|
191 | + $content = $content->render(); |
|
192 | + } |
|
193 | 193 | |
194 | - $this->content = $content ?? ''; |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Prepares the Response before it is sent to the client. |
|
201 | - * |
|
202 | - * @param \Syscodes\Http\Request $request |
|
203 | - * |
|
204 | - * @return $this |
|
205 | - */ |
|
206 | - public function prepare($request) |
|
207 | - { |
|
208 | - $headers = $this->headers; |
|
209 | - |
|
210 | - if ($this->isInformational() || $this->isEmpty()) { |
|
211 | - $this->setContent(null); |
|
212 | - $headers->remove('Content-Type'); |
|
213 | - $headers->remove('Content-Length'); |
|
214 | - } |
|
215 | - |
|
216 | - return $this; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Sets the response status code. |
|
221 | - * |
|
222 | - * @param int $code The status code |
|
223 | - * @param string|null $text The status text |
|
224 | - * |
|
225 | - * @return $this |
|
226 | - * |
|
227 | - * @throws \InvalidArgumentException |
|
228 | - */ |
|
229 | - public function setStatusCode(int $code, $text = null) |
|
230 | - { |
|
231 | - $this->status = $code; |
|
232 | - |
|
233 | - // Valid range? |
|
234 | - if ($this->isInvalid()) { |
|
235 | - throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
236 | - } |
|
237 | - |
|
238 | - // Check if you have an accepted status code if not shows to a message of unknown status |
|
239 | - if (null === $text) { |
|
240 | - $this->statusText = isset($this->statusCodes[$code]) ? $this->statusCodes[$code] : __('response.UnknownStatus'); |
|
241 | - |
|
242 | - return $this; |
|
243 | - } |
|
244 | - |
|
245 | - if (false === $text) { |
|
246 | - $this->statusText = ''; |
|
247 | - |
|
248 | - return $this; |
|
249 | - } |
|
250 | - |
|
251 | - $this->statusText = $text; |
|
252 | - |
|
253 | - return $this; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Is response invalid? |
|
258 | - * |
|
259 | - * @final |
|
260 | - * |
|
261 | - * @return void |
|
262 | - */ |
|
263 | - public function isInvalid(): bool |
|
264 | - { |
|
265 | - return $this->status < 100 || $this->status >= 600; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Is response informative? |
|
270 | - * |
|
271 | - * @final |
|
272 | - * |
|
273 | - * @return void |
|
274 | - */ |
|
275 | - public function isInformational() |
|
276 | - { |
|
277 | - return $this->status >= 100 && $this->status < 200; |
|
278 | - } |
|
194 | + $this->content = $content ?? ''; |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Prepares the Response before it is sent to the client. |
|
201 | + * |
|
202 | + * @param \Syscodes\Http\Request $request |
|
203 | + * |
|
204 | + * @return $this |
|
205 | + */ |
|
206 | + public function prepare($request) |
|
207 | + { |
|
208 | + $headers = $this->headers; |
|
209 | + |
|
210 | + if ($this->isInformational() || $this->isEmpty()) { |
|
211 | + $this->setContent(null); |
|
212 | + $headers->remove('Content-Type'); |
|
213 | + $headers->remove('Content-Length'); |
|
214 | + } |
|
215 | + |
|
216 | + return $this; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Sets the response status code. |
|
221 | + * |
|
222 | + * @param int $code The status code |
|
223 | + * @param string|null $text The status text |
|
224 | + * |
|
225 | + * @return $this |
|
226 | + * |
|
227 | + * @throws \InvalidArgumentException |
|
228 | + */ |
|
229 | + public function setStatusCode(int $code, $text = null) |
|
230 | + { |
|
231 | + $this->status = $code; |
|
232 | + |
|
233 | + // Valid range? |
|
234 | + if ($this->isInvalid()) { |
|
235 | + throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
236 | + } |
|
237 | + |
|
238 | + // Check if you have an accepted status code if not shows to a message of unknown status |
|
239 | + if (null === $text) { |
|
240 | + $this->statusText = isset($this->statusCodes[$code]) ? $this->statusCodes[$code] : __('response.UnknownStatus'); |
|
241 | + |
|
242 | + return $this; |
|
243 | + } |
|
244 | + |
|
245 | + if (false === $text) { |
|
246 | + $this->statusText = ''; |
|
247 | + |
|
248 | + return $this; |
|
249 | + } |
|
250 | + |
|
251 | + $this->statusText = $text; |
|
252 | + |
|
253 | + return $this; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Is response invalid? |
|
258 | + * |
|
259 | + * @final |
|
260 | + * |
|
261 | + * @return void |
|
262 | + */ |
|
263 | + public function isInvalid(): bool |
|
264 | + { |
|
265 | + return $this->status < 100 || $this->status >= 600; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Is response informative? |
|
270 | + * |
|
271 | + * @final |
|
272 | + * |
|
273 | + * @return void |
|
274 | + */ |
|
275 | + public function isInformational() |
|
276 | + { |
|
277 | + return $this->status >= 100 && $this->status < 200; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Is the response a redirect? |
|
282 | - * |
|
283 | - * @final |
|
284 | - * |
|
285 | - * @return void |
|
286 | - */ |
|
287 | - public function isRedirection() |
|
288 | - { |
|
289 | - return $this->status >= 300 && $this->status < 400; |
|
290 | - } |
|
280 | + /** |
|
281 | + * Is the response a redirect? |
|
282 | + * |
|
283 | + * @final |
|
284 | + * |
|
285 | + * @return void |
|
286 | + */ |
|
287 | + public function isRedirection() |
|
288 | + { |
|
289 | + return $this->status >= 300 && $this->status < 400; |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Is the response empty? |
|
294 | - * |
|
295 | - * @final |
|
296 | - * |
|
297 | - * @return void |
|
298 | - */ |
|
299 | - public function isEmpty() |
|
300 | - { |
|
301 | - return in_array($this->status, [204, 304]); |
|
302 | - } |
|
292 | + /** |
|
293 | + * Is the response empty? |
|
294 | + * |
|
295 | + * @final |
|
296 | + * |
|
297 | + * @return void |
|
298 | + */ |
|
299 | + public function isEmpty() |
|
300 | + { |
|
301 | + return in_array($this->status, [204, 304]); |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Is the response a redirect of some form? |
|
306 | - * |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - public function isRedirect() |
|
310 | - { |
|
311 | - return in_array($this->status, [301, 302, 303, 307, 308]); |
|
312 | - } |
|
304 | + /** |
|
305 | + * Is the response a redirect of some form? |
|
306 | + * |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + public function isRedirect() |
|
310 | + { |
|
311 | + return in_array($this->status, [301, 302, 303, 307, 308]); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Returns the Response as an HTTP string. |
|
316 | - * |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - public function __toString() |
|
320 | - { |
|
321 | - return sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText)."\r\n". |
|
322 | - $this->headers."\r\n". |
|
323 | - $this->getContent(); |
|
324 | - } |
|
314 | + /** |
|
315 | + * Returns the Response as an HTTP string. |
|
316 | + * |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + public function __toString() |
|
320 | + { |
|
321 | + return sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText)."\r\n". |
|
322 | + $this->headers."\r\n". |
|
323 | + $this->getContent(); |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * Clone the current Response instance. |
|
328 | - * |
|
329 | - * @return void |
|
330 | - */ |
|
331 | - public function __clone() |
|
332 | - { |
|
333 | - $this->headers = clone $this->headers; |
|
334 | - } |
|
326 | + /** |
|
327 | + * Clone the current Response instance. |
|
328 | + * |
|
329 | + * @return void |
|
330 | + */ |
|
331 | + public function __clone() |
|
332 | + { |
|
333 | + $this->headers = clone $this->headers; |
|
334 | + } |
|
335 | 335 | } |
336 | 336 | \ No newline at end of file |
@@ -122,7 +122,7 @@ |
||
122 | 122 | $replace = 0 === strcasecmp($name, 'Content-Type'); |
123 | 123 | |
124 | 124 | foreach ($values as $value) { |
125 | - header($name.': '. $value, $replace, $this->status); |
|
125 | + header($name.': '.$value, $replace, $this->status); |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 |
@@ -39,868 +39,868 @@ |
||
39 | 39 | */ |
40 | 40 | class Request |
41 | 41 | { |
42 | - /** |
|
43 | - * Holds the global active request instance. |
|
44 | - * |
|
45 | - * @var bool $requestURI |
|
46 | - */ |
|
47 | - protected static $requestURI; |
|
48 | - |
|
49 | - /** |
|
50 | - * The base URL. |
|
51 | - * |
|
52 | - * @var string $baseUrl |
|
53 | - */ |
|
54 | - protected $baseUrl; |
|
55 | - |
|
56 | - /** |
|
57 | - * Gets cookies ($_COOKIE). |
|
58 | - * |
|
59 | - * @var string $cookies |
|
60 | - */ |
|
61 | - public $cookies; |
|
62 | - |
|
63 | - /** |
|
64 | - * Gets the string with format JSON. |
|
65 | - * |
|
66 | - * @var string|resource|null $content |
|
67 | - */ |
|
68 | - protected $content; |
|
69 | - |
|
70 | - /** |
|
71 | - * The default Locale this request. |
|
72 | - * |
|
73 | - * @var string $defaultLocale |
|
74 | - */ |
|
75 | - protected $defaultLocale = 'en'; |
|
42 | + /** |
|
43 | + * Holds the global active request instance. |
|
44 | + * |
|
45 | + * @var bool $requestURI |
|
46 | + */ |
|
47 | + protected static $requestURI; |
|
48 | + |
|
49 | + /** |
|
50 | + * The base URL. |
|
51 | + * |
|
52 | + * @var string $baseUrl |
|
53 | + */ |
|
54 | + protected $baseUrl; |
|
55 | + |
|
56 | + /** |
|
57 | + * Gets cookies ($_COOKIE). |
|
58 | + * |
|
59 | + * @var string $cookies |
|
60 | + */ |
|
61 | + public $cookies; |
|
62 | + |
|
63 | + /** |
|
64 | + * Gets the string with format JSON. |
|
65 | + * |
|
66 | + * @var string|resource|null $content |
|
67 | + */ |
|
68 | + protected $content; |
|
69 | + |
|
70 | + /** |
|
71 | + * The default Locale this request. |
|
72 | + * |
|
73 | + * @var string $defaultLocale |
|
74 | + */ |
|
75 | + protected $defaultLocale = 'en'; |
|
76 | 76 | |
77 | - /** |
|
78 | - * Gets files request ($_FILES). |
|
79 | - * |
|
80 | - * @var string $files |
|
81 | - */ |
|
82 | - public $files; |
|
83 | - |
|
84 | - /** |
|
85 | - * The detected uri and server variables. |
|
86 | - * |
|
87 | - * @var string $http |
|
88 | - */ |
|
89 | - protected $http; |
|
90 | - |
|
91 | - /** |
|
92 | - * The decoded JSON content for the request. |
|
93 | - * |
|
94 | - * @var \Syscodes\Http\Contributors\Parameters|null $json |
|
95 | - */ |
|
96 | - protected $json; |
|
97 | - |
|
98 | - /** |
|
99 | - * The current language of the application. |
|
100 | - * |
|
101 | - * @var string $languages |
|
102 | - */ |
|
103 | - protected $languages; |
|
77 | + /** |
|
78 | + * Gets files request ($_FILES). |
|
79 | + * |
|
80 | + * @var string $files |
|
81 | + */ |
|
82 | + public $files; |
|
83 | + |
|
84 | + /** |
|
85 | + * The detected uri and server variables. |
|
86 | + * |
|
87 | + * @var string $http |
|
88 | + */ |
|
89 | + protected $http; |
|
90 | + |
|
91 | + /** |
|
92 | + * The decoded JSON content for the request. |
|
93 | + * |
|
94 | + * @var \Syscodes\Http\Contributors\Parameters|null $json |
|
95 | + */ |
|
96 | + protected $json; |
|
97 | + |
|
98 | + /** |
|
99 | + * The current language of the application. |
|
100 | + * |
|
101 | + * @var string $languages |
|
102 | + */ |
|
103 | + protected $languages; |
|
104 | 104 | |
105 | - /** |
|
106 | - * The method name. |
|
107 | - * |
|
108 | - * @var string $method |
|
109 | - */ |
|
110 | - protected $method; |
|
111 | - |
|
112 | - /** |
|
113 | - * The path info of URL. |
|
114 | - * |
|
115 | - * @var string $pathInfo |
|
116 | - */ |
|
117 | - protected $pathInfo; |
|
118 | - |
|
119 | - /** |
|
120 | - * Request body parameters ($_POST). |
|
121 | - * |
|
122 | - * @var \Syscodes\Http\Contributors\Parameters $request |
|
123 | - */ |
|
124 | - public $request; |
|
125 | - |
|
126 | - /** |
|
127 | - * Get request URI. |
|
128 | - * |
|
129 | - * @var string $requestToURI |
|
130 | - */ |
|
131 | - protected $requestToURI; |
|
132 | - |
|
133 | - /** |
|
134 | - * The detected uri and server variables ($_FILES). |
|
135 | - * |
|
136 | - * @var array $server |
|
137 | - */ |
|
138 | - public $server = []; |
|
139 | - |
|
140 | - /** |
|
141 | - * List of routes uri. |
|
142 | - * |
|
143 | - * @var string|array $uri |
|
144 | - */ |
|
145 | - public $uri; |
|
146 | - |
|
147 | - /** |
|
148 | - * Stores the valid locale codes. |
|
149 | - * |
|
150 | - * @var array $validLocales |
|
151 | - */ |
|
152 | - protected $validLocales = []; |
|
153 | - |
|
154 | - /** |
|
155 | - * Constructor: Create new the Request class. |
|
156 | - * |
|
157 | - * @param array $request |
|
158 | - * @param array $cookies |
|
159 | - * @param array $files |
|
160 | - * @param array $server |
|
161 | - * @param string|resource|null $content (null by default) |
|
162 | - * |
|
163 | - * @return void |
|
164 | - */ |
|
165 | - public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
166 | - { |
|
167 | - static::$requestURI = $this; |
|
105 | + /** |
|
106 | + * The method name. |
|
107 | + * |
|
108 | + * @var string $method |
|
109 | + */ |
|
110 | + protected $method; |
|
111 | + |
|
112 | + /** |
|
113 | + * The path info of URL. |
|
114 | + * |
|
115 | + * @var string $pathInfo |
|
116 | + */ |
|
117 | + protected $pathInfo; |
|
118 | + |
|
119 | + /** |
|
120 | + * Request body parameters ($_POST). |
|
121 | + * |
|
122 | + * @var \Syscodes\Http\Contributors\Parameters $request |
|
123 | + */ |
|
124 | + public $request; |
|
125 | + |
|
126 | + /** |
|
127 | + * Get request URI. |
|
128 | + * |
|
129 | + * @var string $requestToURI |
|
130 | + */ |
|
131 | + protected $requestToURI; |
|
132 | + |
|
133 | + /** |
|
134 | + * The detected uri and server variables ($_FILES). |
|
135 | + * |
|
136 | + * @var array $server |
|
137 | + */ |
|
138 | + public $server = []; |
|
139 | + |
|
140 | + /** |
|
141 | + * List of routes uri. |
|
142 | + * |
|
143 | + * @var string|array $uri |
|
144 | + */ |
|
145 | + public $uri; |
|
146 | + |
|
147 | + /** |
|
148 | + * Stores the valid locale codes. |
|
149 | + * |
|
150 | + * @var array $validLocales |
|
151 | + */ |
|
152 | + protected $validLocales = []; |
|
153 | + |
|
154 | + /** |
|
155 | + * Constructor: Create new the Request class. |
|
156 | + * |
|
157 | + * @param array $request |
|
158 | + * @param array $cookies |
|
159 | + * @param array $files |
|
160 | + * @param array $server |
|
161 | + * @param string|resource|null $content (null by default) |
|
162 | + * |
|
163 | + * @return void |
|
164 | + */ |
|
165 | + public function __construct(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
166 | + { |
|
167 | + static::$requestURI = $this; |
|
168 | 168 | |
169 | - $this->initialize($request, $cookies, $files, $server, $content); |
|
170 | - |
|
171 | - $this->detectURI(config('app.uriProtocol'), config('app.baseUrl')); |
|
172 | - |
|
173 | - $this->detectLocale(); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Sets the parameters for this request. |
|
178 | - * |
|
179 | - * @param array $request |
|
180 | - * @param array $cookies |
|
181 | - * @param array $files |
|
182 | - * @param array $server |
|
183 | - * |
|
184 | - * @return void |
|
185 | - */ |
|
186 | - public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
187 | - { |
|
188 | - $this->request = new Parameters($request); |
|
189 | - $this->cookies = new Inputs($cookies); |
|
190 | - $this->files = new Files($files); |
|
191 | - $this->server = new Server($server); |
|
192 | - $this->headers = new Headers($this->server->all()); |
|
193 | - |
|
194 | - $this->uri = new URI; |
|
195 | - $this->http = new Http; |
|
196 | - $this->method = null; |
|
197 | - $this->baseUrl = null; |
|
198 | - $this->content = $content; |
|
199 | - $this->pathInfo = null; |
|
200 | - $this->languages = null; |
|
201 | - $this->validLocales = config('app.supportedLocales'); |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Create a new Syscodes HTTP request from server variables. |
|
206 | - * |
|
207 | - * @return static |
|
208 | - */ |
|
209 | - public static function capture() |
|
210 | - { |
|
211 | - return static::createFromRequest(static::createFromRequestGlobals()); |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Creates an Syscodes request from of the Request class instance. |
|
216 | - * |
|
217 | - * @param \Syscodes\Http\Request $request |
|
218 | - * |
|
219 | - * @return static |
|
220 | - */ |
|
221 | - public static function createFromRequest($request) |
|
222 | - { |
|
223 | - $newRequest = (new static)->duplicate( |
|
224 | - $request->request->all(), $request->cookies->all(), |
|
225 | - $request->files->all(), $request->server->all() |
|
226 | - ); |
|
227 | - |
|
228 | - $newRequest->content = $request->content; |
|
229 | - |
|
230 | - return $newRequest; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Creates a new request with value from PHP's super global. |
|
235 | - * |
|
236 | - * @return static |
|
237 | - */ |
|
238 | - public static function createFromRequestGlobals() |
|
239 | - { |
|
240 | - $request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER); |
|
241 | - |
|
242 | - return $request; |
|
243 | - } |
|
244 | - |
|
245 | - /** |
|
246 | - * Creates a new request from a factory. |
|
247 | - * |
|
248 | - * @param array $request |
|
249 | - * @param array $cookies |
|
250 | - * @param array $files |
|
251 | - * @param array $server |
|
252 | - * |
|
253 | - * @return static |
|
254 | - */ |
|
255 | - protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
256 | - { |
|
257 | - if (self::$requestURI) { |
|
258 | - $request = (self::$requestURI)($request, $cookies, $files, $server); |
|
259 | - |
|
260 | - if ( ! $request instanceof self) { |
|
261 | - throw new LogicException('The Request active must return an instance of Syscodes\Http\Request'); |
|
262 | - } |
|
263 | - |
|
264 | - return $request; |
|
265 | - } |
|
266 | - |
|
267 | - return new static($request, $cookies, $files, $server); |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Clones a request and overrides some of its parameters. |
|
272 | - * |
|
273 | - * @param array $request |
|
274 | - * @param array $cookies |
|
275 | - * @param array $files |
|
276 | - * @param array $server |
|
277 | - * |
|
278 | - * @return static |
|
279 | - */ |
|
280 | - public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
281 | - { |
|
282 | - $duplicate = clone $this; |
|
283 | - |
|
284 | - if (null !== $request) { |
|
285 | - $duplicate->request = new Parameters($request); |
|
286 | - } |
|
287 | - |
|
288 | - if (null !== $cookies) { |
|
289 | - $duplicate->cookies = new Inputs($cookies); |
|
290 | - } |
|
291 | - |
|
292 | - if (null !== $files) { |
|
293 | - $duplicate->files = new Files($files); |
|
294 | - } |
|
295 | - |
|
296 | - if (null !== $server) { |
|
297 | - $duplicate->server = new Server($server); |
|
298 | - $duplicate->headers = new Headers($duplicate->server->all()); |
|
299 | - } |
|
300 | - |
|
301 | - $duplicate->uri = new URI; |
|
302 | - $duplicate->http = new Http; |
|
303 | - $duplicate->locale = null; |
|
304 | - $duplicate->method = null; |
|
305 | - $duplicate->baseUrl = null; |
|
306 | - $duplicate->pathInfo = null; |
|
307 | - $duplicate->validLocales = config('app.supportedLocales'); |
|
308 | - |
|
309 | - return $duplicate; |
|
310 | - } |
|
311 | - |
|
312 | - /** |
|
313 | - * Returns the active request currently being used. |
|
314 | - * |
|
315 | - * @param \Syscodes\Http\Request|bool|null $request Overwrite current request |
|
316 | - * before returning, false prevents |
|
317 | - * overwrite |
|
318 | - * |
|
319 | - * @return \Syscodes\Http\Request |
|
320 | - */ |
|
321 | - public static function active($request = false) |
|
322 | - { |
|
323 | - if ($request !== false) { |
|
324 | - static::$requestURI = $request; |
|
325 | - } |
|
326 | - |
|
327 | - return static::$requestURI; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Returns the desired segment, or $default if it does not exist. |
|
332 | - * |
|
333 | - * @param int $index The segment number (1-based index) |
|
334 | - * @param mixed $default Default value to return |
|
335 | - * |
|
336 | - * @return string |
|
337 | - */ |
|
338 | - public function segment($index, $default = null) |
|
339 | - { |
|
340 | - if ($request = static::active()) { |
|
341 | - return $request->uri->getSegment($index, $default); |
|
342 | - } |
|
343 | - |
|
344 | - return null; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Returns all segments in an array. For total of segments |
|
349 | - * used the function PHP count(). |
|
350 | - * |
|
351 | - * @return array |
|
352 | - */ |
|
353 | - public function segments() |
|
354 | - { |
|
355 | - if ($request = static::active()) { |
|
356 | - return $request->uri->getSegments(); |
|
357 | - } |
|
358 | - |
|
359 | - return null; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * Returns the total number of segment. |
|
364 | - * |
|
365 | - * @return int|null |
|
366 | - */ |
|
367 | - public function totalSegments() |
|
368 | - { |
|
369 | - if ($request = static::active()) { |
|
370 | - return $request->uri->getTotalSegments(); |
|
371 | - } |
|
372 | - |
|
373 | - return null; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Detects and returns the current URI based on a number of different server variables. |
|
378 | - * |
|
379 | - * @param string $protocol |
|
380 | - * @param string $baseUrl |
|
381 | - * |
|
382 | - * @return string |
|
383 | - */ |
|
384 | - protected function detectURI(string $protocol, string $baseUrl) |
|
385 | - { |
|
386 | - $this->uri->setPath($this->http->detectPath($protocol)); |
|
387 | - |
|
388 | - $baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl; |
|
389 | - |
|
390 | - if ( ! empty($baseUrl)) { |
|
391 | - $this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME)); |
|
392 | - $this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST)); |
|
393 | - $this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT)); |
|
394 | - } else { |
|
395 | - if ( ! $this->http->isCli()) { |
|
396 | - exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.'); |
|
397 | - } |
|
398 | - } |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Handles setting up the locale, auto-detecting of language. |
|
403 | - * |
|
404 | - * @return void |
|
405 | - */ |
|
406 | - public function detectLocale() |
|
407 | - { |
|
408 | - $this->languages = $this->defaultLocale = config('app.locale'); |
|
409 | - |
|
410 | - $this->setLocale($this->validLocales); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Returns the default locale as set. |
|
415 | - * |
|
416 | - * @return string |
|
417 | - */ |
|
418 | - public function getDefaultLocale() |
|
419 | - { |
|
420 | - return $this->defaultLocale; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Gets the current locale, with a fallback to the default. |
|
425 | - * |
|
426 | - * @return string |
|
427 | - */ |
|
428 | - public function getLocale() |
|
429 | - { |
|
430 | - return $this->languages ?: $this->defaultLocale; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Sets the locale string for this request. |
|
435 | - * |
|
436 | - * @param string $locale |
|
437 | - * |
|
438 | - * @return \Syscodes\Http\Request |
|
439 | - */ |
|
440 | - public function setLocale($locale) |
|
441 | - { |
|
442 | - if ( ! in_array($locale, $this->validLocales)) { |
|
443 | - $locale = $this->defaultLocale; |
|
444 | - } |
|
169 | + $this->initialize($request, $cookies, $files, $server, $content); |
|
170 | + |
|
171 | + $this->detectURI(config('app.uriProtocol'), config('app.baseUrl')); |
|
172 | + |
|
173 | + $this->detectLocale(); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Sets the parameters for this request. |
|
178 | + * |
|
179 | + * @param array $request |
|
180 | + * @param array $cookies |
|
181 | + * @param array $files |
|
182 | + * @param array $server |
|
183 | + * |
|
184 | + * @return void |
|
185 | + */ |
|
186 | + public function initialize(array $request = [], array $cookies = [], array $files = [], array $server = [], $content = null) |
|
187 | + { |
|
188 | + $this->request = new Parameters($request); |
|
189 | + $this->cookies = new Inputs($cookies); |
|
190 | + $this->files = new Files($files); |
|
191 | + $this->server = new Server($server); |
|
192 | + $this->headers = new Headers($this->server->all()); |
|
193 | + |
|
194 | + $this->uri = new URI; |
|
195 | + $this->http = new Http; |
|
196 | + $this->method = null; |
|
197 | + $this->baseUrl = null; |
|
198 | + $this->content = $content; |
|
199 | + $this->pathInfo = null; |
|
200 | + $this->languages = null; |
|
201 | + $this->validLocales = config('app.supportedLocales'); |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Create a new Syscodes HTTP request from server variables. |
|
206 | + * |
|
207 | + * @return static |
|
208 | + */ |
|
209 | + public static function capture() |
|
210 | + { |
|
211 | + return static::createFromRequest(static::createFromRequestGlobals()); |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Creates an Syscodes request from of the Request class instance. |
|
216 | + * |
|
217 | + * @param \Syscodes\Http\Request $request |
|
218 | + * |
|
219 | + * @return static |
|
220 | + */ |
|
221 | + public static function createFromRequest($request) |
|
222 | + { |
|
223 | + $newRequest = (new static)->duplicate( |
|
224 | + $request->request->all(), $request->cookies->all(), |
|
225 | + $request->files->all(), $request->server->all() |
|
226 | + ); |
|
227 | + |
|
228 | + $newRequest->content = $request->content; |
|
229 | + |
|
230 | + return $newRequest; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Creates a new request with value from PHP's super global. |
|
235 | + * |
|
236 | + * @return static |
|
237 | + */ |
|
238 | + public static function createFromRequestGlobals() |
|
239 | + { |
|
240 | + $request = static::createFromRequestFactory($_POST, $_COOKIE, $_FILES, $_SERVER); |
|
241 | + |
|
242 | + return $request; |
|
243 | + } |
|
244 | + |
|
245 | + /** |
|
246 | + * Creates a new request from a factory. |
|
247 | + * |
|
248 | + * @param array $request |
|
249 | + * @param array $cookies |
|
250 | + * @param array $files |
|
251 | + * @param array $server |
|
252 | + * |
|
253 | + * @return static |
|
254 | + */ |
|
255 | + protected static function createFromRequestFactory(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
256 | + { |
|
257 | + if (self::$requestURI) { |
|
258 | + $request = (self::$requestURI)($request, $cookies, $files, $server); |
|
259 | + |
|
260 | + if ( ! $request instanceof self) { |
|
261 | + throw new LogicException('The Request active must return an instance of Syscodes\Http\Request'); |
|
262 | + } |
|
263 | + |
|
264 | + return $request; |
|
265 | + } |
|
266 | + |
|
267 | + return new static($request, $cookies, $files, $server); |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Clones a request and overrides some of its parameters. |
|
272 | + * |
|
273 | + * @param array $request |
|
274 | + * @param array $cookies |
|
275 | + * @param array $files |
|
276 | + * @param array $server |
|
277 | + * |
|
278 | + * @return static |
|
279 | + */ |
|
280 | + public function duplicate(array $request = [], array $cookies = [], array $files = [], array $server = []) |
|
281 | + { |
|
282 | + $duplicate = clone $this; |
|
283 | + |
|
284 | + if (null !== $request) { |
|
285 | + $duplicate->request = new Parameters($request); |
|
286 | + } |
|
287 | + |
|
288 | + if (null !== $cookies) { |
|
289 | + $duplicate->cookies = new Inputs($cookies); |
|
290 | + } |
|
291 | + |
|
292 | + if (null !== $files) { |
|
293 | + $duplicate->files = new Files($files); |
|
294 | + } |
|
295 | + |
|
296 | + if (null !== $server) { |
|
297 | + $duplicate->server = new Server($server); |
|
298 | + $duplicate->headers = new Headers($duplicate->server->all()); |
|
299 | + } |
|
300 | + |
|
301 | + $duplicate->uri = new URI; |
|
302 | + $duplicate->http = new Http; |
|
303 | + $duplicate->locale = null; |
|
304 | + $duplicate->method = null; |
|
305 | + $duplicate->baseUrl = null; |
|
306 | + $duplicate->pathInfo = null; |
|
307 | + $duplicate->validLocales = config('app.supportedLocales'); |
|
308 | + |
|
309 | + return $duplicate; |
|
310 | + } |
|
311 | + |
|
312 | + /** |
|
313 | + * Returns the active request currently being used. |
|
314 | + * |
|
315 | + * @param \Syscodes\Http\Request|bool|null $request Overwrite current request |
|
316 | + * before returning, false prevents |
|
317 | + * overwrite |
|
318 | + * |
|
319 | + * @return \Syscodes\Http\Request |
|
320 | + */ |
|
321 | + public static function active($request = false) |
|
322 | + { |
|
323 | + if ($request !== false) { |
|
324 | + static::$requestURI = $request; |
|
325 | + } |
|
326 | + |
|
327 | + return static::$requestURI; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Returns the desired segment, or $default if it does not exist. |
|
332 | + * |
|
333 | + * @param int $index The segment number (1-based index) |
|
334 | + * @param mixed $default Default value to return |
|
335 | + * |
|
336 | + * @return string |
|
337 | + */ |
|
338 | + public function segment($index, $default = null) |
|
339 | + { |
|
340 | + if ($request = static::active()) { |
|
341 | + return $request->uri->getSegment($index, $default); |
|
342 | + } |
|
343 | + |
|
344 | + return null; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Returns all segments in an array. For total of segments |
|
349 | + * used the function PHP count(). |
|
350 | + * |
|
351 | + * @return array |
|
352 | + */ |
|
353 | + public function segments() |
|
354 | + { |
|
355 | + if ($request = static::active()) { |
|
356 | + return $request->uri->getSegments(); |
|
357 | + } |
|
358 | + |
|
359 | + return null; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * Returns the total number of segment. |
|
364 | + * |
|
365 | + * @return int|null |
|
366 | + */ |
|
367 | + public function totalSegments() |
|
368 | + { |
|
369 | + if ($request = static::active()) { |
|
370 | + return $request->uri->getTotalSegments(); |
|
371 | + } |
|
372 | + |
|
373 | + return null; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Detects and returns the current URI based on a number of different server variables. |
|
378 | + * |
|
379 | + * @param string $protocol |
|
380 | + * @param string $baseUrl |
|
381 | + * |
|
382 | + * @return string |
|
383 | + */ |
|
384 | + protected function detectURI(string $protocol, string $baseUrl) |
|
385 | + { |
|
386 | + $this->uri->setPath($this->http->detectPath($protocol)); |
|
387 | + |
|
388 | + $baseUrl = ! empty($baseUrl) ? rtrim($baseUrl, '/ ').'/' : $baseUrl; |
|
389 | + |
|
390 | + if ( ! empty($baseUrl)) { |
|
391 | + $this->uri->setScheme(parse_url($baseUrl, PHP_URL_SCHEME)); |
|
392 | + $this->uri->setHost(parse_url($baseUrl, PHP_URL_HOST)); |
|
393 | + $this->uri->setPort(parse_url($baseUrl, PHP_URL_PORT)); |
|
394 | + } else { |
|
395 | + if ( ! $this->http->isCli()) { |
|
396 | + exit('You have an empty or invalid base URL. The baseURL value must be set in config/app.php, or through the .env file.'); |
|
397 | + } |
|
398 | + } |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Handles setting up the locale, auto-detecting of language. |
|
403 | + * |
|
404 | + * @return void |
|
405 | + */ |
|
406 | + public function detectLocale() |
|
407 | + { |
|
408 | + $this->languages = $this->defaultLocale = config('app.locale'); |
|
409 | + |
|
410 | + $this->setLocale($this->validLocales); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Returns the default locale as set. |
|
415 | + * |
|
416 | + * @return string |
|
417 | + */ |
|
418 | + public function getDefaultLocale() |
|
419 | + { |
|
420 | + return $this->defaultLocale; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Gets the current locale, with a fallback to the default. |
|
425 | + * |
|
426 | + * @return string |
|
427 | + */ |
|
428 | + public function getLocale() |
|
429 | + { |
|
430 | + return $this->languages ?: $this->defaultLocale; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Sets the locale string for this request. |
|
435 | + * |
|
436 | + * @param string $locale |
|
437 | + * |
|
438 | + * @return \Syscodes\Http\Request |
|
439 | + */ |
|
440 | + public function setLocale($locale) |
|
441 | + { |
|
442 | + if ( ! in_array($locale, $this->validLocales)) { |
|
443 | + $locale = $this->defaultLocale; |
|
444 | + } |
|
445 | 445 | |
446 | - $this->languages = $locale; |
|
447 | - |
|
448 | - try { |
|
449 | - if (class_exists('Locale', false)) { |
|
450 | - Locale::setDefault($locale); |
|
451 | - } |
|
452 | - } catch (Exception $exception) {} |
|
453 | - |
|
454 | - return $this; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Returns the full request string. |
|
459 | - * |
|
460 | - * @return string|null The Request string |
|
461 | - */ |
|
462 | - public function get() |
|
463 | - { |
|
464 | - if ($request = static::active()) { |
|
465 | - return $request->uri->get(); |
|
466 | - } |
|
467 | - |
|
468 | - return null; |
|
469 | - } |
|
470 | - |
|
471 | - /** |
|
472 | - * Get the JSON payload for the request. |
|
473 | - * |
|
474 | - * @param string|null $key (null by default) |
|
475 | - * @param mixed $default (null by default) |
|
476 | - * |
|
477 | - * @return \Syscodes\Http\Contributors\Parameters|mixed |
|
478 | - */ |
|
479 | - public function json($key = null, $default = null) |
|
480 | - { |
|
481 | - if ( ! isset($this->json)) { |
|
482 | - $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
483 | - } |
|
484 | - |
|
485 | - if (is_null($key)) { |
|
486 | - return $this->json; |
|
487 | - } |
|
488 | - |
|
489 | - return Arr::get($this->json->all(), $key, $default); |
|
490 | - } |
|
491 | - |
|
492 | - /** |
|
493 | - * Set the JSON payload for the request |
|
494 | - * |
|
495 | - * @param \Syscodes\Http\Contributors\Parameters $json |
|
496 | - * |
|
497 | - * @return $this |
|
498 | - */ |
|
499 | - public function setJson($json) |
|
500 | - { |
|
501 | - $this->json = $json; |
|
502 | - |
|
503 | - return $this; |
|
504 | - } |
|
505 | - |
|
506 | - /** |
|
507 | - * Returns whether this is an AJAX request or not. |
|
508 | - * |
|
509 | - * @return bool |
|
510 | - */ |
|
511 | - public function isXmlHttpRequest() |
|
512 | - { |
|
513 | - return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
514 | - strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
515 | - } |
|
516 | - |
|
517 | - /** |
|
518 | - * Returns the input method used (GET, POST, DELETE, etc.). |
|
519 | - * |
|
520 | - * @return string |
|
521 | - * |
|
522 | - * @throws \LogicException |
|
523 | - */ |
|
524 | - public function getmethod() |
|
525 | - { |
|
526 | - if (null !== $this->method) { |
|
527 | - return $this->method; |
|
528 | - } |
|
446 | + $this->languages = $locale; |
|
447 | + |
|
448 | + try { |
|
449 | + if (class_exists('Locale', false)) { |
|
450 | + Locale::setDefault($locale); |
|
451 | + } |
|
452 | + } catch (Exception $exception) {} |
|
453 | + |
|
454 | + return $this; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Returns the full request string. |
|
459 | + * |
|
460 | + * @return string|null The Request string |
|
461 | + */ |
|
462 | + public function get() |
|
463 | + { |
|
464 | + if ($request = static::active()) { |
|
465 | + return $request->uri->get(); |
|
466 | + } |
|
467 | + |
|
468 | + return null; |
|
469 | + } |
|
470 | + |
|
471 | + /** |
|
472 | + * Get the JSON payload for the request. |
|
473 | + * |
|
474 | + * @param string|null $key (null by default) |
|
475 | + * @param mixed $default (null by default) |
|
476 | + * |
|
477 | + * @return \Syscodes\Http\Contributors\Parameters|mixed |
|
478 | + */ |
|
479 | + public function json($key = null, $default = null) |
|
480 | + { |
|
481 | + if ( ! isset($this->json)) { |
|
482 | + $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
483 | + } |
|
484 | + |
|
485 | + if (is_null($key)) { |
|
486 | + return $this->json; |
|
487 | + } |
|
488 | + |
|
489 | + return Arr::get($this->json->all(), $key, $default); |
|
490 | + } |
|
491 | + |
|
492 | + /** |
|
493 | + * Set the JSON payload for the request |
|
494 | + * |
|
495 | + * @param \Syscodes\Http\Contributors\Parameters $json |
|
496 | + * |
|
497 | + * @return $this |
|
498 | + */ |
|
499 | + public function setJson($json) |
|
500 | + { |
|
501 | + $this->json = $json; |
|
502 | + |
|
503 | + return $this; |
|
504 | + } |
|
505 | + |
|
506 | + /** |
|
507 | + * Returns whether this is an AJAX request or not. |
|
508 | + * |
|
509 | + * @return bool |
|
510 | + */ |
|
511 | + public function isXmlHttpRequest() |
|
512 | + { |
|
513 | + return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
514 | + strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
515 | + } |
|
516 | + |
|
517 | + /** |
|
518 | + * Returns the input method used (GET, POST, DELETE, etc.). |
|
519 | + * |
|
520 | + * @return string |
|
521 | + * |
|
522 | + * @throws \LogicException |
|
523 | + */ |
|
524 | + public function getmethod() |
|
525 | + { |
|
526 | + if (null !== $this->method) { |
|
527 | + return $this->method; |
|
528 | + } |
|
529 | 529 | |
530 | - $method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
530 | + $method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
531 | 531 | |
532 | - if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
533 | - return $this->method = $method; |
|
534 | - } |
|
532 | + if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
533 | + return $this->method = $method; |
|
534 | + } |
|
535 | 535 | |
536 | - if ( ! preg_match('~^[A-Z]++$#~D', $method)) { |
|
537 | - throw new logicException(sprintf('Invalid method override "%s"', $method)); |
|
538 | - } |
|
539 | - |
|
540 | - return $this->method = $method; |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Sets the request method. |
|
545 | - * |
|
546 | - * @param string $method |
|
547 | - * |
|
548 | - * @return string |
|
549 | - */ |
|
550 | - public function setMethod(string $method) |
|
551 | - { |
|
552 | - $this->method = null; |
|
553 | - |
|
554 | - $this->server->set('REQUEST_METHOD', $method); |
|
555 | - } |
|
536 | + if ( ! preg_match('~^[A-Z]++$#~D', $method)) { |
|
537 | + throw new logicException(sprintf('Invalid method override "%s"', $method)); |
|
538 | + } |
|
539 | + |
|
540 | + return $this->method = $method; |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Sets the request method. |
|
545 | + * |
|
546 | + * @param string $method |
|
547 | + * |
|
548 | + * @return string |
|
549 | + */ |
|
550 | + public function setMethod(string $method) |
|
551 | + { |
|
552 | + $this->method = null; |
|
553 | + |
|
554 | + $this->server->set('REQUEST_METHOD', $method); |
|
555 | + } |
|
556 | 556 | |
557 | - /** |
|
558 | - * Determine if the current request URI matches a pattern. |
|
559 | - * |
|
560 | - * @param mixed ...$patterns |
|
561 | - * |
|
562 | - * @return bool |
|
563 | - */ |
|
564 | - public function is(...$patterns) |
|
565 | - { |
|
566 | - $path = $this->decodedPath(); |
|
557 | + /** |
|
558 | + * Determine if the current request URI matches a pattern. |
|
559 | + * |
|
560 | + * @param mixed ...$patterns |
|
561 | + * |
|
562 | + * @return bool |
|
563 | + */ |
|
564 | + public function is(...$patterns) |
|
565 | + { |
|
566 | + $path = $this->decodedPath(); |
|
567 | 567 | |
568 | - foreach ($patterns as $pattern) { |
|
569 | - if (Str::is($pattern, $path)) { |
|
570 | - return true; |
|
571 | - } |
|
572 | - } |
|
573 | - |
|
574 | - return false; |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Determine if the route name matches a given pattern. |
|
579 | - * |
|
580 | - * @param mixed ...$patterns |
|
581 | - * |
|
582 | - * @return bool |
|
583 | - */ |
|
584 | - public function routeIs(...$patterns) |
|
585 | - { |
|
586 | - return $this->route() && $this->route()->is(...$patterns); |
|
587 | - } |
|
588 | - |
|
589 | - /** |
|
590 | - * Get the route handling the request. |
|
591 | - * |
|
592 | - * @param string|null $param (null by default) |
|
593 | - * @param mixed $default (null by default) |
|
594 | - * |
|
595 | - * @return \Syscodes\Routing\Route|object|string|null |
|
596 | - */ |
|
597 | - public function route($param = null, $default = null) |
|
598 | - { |
|
599 | - $route = $this->getRoute(); |
|
600 | - |
|
601 | - if (is_null($route) || is_null($param)) { |
|
602 | - return $route; |
|
603 | - } |
|
604 | - |
|
605 | - return $route->parameter($param, $default); |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Get the current decoded path info for the request. |
|
610 | - * |
|
611 | - * @return string |
|
612 | - */ |
|
613 | - public function decodedPath() |
|
614 | - { |
|
615 | - return rawurldecode($this->path()); |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * Get the current path info for the request. |
|
620 | - * |
|
621 | - * @return string |
|
622 | - */ |
|
623 | - public function path() |
|
624 | - { |
|
625 | - $path = trim($this->getPathInfo(), '/'); |
|
626 | - |
|
627 | - return $path == '' ? '/' : $path; |
|
628 | - } |
|
629 | - |
|
630 | - /** |
|
631 | - * Retunrs the request body content. |
|
632 | - * |
|
633 | - * @return string |
|
634 | - */ |
|
635 | - public function getContent() |
|
636 | - { |
|
637 | - if (null === $this->content || false === $this->content) |
|
638 | - { |
|
639 | - $this->content = file_get_contents('php://input'); |
|
640 | - } |
|
641 | - |
|
642 | - return $this->content; |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * Returns the path being requested relative to the executed script. |
|
647 | - * |
|
648 | - * @return string |
|
649 | - */ |
|
650 | - public function getPathInfo() |
|
651 | - { |
|
652 | - if (null === $this->pathInfo) |
|
653 | - { |
|
654 | - $this->pathInfo = $this->http->parsePathInfo(); |
|
655 | - } |
|
656 | - |
|
657 | - return $this->pathInfo; |
|
658 | - } |
|
659 | - |
|
660 | - /** |
|
661 | - * Returns the root URL from which this request is executed. |
|
662 | - * |
|
663 | - * @return string |
|
664 | - */ |
|
665 | - public function getBaseUrl() |
|
666 | - { |
|
667 | - if (null === $this->baseUrl) |
|
668 | - { |
|
669 | - $this->baseUrl = $this->http->parseBaseUrl(); |
|
670 | - } |
|
671 | - |
|
672 | - return $this->baseUrl; |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Returns the requested URI. |
|
677 | - * |
|
678 | - * @return string |
|
679 | - */ |
|
680 | - public function getRequestUri() |
|
681 | - { |
|
682 | - if (null === $this->requestToUri) { |
|
683 | - $this->requestToUri = $this->http->parseRequestUri(); |
|
684 | - } |
|
685 | - |
|
686 | - return $this->requestToUri; |
|
687 | - } |
|
568 | + foreach ($patterns as $pattern) { |
|
569 | + if (Str::is($pattern, $path)) { |
|
570 | + return true; |
|
571 | + } |
|
572 | + } |
|
573 | + |
|
574 | + return false; |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Determine if the route name matches a given pattern. |
|
579 | + * |
|
580 | + * @param mixed ...$patterns |
|
581 | + * |
|
582 | + * @return bool |
|
583 | + */ |
|
584 | + public function routeIs(...$patterns) |
|
585 | + { |
|
586 | + return $this->route() && $this->route()->is(...$patterns); |
|
587 | + } |
|
588 | + |
|
589 | + /** |
|
590 | + * Get the route handling the request. |
|
591 | + * |
|
592 | + * @param string|null $param (null by default) |
|
593 | + * @param mixed $default (null by default) |
|
594 | + * |
|
595 | + * @return \Syscodes\Routing\Route|object|string|null |
|
596 | + */ |
|
597 | + public function route($param = null, $default = null) |
|
598 | + { |
|
599 | + $route = $this->getRoute(); |
|
600 | + |
|
601 | + if (is_null($route) || is_null($param)) { |
|
602 | + return $route; |
|
603 | + } |
|
604 | + |
|
605 | + return $route->parameter($param, $default); |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Get the current decoded path info for the request. |
|
610 | + * |
|
611 | + * @return string |
|
612 | + */ |
|
613 | + public function decodedPath() |
|
614 | + { |
|
615 | + return rawurldecode($this->path()); |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * Get the current path info for the request. |
|
620 | + * |
|
621 | + * @return string |
|
622 | + */ |
|
623 | + public function path() |
|
624 | + { |
|
625 | + $path = trim($this->getPathInfo(), '/'); |
|
626 | + |
|
627 | + return $path == '' ? '/' : $path; |
|
628 | + } |
|
629 | + |
|
630 | + /** |
|
631 | + * Retunrs the request body content. |
|
632 | + * |
|
633 | + * @return string |
|
634 | + */ |
|
635 | + public function getContent() |
|
636 | + { |
|
637 | + if (null === $this->content || false === $this->content) |
|
638 | + { |
|
639 | + $this->content = file_get_contents('php://input'); |
|
640 | + } |
|
641 | + |
|
642 | + return $this->content; |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * Returns the path being requested relative to the executed script. |
|
647 | + * |
|
648 | + * @return string |
|
649 | + */ |
|
650 | + public function getPathInfo() |
|
651 | + { |
|
652 | + if (null === $this->pathInfo) |
|
653 | + { |
|
654 | + $this->pathInfo = $this->http->parsePathInfo(); |
|
655 | + } |
|
656 | + |
|
657 | + return $this->pathInfo; |
|
658 | + } |
|
659 | + |
|
660 | + /** |
|
661 | + * Returns the root URL from which this request is executed. |
|
662 | + * |
|
663 | + * @return string |
|
664 | + */ |
|
665 | + public function getBaseUrl() |
|
666 | + { |
|
667 | + if (null === $this->baseUrl) |
|
668 | + { |
|
669 | + $this->baseUrl = $this->http->parseBaseUrl(); |
|
670 | + } |
|
671 | + |
|
672 | + return $this->baseUrl; |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Returns the requested URI. |
|
677 | + * |
|
678 | + * @return string |
|
679 | + */ |
|
680 | + public function getRequestUri() |
|
681 | + { |
|
682 | + if (null === $this->requestToUri) { |
|
683 | + $this->requestToUri = $this->http->parseRequestUri(); |
|
684 | + } |
|
685 | + |
|
686 | + return $this->requestToUri; |
|
687 | + } |
|
688 | 688 | |
689 | - /** |
|
690 | - * Gets the request's scheme. |
|
691 | - * |
|
692 | - * @return string |
|
693 | - */ |
|
694 | - public function getScheme() |
|
695 | - { |
|
696 | - return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Returns the host name. |
|
701 | - * |
|
702 | - * @return void |
|
703 | - */ |
|
704 | - public function getHost() |
|
705 | - { |
|
706 | - if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
707 | - $host = $forawardedHost[0]; |
|
708 | - } elseif ( ! $host = $this->headers->get('HOST')) { |
|
709 | - if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
710 | - $host = $this->server->get('REMOTE_ADDR', ''); |
|
711 | - } |
|
712 | - } |
|
713 | - |
|
714 | - $host = $_SERVER['SERVER_NAME']; |
|
715 | - |
|
716 | - $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
689 | + /** |
|
690 | + * Gets the request's scheme. |
|
691 | + * |
|
692 | + * @return string |
|
693 | + */ |
|
694 | + public function getScheme() |
|
695 | + { |
|
696 | + return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Returns the host name. |
|
701 | + * |
|
702 | + * @return void |
|
703 | + */ |
|
704 | + public function getHost() |
|
705 | + { |
|
706 | + if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
707 | + $host = $forawardedHost[0]; |
|
708 | + } elseif ( ! $host = $this->headers->get('HOST')) { |
|
709 | + if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
710 | + $host = $this->server->get('REMOTE_ADDR', ''); |
|
711 | + } |
|
712 | + } |
|
713 | + |
|
714 | + $host = $_SERVER['SERVER_NAME']; |
|
715 | + |
|
716 | + $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
717 | 717 | |
718 | - return $host; |
|
719 | - } |
|
720 | - |
|
721 | - /** |
|
722 | - * Returns the port on which the request is made. |
|
723 | - * |
|
724 | - * @return int |
|
725 | - */ |
|
726 | - public function getPort() |
|
727 | - { |
|
728 | - if ( ! $this->server->get('HTTP_HOST')) |
|
729 | - { |
|
730 | - return $this->server->get('SERVER_PORT'); |
|
731 | - } |
|
718 | + return $host; |
|
719 | + } |
|
720 | + |
|
721 | + /** |
|
722 | + * Returns the port on which the request is made. |
|
723 | + * |
|
724 | + * @return int |
|
725 | + */ |
|
726 | + public function getPort() |
|
727 | + { |
|
728 | + if ( ! $this->server->get('HTTP_HOST')) |
|
729 | + { |
|
730 | + return $this->server->get('SERVER_PORT'); |
|
731 | + } |
|
732 | 732 | |
733 | - return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
734 | - } |
|
735 | - |
|
736 | - /** |
|
737 | - * Returns the HTTP host being requested. |
|
738 | - * |
|
739 | - * @return string |
|
740 | - */ |
|
741 | - public function getHttpHost() |
|
742 | - { |
|
743 | - $scheme = $this->getScheme(); |
|
744 | - $port = $this->getPort(); |
|
745 | - |
|
746 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) |
|
747 | - { |
|
748 | - return $this->getHost(); |
|
749 | - } |
|
750 | - |
|
751 | - return $this->getHost().':'.$port; |
|
752 | - } |
|
753 | - |
|
754 | - /** |
|
755 | - * Gets the scheme and HTTP host. |
|
756 | - * |
|
757 | - * @return string |
|
758 | - */ |
|
759 | - public function getSchemeWithHttpHost() |
|
760 | - { |
|
761 | - return $this->getScheme().'://'.$this->getHttpHost(); |
|
762 | - } |
|
763 | - |
|
764 | - /** |
|
765 | - * Get the root URL for the application. |
|
766 | - * |
|
767 | - * @return string |
|
768 | - */ |
|
769 | - public function root() |
|
770 | - { |
|
771 | - return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
772 | - } |
|
773 | - |
|
774 | - /** |
|
775 | - * Get the URL for the request. |
|
776 | - * |
|
777 | - * @return string |
|
778 | - */ |
|
779 | - public function url() |
|
780 | - { |
|
781 | - return trim(preg_replace('/\?.*/', '', $this->get()), '/'); |
|
782 | - } |
|
783 | - |
|
784 | - /** |
|
785 | - * Returns the referer. |
|
786 | - * |
|
787 | - * @param string $default |
|
788 | - * |
|
789 | - * @return string |
|
790 | - */ |
|
791 | - public function referer(string $default = '') |
|
792 | - { |
|
793 | - return $this->server->get('HTTP_REFERER', $default); |
|
794 | - } |
|
733 | + return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
734 | + } |
|
735 | + |
|
736 | + /** |
|
737 | + * Returns the HTTP host being requested. |
|
738 | + * |
|
739 | + * @return string |
|
740 | + */ |
|
741 | + public function getHttpHost() |
|
742 | + { |
|
743 | + $scheme = $this->getScheme(); |
|
744 | + $port = $this->getPort(); |
|
745 | + |
|
746 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) |
|
747 | + { |
|
748 | + return $this->getHost(); |
|
749 | + } |
|
750 | + |
|
751 | + return $this->getHost().':'.$port; |
|
752 | + } |
|
753 | + |
|
754 | + /** |
|
755 | + * Gets the scheme and HTTP host. |
|
756 | + * |
|
757 | + * @return string |
|
758 | + */ |
|
759 | + public function getSchemeWithHttpHost() |
|
760 | + { |
|
761 | + return $this->getScheme().'://'.$this->getHttpHost(); |
|
762 | + } |
|
763 | + |
|
764 | + /** |
|
765 | + * Get the root URL for the application. |
|
766 | + * |
|
767 | + * @return string |
|
768 | + */ |
|
769 | + public function root() |
|
770 | + { |
|
771 | + return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
772 | + } |
|
773 | + |
|
774 | + /** |
|
775 | + * Get the URL for the request. |
|
776 | + * |
|
777 | + * @return string |
|
778 | + */ |
|
779 | + public function url() |
|
780 | + { |
|
781 | + return trim(preg_replace('/\?.*/', '', $this->get()), '/'); |
|
782 | + } |
|
783 | + |
|
784 | + /** |
|
785 | + * Returns the referer. |
|
786 | + * |
|
787 | + * @param string $default |
|
788 | + * |
|
789 | + * @return string |
|
790 | + */ |
|
791 | + public function referer(string $default = '') |
|
792 | + { |
|
793 | + return $this->server->get('HTTP_REFERER', $default); |
|
794 | + } |
|
795 | 795 | |
796 | - /** |
|
797 | - * Attempts to detect if the current connection is secure through |
|
798 | - * over HTTPS protocol. |
|
799 | - * |
|
800 | - * @return bool |
|
801 | - */ |
|
802 | - public function secure() |
|
803 | - { |
|
804 | - if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
805 | - return true; |
|
806 | - } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
807 | - return true; |
|
808 | - } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
809 | - return true; |
|
810 | - } |
|
811 | - |
|
812 | - return false; |
|
813 | - } |
|
814 | - |
|
815 | - /** |
|
816 | - * Returns the user agent. |
|
817 | - * |
|
818 | - * @param string|null $default |
|
819 | - * |
|
820 | - * @return string |
|
821 | - */ |
|
822 | - public function userAgent(string $default = null) |
|
823 | - { |
|
824 | - return $this->server->get('HTTP_USER_AGENT', $default); |
|
825 | - } |
|
826 | - |
|
827 | - /** |
|
828 | - * Get the route resolver. |
|
829 | - * |
|
830 | - * @return \Syscodes\Routing\Router |
|
831 | - */ |
|
832 | - public function getRoute() |
|
833 | - { |
|
834 | - return app('router'); |
|
835 | - } |
|
836 | - |
|
837 | - /** |
|
838 | - * Get an element from the request. |
|
839 | - * |
|
840 | - * @return string[] |
|
841 | - */ |
|
842 | - public function __get($key) |
|
843 | - { |
|
844 | - $all = $this->server->all(); |
|
845 | - |
|
846 | - if (array_key_exists($key, $all)) { |
|
847 | - return $all[$key]; |
|
848 | - } else { |
|
849 | - return $key; |
|
850 | - } |
|
851 | - } |
|
852 | - |
|
853 | - /** |
|
854 | - * Returns the Request as an HTTP string. |
|
855 | - * |
|
856 | - * @return string |
|
857 | - */ |
|
858 | - public function __toString() |
|
859 | - { |
|
860 | - try |
|
861 | - { |
|
862 | - $content = $this->getContent(); |
|
863 | - } |
|
864 | - catch (LogicException $e) |
|
865 | - { |
|
866 | - if (PHP_VERSION_ID > 70400) |
|
867 | - { |
|
868 | - throw $e; |
|
869 | - } |
|
870 | - |
|
871 | - return trigger_error($e, E_USER_ERROR); |
|
872 | - } |
|
873 | - |
|
874 | - $cookieHeader = ''; |
|
875 | - $cookies = []; |
|
876 | - |
|
877 | - foreach ($this->cookies as $key => $value) |
|
878 | - { |
|
879 | - $cookies[]= "{$key} = {$value}"; |
|
880 | - } |
|
881 | - |
|
882 | - if ( ! empty($cookies)) |
|
883 | - { |
|
884 | - $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
885 | - } |
|
796 | + /** |
|
797 | + * Attempts to detect if the current connection is secure through |
|
798 | + * over HTTPS protocol. |
|
799 | + * |
|
800 | + * @return bool |
|
801 | + */ |
|
802 | + public function secure() |
|
803 | + { |
|
804 | + if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
805 | + return true; |
|
806 | + } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
807 | + return true; |
|
808 | + } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
809 | + return true; |
|
810 | + } |
|
811 | + |
|
812 | + return false; |
|
813 | + } |
|
814 | + |
|
815 | + /** |
|
816 | + * Returns the user agent. |
|
817 | + * |
|
818 | + * @param string|null $default |
|
819 | + * |
|
820 | + * @return string |
|
821 | + */ |
|
822 | + public function userAgent(string $default = null) |
|
823 | + { |
|
824 | + return $this->server->get('HTTP_USER_AGENT', $default); |
|
825 | + } |
|
826 | + |
|
827 | + /** |
|
828 | + * Get the route resolver. |
|
829 | + * |
|
830 | + * @return \Syscodes\Routing\Router |
|
831 | + */ |
|
832 | + public function getRoute() |
|
833 | + { |
|
834 | + return app('router'); |
|
835 | + } |
|
836 | + |
|
837 | + /** |
|
838 | + * Get an element from the request. |
|
839 | + * |
|
840 | + * @return string[] |
|
841 | + */ |
|
842 | + public function __get($key) |
|
843 | + { |
|
844 | + $all = $this->server->all(); |
|
845 | + |
|
846 | + if (array_key_exists($key, $all)) { |
|
847 | + return $all[$key]; |
|
848 | + } else { |
|
849 | + return $key; |
|
850 | + } |
|
851 | + } |
|
852 | + |
|
853 | + /** |
|
854 | + * Returns the Request as an HTTP string. |
|
855 | + * |
|
856 | + * @return string |
|
857 | + */ |
|
858 | + public function __toString() |
|
859 | + { |
|
860 | + try |
|
861 | + { |
|
862 | + $content = $this->getContent(); |
|
863 | + } |
|
864 | + catch (LogicException $e) |
|
865 | + { |
|
866 | + if (PHP_VERSION_ID > 70400) |
|
867 | + { |
|
868 | + throw $e; |
|
869 | + } |
|
870 | + |
|
871 | + return trigger_error($e, E_USER_ERROR); |
|
872 | + } |
|
873 | + |
|
874 | + $cookieHeader = ''; |
|
875 | + $cookies = []; |
|
876 | + |
|
877 | + foreach ($this->cookies as $key => $value) |
|
878 | + { |
|
879 | + $cookies[]= "{$key} = {$value}"; |
|
880 | + } |
|
881 | + |
|
882 | + if ( ! empty($cookies)) |
|
883 | + { |
|
884 | + $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
885 | + } |
|
886 | 886 | |
887 | - return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
888 | - $this->headers. |
|
889 | - $cookieHeader."\r\n". |
|
890 | - $content; |
|
891 | - } |
|
892 | - |
|
893 | - /** |
|
894 | - * Clones the current request. |
|
895 | - * |
|
896 | - * @return void |
|
897 | - */ |
|
898 | - public function __clone() |
|
899 | - { |
|
900 | - $this->request = clone $this->request; |
|
901 | - $this->cookies = clone $this->cookies; |
|
902 | - $this->files = clone $this->files; |
|
903 | - $this->server = clone $this->server; |
|
904 | - $this->headers = clone $this->headers; |
|
905 | - } |
|
887 | + return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
888 | + $this->headers. |
|
889 | + $cookieHeader."\r\n". |
|
890 | + $content; |
|
891 | + } |
|
892 | + |
|
893 | + /** |
|
894 | + * Clones the current request. |
|
895 | + * |
|
896 | + * @return void |
|
897 | + */ |
|
898 | + public function __clone() |
|
899 | + { |
|
900 | + $this->request = clone $this->request; |
|
901 | + $this->cookies = clone $this->cookies; |
|
902 | + $this->files = clone $this->files; |
|
903 | + $this->server = clone $this->server; |
|
904 | + $this->headers = clone $this->headers; |
|
905 | + } |
|
906 | 906 | } |
907 | 907 | \ No newline at end of file |
@@ -860,8 +860,7 @@ |
||
860 | 860 | try |
861 | 861 | { |
862 | 862 | $content = $this->getContent(); |
863 | - } |
|
864 | - catch (LogicException $e) |
|
863 | + } catch (LogicException $e) |
|
865 | 864 | { |
866 | 865 | if (PHP_VERSION_ID > 70400) |
867 | 866 | { |
@@ -34,497 +34,497 @@ |
||
34 | 34 | */ |
35 | 35 | class URI |
36 | 36 | { |
37 | - /** |
|
38 | - * Returns default schemes/ports. |
|
39 | - * |
|
40 | - * @var array $defaultPorts |
|
41 | - */ |
|
42 | - protected $defaultPorts = [ |
|
43 | - 'http' => 80, |
|
44 | - 'https' => 443, |
|
45 | - 'ftp' => 21, |
|
46 | - 'sftp' => 22 |
|
47 | - ]; |
|
48 | - |
|
49 | - /** |
|
50 | - * The name of any fragment. |
|
51 | - * |
|
52 | - * @var string $fragment |
|
53 | - */ |
|
54 | - protected $fragment = ''; |
|
55 | - |
|
56 | - /** |
|
57 | - * The URI Host. |
|
58 | - * |
|
59 | - * @var string $host |
|
60 | - */ |
|
61 | - protected $host; |
|
37 | + /** |
|
38 | + * Returns default schemes/ports. |
|
39 | + * |
|
40 | + * @var array $defaultPorts |
|
41 | + */ |
|
42 | + protected $defaultPorts = [ |
|
43 | + 'http' => 80, |
|
44 | + 'https' => 443, |
|
45 | + 'ftp' => 21, |
|
46 | + 'sftp' => 22 |
|
47 | + ]; |
|
48 | + |
|
49 | + /** |
|
50 | + * The name of any fragment. |
|
51 | + * |
|
52 | + * @var string $fragment |
|
53 | + */ |
|
54 | + protected $fragment = ''; |
|
55 | + |
|
56 | + /** |
|
57 | + * The URI Host. |
|
58 | + * |
|
59 | + * @var string $host |
|
60 | + */ |
|
61 | + protected $host; |
|
62 | 62 | |
63 | - /** |
|
64 | - * The URI User Password. |
|
65 | - * |
|
66 | - * @var string $password |
|
67 | - */ |
|
68 | - protected $password; |
|
69 | - |
|
70 | - /** |
|
71 | - * The URI path. |
|
72 | - * |
|
73 | - * @var string $path |
|
74 | - */ |
|
75 | - protected $path; |
|
76 | - |
|
77 | - /** |
|
78 | - * The URI Port. |
|
79 | - * |
|
80 | - * @var int $port |
|
81 | - */ |
|
82 | - protected $port; |
|
83 | - |
|
84 | - /** |
|
85 | - * The query string. |
|
86 | - * |
|
87 | - * @var string $query |
|
88 | - */ |
|
89 | - protected $query; |
|
90 | - |
|
91 | - /** |
|
92 | - * The URI Scheme. |
|
93 | - * |
|
94 | - * @var string $scheme |
|
95 | - */ |
|
96 | - protected $scheme = 'http'; |
|
97 | - |
|
98 | - /** |
|
99 | - * The URI segments. |
|
100 | - * |
|
101 | - * @var array $segments |
|
102 | - */ |
|
103 | - protected $segments = []; |
|
104 | - |
|
105 | - /** |
|
106 | - * Whether passwords should be shown in userInfo/authority calls. |
|
107 | - * |
|
108 | - * @var boolean $showPassword |
|
109 | - */ |
|
110 | - protected $showPassword = false; |
|
63 | + /** |
|
64 | + * The URI User Password. |
|
65 | + * |
|
66 | + * @var string $password |
|
67 | + */ |
|
68 | + protected $password; |
|
69 | + |
|
70 | + /** |
|
71 | + * The URI path. |
|
72 | + * |
|
73 | + * @var string $path |
|
74 | + */ |
|
75 | + protected $path; |
|
76 | + |
|
77 | + /** |
|
78 | + * The URI Port. |
|
79 | + * |
|
80 | + * @var int $port |
|
81 | + */ |
|
82 | + protected $port; |
|
83 | + |
|
84 | + /** |
|
85 | + * The query string. |
|
86 | + * |
|
87 | + * @var string $query |
|
88 | + */ |
|
89 | + protected $query; |
|
90 | + |
|
91 | + /** |
|
92 | + * The URI Scheme. |
|
93 | + * |
|
94 | + * @var string $scheme |
|
95 | + */ |
|
96 | + protected $scheme = 'http'; |
|
97 | + |
|
98 | + /** |
|
99 | + * The URI segments. |
|
100 | + * |
|
101 | + * @var array $segments |
|
102 | + */ |
|
103 | + protected $segments = []; |
|
104 | + |
|
105 | + /** |
|
106 | + * Whether passwords should be shown in userInfo/authority calls. |
|
107 | + * |
|
108 | + * @var boolean $showPassword |
|
109 | + */ |
|
110 | + protected $showPassword = false; |
|
111 | 111 | |
112 | - /** |
|
113 | - * The URI User Info. |
|
114 | - * |
|
115 | - * @var string $user |
|
116 | - */ |
|
117 | - protected $user; |
|
118 | - |
|
119 | - /** |
|
120 | - * Constructor. The URI class instance. |
|
121 | - * |
|
122 | - * @param string|null $uri (null by default) |
|
123 | - * |
|
124 | - * @return void |
|
125 | - * |
|
126 | - * @throws \Syscodes\Http\Exceptions\HttpURIException |
|
127 | - */ |
|
128 | - public function __construct(string $uri = null) |
|
129 | - { |
|
130 | - if ( ! is_null($uri)) { |
|
131 | - $this->setUri($uri); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Sets and overwrites any current URI information. |
|
137 | - * |
|
138 | - * @param string|null $uri (null by default) |
|
139 | - * |
|
140 | - * @return mixed |
|
141 | - * |
|
142 | - * @throws \Syscodes\Http\Exceptions\HttpURIException |
|
143 | - */ |
|
144 | - public function setUri(string $uri = null) |
|
145 | - { |
|
146 | - if ( ! is_null($uri)) { |
|
147 | - $parts = parse_url($uri); |
|
148 | - |
|
149 | - if ($parts === false) { |
|
150 | - throw HttpURIException::UnableToParseURI($uri); |
|
151 | - } |
|
152 | - |
|
153 | - $this->applyParts($parts); |
|
154 | - } |
|
155 | - |
|
156 | - return $this; |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Returns the full URI string. |
|
161 | - * |
|
162 | - * @return string The URI string |
|
163 | - */ |
|
164 | - public function get() |
|
165 | - { |
|
166 | - return '/'.ltrim($this->path, '/'); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Sets of URI string. |
|
171 | - * |
|
172 | - * @param string $uri |
|
173 | - * |
|
174 | - * @return $this |
|
175 | - */ |
|
176 | - public function set($uri) |
|
177 | - { |
|
178 | - $this->path = $uri; |
|
179 | - |
|
180 | - return $this; |
|
181 | - } |
|
182 | - |
|
183 | - /** |
|
184 | - * Retrieve the path component of the URI. The path can either be empty or absolute |
|
185 | - * (starting with a slash) or rootless (not starting with a slash). |
|
186 | - * |
|
187 | - * @return string |
|
188 | - */ |
|
189 | - public function getPath() |
|
190 | - { |
|
191 | - return (is_null($this->path) ? '' : $this->path); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Sets the path portion of the URI. |
|
196 | - * |
|
197 | - * @param string $path |
|
198 | - * |
|
199 | - * @return void |
|
200 | - */ |
|
201 | - public function setPath(string $path) |
|
202 | - { |
|
203 | - $this->path = $this->filterPath($path); |
|
204 | - |
|
205 | - $this->filterSegments($this->path); |
|
206 | - |
|
207 | - return $this; |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Encodes any dangerous characters. |
|
212 | - * |
|
213 | - * @param string|null $path |
|
214 | - * |
|
215 | - * @return string |
|
216 | - */ |
|
217 | - protected function filterPath(string $path = null) |
|
218 | - { |
|
219 | - $path = urldecode($path); |
|
220 | - |
|
221 | - return $path; |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * Filter the segments of path. |
|
226 | - * |
|
227 | - * @param string $uri |
|
228 | - * |
|
229 | - * @return string[] |
|
230 | - */ |
|
231 | - protected function filterSegments($uri) |
|
232 | - { |
|
233 | - $this->segments = (empty($uri) ? [] : explode('/', $uri)); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Get the specified URI segment, return default if it doesn't exist. |
|
238 | - * Segment index is 1 based, not 0 based. |
|
239 | - * |
|
240 | - * @param int $index The 1-based segment index |
|
241 | - * @param mixed $default The default value |
|
242 | - * |
|
243 | - * @return mixed |
|
244 | - */ |
|
245 | - public function getSegment(int $index, $default = null) |
|
246 | - { |
|
247 | - return Arr::get($this->getSegments(), $index - 1, $default); |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Returns the segments of the path as an array. |
|
252 | - * |
|
253 | - * @return array The URI segments |
|
254 | - */ |
|
255 | - public function getSegments() |
|
256 | - { |
|
257 | - return array_values(array_filter($this->segments, function ($value) { |
|
258 | - return $value != ''; |
|
259 | - })); |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Returns the total number of segment. |
|
264 | - * |
|
265 | - * @return int |
|
266 | - */ |
|
267 | - public function getTotalSegments() |
|
268 | - { |
|
269 | - return count($this->getSegments()); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Retrieve the scheme component of the URI. |
|
274 | - * |
|
275 | - * @return string |
|
276 | - */ |
|
277 | - public function getScheme() |
|
278 | - { |
|
279 | - return $this->scheme; |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Sets the scheme for this URI. |
|
284 | - * |
|
285 | - * @param string $str |
|
286 | - * |
|
287 | - * @return $this |
|
288 | - */ |
|
289 | - public function setScheme(string $str) |
|
290 | - { |
|
291 | - $str = preg_replace('~:(//)?$~', '', strtolower($str)); |
|
292 | - |
|
293 | - $this->scheme = $str; |
|
294 | - |
|
295 | - return $this->scheme; |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Retrieve the user component of the URI. |
|
300 | - * |
|
301 | - * @return string|null |
|
302 | - */ |
|
303 | - public function getUserInfo() |
|
304 | - { |
|
305 | - $user = $this->user; |
|
306 | - |
|
307 | - if ($this->showPassword === true && ! empty($this->password)) { |
|
308 | - $user .= ":$this->password"; |
|
309 | - } |
|
310 | - |
|
311 | - return $user; |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * Sets the userInfo/Authority portion of the URI. |
|
316 | - * |
|
317 | - * @param string $user |
|
318 | - * @param string $pass |
|
319 | - * |
|
320 | - * @return $this |
|
321 | - */ |
|
322 | - public function setUserInfo(string $user, string $pass) |
|
323 | - { |
|
324 | - $this->user = trim($user); |
|
325 | - $this->password = trim($pass); |
|
326 | - |
|
327 | - return $this; |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * Temporarily sets the URI to show a password in userInfo. |
|
332 | - * |
|
333 | - * @param boolean $option (true by default) |
|
334 | - * |
|
335 | - * @return $this |
|
336 | - */ |
|
337 | - public function showPassword(bool $option = true) |
|
338 | - { |
|
339 | - $this->password = $option; |
|
340 | - |
|
341 | - return $this->password; |
|
342 | - } |
|
343 | - |
|
344 | - /** |
|
345 | - * Retrieve the authority component of the URI. |
|
346 | - * |
|
347 | - * @param boolean $ignore (false by default) |
|
348 | - * |
|
349 | - * @return string |
|
350 | - */ |
|
351 | - public function getAuthority(bool $ignore = false) |
|
352 | - { |
|
353 | - if (empty($this->host)) { |
|
354 | - return ''; |
|
355 | - } |
|
356 | - |
|
357 | - $authority = $this->host; |
|
358 | - |
|
359 | - if ( ! empty($this->getUserInfo())) { |
|
360 | - $authority = $this->getUserInfo().'@'.$authority; |
|
361 | - } |
|
362 | - |
|
363 | - if ( ! empty($this->port) && ! $ignore) { |
|
364 | - if ($this->port !== $this->defaultPorts[$this->scheme]) { |
|
365 | - $authority .= ":$this->port"; |
|
366 | - } |
|
367 | - } |
|
368 | - |
|
369 | - $this->showPassword = false; |
|
370 | - |
|
371 | - return $authority; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Parses the given string an saves the appropriate authority pieces. |
|
376 | - * |
|
377 | - * @param string $str |
|
378 | - * |
|
379 | - * @return $this |
|
380 | - */ |
|
381 | - public function setAuthority(string $str) |
|
382 | - { |
|
383 | - $parts = parse_url($str); |
|
384 | - |
|
385 | - if (empty($parts['host']) && ! empty($parts['path'])) { |
|
386 | - $parts['host'] = $parts['path']; |
|
387 | - unset($parts['path']); |
|
388 | - } |
|
389 | - |
|
390 | - $this->applyParts($parts); |
|
391 | - |
|
392 | - return $this; |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Retrieve the host component of the URI. |
|
397 | - * |
|
398 | - * @return string |
|
399 | - */ |
|
400 | - public function getHost() |
|
401 | - { |
|
402 | - return $this->host; |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Sets the host name to use. |
|
407 | - * |
|
408 | - * @param string $str |
|
409 | - * |
|
410 | - * @return $this |
|
411 | - */ |
|
412 | - public function setHost(string $str) |
|
413 | - { |
|
414 | - $this->host = trim($str); |
|
415 | - |
|
416 | - return $this->host; |
|
417 | - } |
|
418 | - |
|
419 | - /** |
|
420 | - * Retrieve the port component of the URI. |
|
421 | - * |
|
422 | - * @return int|null |
|
423 | - */ |
|
424 | - public function getPort() |
|
425 | - { |
|
426 | - return $this->port; |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * Sets the port portion of the URI. |
|
431 | - * |
|
432 | - * @param int|null $port (null by default) |
|
433 | - * |
|
434 | - * @return string |
|
435 | - */ |
|
436 | - public function setPort(int $port = null) |
|
437 | - { |
|
438 | - if (is_null($port)) { |
|
439 | - return $this; |
|
440 | - } |
|
441 | - |
|
442 | - if ($port <= 0 || $port > 65355) { |
|
443 | - throw HttpURIException::invalidPort($port); |
|
444 | - } |
|
445 | - |
|
446 | - $this->port = $port; |
|
447 | - |
|
448 | - return $this->port; |
|
449 | - } |
|
450 | - |
|
451 | - /** |
|
452 | - * Retrieve a URI fragment. |
|
453 | - * |
|
454 | - * @return string |
|
455 | - */ |
|
456 | - public function getFragment() |
|
457 | - { |
|
458 | - return is_null($this->fragment) ? '' : $this->fragment; |
|
459 | - } |
|
460 | - |
|
461 | - /** |
|
462 | - * Sets the fragment portion of the URI. |
|
463 | - * |
|
464 | - * @param string $str |
|
465 | - * |
|
466 | - * @return $this |
|
467 | - */ |
|
468 | - public function setFragment(string $str) |
|
469 | - { |
|
470 | - $this->fragment = trim($str, '# '); |
|
471 | - |
|
472 | - return $this->fragment; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * Saves our parts from a parse_url call. |
|
477 | - * |
|
478 | - * @param array $parts |
|
479 | - * |
|
480 | - * @return mixed |
|
481 | - */ |
|
482 | - public function applyParts(array $paths) |
|
483 | - { |
|
484 | - if (isset($parts['scheme'])) { |
|
485 | - $this->SetScheme(rtrim($parts['scheme'], ':/')); |
|
486 | - } else { |
|
487 | - $this->setScheme('http'); |
|
488 | - } |
|
489 | - |
|
490 | - if ( ! empty($parts['host'])) { |
|
491 | - $this->host = $parts['host']; |
|
492 | - } |
|
493 | - |
|
494 | - if (isset($parts['port'])) { |
|
495 | - if ( ! is_null($parts['port'])) { |
|
496 | - $this->port = $parts['port']; |
|
497 | - } |
|
498 | - } |
|
499 | - |
|
500 | - if ( ! empty($parts['user'])) { |
|
501 | - $this->user = $parts['user']; |
|
502 | - } |
|
503 | - |
|
504 | - if ( ! empty($parts['pass'])) { |
|
505 | - $this->password = $parts['pass']; |
|
506 | - } |
|
507 | - |
|
508 | - if ( ! empty($parts['path'])) { |
|
509 | - $this->path = $this->filterPath($parts['path']); |
|
510 | - } |
|
511 | - |
|
512 | - if ( ! empty($parts['fragment'])) { |
|
513 | - $this->fragment = $parts['fragment']; |
|
514 | - } |
|
515 | - |
|
516 | - if ( ! empty($parts['path'])) { |
|
517 | - $this->segments = explode('/', $parts['path'], '/'); |
|
518 | - } |
|
519 | - } |
|
520 | - |
|
521 | - /** |
|
522 | - * Returns the URI string. |
|
523 | - * |
|
524 | - * @return string |
|
525 | - */ |
|
526 | - public function __toString() |
|
527 | - { |
|
528 | - return (string) $this->getPath(); |
|
529 | - } |
|
112 | + /** |
|
113 | + * The URI User Info. |
|
114 | + * |
|
115 | + * @var string $user |
|
116 | + */ |
|
117 | + protected $user; |
|
118 | + |
|
119 | + /** |
|
120 | + * Constructor. The URI class instance. |
|
121 | + * |
|
122 | + * @param string|null $uri (null by default) |
|
123 | + * |
|
124 | + * @return void |
|
125 | + * |
|
126 | + * @throws \Syscodes\Http\Exceptions\HttpURIException |
|
127 | + */ |
|
128 | + public function __construct(string $uri = null) |
|
129 | + { |
|
130 | + if ( ! is_null($uri)) { |
|
131 | + $this->setUri($uri); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Sets and overwrites any current URI information. |
|
137 | + * |
|
138 | + * @param string|null $uri (null by default) |
|
139 | + * |
|
140 | + * @return mixed |
|
141 | + * |
|
142 | + * @throws \Syscodes\Http\Exceptions\HttpURIException |
|
143 | + */ |
|
144 | + public function setUri(string $uri = null) |
|
145 | + { |
|
146 | + if ( ! is_null($uri)) { |
|
147 | + $parts = parse_url($uri); |
|
148 | + |
|
149 | + if ($parts === false) { |
|
150 | + throw HttpURIException::UnableToParseURI($uri); |
|
151 | + } |
|
152 | + |
|
153 | + $this->applyParts($parts); |
|
154 | + } |
|
155 | + |
|
156 | + return $this; |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Returns the full URI string. |
|
161 | + * |
|
162 | + * @return string The URI string |
|
163 | + */ |
|
164 | + public function get() |
|
165 | + { |
|
166 | + return '/'.ltrim($this->path, '/'); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Sets of URI string. |
|
171 | + * |
|
172 | + * @param string $uri |
|
173 | + * |
|
174 | + * @return $this |
|
175 | + */ |
|
176 | + public function set($uri) |
|
177 | + { |
|
178 | + $this->path = $uri; |
|
179 | + |
|
180 | + return $this; |
|
181 | + } |
|
182 | + |
|
183 | + /** |
|
184 | + * Retrieve the path component of the URI. The path can either be empty or absolute |
|
185 | + * (starting with a slash) or rootless (not starting with a slash). |
|
186 | + * |
|
187 | + * @return string |
|
188 | + */ |
|
189 | + public function getPath() |
|
190 | + { |
|
191 | + return (is_null($this->path) ? '' : $this->path); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Sets the path portion of the URI. |
|
196 | + * |
|
197 | + * @param string $path |
|
198 | + * |
|
199 | + * @return void |
|
200 | + */ |
|
201 | + public function setPath(string $path) |
|
202 | + { |
|
203 | + $this->path = $this->filterPath($path); |
|
204 | + |
|
205 | + $this->filterSegments($this->path); |
|
206 | + |
|
207 | + return $this; |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Encodes any dangerous characters. |
|
212 | + * |
|
213 | + * @param string|null $path |
|
214 | + * |
|
215 | + * @return string |
|
216 | + */ |
|
217 | + protected function filterPath(string $path = null) |
|
218 | + { |
|
219 | + $path = urldecode($path); |
|
220 | + |
|
221 | + return $path; |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * Filter the segments of path. |
|
226 | + * |
|
227 | + * @param string $uri |
|
228 | + * |
|
229 | + * @return string[] |
|
230 | + */ |
|
231 | + protected function filterSegments($uri) |
|
232 | + { |
|
233 | + $this->segments = (empty($uri) ? [] : explode('/', $uri)); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Get the specified URI segment, return default if it doesn't exist. |
|
238 | + * Segment index is 1 based, not 0 based. |
|
239 | + * |
|
240 | + * @param int $index The 1-based segment index |
|
241 | + * @param mixed $default The default value |
|
242 | + * |
|
243 | + * @return mixed |
|
244 | + */ |
|
245 | + public function getSegment(int $index, $default = null) |
|
246 | + { |
|
247 | + return Arr::get($this->getSegments(), $index - 1, $default); |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Returns the segments of the path as an array. |
|
252 | + * |
|
253 | + * @return array The URI segments |
|
254 | + */ |
|
255 | + public function getSegments() |
|
256 | + { |
|
257 | + return array_values(array_filter($this->segments, function ($value) { |
|
258 | + return $value != ''; |
|
259 | + })); |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Returns the total number of segment. |
|
264 | + * |
|
265 | + * @return int |
|
266 | + */ |
|
267 | + public function getTotalSegments() |
|
268 | + { |
|
269 | + return count($this->getSegments()); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Retrieve the scheme component of the URI. |
|
274 | + * |
|
275 | + * @return string |
|
276 | + */ |
|
277 | + public function getScheme() |
|
278 | + { |
|
279 | + return $this->scheme; |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Sets the scheme for this URI. |
|
284 | + * |
|
285 | + * @param string $str |
|
286 | + * |
|
287 | + * @return $this |
|
288 | + */ |
|
289 | + public function setScheme(string $str) |
|
290 | + { |
|
291 | + $str = preg_replace('~:(//)?$~', '', strtolower($str)); |
|
292 | + |
|
293 | + $this->scheme = $str; |
|
294 | + |
|
295 | + return $this->scheme; |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Retrieve the user component of the URI. |
|
300 | + * |
|
301 | + * @return string|null |
|
302 | + */ |
|
303 | + public function getUserInfo() |
|
304 | + { |
|
305 | + $user = $this->user; |
|
306 | + |
|
307 | + if ($this->showPassword === true && ! empty($this->password)) { |
|
308 | + $user .= ":$this->password"; |
|
309 | + } |
|
310 | + |
|
311 | + return $user; |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * Sets the userInfo/Authority portion of the URI. |
|
316 | + * |
|
317 | + * @param string $user |
|
318 | + * @param string $pass |
|
319 | + * |
|
320 | + * @return $this |
|
321 | + */ |
|
322 | + public function setUserInfo(string $user, string $pass) |
|
323 | + { |
|
324 | + $this->user = trim($user); |
|
325 | + $this->password = trim($pass); |
|
326 | + |
|
327 | + return $this; |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * Temporarily sets the URI to show a password in userInfo. |
|
332 | + * |
|
333 | + * @param boolean $option (true by default) |
|
334 | + * |
|
335 | + * @return $this |
|
336 | + */ |
|
337 | + public function showPassword(bool $option = true) |
|
338 | + { |
|
339 | + $this->password = $option; |
|
340 | + |
|
341 | + return $this->password; |
|
342 | + } |
|
343 | + |
|
344 | + /** |
|
345 | + * Retrieve the authority component of the URI. |
|
346 | + * |
|
347 | + * @param boolean $ignore (false by default) |
|
348 | + * |
|
349 | + * @return string |
|
350 | + */ |
|
351 | + public function getAuthority(bool $ignore = false) |
|
352 | + { |
|
353 | + if (empty($this->host)) { |
|
354 | + return ''; |
|
355 | + } |
|
356 | + |
|
357 | + $authority = $this->host; |
|
358 | + |
|
359 | + if ( ! empty($this->getUserInfo())) { |
|
360 | + $authority = $this->getUserInfo().'@'.$authority; |
|
361 | + } |
|
362 | + |
|
363 | + if ( ! empty($this->port) && ! $ignore) { |
|
364 | + if ($this->port !== $this->defaultPorts[$this->scheme]) { |
|
365 | + $authority .= ":$this->port"; |
|
366 | + } |
|
367 | + } |
|
368 | + |
|
369 | + $this->showPassword = false; |
|
370 | + |
|
371 | + return $authority; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Parses the given string an saves the appropriate authority pieces. |
|
376 | + * |
|
377 | + * @param string $str |
|
378 | + * |
|
379 | + * @return $this |
|
380 | + */ |
|
381 | + public function setAuthority(string $str) |
|
382 | + { |
|
383 | + $parts = parse_url($str); |
|
384 | + |
|
385 | + if (empty($parts['host']) && ! empty($parts['path'])) { |
|
386 | + $parts['host'] = $parts['path']; |
|
387 | + unset($parts['path']); |
|
388 | + } |
|
389 | + |
|
390 | + $this->applyParts($parts); |
|
391 | + |
|
392 | + return $this; |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Retrieve the host component of the URI. |
|
397 | + * |
|
398 | + * @return string |
|
399 | + */ |
|
400 | + public function getHost() |
|
401 | + { |
|
402 | + return $this->host; |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Sets the host name to use. |
|
407 | + * |
|
408 | + * @param string $str |
|
409 | + * |
|
410 | + * @return $this |
|
411 | + */ |
|
412 | + public function setHost(string $str) |
|
413 | + { |
|
414 | + $this->host = trim($str); |
|
415 | + |
|
416 | + return $this->host; |
|
417 | + } |
|
418 | + |
|
419 | + /** |
|
420 | + * Retrieve the port component of the URI. |
|
421 | + * |
|
422 | + * @return int|null |
|
423 | + */ |
|
424 | + public function getPort() |
|
425 | + { |
|
426 | + return $this->port; |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * Sets the port portion of the URI. |
|
431 | + * |
|
432 | + * @param int|null $port (null by default) |
|
433 | + * |
|
434 | + * @return string |
|
435 | + */ |
|
436 | + public function setPort(int $port = null) |
|
437 | + { |
|
438 | + if (is_null($port)) { |
|
439 | + return $this; |
|
440 | + } |
|
441 | + |
|
442 | + if ($port <= 0 || $port > 65355) { |
|
443 | + throw HttpURIException::invalidPort($port); |
|
444 | + } |
|
445 | + |
|
446 | + $this->port = $port; |
|
447 | + |
|
448 | + return $this->port; |
|
449 | + } |
|
450 | + |
|
451 | + /** |
|
452 | + * Retrieve a URI fragment. |
|
453 | + * |
|
454 | + * @return string |
|
455 | + */ |
|
456 | + public function getFragment() |
|
457 | + { |
|
458 | + return is_null($this->fragment) ? '' : $this->fragment; |
|
459 | + } |
|
460 | + |
|
461 | + /** |
|
462 | + * Sets the fragment portion of the URI. |
|
463 | + * |
|
464 | + * @param string $str |
|
465 | + * |
|
466 | + * @return $this |
|
467 | + */ |
|
468 | + public function setFragment(string $str) |
|
469 | + { |
|
470 | + $this->fragment = trim($str, '# '); |
|
471 | + |
|
472 | + return $this->fragment; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * Saves our parts from a parse_url call. |
|
477 | + * |
|
478 | + * @param array $parts |
|
479 | + * |
|
480 | + * @return mixed |
|
481 | + */ |
|
482 | + public function applyParts(array $paths) |
|
483 | + { |
|
484 | + if (isset($parts['scheme'])) { |
|
485 | + $this->SetScheme(rtrim($parts['scheme'], ':/')); |
|
486 | + } else { |
|
487 | + $this->setScheme('http'); |
|
488 | + } |
|
489 | + |
|
490 | + if ( ! empty($parts['host'])) { |
|
491 | + $this->host = $parts['host']; |
|
492 | + } |
|
493 | + |
|
494 | + if (isset($parts['port'])) { |
|
495 | + if ( ! is_null($parts['port'])) { |
|
496 | + $this->port = $parts['port']; |
|
497 | + } |
|
498 | + } |
|
499 | + |
|
500 | + if ( ! empty($parts['user'])) { |
|
501 | + $this->user = $parts['user']; |
|
502 | + } |
|
503 | + |
|
504 | + if ( ! empty($parts['pass'])) { |
|
505 | + $this->password = $parts['pass']; |
|
506 | + } |
|
507 | + |
|
508 | + if ( ! empty($parts['path'])) { |
|
509 | + $this->path = $this->filterPath($parts['path']); |
|
510 | + } |
|
511 | + |
|
512 | + if ( ! empty($parts['fragment'])) { |
|
513 | + $this->fragment = $parts['fragment']; |
|
514 | + } |
|
515 | + |
|
516 | + if ( ! empty($parts['path'])) { |
|
517 | + $this->segments = explode('/', $parts['path'], '/'); |
|
518 | + } |
|
519 | + } |
|
520 | + |
|
521 | + /** |
|
522 | + * Returns the URI string. |
|
523 | + * |
|
524 | + * @return string |
|
525 | + */ |
|
526 | + public function __toString() |
|
527 | + { |
|
528 | + return (string) $this->getPath(); |
|
529 | + } |
|
530 | 530 | } |
@@ -31,275 +31,275 @@ |
||
31 | 31 | */ |
32 | 32 | class Http |
33 | 33 | { |
34 | - /** |
|
35 | - * Determines if this request was made from the command line (CLI). |
|
36 | - * |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function isCli() |
|
40 | - { |
|
41 | - return (PHP_SAPI === 'cli' || defined('STDIN')); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Return's the protocol that the request was made with. |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function protocol() |
|
50 | - { |
|
51 | - if ($this->server('HTTPS') == 'on' || |
|
52 | - $this->server('HTTPS') == 1 || |
|
53 | - $this->server('SERVER_PORT') == 443 || |
|
54 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
55 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
56 | - { |
|
57 | - return 'https'; |
|
58 | - } |
|
59 | - |
|
60 | - return 'http'; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Fetch an item from the COOKIE array. |
|
65 | - * |
|
66 | - * @param string $index The index key |
|
67 | - * @param mixed $default The default value |
|
68 | - * |
|
69 | - * @return string|array |
|
70 | - */ |
|
71 | - public function cookie($index = null, $default = null) |
|
72 | - { |
|
73 | - return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Fetch an item from the FILE array. |
|
78 | - * |
|
79 | - * @param string $index The index key |
|
80 | - * @param mixed $default The default value |
|
81 | - * |
|
82 | - * @return string|array |
|
83 | - */ |
|
84 | - public function file($index = null, $default = null) |
|
85 | - { |
|
86 | - return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Fetch an item from the SERVER array. |
|
91 | - * |
|
92 | - * @param string $index The index key |
|
93 | - * @param mixed $default The default value |
|
94 | - * |
|
95 | - * @return string|array |
|
96 | - */ |
|
97 | - public function server($index = null, $default = null) |
|
98 | - { |
|
99 | - return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
100 | - } |
|
34 | + /** |
|
35 | + * Determines if this request was made from the command line (CLI). |
|
36 | + * |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function isCli() |
|
40 | + { |
|
41 | + return (PHP_SAPI === 'cli' || defined('STDIN')); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Return's the protocol that the request was made with. |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function protocol() |
|
50 | + { |
|
51 | + if ($this->server('HTTPS') == 'on' || |
|
52 | + $this->server('HTTPS') == 1 || |
|
53 | + $this->server('SERVER_PORT') == 443 || |
|
54 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
55 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
56 | + { |
|
57 | + return 'https'; |
|
58 | + } |
|
59 | + |
|
60 | + return 'http'; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Fetch an item from the COOKIE array. |
|
65 | + * |
|
66 | + * @param string $index The index key |
|
67 | + * @param mixed $default The default value |
|
68 | + * |
|
69 | + * @return string|array |
|
70 | + */ |
|
71 | + public function cookie($index = null, $default = null) |
|
72 | + { |
|
73 | + return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Fetch an item from the FILE array. |
|
78 | + * |
|
79 | + * @param string $index The index key |
|
80 | + * @param mixed $default The default value |
|
81 | + * |
|
82 | + * @return string|array |
|
83 | + */ |
|
84 | + public function file($index = null, $default = null) |
|
85 | + { |
|
86 | + return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Fetch an item from the SERVER array. |
|
91 | + * |
|
92 | + * @param string $index The index key |
|
93 | + * @param mixed $default The default value |
|
94 | + * |
|
95 | + * @return string|array |
|
96 | + */ |
|
97 | + public function server($index = null, $default = null) |
|
98 | + { |
|
99 | + return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Gets the URI Protocol based setting, will attempt to detect the path |
|
104 | - * portion of the current URI. |
|
105 | - * |
|
106 | - * @param string $protocol |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - public function detectPath(string $protocol = '') |
|
111 | - { |
|
112 | - if (empty($protocol)) { |
|
113 | - $protocol = 'REQUEST_URI'; |
|
114 | - } |
|
115 | - |
|
116 | - switch($protocol) { |
|
117 | - case 'REQUEST_URI': |
|
118 | - $path = $this->parseRequestUri(); |
|
119 | - break; |
|
120 | - case 'QUERY_STRING': |
|
121 | - $path = $this->parseQueryString(); |
|
122 | - break; |
|
123 | - case 'PATH_INFO': |
|
124 | - default: |
|
125 | - $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
126 | - break; |
|
127 | - } |
|
128 | - |
|
129 | - return $path; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Filters a value from the start of a string in this case the passed URI string. |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - protected function parseRequestUri() |
|
138 | - { |
|
139 | - if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
140 | - return ''; |
|
141 | - } |
|
142 | - |
|
143 | - $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
144 | - $components = parse_url($requestURI); |
|
145 | - $query = $components['query'] ?? ''; |
|
146 | - $uri = $components['path'] ?? ''; |
|
147 | - |
|
148 | - // If the search value is at the start |
|
149 | - if (isset($this->server('SCRIPT_NAME')[0])) { |
|
150 | - if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
151 | - $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
152 | - } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
153 | - $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
154 | - } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
155 | - $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - // This section ensures that even on servers that require the URI to contain |
|
160 | - // the query string (Nginx) is the correctly |
|
161 | - if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
162 | - $query = explode('?', $query, 2); |
|
163 | - $uri = $query[0]; |
|
164 | - $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
165 | - } else { |
|
166 | - $_SERVER['QUERY_STRING'] = $query; |
|
167 | - } |
|
168 | - |
|
169 | - // Parses the string into variables |
|
170 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
171 | - |
|
172 | - if ('/' === $uri || '' === $uri) { |
|
173 | - return ''; |
|
174 | - } |
|
175 | - |
|
176 | - return $this->filterDecode($uri); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Will parse QUERY_STRING and automatically detect the URI from it. |
|
181 | - * |
|
182 | - * @return string |
|
183 | - */ |
|
184 | - protected function parseQueryString() |
|
185 | - { |
|
186 | - $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
187 | - |
|
188 | - if (trim($uri, '/') === '') { |
|
189 | - return ''; |
|
190 | - } elseif (0 === strncmp($uri, '/', 1)) { |
|
191 | - $uri = explode('?', $uri, 2); |
|
192 | - $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
193 | - $uri = $uri[0] ?? ''; |
|
194 | - } |
|
195 | - |
|
196 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
197 | - |
|
198 | - return $this->filterDecode($uri); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
203 | - * |
|
204 | - * @param string $uri |
|
205 | - * |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - protected function filterDecode($uri) |
|
209 | - { |
|
210 | - // Remove all characters except letters, |
|
211 | - // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
212 | - $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
102 | + /** |
|
103 | + * Gets the URI Protocol based setting, will attempt to detect the path |
|
104 | + * portion of the current URI. |
|
105 | + * |
|
106 | + * @param string $protocol |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + public function detectPath(string $protocol = '') |
|
111 | + { |
|
112 | + if (empty($protocol)) { |
|
113 | + $protocol = 'REQUEST_URI'; |
|
114 | + } |
|
115 | + |
|
116 | + switch($protocol) { |
|
117 | + case 'REQUEST_URI': |
|
118 | + $path = $this->parseRequestUri(); |
|
119 | + break; |
|
120 | + case 'QUERY_STRING': |
|
121 | + $path = $this->parseQueryString(); |
|
122 | + break; |
|
123 | + case 'PATH_INFO': |
|
124 | + default: |
|
125 | + $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
126 | + break; |
|
127 | + } |
|
128 | + |
|
129 | + return $path; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Filters a value from the start of a string in this case the passed URI string. |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + protected function parseRequestUri() |
|
138 | + { |
|
139 | + if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
140 | + return ''; |
|
141 | + } |
|
142 | + |
|
143 | + $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
144 | + $components = parse_url($requestURI); |
|
145 | + $query = $components['query'] ?? ''; |
|
146 | + $uri = $components['path'] ?? ''; |
|
147 | + |
|
148 | + // If the search value is at the start |
|
149 | + if (isset($this->server('SCRIPT_NAME')[0])) { |
|
150 | + if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
151 | + $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
152 | + } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
153 | + $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
154 | + } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
155 | + $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + // This section ensures that even on servers that require the URI to contain |
|
160 | + // the query string (Nginx) is the correctly |
|
161 | + if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
162 | + $query = explode('?', $query, 2); |
|
163 | + $uri = $query[0]; |
|
164 | + $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
165 | + } else { |
|
166 | + $_SERVER['QUERY_STRING'] = $query; |
|
167 | + } |
|
168 | + |
|
169 | + // Parses the string into variables |
|
170 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
171 | + |
|
172 | + if ('/' === $uri || '' === $uri) { |
|
173 | + return ''; |
|
174 | + } |
|
175 | + |
|
176 | + return $this->filterDecode($uri); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Will parse QUERY_STRING and automatically detect the URI from it. |
|
181 | + * |
|
182 | + * @return string |
|
183 | + */ |
|
184 | + protected function parseQueryString() |
|
185 | + { |
|
186 | + $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
187 | + |
|
188 | + if (trim($uri, '/') === '') { |
|
189 | + return ''; |
|
190 | + } elseif (0 === strncmp($uri, '/', 1)) { |
|
191 | + $uri = explode('?', $uri, 2); |
|
192 | + $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
193 | + $uri = $uri[0] ?? ''; |
|
194 | + } |
|
195 | + |
|
196 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
197 | + |
|
198 | + return $this->filterDecode($uri); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
203 | + * |
|
204 | + * @param string $uri |
|
205 | + * |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + protected function filterDecode($uri) |
|
209 | + { |
|
210 | + // Remove all characters except letters, |
|
211 | + // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
212 | + $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
213 | 213 | |
214 | - // Return argument if not empty or return a single slash |
|
215 | - return trim($uri, '/') ?: '/'; |
|
216 | - } |
|
214 | + // Return argument if not empty or return a single slash |
|
215 | + return trim($uri, '/') ?: '/'; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Parse the base URL. |
|
220 | - * |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - public function parseBaseUrl() |
|
224 | - { |
|
225 | - $filename = basename($this->server('SCRIPT_FILENAME')); |
|
218 | + /** |
|
219 | + * Parse the base URL. |
|
220 | + * |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + public function parseBaseUrl() |
|
224 | + { |
|
225 | + $filename = basename($this->server('SCRIPT_FILENAME')); |
|
226 | 226 | |
227 | - if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
228 | - $baseUrl = $this->server('SCRIPT_NAME'); |
|
229 | - } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
230 | - $baseUrl = $this->server('PHP_SELF'); |
|
231 | - } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
232 | - $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
233 | - } else { |
|
234 | - $path = $this->server('PHP_SELF', ''); |
|
235 | - $file = $this->server('SCRIPT_FILENAME', ''); |
|
236 | - $segs = explode('/', trim($file, '/')); |
|
237 | - $segs = array_reverse($segs); |
|
238 | - $index = 0; |
|
239 | - $last = count($segs); |
|
240 | - $baseUrl = ''; |
|
227 | + if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
228 | + $baseUrl = $this->server('SCRIPT_NAME'); |
|
229 | + } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
230 | + $baseUrl = $this->server('PHP_SELF'); |
|
231 | + } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
232 | + $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
233 | + } else { |
|
234 | + $path = $this->server('PHP_SELF', ''); |
|
235 | + $file = $this->server('SCRIPT_FILENAME', ''); |
|
236 | + $segs = explode('/', trim($file, '/')); |
|
237 | + $segs = array_reverse($segs); |
|
238 | + $index = 0; |
|
239 | + $last = count($segs); |
|
240 | + $baseUrl = ''; |
|
241 | 241 | |
242 | - do { |
|
243 | - $seg = $segs[$index]; |
|
244 | - $baseUrl = '/'.$seg.$baseUrl; |
|
245 | - ++$index; |
|
246 | - } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
247 | - } |
|
248 | - |
|
249 | - // Does the baseUrl have anything in common with the request_uri? |
|
250 | - $requestUri = $this->parseRequestUri(); |
|
251 | - |
|
252 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
253 | - $requestUri = '/'.$requestUri; |
|
254 | - } |
|
255 | - |
|
256 | - $baseUrl = dirname($baseUrl); |
|
257 | - |
|
258 | - if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
259 | - // no match whatsoever; set it blank |
|
260 | - return ''; |
|
261 | - } |
|
242 | + do { |
|
243 | + $seg = $segs[$index]; |
|
244 | + $baseUrl = '/'.$seg.$baseUrl; |
|
245 | + ++$index; |
|
246 | + } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
247 | + } |
|
248 | + |
|
249 | + // Does the baseUrl have anything in common with the request_uri? |
|
250 | + $requestUri = $this->parseRequestUri(); |
|
251 | + |
|
252 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
253 | + $requestUri = '/'.$requestUri; |
|
254 | + } |
|
255 | + |
|
256 | + $baseUrl = dirname($baseUrl); |
|
257 | + |
|
258 | + if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
259 | + // no match whatsoever; set it blank |
|
260 | + return ''; |
|
261 | + } |
|
262 | 262 | |
263 | - // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
264 | - // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
265 | - // from PATH_INFO or QUERY_STRING |
|
266 | - if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
267 | - $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
268 | - } |
|
263 | + // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
264 | + // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
265 | + // from PATH_INFO or QUERY_STRING |
|
266 | + if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
267 | + $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
268 | + } |
|
269 | 269 | |
270 | - return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Parse the path info. |
|
275 | - * |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - public function parsePathInfo() |
|
279 | - { |
|
280 | - if (null === ($requestUri = $this->parseRequestUri())) { |
|
281 | - return '/'; |
|
282 | - } |
|
283 | - |
|
284 | - // Remove the query string from REQUEST_URI |
|
285 | - if (false !== $pos = strpos($requestUri, '?')) { |
|
286 | - $requestUri = substr($requestUri, 0, $pos); |
|
287 | - } |
|
288 | - |
|
289 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
290 | - $requestUri = '/'.$requestUri; |
|
291 | - } |
|
292 | - |
|
293 | - if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
294 | - return $requestUri; |
|
295 | - } |
|
296 | - |
|
297 | - $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
298 | - |
|
299 | - if (false === $pathInfo && '' === $pathInfo) { |
|
300 | - return '/'; |
|
301 | - } |
|
270 | + return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Parse the path info. |
|
275 | + * |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + public function parsePathInfo() |
|
279 | + { |
|
280 | + if (null === ($requestUri = $this->parseRequestUri())) { |
|
281 | + return '/'; |
|
282 | + } |
|
283 | + |
|
284 | + // Remove the query string from REQUEST_URI |
|
285 | + if (false !== $pos = strpos($requestUri, '?')) { |
|
286 | + $requestUri = substr($requestUri, 0, $pos); |
|
287 | + } |
|
288 | + |
|
289 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
290 | + $requestUri = '/'.$requestUri; |
|
291 | + } |
|
292 | + |
|
293 | + if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
294 | + return $requestUri; |
|
295 | + } |
|
296 | + |
|
297 | + $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
298 | + |
|
299 | + if (false === $pathInfo && '' === $pathInfo) { |
|
300 | + return '/'; |
|
301 | + } |
|
302 | 302 | |
303 | - return (string) $pathInfo; |
|
304 | - } |
|
303 | + return (string) $pathInfo; |
|
304 | + } |
|
305 | 305 | } |
306 | 306 | \ No newline at end of file |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | $protocol = 'REQUEST_URI'; |
114 | 114 | } |
115 | 115 | |
116 | - switch($protocol) { |
|
116 | + switch ($protocol) { |
|
117 | 117 | case 'REQUEST_URI': |
118 | 118 | $path = $this->parseRequestUri(); |
119 | 119 | break; |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $last = count($segs); |
240 | 240 | $baseUrl = ''; |
241 | 241 | |
242 | - do { |
|
242 | + do { |
|
243 | 243 | $seg = $segs[$index]; |
244 | 244 | $baseUrl = '/'.$seg.$baseUrl; |
245 | 245 | ++$index; |
@@ -33,236 +33,236 @@ |
||
33 | 33 | */ |
34 | 34 | class Headers implements IteratorAggregate, Countable |
35 | 35 | { |
36 | - /** |
|
37 | - * An array of HTTP headers. |
|
38 | - * |
|
39 | - * @var array $herders |
|
40 | - */ |
|
41 | - protected $headers = []; |
|
36 | + /** |
|
37 | + * An array of HTTP headers. |
|
38 | + * |
|
39 | + * @var array $herders |
|
40 | + */ |
|
41 | + protected $headers = []; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Specifies the directives for the caching mechanisms in both |
|
45 | - * the requests and the responses. |
|
46 | - * |
|
47 | - * @var array $cacheControl |
|
48 | - */ |
|
49 | - protected $cacheControl = []; |
|
43 | + /** |
|
44 | + * Specifies the directives for the caching mechanisms in both |
|
45 | + * the requests and the responses. |
|
46 | + * |
|
47 | + * @var array $cacheControl |
|
48 | + */ |
|
49 | + protected $cacheControl = []; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Constructor. The Headers class instance. |
|
53 | - * |
|
54 | - * @param array $headers |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function __construct(array $headers = []) |
|
59 | - { |
|
60 | - foreach ($headers as $key => $values) { |
|
61 | - $this->set($key, $values); |
|
62 | - } |
|
63 | - } |
|
51 | + /** |
|
52 | + * Constructor. The Headers class instance. |
|
53 | + * |
|
54 | + * @param array $headers |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function __construct(array $headers = []) |
|
59 | + { |
|
60 | + foreach ($headers as $key => $values) { |
|
61 | + $this->set($key, $values); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Returns all the headers. |
|
67 | - * |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public function all() |
|
71 | - { |
|
72 | - return $this->headers; |
|
73 | - } |
|
65 | + /** |
|
66 | + * Returns all the headers. |
|
67 | + * |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public function all() |
|
71 | + { |
|
72 | + return $this->headers; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Returns the parameter keys. |
|
77 | - * |
|
78 | - * @return array An array of parameter keys |
|
79 | - */ |
|
80 | - public function keys() |
|
81 | - { |
|
82 | - return array_keys($this->all()); |
|
83 | - } |
|
75 | + /** |
|
76 | + * Returns the parameter keys. |
|
77 | + * |
|
78 | + * @return array An array of parameter keys |
|
79 | + */ |
|
80 | + public function keys() |
|
81 | + { |
|
82 | + return array_keys($this->all()); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Replaces the current HTTP headers by a new set. |
|
87 | - * |
|
88 | - * @param array $headers |
|
89 | - * |
|
90 | - * @return void |
|
91 | - */ |
|
92 | - public function replace(array $headers = []) |
|
93 | - { |
|
94 | - $this->headers = []; |
|
95 | - $this->add($headers); |
|
96 | - } |
|
85 | + /** |
|
86 | + * Replaces the current HTTP headers by a new set. |
|
87 | + * |
|
88 | + * @param array $headers |
|
89 | + * |
|
90 | + * @return void |
|
91 | + */ |
|
92 | + public function replace(array $headers = []) |
|
93 | + { |
|
94 | + $this->headers = []; |
|
95 | + $this->add($headers); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * Adds multiple header to the queue. |
|
100 | - * |
|
101 | - * @param array $headers The header name |
|
102 | - * |
|
103 | - * @return mixed |
|
104 | - */ |
|
105 | - public function add(array $headers) |
|
106 | - { |
|
107 | - foreach ($headers as $key => $values) { |
|
108 | - $this->set($key, $values); |
|
109 | - } |
|
98 | + /** |
|
99 | + * Adds multiple header to the queue. |
|
100 | + * |
|
101 | + * @param array $headers The header name |
|
102 | + * |
|
103 | + * @return mixed |
|
104 | + */ |
|
105 | + public function add(array $headers) |
|
106 | + { |
|
107 | + foreach ($headers as $key => $values) { |
|
108 | + $this->set($key, $values); |
|
109 | + } |
|
110 | 110 | |
111 | - return $this; |
|
112 | - } |
|
111 | + return $this; |
|
112 | + } |
|
113 | 113 | |
114 | - /** |
|
115 | - * Returns the headers, with original capitalizations. |
|
116 | - * |
|
117 | - * @return array An array of headers |
|
118 | - */ |
|
119 | - public function allPreserveCase() |
|
120 | - { |
|
121 | - $headers = []; |
|
114 | + /** |
|
115 | + * Returns the headers, with original capitalizations. |
|
116 | + * |
|
117 | + * @return array An array of headers |
|
118 | + */ |
|
119 | + public function allPreserveCase() |
|
120 | + { |
|
121 | + $headers = []; |
|
122 | 122 | |
123 | - foreach ($this->all() as $name => $value) { |
|
124 | - $headers[$name] = $value; |
|
125 | - } |
|
123 | + foreach ($this->all() as $name => $value) { |
|
124 | + $headers[$name] = $value; |
|
125 | + } |
|
126 | 126 | |
127 | - return $headers; |
|
128 | - } |
|
127 | + return $headers; |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Gets a header value by name. |
|
132 | - * |
|
133 | - * @param string $key The header name, or null for all headers |
|
134 | - * @param string|null $default The default value |
|
135 | - * @param bool $option Whether to return the option value or all header values (true by default) |
|
136 | - * |
|
137 | - * @return mixed |
|
138 | - */ |
|
139 | - public function get($key, $default = null, $option = true) |
|
140 | - { |
|
141 | - $key = str_replace('_', '-', strtolower($key)); |
|
130 | + /** |
|
131 | + * Gets a header value by name. |
|
132 | + * |
|
133 | + * @param string $key The header name, or null for all headers |
|
134 | + * @param string|null $default The default value |
|
135 | + * @param bool $option Whether to return the option value or all header values (true by default) |
|
136 | + * |
|
137 | + * @return mixed |
|
138 | + */ |
|
139 | + public function get($key, $default = null, $option = true) |
|
140 | + { |
|
141 | + $key = str_replace('_', '-', strtolower($key)); |
|
142 | 142 | |
143 | - $headers = $this->all(); |
|
143 | + $headers = $this->all(); |
|
144 | 144 | |
145 | - if ( ! array_key_exists($key, $headers)) { |
|
146 | - if (null === $default) { |
|
147 | - return $option ? null : []; |
|
148 | - } |
|
145 | + if ( ! array_key_exists($key, $headers)) { |
|
146 | + if (null === $default) { |
|
147 | + return $option ? null : []; |
|
148 | + } |
|
149 | 149 | |
150 | - return $option ? $default : [$default]; |
|
151 | - } |
|
150 | + return $option ? $default : [$default]; |
|
151 | + } |
|
152 | 152 | |
153 | - if ($option) { |
|
154 | - return count($headers[$key]) ? $headers[$key][0] : $default; |
|
155 | - } |
|
153 | + if ($option) { |
|
154 | + return count($headers[$key]) ? $headers[$key][0] : $default; |
|
155 | + } |
|
156 | 156 | |
157 | - return $headers[$key]; |
|
158 | - } |
|
157 | + return $headers[$key]; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * Sets a header by name. |
|
162 | - * |
|
163 | - * @param string $key The header name |
|
164 | - * @param string $values The value or an array of values |
|
165 | - * @param bool $replace If you want to replace the value exists by the header, |
|
166 | - * it is not overwritten (true by default) / overwritten when it is false |
|
167 | - * |
|
168 | - * @return $this |
|
169 | - */ |
|
170 | - public function set($key, $values, $replace = true) |
|
171 | - { |
|
172 | - $key = str_replace('_', '-', strtolower($key)); |
|
160 | + /** |
|
161 | + * Sets a header by name. |
|
162 | + * |
|
163 | + * @param string $key The header name |
|
164 | + * @param string $values The value or an array of values |
|
165 | + * @param bool $replace If you want to replace the value exists by the header, |
|
166 | + * it is not overwritten (true by default) / overwritten when it is false |
|
167 | + * |
|
168 | + * @return $this |
|
169 | + */ |
|
170 | + public function set($key, $values, $replace = true) |
|
171 | + { |
|
172 | + $key = str_replace('_', '-', strtolower($key)); |
|
173 | 173 | |
174 | - if (is_array($values)) { |
|
175 | - $values = array_values($values); |
|
174 | + if (is_array($values)) { |
|
175 | + $values = array_values($values); |
|
176 | 176 | |
177 | - if (true === $replace || ! isset($this->headers[$key])) { |
|
178 | - $this->headers[$key] = $values; |
|
179 | - } else { |
|
180 | - $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
181 | - } |
|
182 | - } else { |
|
183 | - if (true === $replace || ! isset($this->headers[$key])) { |
|
184 | - $this->headers[$key] = [$values]; |
|
185 | - } else { |
|
186 | - $this->headers[$key][] = $values; |
|
187 | - } |
|
188 | - } |
|
177 | + if (true === $replace || ! isset($this->headers[$key])) { |
|
178 | + $this->headers[$key] = $values; |
|
179 | + } else { |
|
180 | + $this->headers[$key] = array_merge($this->headers[$key], $values); |
|
181 | + } |
|
182 | + } else { |
|
183 | + if (true === $replace || ! isset($this->headers[$key])) { |
|
184 | + $this->headers[$key] = [$values]; |
|
185 | + } else { |
|
186 | + $this->headers[$key][] = $values; |
|
187 | + } |
|
188 | + } |
|
189 | 189 | |
190 | - return $this; |
|
191 | - } |
|
190 | + return $this; |
|
191 | + } |
|
192 | 192 | |
193 | - /** |
|
194 | - * Returns true if the HTTP header is defined. |
|
195 | - * |
|
196 | - * @param string $key The HTTP header |
|
197 | - * |
|
198 | - * @return bool true if the parameter exists, false otherwise |
|
199 | - */ |
|
200 | - public function has($key) |
|
201 | - { |
|
202 | - return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all()); |
|
203 | - } |
|
193 | + /** |
|
194 | + * Returns true if the HTTP header is defined. |
|
195 | + * |
|
196 | + * @param string $key The HTTP header |
|
197 | + * |
|
198 | + * @return bool true if the parameter exists, false otherwise |
|
199 | + */ |
|
200 | + public function has($key) |
|
201 | + { |
|
202 | + return array_key_exists(str_replace('_', '-', strtolower($key)), $this->all()); |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * Removes a header. |
|
207 | - * |
|
208 | - * @param string $name The header name |
|
209 | - * |
|
210 | - * @return mixed |
|
211 | - */ |
|
212 | - public function remove($key) |
|
213 | - { |
|
214 | - $key = str_replace('_', '-', strtolower($key)); |
|
205 | + /** |
|
206 | + * Removes a header. |
|
207 | + * |
|
208 | + * @param string $name The header name |
|
209 | + * |
|
210 | + * @return mixed |
|
211 | + */ |
|
212 | + public function remove($key) |
|
213 | + { |
|
214 | + $key = str_replace('_', '-', strtolower($key)); |
|
215 | 215 | |
216 | - unset($this->headers[$key]); |
|
216 | + unset($this->headers[$key]); |
|
217 | 217 | |
218 | - if ('cache-control' === $key) { |
|
219 | - $this->cacheControl = []; |
|
220 | - } |
|
221 | - } |
|
218 | + if ('cache-control' === $key) { |
|
219 | + $this->cacheControl = []; |
|
220 | + } |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * Returns an iterator for headers. |
|
225 | - * |
|
226 | - * @return \ArrayIterator An \ArrayIterator instance |
|
227 | - */ |
|
228 | - public function getIterator() |
|
229 | - { |
|
230 | - return new ArrayIterator($this->headers); |
|
231 | - } |
|
223 | + /** |
|
224 | + * Returns an iterator for headers. |
|
225 | + * |
|
226 | + * @return \ArrayIterator An \ArrayIterator instance |
|
227 | + */ |
|
228 | + public function getIterator() |
|
229 | + { |
|
230 | + return new ArrayIterator($this->headers); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * Returns the number of headers. |
|
235 | - * |
|
236 | - * @return int The number of headers |
|
237 | - */ |
|
238 | - public function count() |
|
239 | - { |
|
240 | - return count($this->headers); |
|
241 | - } |
|
233 | + /** |
|
234 | + * Returns the number of headers. |
|
235 | + * |
|
236 | + * @return int The number of headers |
|
237 | + */ |
|
238 | + public function count() |
|
239 | + { |
|
240 | + return count($this->headers); |
|
241 | + } |
|
242 | 242 | |
243 | - /** |
|
244 | - * Returns the headers as a string. |
|
245 | - * |
|
246 | - * @return string The headers |
|
247 | - */ |
|
248 | - public function __toString() |
|
249 | - { |
|
250 | - if ( ! $headers = $this->all()) { |
|
251 | - return ''; |
|
252 | - } |
|
243 | + /** |
|
244 | + * Returns the headers as a string. |
|
245 | + * |
|
246 | + * @return string The headers |
|
247 | + */ |
|
248 | + public function __toString() |
|
249 | + { |
|
250 | + if ( ! $headers = $this->all()) { |
|
251 | + return ''; |
|
252 | + } |
|
253 | 253 | |
254 | - ksort($headers); |
|
255 | - $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
256 | - $content = ''; |
|
254 | + ksort($headers); |
|
255 | + $max = max(array_map('strlen', array_keys($headers))) + 1; |
|
256 | + $content = ''; |
|
257 | 257 | |
258 | - foreach ($headers as $name => $values) { |
|
259 | - $name = ucwords($name, '-'); |
|
258 | + foreach ($headers as $name => $values) { |
|
259 | + $name = ucwords($name, '-'); |
|
260 | 260 | |
261 | - foreach ($values as $value) { |
|
262 | - $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
263 | - } |
|
264 | - } |
|
261 | + foreach ($values as $value) { |
|
262 | + $content .= sprintf("%-{$max}s %s\r\n", $name.':', $value); |
|
263 | + } |
|
264 | + } |
|
265 | 265 | |
266 | - return $content; |
|
267 | - } |
|
266 | + return $content; |
|
267 | + } |
|
268 | 268 | } |
269 | 269 | \ No newline at end of file |
@@ -33,156 +33,156 @@ |
||
33 | 33 | */ |
34 | 34 | class Parameters implements IteratorAggregate, Countable |
35 | 35 | { |
36 | - /** |
|
37 | - * Array parameters from the Server global. |
|
38 | - * |
|
39 | - * @var array $parameters |
|
40 | - */ |
|
41 | - protected $parameters = []; |
|
36 | + /** |
|
37 | + * Array parameters from the Server global. |
|
38 | + * |
|
39 | + * @var array $parameters |
|
40 | + */ |
|
41 | + protected $parameters = []; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Parameter Object Constructor. |
|
45 | - * |
|
46 | - * @param array $parameters |
|
47 | - * |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function __construct(array $parameters = []) |
|
51 | - { |
|
52 | - $this->parameters = $parameters; |
|
53 | - } |
|
43 | + /** |
|
44 | + * Parameter Object Constructor. |
|
45 | + * |
|
46 | + * @param array $parameters |
|
47 | + * |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function __construct(array $parameters = []) |
|
51 | + { |
|
52 | + $this->parameters = $parameters; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Returns the parameters. |
|
57 | - * |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function all() |
|
61 | - { |
|
62 | - return $this->parameters; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Returns the parameters. |
|
57 | + * |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function all() |
|
61 | + { |
|
62 | + return $this->parameters; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Returns the parameter keys. |
|
67 | - * |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public function keys() |
|
71 | - { |
|
72 | - return array_keys($this->parameters); |
|
73 | - } |
|
65 | + /** |
|
66 | + * Returns the parameter keys. |
|
67 | + * |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public function keys() |
|
71 | + { |
|
72 | + return array_keys($this->parameters); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Replaces the current parameters. |
|
77 | - * |
|
78 | - * @param array $parameters |
|
79 | - * |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - public function replace(array $parameters = []) |
|
83 | - { |
|
84 | - $this->parameters = $parameters; |
|
85 | - } |
|
75 | + /** |
|
76 | + * Replaces the current parameters. |
|
77 | + * |
|
78 | + * @param array $parameters |
|
79 | + * |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + public function replace(array $parameters = []) |
|
83 | + { |
|
84 | + $this->parameters = $parameters; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Adds parameters. |
|
89 | - * |
|
90 | - * @param array $parameters |
|
91 | - * |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function add(array $parameters = []) |
|
95 | - { |
|
96 | - $this->parameters = array_replace($this->parameters, $parameters); |
|
97 | - } |
|
87 | + /** |
|
88 | + * Adds parameters. |
|
89 | + * |
|
90 | + * @param array $parameters |
|
91 | + * |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function add(array $parameters = []) |
|
95 | + { |
|
96 | + $this->parameters = array_replace($this->parameters, $parameters); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get a parameter array item. |
|
101 | - * |
|
102 | - * @param string $key |
|
103 | - * @param string|null $default (null by default) |
|
104 | - * |
|
105 | - * @return mixed |
|
106 | - */ |
|
107 | - public function get($key, $default = null) |
|
108 | - { |
|
109 | - if ($this->has($key)) { |
|
110 | - return $this->parameters[$key]; |
|
111 | - } |
|
99 | + /** |
|
100 | + * Get a parameter array item. |
|
101 | + * |
|
102 | + * @param string $key |
|
103 | + * @param string|null $default (null by default) |
|
104 | + * |
|
105 | + * @return mixed |
|
106 | + */ |
|
107 | + public function get($key, $default = null) |
|
108 | + { |
|
109 | + if ($this->has($key)) { |
|
110 | + return $this->parameters[$key]; |
|
111 | + } |
|
112 | 112 | |
113 | - return $default; |
|
114 | - } |
|
113 | + return $default; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Check if a parameter array item exists. |
|
118 | - * |
|
119 | - * @param string $key |
|
120 | - * |
|
121 | - * @return mixed |
|
122 | - */ |
|
123 | - public function has($key) |
|
124 | - { |
|
125 | - return Arr::exists($this->parameters, $key); |
|
126 | - } |
|
116 | + /** |
|
117 | + * Check if a parameter array item exists. |
|
118 | + * |
|
119 | + * @param string $key |
|
120 | + * |
|
121 | + * @return mixed |
|
122 | + */ |
|
123 | + public function has($key) |
|
124 | + { |
|
125 | + return Arr::exists($this->parameters, $key); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Set a parameter array item. |
|
130 | - * |
|
131 | - * @param string $key |
|
132 | - * @param string $value |
|
133 | - * |
|
134 | - * @return mixed |
|
135 | - */ |
|
136 | - public function set($key, $value) |
|
137 | - { |
|
138 | - $this->parameters[$key] = $value; |
|
139 | - } |
|
128 | + /** |
|
129 | + * Set a parameter array item. |
|
130 | + * |
|
131 | + * @param string $key |
|
132 | + * @param string $value |
|
133 | + * |
|
134 | + * @return mixed |
|
135 | + */ |
|
136 | + public function set($key, $value) |
|
137 | + { |
|
138 | + $this->parameters[$key] = $value; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Remove a parameter array item. |
|
143 | - * |
|
144 | - * @param string $key |
|
145 | - * |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - public function remove($key) |
|
149 | - { |
|
150 | - if ($this->has($key)) { |
|
151 | - unset($this->parameters[$key]); |
|
152 | - } |
|
153 | - } |
|
141 | + /** |
|
142 | + * Remove a parameter array item. |
|
143 | + * |
|
144 | + * @param string $key |
|
145 | + * |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + public function remove($key) |
|
149 | + { |
|
150 | + if ($this->has($key)) { |
|
151 | + unset($this->parameters[$key]); |
|
152 | + } |
|
153 | + } |
|
154 | 154 | |
155 | - /* |
|
155 | + /* |
|
156 | 156 | |----------------------------------------------------------------- |
157 | 157 | | IteratorAggregate Method |
158 | 158 | |----------------------------------------------------------------- |
159 | 159 | */ |
160 | 160 | |
161 | - /** |
|
162 | - * Retrieve an external iterator. |
|
163 | - * |
|
164 | - * @see \IteratorAggregate::getIterator |
|
165 | - * |
|
166 | - * @return new \ArrayIterator |
|
167 | - */ |
|
168 | - public function getIterator() |
|
169 | - { |
|
170 | - return new ArrayIterator($this->parameters); |
|
171 | - } |
|
161 | + /** |
|
162 | + * Retrieve an external iterator. |
|
163 | + * |
|
164 | + * @see \IteratorAggregate::getIterator |
|
165 | + * |
|
166 | + * @return new \ArrayIterator |
|
167 | + */ |
|
168 | + public function getIterator() |
|
169 | + { |
|
170 | + return new ArrayIterator($this->parameters); |
|
171 | + } |
|
172 | 172 | |
173 | - /* |
|
173 | + /* |
|
174 | 174 | |----------------------------------------------------------------- |
175 | 175 | | Countable Method |
176 | 176 | |----------------------------------------------------------------- |
177 | 177 | */ |
178 | 178 | |
179 | - /** |
|
180 | - * Returns the number of parameters. |
|
181 | - * |
|
182 | - * @return int The number of parameters |
|
183 | - */ |
|
184 | - public function count() |
|
185 | - { |
|
186 | - return count($this->parameters); |
|
187 | - } |
|
179 | + /** |
|
180 | + * Returns the number of parameters. |
|
181 | + * |
|
182 | + * @return int The number of parameters |
|
183 | + */ |
|
184 | + public function count() |
|
185 | + { |
|
186 | + return count($this->parameters); |
|
187 | + } |
|
188 | 188 | } |
189 | 189 | \ No newline at end of file |
@@ -33,46 +33,46 @@ discard block |
||
33 | 33 | final class Inputs extends Parameters |
34 | 34 | { |
35 | 35 | /** |
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - public function all(string $key = null) |
|
39 | - { |
|
40 | - return parent::all($key); |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + public function all(string $key = null) |
|
39 | + { |
|
40 | + return parent::all($key); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - public function replace(array $inputs = []) |
|
47 | - { |
|
48 | - $this->parameters = []; |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + public function replace(array $inputs = []) |
|
47 | + { |
|
48 | + $this->parameters = []; |
|
49 | 49 | $this->add($inputs); |
50 | - } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Adds input values. |
|
52 | + /** |
|
53 | + * Adds input values. |
|
54 | 54 | * |
55 | 55 | * @param array $inputs |
56 | 56 | * |
57 | 57 | * @return mixed |
58 | - */ |
|
59 | - public function add(array $inputs = []) |
|
60 | - { |
|
58 | + */ |
|
59 | + public function add(array $inputs = []) |
|
60 | + { |
|
61 | 61 | foreach ($inputs as $key => $file) { |
62 | 62 | $this->set($key, $file); |
63 | 63 | } |
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
67 | - * Gets a string input value by name. |
|
68 | - * |
|
69 | - * @param string $key |
|
70 | - * @param string|null $default (null by default) |
|
71 | - * |
|
72 | - * @return string|null |
|
73 | - */ |
|
74 | - public function get($key, $default = null) |
|
75 | - { |
|
67 | + * Gets a string input value by name. |
|
68 | + * |
|
69 | + * @param string $key |
|
70 | + * @param string|null $default (null by default) |
|
71 | + * |
|
72 | + * @return string|null |
|
73 | + */ |
|
74 | + public function get($key, $default = null) |
|
75 | + { |
|
76 | 76 | if (null !== $default && ! is_scalar($default) && ! (is_object($default)) && ! method_exist($default, '__toString')) { |
77 | 77 | throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
78 | 78 | } |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
90 | - * Sets an input by name. |
|
91 | - * |
|
92 | - * @param string $key |
|
93 | - * @param string|array|null $value |
|
94 | - * |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - public function set($key, $value) |
|
98 | - { |
|
90 | + * Sets an input by name. |
|
91 | + * |
|
92 | + * @param string $key |
|
93 | + * @param string|array|null $value |
|
94 | + * |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + public function set($key, $value) |
|
98 | + { |
|
99 | 99 | if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exist($value, '__toString')) { |
100 | 100 | 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__)); |
101 | 101 | } |
102 | 102 | |
103 | - $this->parameters[$key] = $value; |
|
104 | - } |
|
103 | + $this->parameters[$key] = $value; |
|
104 | + } |
|
105 | 105 | } |
106 | 106 | \ No newline at end of file |