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