@@ -30,210 +30,210 @@ |
||
30 | 30 | */ |
31 | 31 | class ResponseHeaders extends Headers |
32 | 32 | { |
33 | - const COOKIE_FLAT = 'flat'; |
|
34 | - const COOKIE_ARRAY = 'array'; |
|
33 | + const COOKIE_FLAT = 'flat'; |
|
34 | + const COOKIE_ARRAY = 'array'; |
|
35 | 35 | |
36 | - /** |
|
37 | - * The list of cookies. |
|
38 | - * |
|
39 | - * @var array $cookies |
|
40 | - */ |
|
41 | - protected $cookies = []; |
|
36 | + /** |
|
37 | + * The list of cookies. |
|
38 | + * |
|
39 | + * @var array $cookies |
|
40 | + */ |
|
41 | + protected $cookies = []; |
|
42 | 42 | |
43 | 43 | /** |
44 | 44 | * The header names. |
45 | - * |
|
46 | - * @var array $headerNames |
|
45 | + * |
|
46 | + * @var array $headerNames |
|
47 | + */ |
|
48 | + protected $headerNames = []; |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructor. Create a new ResponseHeaders class instance. |
|
52 | + * |
|
53 | + * @param array $headers |
|
54 | + * |
|
55 | + * @return void |
|
47 | 56 | */ |
48 | - protected $headerNames = []; |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructor. Create a new ResponseHeaders class instance. |
|
52 | - * |
|
53 | - * @param array $headers |
|
54 | - * |
|
55 | - * @return void |
|
56 | - */ |
|
57 | - public function __construct(array $headers = []) |
|
58 | - { |
|
59 | - parent::__construct($headers); |
|
57 | + public function __construct(array $headers = []) |
|
58 | + { |
|
59 | + parent::__construct($headers); |
|
60 | 60 | |
61 | - if ( ! isset($this->headers['cache-control'])) { |
|
62 | - $this->set('Cache-Control', ''); |
|
63 | - } |
|
64 | - |
|
65 | - if ( ! isset($this->headers['date'])) { |
|
66 | - $this->initDate(); |
|
67 | - } |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns the headers, without cookies. |
|
72 | - * |
|
73 | - * @return mixed |
|
74 | - */ |
|
75 | - public function allPreserveCaseWithoutCookies() |
|
76 | - { |
|
77 | - $headers = $this->allPreserveCase(); |
|
61 | + if ( ! isset($this->headers['cache-control'])) { |
|
62 | + $this->set('Cache-Control', ''); |
|
63 | + } |
|
64 | + |
|
65 | + if ( ! isset($this->headers['date'])) { |
|
66 | + $this->initDate(); |
|
67 | + } |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns the headers, without cookies. |
|
72 | + * |
|
73 | + * @return mixed |
|
74 | + */ |
|
75 | + public function allPreserveCaseWithoutCookies() |
|
76 | + { |
|
77 | + $headers = $this->allPreserveCase(); |
|
78 | 78 | |
79 | - if (isset($this->headerNames['set-cookie'])) { |
|
80 | - unset($headers[$this->headerNames['set-cookie']]); |
|
81 | - } |
|
79 | + if (isset($this->headerNames['set-cookie'])) { |
|
80 | + unset($headers[$this->headerNames['set-cookie']]); |
|
81 | + } |
|
82 | 82 | |
83 | - return $headers; |
|
84 | - } |
|
83 | + return $headers; |
|
84 | + } |
|
85 | 85 | |
86 | 86 | /** |
87 | - * Returns the headers, with original capitalizations. |
|
88 | - * |
|
89 | - * @return array An array of headers |
|
90 | - */ |
|
91 | - public function allPreserveCase(): array |
|
92 | - { |
|
93 | - $headers = []; |
|
87 | + * Returns the headers, with original capitalizations. |
|
88 | + * |
|
89 | + * @return array An array of headers |
|
90 | + */ |
|
91 | + public function allPreserveCase(): array |
|
92 | + { |
|
93 | + $headers = []; |
|
94 | 94 | |
95 | - foreach ($this->all() as $name => $value) { |
|
96 | - $headers[$this->headerNames[$name] ?? $name] = $value; |
|
97 | - } |
|
95 | + foreach ($this->all() as $name => $value) { |
|
96 | + $headers[$this->headerNames[$name] ?? $name] = $value; |
|
97 | + } |
|
98 | 98 | |
99 | - return $headers; |
|
100 | - } |
|
99 | + return $headers; |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Returns all the headers. |
|
104 | - * |
|
105 | - * @param string|null $key The name of the headers |
|
106 | - * |
|
107 | - * @return array |
|
108 | - */ |
|
109 | - public function all(string $key = null): array |
|
110 | - { |
|
111 | - $headers = parent::all(); |
|
102 | + /** |
|
103 | + * Returns all the headers. |
|
104 | + * |
|
105 | + * @param string|null $key The name of the headers |
|
106 | + * |
|
107 | + * @return array |
|
108 | + */ |
|
109 | + public function all(string $key = null): array |
|
110 | + { |
|
111 | + $headers = parent::all(); |
|
112 | 112 | |
113 | - if (null !== $key) { |
|
114 | - $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
113 | + if (null !== $key) { |
|
114 | + $key = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
115 | 115 | |
116 | - return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies()); |
|
117 | - } |
|
116 | + return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies()); |
|
117 | + } |
|
118 | 118 | |
119 | - foreach ($this->getCookies() as $cookie) { |
|
120 | - $headers['set-cookie'][] = (string) $cookie; |
|
121 | - } |
|
119 | + foreach ($this->getCookies() as $cookie) { |
|
120 | + $headers['set-cookie'][] = (string) $cookie; |
|
121 | + } |
|
122 | 122 | |
123 | - return $headers; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Replaces the current HTTP headers by a new set. |
|
128 | - * |
|
129 | - * @param array $headers |
|
130 | - * |
|
131 | - * @return void |
|
132 | - */ |
|
133 | - public function replace(array $headers = []): void |
|
134 | - { |
|
135 | - $this->headerNames = []; |
|
136 | - |
|
137 | - parent::replace($headers); |
|
138 | - |
|
139 | - if ( ! isset($this->headers['cache-control'])) { |
|
140 | - $this->set('Cache-Control', ''); |
|
141 | - } |
|
123 | + return $headers; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Replaces the current HTTP headers by a new set. |
|
128 | + * |
|
129 | + * @param array $headers |
|
130 | + * |
|
131 | + * @return void |
|
132 | + */ |
|
133 | + public function replace(array $headers = []): void |
|
134 | + { |
|
135 | + $this->headerNames = []; |
|
136 | + |
|
137 | + parent::replace($headers); |
|
138 | + |
|
139 | + if ( ! isset($this->headers['cache-control'])) { |
|
140 | + $this->set('Cache-Control', ''); |
|
141 | + } |
|
142 | 142 | |
143 | - if ( ! isset($this->headers['date'])) { |
|
144 | - $this->initDate(); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * Sets a header by name. |
|
150 | - * |
|
151 | - * @param string $key The header name |
|
152 | - * @param string|string[]|null $values The value or an array of values |
|
153 | - * @param bool $replace If you want to replace the value exists by the header, |
|
154 | - * it is not overwritten / overwritten when it is false |
|
155 | - * |
|
156 | - * @return void |
|
157 | - */ |
|
158 | - public function set(string $key, $values, bool $replace = true): void |
|
159 | - { |
|
160 | - $unique = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
143 | + if ( ! isset($this->headers['date'])) { |
|
144 | + $this->initDate(); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * Sets a header by name. |
|
150 | + * |
|
151 | + * @param string $key The header name |
|
152 | + * @param string|string[]|null $values The value or an array of values |
|
153 | + * @param bool $replace If you want to replace the value exists by the header, |
|
154 | + * it is not overwritten / overwritten when it is false |
|
155 | + * |
|
156 | + * @return void |
|
157 | + */ |
|
158 | + public function set(string $key, $values, bool $replace = true): void |
|
159 | + { |
|
160 | + $unique = strtr($key, self::STRING_UPPER, self::STRING_LOWER); |
|
161 | 161 | |
162 | - if ('set-cookie' === $unique) { |
|
163 | - if ($replace) { |
|
164 | - $this->cookies = []; |
|
165 | - } |
|
162 | + if ('set-cookie' === $unique) { |
|
163 | + if ($replace) { |
|
164 | + $this->cookies = []; |
|
165 | + } |
|
166 | 166 | |
167 | - foreach ((array) $values as $cookie) { |
|
168 | - $this->setCookie($cookie); |
|
169 | - } |
|
167 | + foreach ((array) $values as $cookie) { |
|
168 | + $this->setCookie($cookie); |
|
169 | + } |
|
170 | 170 | |
171 | - $this->headerNames[$unique] = $key; |
|
171 | + $this->headerNames[$unique] = $key; |
|
172 | 172 | |
173 | - return; |
|
174 | - } |
|
173 | + return; |
|
174 | + } |
|
175 | 175 | |
176 | - $this->headerNames[$unique] = $key; |
|
176 | + $this->headerNames[$unique] = $key; |
|
177 | 177 | |
178 | - parent::set($key, $values, $replace); |
|
179 | - } |
|
178 | + parent::set($key, $values, $replace); |
|
179 | + } |
|
180 | 180 | |
181 | - /** |
|
182 | - * Gets an array with all cookies. |
|
183 | - * |
|
184 | - * @param string $format |
|
185 | - * |
|
186 | - * @return Cookie[] |
|
187 | - * |
|
188 | - * @throws \InvalidArgumentException |
|
189 | - */ |
|
190 | - public function getCookies(string $format = self::COOKIE_FLAT): array |
|
191 | - { |
|
192 | - if ( ! in_array($format, [self::COOKIE_FLAT, self::COOKIE_ARRAY])) { |
|
193 | - throw new InvalidArgumentException( |
|
194 | - sprintf('Format "%s" invalid (%s)', $format, implode(', ', [self::COOKIE_FLAT, self::COOKIE_ARRAY]) |
|
195 | - )); |
|
196 | - } |
|
181 | + /** |
|
182 | + * Gets an array with all cookies. |
|
183 | + * |
|
184 | + * @param string $format |
|
185 | + * |
|
186 | + * @return Cookie[] |
|
187 | + * |
|
188 | + * @throws \InvalidArgumentException |
|
189 | + */ |
|
190 | + public function getCookies(string $format = self::COOKIE_FLAT): array |
|
191 | + { |
|
192 | + if ( ! in_array($format, [self::COOKIE_FLAT, self::COOKIE_ARRAY])) { |
|
193 | + throw new InvalidArgumentException( |
|
194 | + sprintf('Format "%s" invalid (%s)', $format, implode(', ', [self::COOKIE_FLAT, self::COOKIE_ARRAY]) |
|
195 | + )); |
|
196 | + } |
|
197 | 197 | |
198 | - if (self::COOKIE_ARRAY === $format) { |
|
199 | - return $this->cookies; |
|
200 | - } |
|
198 | + if (self::COOKIE_ARRAY === $format) { |
|
199 | + return $this->cookies; |
|
200 | + } |
|
201 | 201 | |
202 | - $stringCookies = []; |
|
203 | - |
|
204 | - foreach ($this->cookies as $path) { |
|
205 | - foreach ($path as $cookies) { |
|
206 | - foreach ($cookies as $cookie) { |
|
207 | - $stringCookies[] = $cookie; |
|
208 | - } |
|
209 | - } |
|
210 | - } |
|
202 | + $stringCookies = []; |
|
203 | + |
|
204 | + foreach ($this->cookies as $path) { |
|
205 | + foreach ($path as $cookies) { |
|
206 | + foreach ($cookies as $cookie) { |
|
207 | + $stringCookies[] = $cookie; |
|
208 | + } |
|
209 | + } |
|
210 | + } |
|
211 | 211 | |
212 | - return $stringCookies; |
|
212 | + return $stringCookies; |
|
213 | 213 | } |
214 | 214 | |
215 | - /** |
|
216 | - * Sets the cookie. |
|
217 | - * |
|
218 | - * @param \Syscodes\Components\Http\Cookie $cookie |
|
219 | - * |
|
220 | - * @return static |
|
221 | - */ |
|
222 | - public function setCookie(Cookie $cookie): static |
|
223 | - { |
|
224 | - $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; |
|
225 | - $this->headerNames['set-cookie'] = 'Set-Cookie'; |
|
226 | - |
|
227 | - return $this; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Initialize the date. |
|
232 | - * |
|
233 | - * @return void |
|
234 | - */ |
|
235 | - private function initDate(): void |
|
236 | - { |
|
237 | - $this->set('Date', gmdate('D, d M Y H:i:s').' GMT'); |
|
238 | - } |
|
215 | + /** |
|
216 | + * Sets the cookie. |
|
217 | + * |
|
218 | + * @param \Syscodes\Components\Http\Cookie $cookie |
|
219 | + * |
|
220 | + * @return static |
|
221 | + */ |
|
222 | + public function setCookie(Cookie $cookie): static |
|
223 | + { |
|
224 | + $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie; |
|
225 | + $this->headerNames['set-cookie'] = 'Set-Cookie'; |
|
226 | + |
|
227 | + return $this; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Initialize the date. |
|
232 | + * |
|
233 | + * @return void |
|
234 | + */ |
|
235 | + private function initDate(): void |
|
236 | + { |
|
237 | + $this->set('Date', gmdate('D, d M Y H:i:s').' GMT'); |
|
238 | + } |
|
239 | 239 | } |
240 | 240 | \ No newline at end of file |
@@ -29,211 +29,211 @@ |
||
29 | 29 | */ |
30 | 30 | trait HttpResources |
31 | 31 | { |
32 | - /** |
|
33 | - * Filters a value from the start of a string in this case the passed URI string. |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - protected function parseRequestUri(): string |
|
38 | - { |
|
39 | - $requestUri = ''; |
|
40 | - |
|
41 | - if ('1' == $this->server->get('IIS_WasUrlRewritten') && '' != $this->server->get('UNENCODED_URL')) { |
|
42 | - // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem) |
|
43 | - $requestUri = $this->server->get('UNENCODED_URL'); |
|
44 | - $this->server->remove('UNENCODED_URL'); |
|
45 | - $this->server->remove('IIS_WasUrlRewritten'); |
|
46 | - } elseif ($this->server->has('REQUEST_URI')) { |
|
47 | - $requestUri = $this->server->get('REQUEST_URI'); |
|
32 | + /** |
|
33 | + * Filters a value from the start of a string in this case the passed URI string. |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + protected function parseRequestUri(): string |
|
38 | + { |
|
39 | + $requestUri = ''; |
|
40 | + |
|
41 | + if ('1' == $this->server->get('IIS_WasUrlRewritten') && '' != $this->server->get('UNENCODED_URL')) { |
|
42 | + // IIS7 with URL Rewrite: make sure we get the unencoded URL (double slash problem) |
|
43 | + $requestUri = $this->server->get('UNENCODED_URL'); |
|
44 | + $this->server->remove('UNENCODED_URL'); |
|
45 | + $this->server->remove('IIS_WasUrlRewritten'); |
|
46 | + } elseif ($this->server->has('REQUEST_URI')) { |
|
47 | + $requestUri = $this->server->get('REQUEST_URI'); |
|
48 | 48 | |
49 | - if ('' !== $requestUri && '/' === $requestUri[0]) { |
|
50 | - // To only use path and query remove the fragment. |
|
51 | - if (false !== $pos = strpos($requestUri, '#')) { |
|
52 | - $requestUri = substr($requestUri, 0, $pos); |
|
53 | - } |
|
54 | - } else { |
|
55 | - // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, |
|
56 | - // only use URL path. |
|
57 | - $uriComponents = parse_url($requestUri); |
|
49 | + if ('' !== $requestUri && '/' === $requestUri[0]) { |
|
50 | + // To only use path and query remove the fragment. |
|
51 | + if (false !== $pos = strpos($requestUri, '#')) { |
|
52 | + $requestUri = substr($requestUri, 0, $pos); |
|
53 | + } |
|
54 | + } else { |
|
55 | + // HTTP proxy reqs setup request URI with scheme and host [and port] + the URL path, |
|
56 | + // only use URL path. |
|
57 | + $uriComponents = parse_url($requestUri); |
|
58 | 58 | |
59 | - if (isset($uriComponents['path'])) { |
|
60 | - $requestUri = $uriComponents['path']; |
|
61 | - } |
|
59 | + if (isset($uriComponents['path'])) { |
|
60 | + $requestUri = $uriComponents['path']; |
|
61 | + } |
|
62 | 62 | |
63 | - if (isset($uriComponents['query'])) { |
|
64 | - $requestUri .= '?'.$uriComponents['query']; |
|
65 | - } |
|
66 | - } |
|
67 | - } elseif ($this->server->has('ORIG_PATH_INFO')) { |
|
68 | - // IIS 5.0, PHP as CGI |
|
69 | - $requestUri = $this->server->get('ORIG_PATH_INFO'); |
|
63 | + if (isset($uriComponents['query'])) { |
|
64 | + $requestUri .= '?'.$uriComponents['query']; |
|
65 | + } |
|
66 | + } |
|
67 | + } elseif ($this->server->has('ORIG_PATH_INFO')) { |
|
68 | + // IIS 5.0, PHP as CGI |
|
69 | + $requestUri = $this->server->get('ORIG_PATH_INFO'); |
|
70 | 70 | |
71 | - if ('' != $this->server->get('QUERY_STRING')) { |
|
72 | - $requestUri .= '?'.$this->server->get('QUERY_STRING'); |
|
73 | - } |
|
71 | + if ('' != $this->server->get('QUERY_STRING')) { |
|
72 | + $requestUri .= '?'.$this->server->get('QUERY_STRING'); |
|
73 | + } |
|
74 | 74 | |
75 | - $this->server->remove('ORIG_PATH_INFO'); |
|
76 | - } |
|
75 | + $this->server->remove('ORIG_PATH_INFO'); |
|
76 | + } |
|
77 | 77 | |
78 | - // normalize the request URI to ease creating sub-requests from this request |
|
79 | - $this->server->set('REQUEST_URI', $requestUri); |
|
78 | + // normalize the request URI to ease creating sub-requests from this request |
|
79 | + $this->server->set('REQUEST_URI', $requestUri); |
|
80 | 80 | |
81 | - return $this->filterDecode($requestUri); |
|
82 | - } |
|
81 | + return $this->filterDecode($requestUri); |
|
82 | + } |
|
83 | 83 | |
84 | - /** |
|
85 | - * Will parse QUERY_STRING and automatically detect the URI from it. |
|
86 | - * |
|
87 | - * @return string |
|
88 | - */ |
|
89 | - protected function parseQueryString(): string |
|
90 | - { |
|
91 | - $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
84 | + /** |
|
85 | + * Will parse QUERY_STRING and automatically detect the URI from it. |
|
86 | + * |
|
87 | + * @return string |
|
88 | + */ |
|
89 | + protected function parseQueryString(): string |
|
90 | + { |
|
91 | + $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
92 | 92 | |
93 | - if (trim($uri, '/') === '') { |
|
94 | - return ''; |
|
95 | - } elseif (0 === strncmp($uri, '/', 1)) { |
|
96 | - $uri = explode('?', $uri, 2); |
|
97 | - $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
98 | - $uri = $uri[0] ?? ''; |
|
99 | - } |
|
93 | + if (trim($uri, '/') === '') { |
|
94 | + return ''; |
|
95 | + } elseif (0 === strncmp($uri, '/', 1)) { |
|
96 | + $uri = explode('?', $uri, 2); |
|
97 | + $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
98 | + $uri = $uri[0] ?? ''; |
|
99 | + } |
|
100 | 100 | |
101 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
101 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
102 | 102 | |
103 | - return $this->filterDecode($uri); |
|
104 | - } |
|
103 | + return $this->filterDecode($uri); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Parse the base URL. |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public function parseBaseUrl(): string |
|
112 | - { |
|
113 | - $filename = basename($this->server('SCRIPT_FILENAME')); |
|
114 | - |
|
115 | - if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
116 | - $baseUrl = $this->server('SCRIPT_NAME'); |
|
117 | - } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
118 | - $baseUrl = $this->server('PHP_SELF'); |
|
119 | - } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
120 | - $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
121 | - } else { |
|
122 | - $path = $this->server('PHP_SELF', ''); |
|
123 | - $file = $this->server('SCRIPT_NAME', ''); |
|
124 | - $segs = explode('/', trim($file, '/')); |
|
125 | - $segs = array_reverse($segs); |
|
126 | - $index = 0; |
|
127 | - $last = count($segs); |
|
128 | - $baseUrl = ''; |
|
106 | + /** |
|
107 | + * Parse the base URL. |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public function parseBaseUrl(): string |
|
112 | + { |
|
113 | + $filename = basename($this->server('SCRIPT_FILENAME')); |
|
114 | + |
|
115 | + if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
116 | + $baseUrl = $this->server('SCRIPT_NAME'); |
|
117 | + } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
118 | + $baseUrl = $this->server('PHP_SELF'); |
|
119 | + } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
120 | + $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
121 | + } else { |
|
122 | + $path = $this->server('PHP_SELF', ''); |
|
123 | + $file = $this->server('SCRIPT_NAME', ''); |
|
124 | + $segs = explode('/', trim($file, '/')); |
|
125 | + $segs = array_reverse($segs); |
|
126 | + $index = 0; |
|
127 | + $last = count($segs); |
|
128 | + $baseUrl = ''; |
|
129 | 129 | |
130 | - do { |
|
131 | - $seg = $segs[$index]; |
|
132 | - $baseUrl = '/'.$seg.$baseUrl; |
|
133 | - ++$index; |
|
134 | - } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
135 | - } |
|
136 | - |
|
137 | - // Does the baseUrl have anything in common with the request_uri? |
|
138 | - $requestUri = $this->getRequestUri(); |
|
139 | - |
|
140 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
141 | - $requestUri = '/'.$requestUri; |
|
142 | - } |
|
143 | - |
|
144 | - if ($baseUrl && null !== $uri = $this->getUrlencoded($requestUri, $baseUrl)) { |
|
145 | - // Full $baseUrl matches |
|
146 | - return $this->filterDecode($uri); |
|
147 | - } |
|
148 | - |
|
149 | - if ($baseUrl && null !== $uri = $this->getUrlencoded($requestUri, rtrim(dirname($baseUrl), '/'.DIRECTORY_SEPARATOR))) { |
|
150 | - // Directory portion of $baseUrl matches |
|
151 | - return $this->filterDecode($uri); |
|
152 | - } |
|
130 | + do { |
|
131 | + $seg = $segs[$index]; |
|
132 | + $baseUrl = '/'.$seg.$baseUrl; |
|
133 | + ++$index; |
|
134 | + } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
135 | + } |
|
136 | + |
|
137 | + // Does the baseUrl have anything in common with the request_uri? |
|
138 | + $requestUri = $this->getRequestUri(); |
|
139 | + |
|
140 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
141 | + $requestUri = '/'.$requestUri; |
|
142 | + } |
|
143 | + |
|
144 | + if ($baseUrl && null !== $uri = $this->getUrlencoded($requestUri, $baseUrl)) { |
|
145 | + // Full $baseUrl matches |
|
146 | + return $this->filterDecode($uri); |
|
147 | + } |
|
148 | + |
|
149 | + if ($baseUrl && null !== $uri = $this->getUrlencoded($requestUri, rtrim(dirname($baseUrl), '/'.DIRECTORY_SEPARATOR))) { |
|
150 | + // Directory portion of $baseUrl matches |
|
151 | + return $this->filterDecode($uri); |
|
152 | + } |
|
153 | 153 | |
154 | - $baseUrl = dirname($baseUrl ?? ''); |
|
154 | + $baseUrl = dirname($baseUrl ?? ''); |
|
155 | 155 | |
156 | - // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
157 | - // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
158 | - // from PATH_INFO or QUERY_STRING |
|
159 | - if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
160 | - $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
161 | - } |
|
156 | + // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
157 | + // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
158 | + // from PATH_INFO or QUERY_STRING |
|
159 | + if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
160 | + $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
161 | + } |
|
162 | 162 | |
163 | - return $this->filterDecode($baseUrl); |
|
164 | - } |
|
163 | + return $this->filterDecode($baseUrl); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * Returns the prefix as encoded in the string when the string starts with |
|
168 | - * the given prefix, null otherwise. |
|
169 | - * |
|
170 | - * return string|null |
|
171 | - */ |
|
172 | - private function getUrlencoded(string $string, string $prefix): ?string |
|
173 | - { |
|
174 | - if ( ! Str::startsWith(rawurldecode($string), $prefix)) { |
|
175 | - return null; |
|
176 | - } |
|
177 | - |
|
178 | - $length = strlen($prefix); |
|
179 | - |
|
180 | - if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $length), $string, $match)) { |
|
181 | - return $match[0]; |
|
182 | - } |
|
183 | - |
|
184 | - return null; |
|
185 | - } |
|
166 | + /** |
|
167 | + * Returns the prefix as encoded in the string when the string starts with |
|
168 | + * the given prefix, null otherwise. |
|
169 | + * |
|
170 | + * return string|null |
|
171 | + */ |
|
172 | + private function getUrlencoded(string $string, string $prefix): ?string |
|
173 | + { |
|
174 | + if ( ! Str::startsWith(rawurldecode($string), $prefix)) { |
|
175 | + return null; |
|
176 | + } |
|
177 | + |
|
178 | + $length = strlen($prefix); |
|
179 | + |
|
180 | + if (preg_match(sprintf('#^(%%[[:xdigit:]]{2}|.){%d}#', $length), $string, $match)) { |
|
181 | + return $match[0]; |
|
182 | + } |
|
183 | + |
|
184 | + return null; |
|
185 | + } |
|
186 | 186 | |
187 | 187 | |
188 | - /** |
|
189 | - * Parse the path info. |
|
190 | - * |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - public function parsePathInfo(): string |
|
194 | - { |
|
195 | - if (null === ($requestUri = $this->getRequestUri())) { |
|
196 | - return '/'; |
|
197 | - } |
|
198 | - |
|
199 | - // Remove the query string from REQUEST_URI |
|
200 | - if (false !== $pos = strpos($requestUri, '?')) { |
|
201 | - $requestUri = substr($requestUri, 0, $pos); |
|
202 | - } |
|
203 | - |
|
204 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
205 | - $requestUri = '/'.$requestUri; |
|
206 | - } |
|
207 | - |
|
208 | - if (null === ($baseUrl = $this->getBaseUrl())) { |
|
209 | - return $requestUri; |
|
210 | - } |
|
211 | - |
|
212 | - $pathInfo = substr($requestUri, \strlen($baseUrl)); |
|
213 | - |
|
214 | - if (false === $pathInfo || '' === $pathInfo) { |
|
215 | - // If substr() returns false then PATH_INFO is set to an empty string |
|
216 | - return '/'; |
|
217 | - } |
|
218 | - |
|
219 | - return $this->filterDecode($pathInfo); |
|
220 | - } |
|
188 | + /** |
|
189 | + * Parse the path info. |
|
190 | + * |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + public function parsePathInfo(): string |
|
194 | + { |
|
195 | + if (null === ($requestUri = $this->getRequestUri())) { |
|
196 | + return '/'; |
|
197 | + } |
|
198 | + |
|
199 | + // Remove the query string from REQUEST_URI |
|
200 | + if (false !== $pos = strpos($requestUri, '?')) { |
|
201 | + $requestUri = substr($requestUri, 0, $pos); |
|
202 | + } |
|
203 | + |
|
204 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
205 | + $requestUri = '/'.$requestUri; |
|
206 | + } |
|
207 | + |
|
208 | + if (null === ($baseUrl = $this->getBaseUrl())) { |
|
209 | + return $requestUri; |
|
210 | + } |
|
211 | + |
|
212 | + $pathInfo = substr($requestUri, \strlen($baseUrl)); |
|
213 | + |
|
214 | + if (false === $pathInfo || '' === $pathInfo) { |
|
215 | + // If substr() returns false then PATH_INFO is set to an empty string |
|
216 | + return '/'; |
|
217 | + } |
|
218 | + |
|
219 | + return $this->filterDecode($pathInfo); |
|
220 | + } |
|
221 | 221 | |
222 | - /** |
|
223 | - * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
224 | - * |
|
225 | - * @param string $uri |
|
226 | - * |
|
227 | - * @return string |
|
228 | - */ |
|
229 | - protected function filterDecode($uri): string |
|
230 | - { |
|
231 | - // Remove all characters except letters, |
|
232 | - // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
233 | - $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
234 | - $uri = mb_strtolower(trim($uri), 'UTF-8'); |
|
235 | - |
|
236 | - // Return argument if not empty or return a single slash |
|
237 | - return trim($uri); |
|
238 | - } |
|
222 | + /** |
|
223 | + * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
224 | + * |
|
225 | + * @param string $uri |
|
226 | + * |
|
227 | + * @return string |
|
228 | + */ |
|
229 | + protected function filterDecode($uri): string |
|
230 | + { |
|
231 | + // Remove all characters except letters, |
|
232 | + // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
233 | + $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
234 | + $uri = mb_strtolower(trim($uri), 'UTF-8'); |
|
235 | + |
|
236 | + // Return argument if not empty or return a single slash |
|
237 | + return trim($uri); |
|
238 | + } |
|
239 | 239 | } |
240 | 240 | \ No newline at end of file |
@@ -127,7 +127,7 @@ |
||
127 | 127 | $last = count($segs); |
128 | 128 | $baseUrl = ''; |
129 | 129 | |
130 | - do { |
|
130 | + do { |
|
131 | 131 | $seg = $segs[$index]; |
132 | 132 | $baseUrl = '/'.$seg.$baseUrl; |
133 | 133 | ++$index; |
@@ -49,7 +49,7 @@ |
||
49 | 49 | * |
50 | 50 | * @var \Syscodes\Components\Http\ResponseHeaders $headers |
51 | 51 | */ |
52 | - public $headers; |
|
52 | + public $headers; |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * The HTTP protocol version. |
@@ -30,209 +30,209 @@ |
||
30 | 30 | */ |
31 | 31 | trait HttpStatusCode |
32 | 32 | { |
33 | - /** |
|
34 | - * The HTTP status code. |
|
35 | - * |
|
36 | - * @var int $statusCode |
|
37 | - */ |
|
38 | - protected $statusCode = 200; |
|
39 | - |
|
40 | - /** |
|
41 | - * An array of status codes and messages. |
|
42 | - * |
|
43 | - * @var array $statusCodeTexts |
|
44 | - */ |
|
45 | - public static $statusCodeTexts = [ |
|
46 | - // 1xx: Informational |
|
47 | - 100 => 'Continue', |
|
48 | - 101 => 'Switching Protocols', |
|
49 | - 102 => 'Processing', |
|
50 | - |
|
51 | - // 2xx: Success |
|
52 | - 200 => 'OK', |
|
53 | - 201 => 'Created', |
|
54 | - 202 => 'Accepted', |
|
55 | - 203 => 'Non-Authoritative Information', |
|
56 | - 204 => 'No Content', |
|
57 | - 205 => 'Reset Content', |
|
58 | - 206 => 'Partial Content', |
|
59 | - 207 => 'Multi-Status', |
|
60 | - 208 => 'Already Reported', |
|
61 | - 226 => 'IM Used', |
|
62 | - |
|
63 | - // 3xx: Redirection |
|
64 | - 300 => 'Multiple Choices', |
|
65 | - 301 => 'Moved Permanently', |
|
66 | - 302 => 'Found', |
|
67 | - 303 => 'See Other', |
|
68 | - 304 => 'Not Modified', |
|
69 | - 305 => 'Use Proxy', |
|
70 | - 307 => 'Temporary Redirect', |
|
71 | - 308 => 'Permanent Redirect', |
|
72 | - |
|
73 | - // 4xx: Client error |
|
74 | - 400 => 'Bad Request', |
|
75 | - 401 => 'Unauthorized', |
|
76 | - 402 => 'Payment Required', |
|
77 | - 403 => 'Forbidden', |
|
78 | - 404 => 'Not Found', |
|
79 | - 405 => 'Method Not Allowed', |
|
80 | - 406 => 'Not Acceptable', |
|
81 | - 407 => 'Proxy Authentication Required', |
|
82 | - 408 => 'Request Timeout', |
|
83 | - 409 => 'Conflict', |
|
84 | - 410 => 'Gone', |
|
85 | - 411 => 'Length Required', |
|
86 | - 412 => 'Precondition Failed', |
|
87 | - 413 => 'Request Entity Too Large', |
|
88 | - 414 => 'Request-URI Too Long', |
|
89 | - 415 => 'Unsupported Media Type', |
|
90 | - 416 => 'Requested Range Not Satisfiable', |
|
91 | - 417 => 'Expectation Failed', |
|
92 | - 418 => 'I\'m a Teapot', |
|
93 | - // 419 (Authentication Timeout) is a non-standard status code with unknown origin |
|
94 | - 421 => 'Misdirected Request', |
|
95 | - 422 => 'Unprocessable Entity', |
|
96 | - 423 => 'Locked', |
|
97 | - 424 => 'Failed Dependency', |
|
98 | - 426 => 'Upgrade Required', |
|
99 | - 428 => 'Precondition Required', |
|
100 | - 429 => 'Too Many Requests', |
|
101 | - 431 => 'Request Header Fields Too Large', |
|
102 | - 451 => 'Unavailable For Legal Reasons', |
|
103 | - |
|
104 | - // 5xx: Server error |
|
105 | - 500 => 'Internal Server Error', |
|
106 | - 501 => 'Not Implemented', |
|
107 | - 502 => 'Bad Gateway', |
|
108 | - 503 => 'Service Unavailable', |
|
109 | - 504 => 'Gateway Timeout', |
|
110 | - 505 => 'HTTP Version Not Supported', |
|
111 | - 506 => 'Variant Also Negotiates', |
|
112 | - 507 => 'Insufficient Storage', |
|
113 | - 508 => 'Loop Detected', |
|
114 | - 509 => 'Bandwidth Limit Exceeded', |
|
115 | - 510 => 'Not Extended', |
|
116 | - 511 => 'Network Authentication Required' |
|
117 | - ]; |
|
118 | - |
|
119 | - /** |
|
120 | - * Gets string of status code. |
|
121 | - * |
|
122 | - * @var string $statusText |
|
123 | - */ |
|
124 | - protected $statusText; |
|
125 | - |
|
126 | - /** |
|
127 | - * Gets the response status code. |
|
128 | - * |
|
129 | - * The status code is a 3-digit code to specify server response results to the browser. |
|
130 | - * |
|
131 | - * @return int |
|
132 | - * |
|
133 | - * @throws \BadMethodCallException |
|
134 | - */ |
|
135 | - public function getStatusCode(): int |
|
136 | - { |
|
137 | - if (empty($this->statusCode)) { |
|
138 | - throw new BadMethodCallException('HTTP Response is missing a status code'); |
|
139 | - } |
|
140 | - |
|
141 | - return $this->statusCode; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Sets the response status code. |
|
146 | - * |
|
147 | - * @param int $code The status code |
|
148 | - * @param string|null $text The status text |
|
149 | - * |
|
150 | - * @return static |
|
151 | - * |
|
152 | - * @throws \InvalidArgumentException |
|
153 | - */ |
|
154 | - public function setStatusCode(int $code, $text = null): static |
|
155 | - { |
|
156 | - $this->statusCode = $code; |
|
157 | - |
|
158 | - // Valid range? |
|
159 | - if ($this->isInvalid()) { |
|
160 | - throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
161 | - } |
|
162 | - |
|
163 | - // Check if you have an accepted status code if not shows to a message of unknown status |
|
164 | - if (null === $text) { |
|
165 | - $this->statusText = self::$statusCodeTexts[$code] ?? __('response.UnknownStatus'); |
|
166 | - |
|
167 | - return $this; |
|
168 | - } |
|
169 | - |
|
170 | - if (false === $text) { |
|
171 | - $this->statusText = ''; |
|
172 | - |
|
173 | - return $this; |
|
174 | - } |
|
175 | - |
|
176 | - $this->statusText = $text; |
|
177 | - |
|
178 | - return $this; |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Is response invalid? |
|
183 | - * |
|
184 | - * @final |
|
185 | - * |
|
186 | - * @return bool |
|
187 | - */ |
|
188 | - public function isInvalid(): bool |
|
189 | - { |
|
190 | - return $this->statusCode < 100 || $this->statusCode >= 600; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Is response informative? |
|
195 | - * |
|
196 | - * @final |
|
197 | - * |
|
198 | - * @return bool |
|
199 | - */ |
|
200 | - public function isInformational(): bool |
|
201 | - { |
|
202 | - return $this->statusCode >= 100 && $this->statusCode < 200; |
|
203 | - } |
|
33 | + /** |
|
34 | + * The HTTP status code. |
|
35 | + * |
|
36 | + * @var int $statusCode |
|
37 | + */ |
|
38 | + protected $statusCode = 200; |
|
39 | + |
|
40 | + /** |
|
41 | + * An array of status codes and messages. |
|
42 | + * |
|
43 | + * @var array $statusCodeTexts |
|
44 | + */ |
|
45 | + public static $statusCodeTexts = [ |
|
46 | + // 1xx: Informational |
|
47 | + 100 => 'Continue', |
|
48 | + 101 => 'Switching Protocols', |
|
49 | + 102 => 'Processing', |
|
50 | + |
|
51 | + // 2xx: Success |
|
52 | + 200 => 'OK', |
|
53 | + 201 => 'Created', |
|
54 | + 202 => 'Accepted', |
|
55 | + 203 => 'Non-Authoritative Information', |
|
56 | + 204 => 'No Content', |
|
57 | + 205 => 'Reset Content', |
|
58 | + 206 => 'Partial Content', |
|
59 | + 207 => 'Multi-Status', |
|
60 | + 208 => 'Already Reported', |
|
61 | + 226 => 'IM Used', |
|
62 | + |
|
63 | + // 3xx: Redirection |
|
64 | + 300 => 'Multiple Choices', |
|
65 | + 301 => 'Moved Permanently', |
|
66 | + 302 => 'Found', |
|
67 | + 303 => 'See Other', |
|
68 | + 304 => 'Not Modified', |
|
69 | + 305 => 'Use Proxy', |
|
70 | + 307 => 'Temporary Redirect', |
|
71 | + 308 => 'Permanent Redirect', |
|
72 | + |
|
73 | + // 4xx: Client error |
|
74 | + 400 => 'Bad Request', |
|
75 | + 401 => 'Unauthorized', |
|
76 | + 402 => 'Payment Required', |
|
77 | + 403 => 'Forbidden', |
|
78 | + 404 => 'Not Found', |
|
79 | + 405 => 'Method Not Allowed', |
|
80 | + 406 => 'Not Acceptable', |
|
81 | + 407 => 'Proxy Authentication Required', |
|
82 | + 408 => 'Request Timeout', |
|
83 | + 409 => 'Conflict', |
|
84 | + 410 => 'Gone', |
|
85 | + 411 => 'Length Required', |
|
86 | + 412 => 'Precondition Failed', |
|
87 | + 413 => 'Request Entity Too Large', |
|
88 | + 414 => 'Request-URI Too Long', |
|
89 | + 415 => 'Unsupported Media Type', |
|
90 | + 416 => 'Requested Range Not Satisfiable', |
|
91 | + 417 => 'Expectation Failed', |
|
92 | + 418 => 'I\'m a Teapot', |
|
93 | + // 419 (Authentication Timeout) is a non-standard status code with unknown origin |
|
94 | + 421 => 'Misdirected Request', |
|
95 | + 422 => 'Unprocessable Entity', |
|
96 | + 423 => 'Locked', |
|
97 | + 424 => 'Failed Dependency', |
|
98 | + 426 => 'Upgrade Required', |
|
99 | + 428 => 'Precondition Required', |
|
100 | + 429 => 'Too Many Requests', |
|
101 | + 431 => 'Request Header Fields Too Large', |
|
102 | + 451 => 'Unavailable For Legal Reasons', |
|
103 | + |
|
104 | + // 5xx: Server error |
|
105 | + 500 => 'Internal Server Error', |
|
106 | + 501 => 'Not Implemented', |
|
107 | + 502 => 'Bad Gateway', |
|
108 | + 503 => 'Service Unavailable', |
|
109 | + 504 => 'Gateway Timeout', |
|
110 | + 505 => 'HTTP Version Not Supported', |
|
111 | + 506 => 'Variant Also Negotiates', |
|
112 | + 507 => 'Insufficient Storage', |
|
113 | + 508 => 'Loop Detected', |
|
114 | + 509 => 'Bandwidth Limit Exceeded', |
|
115 | + 510 => 'Not Extended', |
|
116 | + 511 => 'Network Authentication Required' |
|
117 | + ]; |
|
118 | + |
|
119 | + /** |
|
120 | + * Gets string of status code. |
|
121 | + * |
|
122 | + * @var string $statusText |
|
123 | + */ |
|
124 | + protected $statusText; |
|
125 | + |
|
126 | + /** |
|
127 | + * Gets the response status code. |
|
128 | + * |
|
129 | + * The status code is a 3-digit code to specify server response results to the browser. |
|
130 | + * |
|
131 | + * @return int |
|
132 | + * |
|
133 | + * @throws \BadMethodCallException |
|
134 | + */ |
|
135 | + public function getStatusCode(): int |
|
136 | + { |
|
137 | + if (empty($this->statusCode)) { |
|
138 | + throw new BadMethodCallException('HTTP Response is missing a status code'); |
|
139 | + } |
|
140 | + |
|
141 | + return $this->statusCode; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Sets the response status code. |
|
146 | + * |
|
147 | + * @param int $code The status code |
|
148 | + * @param string|null $text The status text |
|
149 | + * |
|
150 | + * @return static |
|
151 | + * |
|
152 | + * @throws \InvalidArgumentException |
|
153 | + */ |
|
154 | + public function setStatusCode(int $code, $text = null): static |
|
155 | + { |
|
156 | + $this->statusCode = $code; |
|
157 | + |
|
158 | + // Valid range? |
|
159 | + if ($this->isInvalid()) { |
|
160 | + throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
161 | + } |
|
162 | + |
|
163 | + // Check if you have an accepted status code if not shows to a message of unknown status |
|
164 | + if (null === $text) { |
|
165 | + $this->statusText = self::$statusCodeTexts[$code] ?? __('response.UnknownStatus'); |
|
166 | + |
|
167 | + return $this; |
|
168 | + } |
|
169 | + |
|
170 | + if (false === $text) { |
|
171 | + $this->statusText = ''; |
|
172 | + |
|
173 | + return $this; |
|
174 | + } |
|
175 | + |
|
176 | + $this->statusText = $text; |
|
177 | + |
|
178 | + return $this; |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Is response invalid? |
|
183 | + * |
|
184 | + * @final |
|
185 | + * |
|
186 | + * @return bool |
|
187 | + */ |
|
188 | + public function isInvalid(): bool |
|
189 | + { |
|
190 | + return $this->statusCode < 100 || $this->statusCode >= 600; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Is response informative? |
|
195 | + * |
|
196 | + * @final |
|
197 | + * |
|
198 | + * @return bool |
|
199 | + */ |
|
200 | + public function isInformational(): bool |
|
201 | + { |
|
202 | + return $this->statusCode >= 100 && $this->statusCode < 200; |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * Is the response a redirect? |
|
207 | - * |
|
208 | - * @final |
|
209 | - * |
|
210 | - * @return void |
|
211 | - */ |
|
212 | - public function isRedirection(): bool |
|
213 | - { |
|
214 | - return $this->statusCode >= 300 && $this->statusCode < 400; |
|
215 | - } |
|
205 | + /** |
|
206 | + * Is the response a redirect? |
|
207 | + * |
|
208 | + * @final |
|
209 | + * |
|
210 | + * @return void |
|
211 | + */ |
|
212 | + public function isRedirection(): bool |
|
213 | + { |
|
214 | + return $this->statusCode >= 300 && $this->statusCode < 400; |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Is the response empty? |
|
219 | - * |
|
220 | - * @final |
|
221 | - * |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - public function isEmpty(): bool |
|
225 | - { |
|
226 | - return in_array($this->statusCode, [204, 304]); |
|
227 | - } |
|
217 | + /** |
|
218 | + * Is the response empty? |
|
219 | + * |
|
220 | + * @final |
|
221 | + * |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + public function isEmpty(): bool |
|
225 | + { |
|
226 | + return in_array($this->statusCode, [204, 304]); |
|
227 | + } |
|
228 | 228 | |
229 | - /** |
|
230 | - * Is the response a redirect of some form? |
|
231 | - * |
|
232 | - * @return bool |
|
233 | - */ |
|
234 | - public function isRedirect(): bool |
|
235 | - { |
|
236 | - return in_array($this->statusCode, [301, 302, 303, 307, 308]); |
|
237 | - } |
|
229 | + /** |
|
230 | + * Is the response a redirect of some form? |
|
231 | + * |
|
232 | + * @return bool |
|
233 | + */ |
|
234 | + public function isRedirect(): bool |
|
235 | + { |
|
236 | + return in_array($this->statusCode, [301, 302, 303, 307, 308]); |
|
237 | + } |
|
238 | 238 | } |
239 | 239 | \ No newline at end of file |
@@ -127,13 +127,13 @@ |
||
127 | 127 | } |
128 | 128 | |
129 | 129 | /** |
130 | - * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
131 | - * Can redirect via a Location header. |
|
132 | - * |
|
133 | - * @param string $url The url |
|
134 | - * |
|
135 | - * @return static |
|
136 | - */ |
|
130 | + * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
131 | + * Can redirect via a Location header. |
|
132 | + * |
|
133 | + * @param string $url The url |
|
134 | + * |
|
135 | + * @return static |
|
136 | + */ |
|
137 | 137 | public function setTargetUrl($url): static |
138 | 138 | { |
139 | 139 | if ('' === ($url ?? '')) { |
@@ -38,160 +38,160 @@ discard block |
||
38 | 38 | */ |
39 | 39 | trait HttpRequest |
40 | 40 | { |
41 | - /** |
|
42 | - * Get the http method parameter. |
|
43 | - * |
|
44 | - * @var bool $httpMethodParameterOverride |
|
45 | - */ |
|
46 | - protected static $httpMethodParameterOverride = false; |
|
47 | - |
|
48 | - /** |
|
49 | - * Holds the global active request instance. |
|
50 | - * |
|
51 | - * @var bool $requestURI |
|
52 | - */ |
|
53 | - protected static $requestURI; |
|
54 | - |
|
55 | - /** |
|
56 | - * Get the custom parameters. |
|
57 | - * |
|
58 | - * @var \Syscodes\Components\Http\Loaders\Parameters $attributes |
|
59 | - */ |
|
60 | - public $attributes; |
|
61 | - |
|
62 | - /** |
|
63 | - * The base URL. |
|
64 | - * |
|
65 | - * @var string $baseUrl |
|
66 | - */ |
|
67 | - protected $baseUrl; |
|
68 | - |
|
69 | - /** |
|
70 | - * Get the client ip. |
|
71 | - * |
|
72 | - * @var mixed $clientIp |
|
73 | - */ |
|
74 | - protected $clientIp; |
|
75 | - |
|
76 | - /** |
|
77 | - * Gets cookies ($_COOKIE). |
|
78 | - * |
|
79 | - * @var \Syscodes\Components\Http\Loaders\Inputs $cookies |
|
80 | - */ |
|
81 | - public $cookies; |
|
82 | - |
|
83 | - /** |
|
84 | - * Gets the string with format JSON. |
|
85 | - * |
|
86 | - * @var string|resource|object|null $content |
|
87 | - */ |
|
88 | - protected $content; |
|
89 | - |
|
90 | - /** |
|
91 | - * The default Locale this request. |
|
92 | - * |
|
93 | - * @var string $defaultLocale |
|
94 | - */ |
|
95 | - protected $defaultLocale = 'en'; |
|
41 | + /** |
|
42 | + * Get the http method parameter. |
|
43 | + * |
|
44 | + * @var bool $httpMethodParameterOverride |
|
45 | + */ |
|
46 | + protected static $httpMethodParameterOverride = false; |
|
47 | + |
|
48 | + /** |
|
49 | + * Holds the global active request instance. |
|
50 | + * |
|
51 | + * @var bool $requestURI |
|
52 | + */ |
|
53 | + protected static $requestURI; |
|
54 | + |
|
55 | + /** |
|
56 | + * Get the custom parameters. |
|
57 | + * |
|
58 | + * @var \Syscodes\Components\Http\Loaders\Parameters $attributes |
|
59 | + */ |
|
60 | + public $attributes; |
|
61 | + |
|
62 | + /** |
|
63 | + * The base URL. |
|
64 | + * |
|
65 | + * @var string $baseUrl |
|
66 | + */ |
|
67 | + protected $baseUrl; |
|
68 | + |
|
69 | + /** |
|
70 | + * Get the client ip. |
|
71 | + * |
|
72 | + * @var mixed $clientIp |
|
73 | + */ |
|
74 | + protected $clientIp; |
|
75 | + |
|
76 | + /** |
|
77 | + * Gets cookies ($_COOKIE). |
|
78 | + * |
|
79 | + * @var \Syscodes\Components\Http\Loaders\Inputs $cookies |
|
80 | + */ |
|
81 | + public $cookies; |
|
82 | + |
|
83 | + /** |
|
84 | + * Gets the string with format JSON. |
|
85 | + * |
|
86 | + * @var string|resource|object|null $content |
|
87 | + */ |
|
88 | + protected $content; |
|
89 | + |
|
90 | + /** |
|
91 | + * The default Locale this request. |
|
92 | + * |
|
93 | + * @var string $defaultLocale |
|
94 | + */ |
|
95 | + protected $defaultLocale = 'en'; |
|
96 | 96 | |
97 | - /** |
|
98 | - * Gets files request ($_FILES). |
|
99 | - * |
|
100 | - * @var \Syscodes\Components\Http\Loaders\Files $files |
|
101 | - */ |
|
102 | - public $files; |
|
97 | + /** |
|
98 | + * Gets files request ($_FILES). |
|
99 | + * |
|
100 | + * @var \Syscodes\Components\Http\Loaders\Files $files |
|
101 | + */ |
|
102 | + public $files; |
|
103 | 103 | |
104 | - /** |
|
105 | - * Get the headers request ($_SERVER). |
|
106 | - * |
|
107 | - * @var \Syscodes\Components\Http\Loaders\Headers $headers |
|
108 | - */ |
|
109 | - public $headers; |
|
110 | - |
|
111 | - /** |
|
112 | - * The current language of the application. |
|
113 | - * |
|
114 | - * @var string $languages |
|
115 | - */ |
|
116 | - protected $languages; |
|
104 | + /** |
|
105 | + * Get the headers request ($_SERVER). |
|
106 | + * |
|
107 | + * @var \Syscodes\Components\Http\Loaders\Headers $headers |
|
108 | + */ |
|
109 | + public $headers; |
|
110 | + |
|
111 | + /** |
|
112 | + * The current language of the application. |
|
113 | + * |
|
114 | + * @var string $languages |
|
115 | + */ |
|
116 | + protected $languages; |
|
117 | 117 | |
118 | - /** |
|
119 | - * Get the locale. |
|
120 | - * |
|
121 | - * @var string $locale |
|
122 | - */ |
|
123 | - protected $locale; |
|
118 | + /** |
|
119 | + * Get the locale. |
|
120 | + * |
|
121 | + * @var string $locale |
|
122 | + */ |
|
123 | + protected $locale; |
|
124 | 124 | |
125 | - /** |
|
126 | - * The method name. |
|
127 | - * |
|
128 | - * @var string $method |
|
129 | - */ |
|
130 | - protected $method; |
|
131 | - |
|
132 | - /** |
|
133 | - * Query string parameters ($_GET). |
|
134 | - * |
|
135 | - * @var \Syscodes\Components\Http\Loaders\Parameters $query |
|
136 | - */ |
|
137 | - public $query; |
|
138 | - |
|
139 | - /** |
|
140 | - * Request body parameters ($_POST). |
|
141 | - * |
|
142 | - * @var \Syscodes\Components\Http\Loaders\Parameters $request |
|
143 | - */ |
|
144 | - public $request; |
|
145 | - |
|
146 | - /** |
|
147 | - * The detected uri and server variables ($_SERVER). |
|
148 | - * |
|
149 | - * @var \Syscodes\Components\Http\Loaders\Server $server |
|
150 | - */ |
|
151 | - public $server; |
|
152 | - |
|
153 | - /** |
|
154 | - * List of routes uri. |
|
155 | - * |
|
156 | - * @var \Syscodes\Components\Http\URI $uri |
|
157 | - */ |
|
158 | - public $uri; |
|
159 | - |
|
160 | - /** |
|
161 | - * Stores the valid locale codes. |
|
162 | - * |
|
163 | - * @var array $validLocales |
|
164 | - */ |
|
165 | - protected $validLocales = []; |
|
166 | - |
|
167 | - /** |
|
168 | - * Constructor. Create new the Request class. |
|
169 | - * |
|
170 | - * @param array $query |
|
171 | - * @param array $request |
|
172 | - * @param array $attributes |
|
173 | - * @param array $cookies |
|
174 | - * @param array $files |
|
175 | - * @param array $server |
|
176 | - * @param string|resource|null $content |
|
177 | - * |
|
178 | - * @return void |
|
179 | - */ |
|
180 | - public function __construct( |
|
181 | - array $query = [], |
|
182 | - array $request = [], |
|
183 | - array $attributes = [], |
|
184 | - array $cookies = [], |
|
185 | - array $files = [], |
|
186 | - array $server = [], |
|
187 | - $content = null |
|
188 | - ) { |
|
189 | - $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content); |
|
125 | + /** |
|
126 | + * The method name. |
|
127 | + * |
|
128 | + * @var string $method |
|
129 | + */ |
|
130 | + protected $method; |
|
131 | + |
|
132 | + /** |
|
133 | + * Query string parameters ($_GET). |
|
134 | + * |
|
135 | + * @var \Syscodes\Components\Http\Loaders\Parameters $query |
|
136 | + */ |
|
137 | + public $query; |
|
138 | + |
|
139 | + /** |
|
140 | + * Request body parameters ($_POST). |
|
141 | + * |
|
142 | + * @var \Syscodes\Components\Http\Loaders\Parameters $request |
|
143 | + */ |
|
144 | + public $request; |
|
145 | + |
|
146 | + /** |
|
147 | + * The detected uri and server variables ($_SERVER). |
|
148 | + * |
|
149 | + * @var \Syscodes\Components\Http\Loaders\Server $server |
|
150 | + */ |
|
151 | + public $server; |
|
152 | + |
|
153 | + /** |
|
154 | + * List of routes uri. |
|
155 | + * |
|
156 | + * @var \Syscodes\Components\Http\URI $uri |
|
157 | + */ |
|
158 | + public $uri; |
|
159 | + |
|
160 | + /** |
|
161 | + * Stores the valid locale codes. |
|
162 | + * |
|
163 | + * @var array $validLocales |
|
164 | + */ |
|
165 | + protected $validLocales = []; |
|
166 | + |
|
167 | + /** |
|
168 | + * Constructor. Create new the Request class. |
|
169 | + * |
|
170 | + * @param array $query |
|
171 | + * @param array $request |
|
172 | + * @param array $attributes |
|
173 | + * @param array $cookies |
|
174 | + * @param array $files |
|
175 | + * @param array $server |
|
176 | + * @param string|resource|null $content |
|
177 | + * |
|
178 | + * @return void |
|
179 | + */ |
|
180 | + public function __construct( |
|
181 | + array $query = [], |
|
182 | + array $request = [], |
|
183 | + array $attributes = [], |
|
184 | + array $cookies = [], |
|
185 | + array $files = [], |
|
186 | + array $server = [], |
|
187 | + $content = null |
|
188 | + ) { |
|
189 | + $this->initialize($query, $request, $attributes, $cookies, $files, $server, $content); |
|
190 | 190 | |
191 | - $this->detectLocale(); |
|
192 | - } |
|
191 | + $this->detectLocale(); |
|
192 | + } |
|
193 | 193 | |
194 | - /** |
|
194 | + /** |
|
195 | 195 | * Enables support for the _method request parameter to determine the intended HTTP method. |
196 | 196 | * |
197 | 197 | * @return void |
@@ -201,361 +201,361 @@ discard block |
||
201 | 201 | self::$httpMethodParameterOverride = true; |
202 | 202 | } |
203 | 203 | |
204 | - /** |
|
205 | - * Checks whether support for the _method request parameter is enabled. |
|
206 | - * |
|
207 | - * @return bool |
|
208 | - */ |
|
209 | - public static function getHttpMethodParameterOverride(): bool |
|
210 | - { |
|
211 | - return self::$httpMethodParameterOverride; |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Creates an Syscodes request from of the Request class instance. |
|
216 | - * |
|
217 | - * @param \Syscodes\Components\Http\Request $request |
|
218 | - * |
|
219 | - * @return static |
|
220 | - */ |
|
221 | - public static function createFromRequest($request): static |
|
222 | - { |
|
223 | - $newRequest = (new static)->duplicate( |
|
224 | - $request->query->all(), $request->request->all(), $request->attributes->all(), |
|
225 | - $request->cookies->all(), $request->files->all(), $request->server->all() |
|
226 | - ); |
|
204 | + /** |
|
205 | + * Checks whether support for the _method request parameter is enabled. |
|
206 | + * |
|
207 | + * @return bool |
|
208 | + */ |
|
209 | + public static function getHttpMethodParameterOverride(): bool |
|
210 | + { |
|
211 | + return self::$httpMethodParameterOverride; |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Creates an Syscodes request from of the Request class instance. |
|
216 | + * |
|
217 | + * @param \Syscodes\Components\Http\Request $request |
|
218 | + * |
|
219 | + * @return static |
|
220 | + */ |
|
221 | + public static function createFromRequest($request): static |
|
222 | + { |
|
223 | + $newRequest = (new static)->duplicate( |
|
224 | + $request->query->all(), $request->request->all(), $request->attributes->all(), |
|
225 | + $request->cookies->all(), $request->files->all(), $request->server->all() |
|
226 | + ); |
|
227 | 227 | |
228 | - $newRequest->headers->replace($request->headers->all()); |
|
228 | + $newRequest->headers->replace($request->headers->all()); |
|
229 | 229 | |
230 | - $newRequest->content = $request->content; |
|
230 | + $newRequest->content = $request->content; |
|
231 | 231 | |
232 | - if ($newRequest->isJson()) { |
|
233 | - $newRequest->request = $newRequest->json(); |
|
234 | - } |
|
232 | + if ($newRequest->isJson()) { |
|
233 | + $newRequest->request = $newRequest->json(); |
|
234 | + } |
|
235 | 235 | |
236 | - return $newRequest; |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Creates a new request with value from PHP's super global. |
|
241 | - * |
|
242 | - * @return static |
|
243 | - */ |
|
244 | - public static function createFromRequestGlobals(): static |
|
245 | - { |
|
246 | - $request = static::createFromRequestFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER); |
|
247 | - |
|
248 | - if (Str::startsWith($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded') |
|
249 | - && in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])) { |
|
250 | - parse_str($request->getContent(), $data); |
|
251 | - $request->request = new Inputs($data); |
|
252 | - } |
|
253 | - |
|
254 | - return $request; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Creates a new request from a factory. |
|
259 | - * |
|
260 | - * @param array $query |
|
261 | - * @param array $request |
|
262 | - * @param array $attributes |
|
263 | - * @param array $cookies |
|
264 | - * @param array $files |
|
265 | - * @param array $server |
|
266 | - * |
|
267 | - * @return static |
|
268 | - */ |
|
269 | - private static function createFromRequestFactory( |
|
270 | - array $query = [], |
|
271 | - array $request = [], |
|
272 | - array $attributes = [] , |
|
273 | - array $cookies = [], |
|
274 | - array $files = [], |
|
275 | - array $server = [] |
|
276 | - ): static { |
|
277 | - if (self::$requestURI) { |
|
278 | - $request = (self::$requestURI)($query, $request, [], $cookies, $files, $server); |
|
279 | - |
|
280 | - if ( ! $request instanceof self) { |
|
281 | - throw new LogicException('The Request active must return an instance of Syscodes\Components\Http\Request'); |
|
282 | - } |
|
283 | - |
|
284 | - return $request; |
|
285 | - } |
|
286 | - |
|
287 | - return new static($query, $request, $attributes, $cookies, $files, $server); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * Returns the factory request currently being used. |
|
292 | - * |
|
293 | - * @param \Syscodes\Components\Http\Request|callable|null $request |
|
294 | - * |
|
295 | - * @return void |
|
296 | - */ |
|
297 | - public static function setFactory(?callable $request): void |
|
298 | - { |
|
299 | - self::$requestURI = $request; |
|
300 | - } |
|
301 | - |
|
302 | - /** |
|
303 | - * Sets the parameters for this request. |
|
304 | - * |
|
305 | - * @param array $query |
|
306 | - * @param array $request |
|
307 | - * @param array $attributes |
|
308 | - * @param array $cookies |
|
309 | - * @param array $files |
|
310 | - * @param array $server |
|
311 | - * |
|
312 | - * @return void |
|
313 | - */ |
|
314 | - public function initialize( |
|
315 | - array $query = [], |
|
316 | - array $request = [], |
|
317 | - array $attributes = [], |
|
318 | - array $cookies = [], |
|
319 | - array $files = [], |
|
320 | - array $server = [], |
|
321 | - $content = null |
|
322 | - ): void { |
|
323 | - $this->query = new Inputs($query); |
|
324 | - $this->request = new Inputs($request); |
|
325 | - $this->attributes = new Parameters($attributes); |
|
326 | - $this->cookies = new Inputs($cookies); |
|
327 | - $this->files = new Files($files); |
|
328 | - $this->server = new Server($server); |
|
329 | - $this->headers = new Headers($this->server->all()); |
|
330 | - |
|
331 | - // Variables initialized |
|
332 | - $this->uri = new URI; |
|
333 | - $this->method = null; |
|
334 | - $this->baseUrl = null; |
|
335 | - $this->content = $content; |
|
336 | - $this->pathInfo = null; |
|
337 | - $this->languages = null; |
|
338 | - $this->acceptableContentTypes = null; |
|
339 | - $this->validLocales = config('app.supportedLocales'); |
|
340 | - $this->clientIp = new RequestClientIP($this->server->all()); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Clones a request and overrides some of its parameters. |
|
345 | - * |
|
346 | - * @param array|null $query |
|
347 | - * @param array|null $request |
|
348 | - * @param array|null $attributes |
|
349 | - * @param array|null $cookies |
|
350 | - * @param array|null $files |
|
351 | - * @param array|null $server |
|
352 | - * |
|
353 | - * @return static |
|
354 | - */ |
|
355 | - public function duplicate( |
|
356 | - array $query = null, |
|
357 | - array $request = null, |
|
358 | - array $attributes = null, |
|
359 | - array $cookies = null, |
|
360 | - array $files = null, |
|
361 | - array $server = null |
|
362 | - ): static { |
|
363 | - $duplicate = clone $this; |
|
364 | - |
|
365 | - if (null !== $query) { |
|
366 | - $duplicate->query = new Inputs($query); |
|
367 | - } |
|
368 | - |
|
369 | - if (null !== $request) { |
|
370 | - $duplicate->request = new Inputs($request); |
|
371 | - } |
|
372 | - |
|
373 | - if (null !== $attributes) { |
|
374 | - $duplicate->attributes = new Parameters($attributes); |
|
375 | - } |
|
376 | - |
|
377 | - if (null !== $cookies) { |
|
378 | - $duplicate->cookies = new Inputs($cookies); |
|
379 | - } |
|
380 | - |
|
381 | - if (null !== $files) { |
|
382 | - $duplicate->files = new Files($files); |
|
383 | - } |
|
384 | - |
|
385 | - if (null !== $server) { |
|
386 | - $duplicate->server = new Server($server); |
|
387 | - $duplicate->headers = new Headers($duplicate->server->all()); |
|
388 | - } |
|
389 | - |
|
390 | - $duplicate->uri = new URI; |
|
391 | - $duplicate->locale = null; |
|
392 | - $duplicate->method = null; |
|
393 | - $duplicate->baseUrl = null; |
|
394 | - $duplicate->pathInfo = null; |
|
395 | - $duplicate->validLocales = config('app.supportedLocales'); |
|
396 | - $duplicate->clientIp = new RequestClientIP($duplicate->server->all()); |
|
397 | - |
|
398 | - return $duplicate; |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Handles setting up the locale, auto-detecting of language. |
|
403 | - * |
|
404 | - * @return void |
|
405 | - */ |
|
406 | - public function detectLocale(): void |
|
407 | - { |
|
408 | - $this->languages = $this->defaultLocale = config('app.locale'); |
|
409 | - |
|
410 | - $this->setLocale($this->validLocales[0]); |
|
411 | - } |
|
412 | - |
|
413 | - /** |
|
414 | - * Returns the default locale as set. |
|
415 | - * |
|
416 | - * @return string |
|
417 | - */ |
|
418 | - public function getDefaultLocale(): string |
|
419 | - { |
|
420 | - return $this->defaultLocale; |
|
421 | - } |
|
422 | - |
|
423 | - /** |
|
424 | - * Gets the current locale, with a fallback to the default. |
|
425 | - * |
|
426 | - * @return string |
|
427 | - */ |
|
428 | - public function getLocale(): string |
|
429 | - { |
|
430 | - return $this->languages ?: $this->defaultLocale; |
|
431 | - } |
|
432 | - |
|
433 | - /** |
|
434 | - * Sets the locale string for this request. |
|
435 | - * |
|
436 | - * @param string $locale |
|
437 | - * |
|
438 | - * @return static |
|
439 | - */ |
|
440 | - public function setLocale(string $locale): static |
|
441 | - { |
|
442 | - if ( ! in_array($locale, $this->validLocales, true)) { |
|
443 | - $locale = $this->defaultLocale; |
|
444 | - } |
|
236 | + return $newRequest; |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Creates a new request with value from PHP's super global. |
|
241 | + * |
|
242 | + * @return static |
|
243 | + */ |
|
244 | + public static function createFromRequestGlobals(): static |
|
245 | + { |
|
246 | + $request = static::createFromRequestFactory($_GET, $_POST, [], $_COOKIE, $_FILES, $_SERVER); |
|
247 | + |
|
248 | + if (Str::startsWith($request->headers->get('CONTENT_TYPE', ''), 'application/x-www-form-urlencoded') |
|
249 | + && in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), ['PUT', 'DELETE', 'PATCH'])) { |
|
250 | + parse_str($request->getContent(), $data); |
|
251 | + $request->request = new Inputs($data); |
|
252 | + } |
|
253 | + |
|
254 | + return $request; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Creates a new request from a factory. |
|
259 | + * |
|
260 | + * @param array $query |
|
261 | + * @param array $request |
|
262 | + * @param array $attributes |
|
263 | + * @param array $cookies |
|
264 | + * @param array $files |
|
265 | + * @param array $server |
|
266 | + * |
|
267 | + * @return static |
|
268 | + */ |
|
269 | + private static function createFromRequestFactory( |
|
270 | + array $query = [], |
|
271 | + array $request = [], |
|
272 | + array $attributes = [] , |
|
273 | + array $cookies = [], |
|
274 | + array $files = [], |
|
275 | + array $server = [] |
|
276 | + ): static { |
|
277 | + if (self::$requestURI) { |
|
278 | + $request = (self::$requestURI)($query, $request, [], $cookies, $files, $server); |
|
279 | + |
|
280 | + if ( ! $request instanceof self) { |
|
281 | + throw new LogicException('The Request active must return an instance of Syscodes\Components\Http\Request'); |
|
282 | + } |
|
283 | + |
|
284 | + return $request; |
|
285 | + } |
|
286 | + |
|
287 | + return new static($query, $request, $attributes, $cookies, $files, $server); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * Returns the factory request currently being used. |
|
292 | + * |
|
293 | + * @param \Syscodes\Components\Http\Request|callable|null $request |
|
294 | + * |
|
295 | + * @return void |
|
296 | + */ |
|
297 | + public static function setFactory(?callable $request): void |
|
298 | + { |
|
299 | + self::$requestURI = $request; |
|
300 | + } |
|
301 | + |
|
302 | + /** |
|
303 | + * Sets the parameters for this request. |
|
304 | + * |
|
305 | + * @param array $query |
|
306 | + * @param array $request |
|
307 | + * @param array $attributes |
|
308 | + * @param array $cookies |
|
309 | + * @param array $files |
|
310 | + * @param array $server |
|
311 | + * |
|
312 | + * @return void |
|
313 | + */ |
|
314 | + public function initialize( |
|
315 | + array $query = [], |
|
316 | + array $request = [], |
|
317 | + array $attributes = [], |
|
318 | + array $cookies = [], |
|
319 | + array $files = [], |
|
320 | + array $server = [], |
|
321 | + $content = null |
|
322 | + ): void { |
|
323 | + $this->query = new Inputs($query); |
|
324 | + $this->request = new Inputs($request); |
|
325 | + $this->attributes = new Parameters($attributes); |
|
326 | + $this->cookies = new Inputs($cookies); |
|
327 | + $this->files = new Files($files); |
|
328 | + $this->server = new Server($server); |
|
329 | + $this->headers = new Headers($this->server->all()); |
|
330 | + |
|
331 | + // Variables initialized |
|
332 | + $this->uri = new URI; |
|
333 | + $this->method = null; |
|
334 | + $this->baseUrl = null; |
|
335 | + $this->content = $content; |
|
336 | + $this->pathInfo = null; |
|
337 | + $this->languages = null; |
|
338 | + $this->acceptableContentTypes = null; |
|
339 | + $this->validLocales = config('app.supportedLocales'); |
|
340 | + $this->clientIp = new RequestClientIP($this->server->all()); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Clones a request and overrides some of its parameters. |
|
345 | + * |
|
346 | + * @param array|null $query |
|
347 | + * @param array|null $request |
|
348 | + * @param array|null $attributes |
|
349 | + * @param array|null $cookies |
|
350 | + * @param array|null $files |
|
351 | + * @param array|null $server |
|
352 | + * |
|
353 | + * @return static |
|
354 | + */ |
|
355 | + public function duplicate( |
|
356 | + array $query = null, |
|
357 | + array $request = null, |
|
358 | + array $attributes = null, |
|
359 | + array $cookies = null, |
|
360 | + array $files = null, |
|
361 | + array $server = null |
|
362 | + ): static { |
|
363 | + $duplicate = clone $this; |
|
364 | + |
|
365 | + if (null !== $query) { |
|
366 | + $duplicate->query = new Inputs($query); |
|
367 | + } |
|
368 | + |
|
369 | + if (null !== $request) { |
|
370 | + $duplicate->request = new Inputs($request); |
|
371 | + } |
|
372 | + |
|
373 | + if (null !== $attributes) { |
|
374 | + $duplicate->attributes = new Parameters($attributes); |
|
375 | + } |
|
376 | + |
|
377 | + if (null !== $cookies) { |
|
378 | + $duplicate->cookies = new Inputs($cookies); |
|
379 | + } |
|
380 | + |
|
381 | + if (null !== $files) { |
|
382 | + $duplicate->files = new Files($files); |
|
383 | + } |
|
384 | + |
|
385 | + if (null !== $server) { |
|
386 | + $duplicate->server = new Server($server); |
|
387 | + $duplicate->headers = new Headers($duplicate->server->all()); |
|
388 | + } |
|
389 | + |
|
390 | + $duplicate->uri = new URI; |
|
391 | + $duplicate->locale = null; |
|
392 | + $duplicate->method = null; |
|
393 | + $duplicate->baseUrl = null; |
|
394 | + $duplicate->pathInfo = null; |
|
395 | + $duplicate->validLocales = config('app.supportedLocales'); |
|
396 | + $duplicate->clientIp = new RequestClientIP($duplicate->server->all()); |
|
397 | + |
|
398 | + return $duplicate; |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Handles setting up the locale, auto-detecting of language. |
|
403 | + * |
|
404 | + * @return void |
|
405 | + */ |
|
406 | + public function detectLocale(): void |
|
407 | + { |
|
408 | + $this->languages = $this->defaultLocale = config('app.locale'); |
|
409 | + |
|
410 | + $this->setLocale($this->validLocales[0]); |
|
411 | + } |
|
412 | + |
|
413 | + /** |
|
414 | + * Returns the default locale as set. |
|
415 | + * |
|
416 | + * @return string |
|
417 | + */ |
|
418 | + public function getDefaultLocale(): string |
|
419 | + { |
|
420 | + return $this->defaultLocale; |
|
421 | + } |
|
422 | + |
|
423 | + /** |
|
424 | + * Gets the current locale, with a fallback to the default. |
|
425 | + * |
|
426 | + * @return string |
|
427 | + */ |
|
428 | + public function getLocale(): string |
|
429 | + { |
|
430 | + return $this->languages ?: $this->defaultLocale; |
|
431 | + } |
|
432 | + |
|
433 | + /** |
|
434 | + * Sets the locale string for this request. |
|
435 | + * |
|
436 | + * @param string $locale |
|
437 | + * |
|
438 | + * @return static |
|
439 | + */ |
|
440 | + public function setLocale(string $locale): static |
|
441 | + { |
|
442 | + if ( ! in_array($locale, $this->validLocales, true)) { |
|
443 | + $locale = $this->defaultLocale; |
|
444 | + } |
|
445 | 445 | |
446 | - $this->languages = $locale; |
|
446 | + $this->languages = $locale; |
|
447 | 447 | |
448 | - Locale::setDefault($locale); |
|
448 | + Locale::setDefault($locale); |
|
449 | 449 | |
450 | - return $this; |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Returns the host name. |
|
455 | - * |
|
456 | - * @return string |
|
457 | - */ |
|
458 | - public function getHost(): string |
|
459 | - { |
|
460 | - if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
461 | - $host = $forwardedHost[0]; |
|
462 | - } elseif ( ! $host = $this->headers->get('HOST')) { |
|
463 | - if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
464 | - $host = $this->server->get('REMOTE_ADDR', ''); |
|
465 | - } |
|
466 | - } |
|
467 | - |
|
468 | - $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
450 | + return $this; |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Returns the host name. |
|
455 | + * |
|
456 | + * @return string |
|
457 | + */ |
|
458 | + public function getHost(): string |
|
459 | + { |
|
460 | + if ($forwardedHost = $this->server->get('HTTP_X_FORWARDED_HOST')) { |
|
461 | + $host = $forwardedHost[0]; |
|
462 | + } elseif ( ! $host = $this->headers->get('HOST')) { |
|
463 | + if ( ! $host = $this->server->get('SERVER_NAME')) { |
|
464 | + $host = $this->server->get('REMOTE_ADDR', ''); |
|
465 | + } |
|
466 | + } |
|
467 | + |
|
468 | + $host = strtolower(preg_replace('/:\d+$/', '', trim(($host)))); |
|
469 | 469 | |
470 | - return $this->uri->setHost($host); |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * Returns the port on which the request is made. |
|
475 | - * |
|
476 | - * @return int |
|
477 | - */ |
|
478 | - public function getPort(): int |
|
479 | - { |
|
480 | - if ( ! $this->server->get('HTTP_HOST')) { |
|
481 | - return $this->server->get('SERVER_PORT'); |
|
482 | - } |
|
470 | + return $this->uri->setHost($host); |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * Returns the port on which the request is made. |
|
475 | + * |
|
476 | + * @return int |
|
477 | + */ |
|
478 | + public function getPort(): int |
|
479 | + { |
|
480 | + if ( ! $this->server->get('HTTP_HOST')) { |
|
481 | + return $this->server->get('SERVER_PORT'); |
|
482 | + } |
|
483 | 483 | |
484 | - return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Gets the request's scheme. |
|
489 | - * |
|
490 | - * @return string |
|
491 | - */ |
|
492 | - public function getScheme(): string |
|
493 | - { |
|
494 | - return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
495 | - } |
|
496 | - |
|
497 | - /** |
|
498 | - * Get the user. |
|
499 | - * |
|
500 | - * @return string|null |
|
501 | - */ |
|
502 | - public function getUser(): ?string |
|
503 | - { |
|
504 | - $user = $this->uri->setUser( |
|
505 | - $this->headers->get('PHP_AUTH_USER') |
|
506 | - ); |
|
507 | - |
|
508 | - return $user; |
|
509 | - } |
|
510 | - |
|
511 | - /** |
|
512 | - * Get the password. |
|
513 | - * |
|
514 | - * @return string|null |
|
515 | - */ |
|
516 | - public function getPassword(): ?string |
|
517 | - { |
|
518 | - $password = $this->uri->setPassword( |
|
519 | - $this->headers->get('PHP_AUTH_PW') |
|
520 | - ); |
|
521 | - |
|
522 | - return $password; |
|
523 | - } |
|
524 | - |
|
525 | - /** |
|
526 | - * Gets the user info. |
|
527 | - * |
|
528 | - * @return string|null |
|
529 | - */ |
|
530 | - public function getUserInfo(): ?string |
|
531 | - { |
|
532 | - return $this->uri->getUserInfo(); |
|
533 | - } |
|
534 | - |
|
535 | - /** |
|
536 | - * Returns the HTTP host being requested. |
|
537 | - * |
|
538 | - * @return string |
|
539 | - */ |
|
540 | - public function getHttpHost(): string |
|
541 | - { |
|
542 | - $scheme = $this->getScheme(); |
|
543 | - $port = $this->getPort(); |
|
544 | - |
|
545 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
546 | - return $this->getHost(); |
|
547 | - } |
|
548 | - |
|
549 | - return $this->getHost().':'.$port; |
|
550 | - } |
|
551 | - |
|
552 | - /** |
|
553 | - * Gets the scheme and HTTP host. |
|
554 | - * |
|
555 | - * @return string |
|
556 | - */ |
|
557 | - public function getSchemeWithHttpHost(): string |
|
558 | - { |
|
559 | - return $this->getScheme().'://'.$this->getHttpHost(); |
|
560 | - } |
|
484 | + return 'https' === $this->getScheme() ? $this->uri->setPort(443) : $this->uri->setPort(80); |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Gets the request's scheme. |
|
489 | + * |
|
490 | + * @return string |
|
491 | + */ |
|
492 | + public function getScheme(): string |
|
493 | + { |
|
494 | + return $this->secure() ? $this->uri->setScheme('https') : $this->uri->setScheme('http'); |
|
495 | + } |
|
496 | + |
|
497 | + /** |
|
498 | + * Get the user. |
|
499 | + * |
|
500 | + * @return string|null |
|
501 | + */ |
|
502 | + public function getUser(): ?string |
|
503 | + { |
|
504 | + $user = $this->uri->setUser( |
|
505 | + $this->headers->get('PHP_AUTH_USER') |
|
506 | + ); |
|
507 | + |
|
508 | + return $user; |
|
509 | + } |
|
510 | + |
|
511 | + /** |
|
512 | + * Get the password. |
|
513 | + * |
|
514 | + * @return string|null |
|
515 | + */ |
|
516 | + public function getPassword(): ?string |
|
517 | + { |
|
518 | + $password = $this->uri->setPassword( |
|
519 | + $this->headers->get('PHP_AUTH_PW') |
|
520 | + ); |
|
521 | + |
|
522 | + return $password; |
|
523 | + } |
|
524 | + |
|
525 | + /** |
|
526 | + * Gets the user info. |
|
527 | + * |
|
528 | + * @return string|null |
|
529 | + */ |
|
530 | + public function getUserInfo(): ?string |
|
531 | + { |
|
532 | + return $this->uri->getUserInfo(); |
|
533 | + } |
|
534 | + |
|
535 | + /** |
|
536 | + * Returns the HTTP host being requested. |
|
537 | + * |
|
538 | + * @return string |
|
539 | + */ |
|
540 | + public function getHttpHost(): string |
|
541 | + { |
|
542 | + $scheme = $this->getScheme(); |
|
543 | + $port = $this->getPort(); |
|
544 | + |
|
545 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
546 | + return $this->getHost(); |
|
547 | + } |
|
548 | + |
|
549 | + return $this->getHost().':'.$port; |
|
550 | + } |
|
551 | + |
|
552 | + /** |
|
553 | + * Gets the scheme and HTTP host. |
|
554 | + * |
|
555 | + * @return string |
|
556 | + */ |
|
557 | + public function getSchemeWithHttpHost(): string |
|
558 | + { |
|
559 | + return $this->getScheme().'://'.$this->getHttpHost(); |
|
560 | + } |
|
561 | 561 | } |
562 | 562 | \ No newline at end of file |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | private static function createFromRequestFactory( |
270 | 270 | array $query = [], |
271 | 271 | array $request = [], |
272 | - array $attributes = [] , |
|
272 | + array $attributes = [], |
|
273 | 273 | array $cookies = [], |
274 | 274 | array $files = [], |
275 | 275 | array $server = [] |
@@ -542,7 +542,7 @@ discard block |
||
542 | 542 | $scheme = $this->getScheme(); |
543 | 543 | $port = $this->getPort(); |
544 | 544 | |
545 | - if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
545 | + if (('http' === $scheme && 80 === $port) || ('https' === $scheme && 443 === $port)) { |
|
546 | 546 | return $this->getHost(); |
547 | 547 | } |
548 | 548 |
@@ -43,283 +43,283 @@ discard block |
||
43 | 43 | */ |
44 | 44 | class Request |
45 | 45 | { |
46 | - use HttpRequest, |
|
47 | - HttpResources, |
|
48 | - CanBePrecognitive, |
|
49 | - InteractsWithInput, |
|
50 | - InteractsWithFlashData, |
|
51 | - InteractsWithContentTypes; |
|
52 | - |
|
53 | - /** |
|
54 | - * Get the acceptable of content types. |
|
55 | - * |
|
56 | - * @var string[] $acceptableContenTypes |
|
57 | - */ |
|
58 | - protected $acceptableContentTypes; |
|
59 | - |
|
60 | - /** |
|
61 | - * The decoded JSON content for the request. |
|
62 | - * |
|
63 | - * @var \Syscodes\Components\Http\Loaders\Parameters|null $json |
|
64 | - */ |
|
65 | - protected $json; |
|
66 | - |
|
67 | - /** |
|
68 | - * The path info of URL. |
|
69 | - * |
|
70 | - * @var string $pathInfo |
|
71 | - */ |
|
72 | - protected $pathInfo; |
|
73 | - |
|
74 | - /** |
|
75 | - * Get request URI. |
|
76 | - * |
|
77 | - * @var string $requestToUri |
|
78 | - */ |
|
79 | - protected $requestToUri; |
|
80 | - |
|
81 | - /** |
|
82 | - * Get the route resolver callback. |
|
83 | - * |
|
84 | - * @var \Closure $routeResolver |
|
85 | - */ |
|
86 | - protected $routeResolver; |
|
87 | - |
|
88 | - /** |
|
89 | - * The Session implementation. |
|
90 | - * |
|
91 | - * @var \Syscodes\Components\Contracts\Session\Session $session |
|
92 | - */ |
|
93 | - protected $session; |
|
94 | - |
|
95 | - /** |
|
96 | - * Create a new Syscodes HTTP request from server variables. |
|
97 | - * |
|
98 | - * @return static |
|
99 | - */ |
|
100 | - public static function capture(): static |
|
101 | - { |
|
102 | - static::enabledHttpMethodParameterOverride(); |
|
46 | + use HttpRequest, |
|
47 | + HttpResources, |
|
48 | + CanBePrecognitive, |
|
49 | + InteractsWithInput, |
|
50 | + InteractsWithFlashData, |
|
51 | + InteractsWithContentTypes; |
|
52 | + |
|
53 | + /** |
|
54 | + * Get the acceptable of content types. |
|
55 | + * |
|
56 | + * @var string[] $acceptableContenTypes |
|
57 | + */ |
|
58 | + protected $acceptableContentTypes; |
|
59 | + |
|
60 | + /** |
|
61 | + * The decoded JSON content for the request. |
|
62 | + * |
|
63 | + * @var \Syscodes\Components\Http\Loaders\Parameters|null $json |
|
64 | + */ |
|
65 | + protected $json; |
|
66 | + |
|
67 | + /** |
|
68 | + * The path info of URL. |
|
69 | + * |
|
70 | + * @var string $pathInfo |
|
71 | + */ |
|
72 | + protected $pathInfo; |
|
73 | + |
|
74 | + /** |
|
75 | + * Get request URI. |
|
76 | + * |
|
77 | + * @var string $requestToUri |
|
78 | + */ |
|
79 | + protected $requestToUri; |
|
80 | + |
|
81 | + /** |
|
82 | + * Get the route resolver callback. |
|
83 | + * |
|
84 | + * @var \Closure $routeResolver |
|
85 | + */ |
|
86 | + protected $routeResolver; |
|
87 | + |
|
88 | + /** |
|
89 | + * The Session implementation. |
|
90 | + * |
|
91 | + * @var \Syscodes\Components\Contracts\Session\Session $session |
|
92 | + */ |
|
93 | + protected $session; |
|
94 | + |
|
95 | + /** |
|
96 | + * Create a new Syscodes HTTP request from server variables. |
|
97 | + * |
|
98 | + * @return static |
|
99 | + */ |
|
100 | + public static function capture(): static |
|
101 | + { |
|
102 | + static::enabledHttpMethodParameterOverride(); |
|
103 | 103 | |
104 | - return static::createFromRequest(static::createFromRequestGlobals()); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Returns the desired segment, or $default if it does not exist. |
|
109 | - * |
|
110 | - * @param int $index The segment number (1-based index) |
|
111 | - * @param mixed $default Default value to return |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function segment($index, $default = null) |
|
116 | - { |
|
117 | - return $this->uri->getSegment($index, $default); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns all segments in an array. For total of segments |
|
122 | - * used the function PHP count(). |
|
123 | - * |
|
124 | - * @return array|null |
|
125 | - */ |
|
126 | - public function segments() |
|
127 | - { |
|
128 | - return $this->uri->getSegments(); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Returns the total number of segment. |
|
133 | - * |
|
134 | - * @return int|null |
|
135 | - */ |
|
136 | - public function totalSegments() |
|
137 | - { |
|
138 | - return $this->uri->getTotalSegments(); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Returns the full request string. |
|
143 | - * |
|
144 | - * @param string $key |
|
145 | - * @param mixed $default |
|
146 | - * |
|
147 | - * @return mixed |
|
148 | - */ |
|
149 | - public function get(string $key, $default = null) |
|
150 | - { |
|
151 | - if ($this !== $result = $this->attributes->get($key, $this)) { |
|
152 | - return $result; |
|
153 | - } |
|
154 | - |
|
155 | - if ($this->query->has($key)) { |
|
156 | - return $this->query->all()[$key]; |
|
157 | - } |
|
104 | + return static::createFromRequest(static::createFromRequestGlobals()); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Returns the desired segment, or $default if it does not exist. |
|
109 | + * |
|
110 | + * @param int $index The segment number (1-based index) |
|
111 | + * @param mixed $default Default value to return |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function segment($index, $default = null) |
|
116 | + { |
|
117 | + return $this->uri->getSegment($index, $default); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns all segments in an array. For total of segments |
|
122 | + * used the function PHP count(). |
|
123 | + * |
|
124 | + * @return array|null |
|
125 | + */ |
|
126 | + public function segments() |
|
127 | + { |
|
128 | + return $this->uri->getSegments(); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Returns the total number of segment. |
|
133 | + * |
|
134 | + * @return int|null |
|
135 | + */ |
|
136 | + public function totalSegments() |
|
137 | + { |
|
138 | + return $this->uri->getTotalSegments(); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Returns the full request string. |
|
143 | + * |
|
144 | + * @param string $key |
|
145 | + * @param mixed $default |
|
146 | + * |
|
147 | + * @return mixed |
|
148 | + */ |
|
149 | + public function get(string $key, $default = null) |
|
150 | + { |
|
151 | + if ($this !== $result = $this->attributes->get($key, $this)) { |
|
152 | + return $result; |
|
153 | + } |
|
154 | + |
|
155 | + if ($this->query->has($key)) { |
|
156 | + return $this->query->all()[$key]; |
|
157 | + } |
|
158 | 158 | |
159 | - if ($this->request->has($key)) { |
|
160 | - return $this->request->all()[$key]; |
|
161 | - } |
|
159 | + if ($this->request->has($key)) { |
|
160 | + return $this->request->all()[$key]; |
|
161 | + } |
|
162 | 162 | |
163 | - return $default; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Gets the Session. |
|
168 | - * |
|
169 | - * @return \Syscodes\Components\Http\Session\SessionInterface |
|
170 | - * |
|
171 | - * @throws \Syscodes\Components\Http\Exceptions\SessionNotFoundException |
|
172 | - */ |
|
173 | - public function getSession() |
|
174 | - { |
|
175 | - $this->hasSession() |
|
176 | - ? new SessionDecorator($this->session()) |
|
177 | - : throw new SessionNotFoundException; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Whether the request contains a Session object. |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function hasSession(): bool |
|
186 | - { |
|
187 | - return ! is_null($this->session); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Get the session associated with the request. |
|
192 | - * |
|
193 | - * @return \Syscodes\Components\Contracts\Session\Session |
|
194 | - * |
|
195 | - * @throws RuntimeException |
|
196 | - */ |
|
197 | - public function session() |
|
198 | - { |
|
199 | - if ( ! $this->hasSession()) { |
|
200 | - throw new RuntimeException('Session store not set on request'); |
|
201 | - } |
|
163 | + return $default; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Gets the Session. |
|
168 | + * |
|
169 | + * @return \Syscodes\Components\Http\Session\SessionInterface |
|
170 | + * |
|
171 | + * @throws \Syscodes\Components\Http\Exceptions\SessionNotFoundException |
|
172 | + */ |
|
173 | + public function getSession() |
|
174 | + { |
|
175 | + $this->hasSession() |
|
176 | + ? new SessionDecorator($this->session()) |
|
177 | + : throw new SessionNotFoundException; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Whether the request contains a Session object. |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function hasSession(): bool |
|
186 | + { |
|
187 | + return ! is_null($this->session); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Get the session associated with the request. |
|
192 | + * |
|
193 | + * @return \Syscodes\Components\Contracts\Session\Session |
|
194 | + * |
|
195 | + * @throws RuntimeException |
|
196 | + */ |
|
197 | + public function session() |
|
198 | + { |
|
199 | + if ( ! $this->hasSession()) { |
|
200 | + throw new RuntimeException('Session store not set on request'); |
|
201 | + } |
|
202 | 202 | |
203 | - return $this->session; |
|
204 | - } |
|
203 | + return $this->session; |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Set the session instance on the request. |
|
208 | - * |
|
209 | - * @param \Syscodes\Components\Contracts\Session\Session $session |
|
210 | - * |
|
211 | - * @return void |
|
212 | - */ |
|
213 | - public function setSession($session): void |
|
214 | - { |
|
215 | - $this->session = $session; |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Get the JSON payload for the request. |
|
220 | - * |
|
221 | - * @param string|null $key |
|
222 | - * @param mixed $default |
|
223 | - * |
|
224 | - * @return \Syscodes\Components\Http\Loaders\Parameters|mixed |
|
225 | - */ |
|
226 | - public function json($key = null, $default = null) |
|
227 | - { |
|
228 | - if ( ! isset($this->json)) { |
|
229 | - $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
230 | - } |
|
231 | - |
|
232 | - if (is_null($key)) { |
|
233 | - return $this->json; |
|
234 | - } |
|
235 | - |
|
236 | - return Arr::get($this->json->all(), $key, $default); |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * Set the JSON payload for the request. |
|
241 | - * |
|
242 | - * @param \Syscodes\Components\Http\Loaders\Parameters $json |
|
243 | - * |
|
244 | - * @return static |
|
245 | - */ |
|
246 | - public function setJson($json): static |
|
247 | - { |
|
248 | - $this->json = $json; |
|
249 | - |
|
250 | - return $this; |
|
251 | - } |
|
206 | + /** |
|
207 | + * Set the session instance on the request. |
|
208 | + * |
|
209 | + * @param \Syscodes\Components\Contracts\Session\Session $session |
|
210 | + * |
|
211 | + * @return void |
|
212 | + */ |
|
213 | + public function setSession($session): void |
|
214 | + { |
|
215 | + $this->session = $session; |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Get the JSON payload for the request. |
|
220 | + * |
|
221 | + * @param string|null $key |
|
222 | + * @param mixed $default |
|
223 | + * |
|
224 | + * @return \Syscodes\Components\Http\Loaders\Parameters|mixed |
|
225 | + */ |
|
226 | + public function json($key = null, $default = null) |
|
227 | + { |
|
228 | + if ( ! isset($this->json)) { |
|
229 | + $this->json = new Parameters((array) json_decode($this->getContent(), true)); |
|
230 | + } |
|
231 | + |
|
232 | + if (is_null($key)) { |
|
233 | + return $this->json; |
|
234 | + } |
|
235 | + |
|
236 | + return Arr::get($this->json->all(), $key, $default); |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * Set the JSON payload for the request. |
|
241 | + * |
|
242 | + * @param \Syscodes\Components\Http\Loaders\Parameters $json |
|
243 | + * |
|
244 | + * @return static |
|
245 | + */ |
|
246 | + public function setJson($json): static |
|
247 | + { |
|
248 | + $this->json = $json; |
|
249 | + |
|
250 | + return $this; |
|
251 | + } |
|
252 | 252 | |
253 | - /** |
|
254 | - * Gets a list of content types acceptable by the client browser in preferable order. |
|
255 | - * |
|
256 | - * @return string[] |
|
257 | - */ |
|
258 | - public function getAcceptableContentTypes(): array |
|
259 | - { |
|
260 | - if (null !== $this->acceptableContentTypes) { |
|
261 | - return $this->acceptableContentTypes; |
|
262 | - } |
|
253 | + /** |
|
254 | + * Gets a list of content types acceptable by the client browser in preferable order. |
|
255 | + * |
|
256 | + * @return string[] |
|
257 | + */ |
|
258 | + public function getAcceptableContentTypes(): array |
|
259 | + { |
|
260 | + if (null !== $this->acceptableContentTypes) { |
|
261 | + return $this->acceptableContentTypes; |
|
262 | + } |
|
263 | 263 | |
264 | - return $this->acceptableContentTypes = array_map('strval', [$this->headers->get('Accept')]); |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * Returns whether this is an AJAX request or not. |
|
269 | - * Alias of isXmlHttpRequest(). |
|
270 | - * |
|
271 | - * @return bool |
|
272 | - */ |
|
273 | - public function ajax(): bool |
|
274 | - { |
|
275 | - return $this->isXmlHttpRequest(); |
|
276 | - } |
|
277 | - |
|
278 | - /** |
|
279 | - * Returns whether this is an AJAX request or not. |
|
280 | - * |
|
281 | - * @return bool |
|
282 | - */ |
|
283 | - public function isXmlHttpRequest(): bool |
|
284 | - { |
|
285 | - return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
286 | - strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
287 | - } |
|
264 | + return $this->acceptableContentTypes = array_map('strval', [$this->headers->get('Accept')]); |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * Returns whether this is an AJAX request or not. |
|
269 | + * Alias of isXmlHttpRequest(). |
|
270 | + * |
|
271 | + * @return bool |
|
272 | + */ |
|
273 | + public function ajax(): bool |
|
274 | + { |
|
275 | + return $this->isXmlHttpRequest(); |
|
276 | + } |
|
277 | + |
|
278 | + /** |
|
279 | + * Returns whether this is an AJAX request or not. |
|
280 | + * |
|
281 | + * @return bool |
|
282 | + */ |
|
283 | + public function isXmlHttpRequest(): bool |
|
284 | + { |
|
285 | + return ! empty($this->server->get('HTTP_X_REQUESTED_WITH')) && |
|
286 | + strtolower($this->server->get('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest'; |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * Determine if the request is the result of a PJAX call. |
|
291 | - * |
|
292 | - * @return bool |
|
293 | - */ |
|
294 | - public function pjax(): bool |
|
295 | - { |
|
296 | - return $this->headers->get('X-PJAX') == true; |
|
297 | - } |
|
289 | + /** |
|
290 | + * Determine if the request is the result of a PJAX call. |
|
291 | + * |
|
292 | + * @return bool |
|
293 | + */ |
|
294 | + public function pjax(): bool |
|
295 | + { |
|
296 | + return $this->headers->get('X-PJAX') == true; |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Determine if the request is the result of a prefetch call. |
|
301 | - * |
|
302 | - * @return bool |
|
303 | - */ |
|
304 | - public function prefetch(): bool |
|
305 | - { |
|
306 | - return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 || |
|
307 | - strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0; |
|
308 | - } |
|
309 | - |
|
310 | - /** |
|
311 | - * Checks if the method request is of specified type. |
|
312 | - * |
|
313 | - * @param string $method |
|
314 | - * |
|
315 | - * @return bool |
|
316 | - */ |
|
317 | - public function isMethod(string $method): bool |
|
318 | - { |
|
319 | - return $this->getMethod() === strtoupper($method); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
299 | + /** |
|
300 | + * Determine if the request is the result of a prefetch call. |
|
301 | + * |
|
302 | + * @return bool |
|
303 | + */ |
|
304 | + public function prefetch(): bool |
|
305 | + { |
|
306 | + return strcasecmp($this->server->get('HTTP_X_MOZ') ?? '', 'prefetch') === 0 || |
|
307 | + strcasecmp($this->headers->get('Purpose') ?? '', 'prefetch') === 0; |
|
308 | + } |
|
309 | + |
|
310 | + /** |
|
311 | + * Checks if the method request is of specified type. |
|
312 | + * |
|
313 | + * @param string $method |
|
314 | + * |
|
315 | + * @return bool |
|
316 | + */ |
|
317 | + public function isMethod(string $method): bool |
|
318 | + { |
|
319 | + return $this->getMethod() === strtoupper($method); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | 323 | * Alias of the request method. |
324 | 324 | * |
325 | 325 | * @return string |
@@ -329,414 +329,414 @@ discard block |
||
329 | 329 | return $this->getMethod(); |
330 | 330 | } |
331 | 331 | |
332 | - /** |
|
333 | - * Returns the input method used (GET, POST, DELETE, etc.). |
|
334 | - * |
|
335 | - * @return string |
|
336 | - * |
|
337 | - * @throws \LogicException |
|
338 | - */ |
|
339 | - public function getmethod(): string |
|
340 | - { |
|
341 | - if (null !== $this->method) { |
|
342 | - return $this->method; |
|
343 | - } |
|
332 | + /** |
|
333 | + * Returns the input method used (GET, POST, DELETE, etc.). |
|
334 | + * |
|
335 | + * @return string |
|
336 | + * |
|
337 | + * @throws \LogicException |
|
338 | + */ |
|
339 | + public function getmethod(): string |
|
340 | + { |
|
341 | + if (null !== $this->method) { |
|
342 | + return $this->method; |
|
343 | + } |
|
344 | 344 | |
345 | - $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
345 | + $this->method = strtoupper($this->server->get('REQUEST_METHOD', 'GET')); |
|
346 | 346 | |
347 | - if ('POST' !== $this->method) { |
|
348 | - return $this->method; |
|
349 | - } |
|
347 | + if ('POST' !== $this->method) { |
|
348 | + return $this->method; |
|
349 | + } |
|
350 | 350 | |
351 | - $method = $this->headers->get('X-HTTP-METHOD-OVERRIDE'); |
|
351 | + $method = $this->headers->get('X-HTTP-METHOD-OVERRIDE'); |
|
352 | 352 | |
353 | - if ( ! $method && self::$httpMethodParameterOverride) { |
|
354 | - $method = $this->request->get('_method', $this->query->get('_method', 'POST')); |
|
355 | - } |
|
353 | + if ( ! $method && self::$httpMethodParameterOverride) { |
|
354 | + $method = $this->request->get('_method', $this->query->get('_method', 'POST')); |
|
355 | + } |
|
356 | 356 | |
357 | - if ( ! is_string($method)) { |
|
358 | - return $this->method; |
|
359 | - } |
|
357 | + if ( ! is_string($method)) { |
|
358 | + return $this->method; |
|
359 | + } |
|
360 | 360 | |
361 | - $method = strtoupper($method); |
|
361 | + $method = strtoupper($method); |
|
362 | 362 | |
363 | - if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
364 | - return $this->method = $method; |
|
365 | - } |
|
363 | + if (in_array($method, ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'CONNECT', 'OPTIONS', 'PATCH', 'PURGE', 'TRACE'], true)) { |
|
364 | + return $this->method = $method; |
|
365 | + } |
|
366 | 366 | |
367 | - if ( ! preg_match('/^[A-Z]++$/D', $method)) { |
|
368 | - throw new LogicException(sprintf('Invalid method override "%s".', $method)); |
|
369 | - } |
|
367 | + if ( ! preg_match('/^[A-Z]++$/D', $method)) { |
|
368 | + throw new LogicException(sprintf('Invalid method override "%s".', $method)); |
|
369 | + } |
|
370 | 370 | |
371 | - return $this->method = $method; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Sets the request method. |
|
376 | - * |
|
377 | - * @param string $method |
|
378 | - * |
|
379 | - * @return void |
|
380 | - */ |
|
381 | - public function setMethod(string $method): void |
|
382 | - { |
|
383 | - $this->method = null; |
|
384 | - |
|
385 | - $this->server->set('REQUEST_METHOD', $method); |
|
386 | - } |
|
387 | - |
|
388 | - /** |
|
389 | - * Get the input source for the request. |
|
390 | - * |
|
391 | - * @return \Syscodes\Components\Http\Loaders\Parameters |
|
392 | - */ |
|
393 | - public function getInputSource() |
|
394 | - { |
|
395 | - if ($this->isJson()) { |
|
396 | - return $this->json(); |
|
397 | - } |
|
398 | - |
|
399 | - return in_array($this->getMethod(), ['GET', 'HEAD']) ? $this->query : $this->request; |
|
400 | - } |
|
371 | + return $this->method = $method; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Sets the request method. |
|
376 | + * |
|
377 | + * @param string $method |
|
378 | + * |
|
379 | + * @return void |
|
380 | + */ |
|
381 | + public function setMethod(string $method): void |
|
382 | + { |
|
383 | + $this->method = null; |
|
384 | + |
|
385 | + $this->server->set('REQUEST_METHOD', $method); |
|
386 | + } |
|
387 | + |
|
388 | + /** |
|
389 | + * Get the input source for the request. |
|
390 | + * |
|
391 | + * @return \Syscodes\Components\Http\Loaders\Parameters |
|
392 | + */ |
|
393 | + public function getInputSource() |
|
394 | + { |
|
395 | + if ($this->isJson()) { |
|
396 | + return $this->json(); |
|
397 | + } |
|
398 | + |
|
399 | + return in_array($this->getMethod(), ['GET', 'HEAD']) ? $this->query : $this->request; |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Determine if the current request URI matches a pattern. |
|
404 | - * |
|
405 | - * @param mixed ...$patterns |
|
406 | - * |
|
407 | - * @return bool |
|
408 | - */ |
|
409 | - public function is(...$patterns): bool |
|
410 | - { |
|
411 | - $path = $this->decodedPath(); |
|
402 | + /** |
|
403 | + * Determine if the current request URI matches a pattern. |
|
404 | + * |
|
405 | + * @param mixed ...$patterns |
|
406 | + * |
|
407 | + * @return bool |
|
408 | + */ |
|
409 | + public function is(...$patterns): bool |
|
410 | + { |
|
411 | + $path = $this->decodedPath(); |
|
412 | 412 | |
413 | - foreach ($patterns as $pattern) { |
|
414 | - if (Str::is($pattern, $path)) { |
|
415 | - return true; |
|
416 | - } |
|
417 | - } |
|
418 | - |
|
419 | - return false; |
|
420 | - } |
|
421 | - |
|
422 | - /** |
|
423 | - * Determine if the route name matches a given pattern. |
|
424 | - * |
|
425 | - * @param mixed ...$patterns |
|
426 | - * |
|
427 | - * @return bool |
|
428 | - */ |
|
429 | - public function routeIs(...$patterns): bool |
|
430 | - { |
|
431 | - return $this->route() && $this->route()->is(...$patterns); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Get the route handling the request. |
|
436 | - * |
|
437 | - * @param string|null $param |
|
438 | - * @param mixed $default |
|
439 | - * |
|
440 | - * @return \Syscodes\Components\Routing\Route|object|string|null |
|
441 | - */ |
|
442 | - public function route($param = null, $default = null) |
|
443 | - { |
|
444 | - $route = call_user_func($this->getRouteResolver()); |
|
445 | - |
|
446 | - if (is_null($route) || is_null($param)) { |
|
447 | - return $route; |
|
448 | - } |
|
449 | - |
|
450 | - return $route->parameter($param, $default); |
|
451 | - } |
|
452 | - |
|
453 | - /** |
|
454 | - * Get the current decoded path info for the request. |
|
455 | - * |
|
456 | - * @return string |
|
457 | - */ |
|
458 | - public function decodedPath(): string |
|
459 | - { |
|
460 | - return rawurldecode($this->path()); |
|
461 | - } |
|
462 | - |
|
463 | - /** |
|
464 | - * Get the current path info for the request. |
|
465 | - * |
|
466 | - * @return string |
|
467 | - */ |
|
468 | - public function path(): string |
|
469 | - { |
|
470 | - $path = trim($this->getPathInfo(), '/'); |
|
471 | - |
|
472 | - return $path == '' ? '/' : $path; |
|
473 | - } |
|
474 | - |
|
475 | - /** |
|
476 | - * Get the full URL for the request. |
|
477 | - * |
|
478 | - * @return string |
|
479 | - */ |
|
480 | - public function fullUrl(): string |
|
481 | - { |
|
482 | - $query = $this->getQueryString(); |
|
413 | + foreach ($patterns as $pattern) { |
|
414 | + if (Str::is($pattern, $path)) { |
|
415 | + return true; |
|
416 | + } |
|
417 | + } |
|
418 | + |
|
419 | + return false; |
|
420 | + } |
|
421 | + |
|
422 | + /** |
|
423 | + * Determine if the route name matches a given pattern. |
|
424 | + * |
|
425 | + * @param mixed ...$patterns |
|
426 | + * |
|
427 | + * @return bool |
|
428 | + */ |
|
429 | + public function routeIs(...$patterns): bool |
|
430 | + { |
|
431 | + return $this->route() && $this->route()->is(...$patterns); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Get the route handling the request. |
|
436 | + * |
|
437 | + * @param string|null $param |
|
438 | + * @param mixed $default |
|
439 | + * |
|
440 | + * @return \Syscodes\Components\Routing\Route|object|string|null |
|
441 | + */ |
|
442 | + public function route($param = null, $default = null) |
|
443 | + { |
|
444 | + $route = call_user_func($this->getRouteResolver()); |
|
445 | + |
|
446 | + if (is_null($route) || is_null($param)) { |
|
447 | + return $route; |
|
448 | + } |
|
449 | + |
|
450 | + return $route->parameter($param, $default); |
|
451 | + } |
|
452 | + |
|
453 | + /** |
|
454 | + * Get the current decoded path info for the request. |
|
455 | + * |
|
456 | + * @return string |
|
457 | + */ |
|
458 | + public function decodedPath(): string |
|
459 | + { |
|
460 | + return rawurldecode($this->path()); |
|
461 | + } |
|
462 | + |
|
463 | + /** |
|
464 | + * Get the current path info for the request. |
|
465 | + * |
|
466 | + * @return string |
|
467 | + */ |
|
468 | + public function path(): string |
|
469 | + { |
|
470 | + $path = trim($this->getPathInfo(), '/'); |
|
471 | + |
|
472 | + return $path == '' ? '/' : $path; |
|
473 | + } |
|
474 | + |
|
475 | + /** |
|
476 | + * Get the full URL for the request. |
|
477 | + * |
|
478 | + * @return string |
|
479 | + */ |
|
480 | + public function fullUrl(): string |
|
481 | + { |
|
482 | + $query = $this->getQueryString(); |
|
483 | 483 | |
484 | - $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; |
|
484 | + $question = $this->getBaseUrl().$this->getPathInfo() === '/' ? '/?' : '?'; |
|
485 | 485 | |
486 | - return $query ? $this->url().$question.$query : $this->url(); |
|
487 | - } |
|
486 | + return $query ? $this->url().$question.$query : $this->url(); |
|
487 | + } |
|
488 | 488 | |
489 | - /** |
|
490 | - * Generates the normalized query string for the Request. |
|
491 | - * |
|
492 | - * @return string |
|
493 | - */ |
|
494 | - public function getQueryString(): ?string |
|
495 | - { |
|
496 | - $queryString = RequestUtils::normalizedQueryString($this->server->get('QUERY_STRING')); |
|
489 | + /** |
|
490 | + * Generates the normalized query string for the Request. |
|
491 | + * |
|
492 | + * @return string |
|
493 | + */ |
|
494 | + public function getQueryString(): ?string |
|
495 | + { |
|
496 | + $queryString = RequestUtils::normalizedQueryString($this->server->get('QUERY_STRING')); |
|
497 | 497 | |
498 | - return '' === $queryString ? null : $queryString; |
|
499 | - } |
|
500 | - |
|
501 | - /** |
|
502 | - * Retunrs the request body content. |
|
503 | - * |
|
504 | - * @return string |
|
505 | - */ |
|
506 | - public function getContent(): string |
|
507 | - { |
|
508 | - if (null === $this->content || false === $this->content) { |
|
509 | - $this->content = file_get_contents('php://input'); |
|
510 | - } |
|
511 | - |
|
512 | - return $this->content; |
|
513 | - } |
|
514 | - |
|
515 | - /** |
|
516 | - * Returns the path being requested relative to the executed script. |
|
517 | - * |
|
518 | - * @return string |
|
519 | - */ |
|
520 | - public function getPathInfo(): string |
|
521 | - { |
|
522 | - if (null === $this->pathInfo) { |
|
523 | - $this->pathInfo = $this->parsePathInfo(); |
|
524 | - } |
|
525 | - |
|
526 | - return $this->pathInfo; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Returns the root URL from which this request is executed. |
|
531 | - * |
|
532 | - * @return string |
|
533 | - */ |
|
534 | - public function getBaseUrl(): string |
|
535 | - { |
|
536 | - if (null === $this->baseUrl) { |
|
537 | - $this->baseUrl = $this->parseBaseUrl(); |
|
538 | - } |
|
539 | - |
|
540 | - return $this->baseUrl; |
|
541 | - } |
|
542 | - |
|
543 | - /** |
|
544 | - * Returns the requested URI. |
|
545 | - * |
|
546 | - * @return string |
|
547 | - */ |
|
548 | - public function getRequestUri(): string |
|
549 | - { |
|
550 | - if (null === $this->requestToUri) { |
|
551 | - $this->requestToUri = $this->parseRequestUri(); |
|
552 | - } |
|
553 | - |
|
554 | - return $this->requestToUri; |
|
555 | - } |
|
498 | + return '' === $queryString ? null : $queryString; |
|
499 | + } |
|
500 | + |
|
501 | + /** |
|
502 | + * Retunrs the request body content. |
|
503 | + * |
|
504 | + * @return string |
|
505 | + */ |
|
506 | + public function getContent(): string |
|
507 | + { |
|
508 | + if (null === $this->content || false === $this->content) { |
|
509 | + $this->content = file_get_contents('php://input'); |
|
510 | + } |
|
511 | + |
|
512 | + return $this->content; |
|
513 | + } |
|
514 | + |
|
515 | + /** |
|
516 | + * Returns the path being requested relative to the executed script. |
|
517 | + * |
|
518 | + * @return string |
|
519 | + */ |
|
520 | + public function getPathInfo(): string |
|
521 | + { |
|
522 | + if (null === $this->pathInfo) { |
|
523 | + $this->pathInfo = $this->parsePathInfo(); |
|
524 | + } |
|
525 | + |
|
526 | + return $this->pathInfo; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Returns the root URL from which this request is executed. |
|
531 | + * |
|
532 | + * @return string |
|
533 | + */ |
|
534 | + public function getBaseUrl(): string |
|
535 | + { |
|
536 | + if (null === $this->baseUrl) { |
|
537 | + $this->baseUrl = $this->parseBaseUrl(); |
|
538 | + } |
|
539 | + |
|
540 | + return $this->baseUrl; |
|
541 | + } |
|
542 | + |
|
543 | + /** |
|
544 | + * Returns the requested URI. |
|
545 | + * |
|
546 | + * @return string |
|
547 | + */ |
|
548 | + public function getRequestUri(): string |
|
549 | + { |
|
550 | + if (null === $this->requestToUri) { |
|
551 | + $this->requestToUri = $this->parseRequestUri(); |
|
552 | + } |
|
553 | + |
|
554 | + return $this->requestToUri; |
|
555 | + } |
|
556 | 556 | |
557 | - /** |
|
558 | - * Generates a normalized URI (URL) for the Request. |
|
559 | - * |
|
560 | - * @return string |
|
561 | - */ |
|
562 | - public function getUri(): string |
|
563 | - { |
|
564 | - if (null !== $query = $this->getQueryString()) { |
|
565 | - $query = '?'.$query; |
|
566 | - } |
|
557 | + /** |
|
558 | + * Generates a normalized URI (URL) for the Request. |
|
559 | + * |
|
560 | + * @return string |
|
561 | + */ |
|
562 | + public function getUri(): string |
|
563 | + { |
|
564 | + if (null !== $query = $this->getQueryString()) { |
|
565 | + $query = '?'.$query; |
|
566 | + } |
|
567 | 567 | |
568 | - return $this->getSchemeWithHttpHost().$this->getBaseUrl().$this->getPathInfo().$query; |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Get the root URL for the application. |
|
573 | - * |
|
574 | - * @return string |
|
575 | - */ |
|
576 | - public function root(): string |
|
577 | - { |
|
578 | - return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
579 | - } |
|
580 | - |
|
581 | - /** |
|
582 | - * Get the URL for the request. |
|
583 | - * |
|
584 | - * @return string |
|
585 | - */ |
|
586 | - public function url(): string |
|
587 | - { |
|
588 | - // Changed $this->path() for $this->getUri() |
|
589 | - return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * Returns the referer. |
|
594 | - * |
|
595 | - * @param string $default |
|
596 | - * |
|
597 | - * @return string |
|
598 | - */ |
|
599 | - public function referer(string $default = ''): string |
|
600 | - { |
|
601 | - return $this->server->get('HTTP_REFERER', $default); |
|
602 | - } |
|
568 | + return $this->getSchemeWithHttpHost().$this->getBaseUrl().$this->getPathInfo().$query; |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Get the root URL for the application. |
|
573 | + * |
|
574 | + * @return string |
|
575 | + */ |
|
576 | + public function root(): string |
|
577 | + { |
|
578 | + return rtrim($this->getSchemeWithHttpHost().$this->getBaseUrl(), '/'); |
|
579 | + } |
|
580 | + |
|
581 | + /** |
|
582 | + * Get the URL for the request. |
|
583 | + * |
|
584 | + * @return string |
|
585 | + */ |
|
586 | + public function url(): string |
|
587 | + { |
|
588 | + // Changed $this->path() for $this->getUri() |
|
589 | + return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/'); |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * Returns the referer. |
|
594 | + * |
|
595 | + * @param string $default |
|
596 | + * |
|
597 | + * @return string |
|
598 | + */ |
|
599 | + public function referer(string $default = ''): string |
|
600 | + { |
|
601 | + return $this->server->get('HTTP_REFERER', $default); |
|
602 | + } |
|
603 | 603 | |
604 | - /** |
|
605 | - * Attempts to detect if the current connection is secure through |
|
606 | - * over HTTPS protocol. |
|
607 | - * |
|
608 | - * @return bool |
|
609 | - */ |
|
610 | - public function secure(): bool |
|
611 | - { |
|
612 | - if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
613 | - return true; |
|
614 | - } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
615 | - return true; |
|
616 | - } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
617 | - return true; |
|
618 | - } |
|
619 | - |
|
620 | - return false; |
|
621 | - } |
|
622 | - |
|
623 | - /** |
|
624 | - * Returns the user agent. |
|
625 | - * |
|
626 | - * @param string|null $default |
|
627 | - * |
|
628 | - * @return string |
|
629 | - */ |
|
630 | - public function userAgent(string $default = null): string |
|
631 | - { |
|
632 | - return $this->server->get('HTTP_USER_AGENT', $default); |
|
633 | - } |
|
604 | + /** |
|
605 | + * Attempts to detect if the current connection is secure through |
|
606 | + * over HTTPS protocol. |
|
607 | + * |
|
608 | + * @return bool |
|
609 | + */ |
|
610 | + public function secure(): bool |
|
611 | + { |
|
612 | + if ( ! empty($this->server->get('HTTPS')) && strtolower($this->server->get('HTTPS')) !== 'off') { |
|
613 | + return true; |
|
614 | + } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $this->server->get('HTTP_X_FORWARDED_PROTO') === 'https') { |
|
615 | + return true; |
|
616 | + } elseif ( ! empty($this->server->get('HTTP_FRONT_END_HTTPS')) && strtolower($this->server->get('HTTP_FRONT_END_HTTPS')) !== 'off') { |
|
617 | + return true; |
|
618 | + } |
|
619 | + |
|
620 | + return false; |
|
621 | + } |
|
622 | + |
|
623 | + /** |
|
624 | + * Returns the user agent. |
|
625 | + * |
|
626 | + * @param string|null $default |
|
627 | + * |
|
628 | + * @return string |
|
629 | + */ |
|
630 | + public function userAgent(string $default = null): string |
|
631 | + { |
|
632 | + return $this->server->get('HTTP_USER_AGENT', $default); |
|
633 | + } |
|
634 | 634 | |
635 | - /** |
|
636 | - * Get the client IP address. |
|
637 | - * |
|
638 | - * @return string|null |
|
639 | - */ |
|
640 | - public function ip(): ?string |
|
641 | - { |
|
642 | - return $this->clientIp->getClientIp(); |
|
643 | - } |
|
644 | - |
|
645 | - /** |
|
646 | - * Get the route resolver callback. |
|
647 | - * |
|
648 | - * @return \Closure |
|
649 | - */ |
|
650 | - public function getRouteResolver(): Closure |
|
651 | - { |
|
652 | - return $this->routeResolver ?: function () { |
|
653 | - // |
|
654 | - }; |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * Set the route resolver callback. |
|
659 | - * |
|
660 | - * @param \Closure $callback |
|
661 | - * |
|
662 | - * @return static |
|
663 | - */ |
|
664 | - public function setRouteResolver(Closure $callback): static |
|
665 | - { |
|
666 | - $this->routeResolver = $callback; |
|
667 | - |
|
668 | - return $this; |
|
669 | - } |
|
635 | + /** |
|
636 | + * Get the client IP address. |
|
637 | + * |
|
638 | + * @return string|null |
|
639 | + */ |
|
640 | + public function ip(): ?string |
|
641 | + { |
|
642 | + return $this->clientIp->getClientIp(); |
|
643 | + } |
|
644 | + |
|
645 | + /** |
|
646 | + * Get the route resolver callback. |
|
647 | + * |
|
648 | + * @return \Closure |
|
649 | + */ |
|
650 | + public function getRouteResolver(): Closure |
|
651 | + { |
|
652 | + return $this->routeResolver ?: function () { |
|
653 | + // |
|
654 | + }; |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * Set the route resolver callback. |
|
659 | + * |
|
660 | + * @param \Closure $callback |
|
661 | + * |
|
662 | + * @return static |
|
663 | + */ |
|
664 | + public function setRouteResolver(Closure $callback): static |
|
665 | + { |
|
666 | + $this->routeResolver = $callback; |
|
667 | + |
|
668 | + return $this; |
|
669 | + } |
|
670 | 670 | |
671 | - /** |
|
672 | - * Magic method. |
|
673 | - * |
|
674 | - * Check if an input element is set on the request. |
|
675 | - * |
|
676 | - * @param string $key |
|
677 | - * |
|
678 | - * @return bool |
|
679 | - */ |
|
680 | - public function __isset($key) |
|
681 | - { |
|
682 | - return ! is_null($this->__get($key)); |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * Magic method. |
|
687 | - * |
|
688 | - * Get an element from the request. |
|
689 | - * |
|
690 | - * @return string[] |
|
691 | - */ |
|
692 | - public function __get($key) |
|
693 | - { |
|
694 | - return Arr::get($this->all(), $key, fn () => $this->route($key)); |
|
695 | - } |
|
696 | - |
|
697 | - /** |
|
698 | - * Magic method. |
|
699 | - * |
|
700 | - * Returns the Request as an HTTP string. |
|
701 | - * |
|
702 | - * @return string |
|
703 | - */ |
|
704 | - public function __toString(): string |
|
705 | - { |
|
706 | - $content = $this->getContent(); |
|
707 | - |
|
708 | - $cookieHeader = ''; |
|
709 | - $cookies = []; |
|
710 | - |
|
711 | - foreach ($this->cookies as $key => $value) { |
|
712 | - $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
713 | - } |
|
714 | - |
|
715 | - if ($cookies) { |
|
716 | - $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
717 | - } |
|
671 | + /** |
|
672 | + * Magic method. |
|
673 | + * |
|
674 | + * Check if an input element is set on the request. |
|
675 | + * |
|
676 | + * @param string $key |
|
677 | + * |
|
678 | + * @return bool |
|
679 | + */ |
|
680 | + public function __isset($key) |
|
681 | + { |
|
682 | + return ! is_null($this->__get($key)); |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * Magic method. |
|
687 | + * |
|
688 | + * Get an element from the request. |
|
689 | + * |
|
690 | + * @return string[] |
|
691 | + */ |
|
692 | + public function __get($key) |
|
693 | + { |
|
694 | + return Arr::get($this->all(), $key, fn () => $this->route($key)); |
|
695 | + } |
|
696 | + |
|
697 | + /** |
|
698 | + * Magic method. |
|
699 | + * |
|
700 | + * Returns the Request as an HTTP string. |
|
701 | + * |
|
702 | + * @return string |
|
703 | + */ |
|
704 | + public function __toString(): string |
|
705 | + { |
|
706 | + $content = $this->getContent(); |
|
707 | + |
|
708 | + $cookieHeader = ''; |
|
709 | + $cookies = []; |
|
710 | + |
|
711 | + foreach ($this->cookies as $key => $value) { |
|
712 | + $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
713 | + } |
|
714 | + |
|
715 | + if ($cookies) { |
|
716 | + $cookieHeader = 'Cookie: '.implode('; ', $cookies)."\r\n"; |
|
717 | + } |
|
718 | 718 | |
719 | - return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
720 | - $this->headers. |
|
721 | - $cookieHeader."\r\n". |
|
722 | - $content; |
|
723 | - } |
|
724 | - |
|
725 | - /** |
|
726 | - * Magic method. |
|
727 | - * |
|
728 | - * Clones the current request. |
|
729 | - * |
|
730 | - * @return void |
|
731 | - */ |
|
732 | - public function __clone() |
|
733 | - { |
|
734 | - $this->query = clone $this->query; |
|
735 | - $this->request = clone $this->request; |
|
736 | - $this->attributes = clone $this->attributes; |
|
737 | - $this->cookies = clone $this->cookies; |
|
738 | - $this->files = clone $this->files; |
|
739 | - $this->server = clone $this->server; |
|
740 | - $this->headers = clone $this->headers; |
|
741 | - } |
|
719 | + return sprintf('%s %s %s', $this->getMethod(), $this->getRequestUri(), $this->server->get('SERVER_PROTOCOL'))."\r\n". |
|
720 | + $this->headers. |
|
721 | + $cookieHeader."\r\n". |
|
722 | + $content; |
|
723 | + } |
|
724 | + |
|
725 | + /** |
|
726 | + * Magic method. |
|
727 | + * |
|
728 | + * Clones the current request. |
|
729 | + * |
|
730 | + * @return void |
|
731 | + */ |
|
732 | + public function __clone() |
|
733 | + { |
|
734 | + $this->query = clone $this->query; |
|
735 | + $this->request = clone $this->request; |
|
736 | + $this->attributes = clone $this->attributes; |
|
737 | + $this->cookies = clone $this->cookies; |
|
738 | + $this->files = clone $this->files; |
|
739 | + $this->server = clone $this->server; |
|
740 | + $this->headers = clone $this->headers; |
|
741 | + } |
|
742 | 742 | } |
743 | 743 | \ No newline at end of file |
@@ -649,7 +649,7 @@ discard block |
||
649 | 649 | */ |
650 | 650 | public function getRouteResolver(): Closure |
651 | 651 | { |
652 | - return $this->routeResolver ?: function () { |
|
652 | + return $this->routeResolver ?: function() { |
|
653 | 653 | // |
654 | 654 | }; |
655 | 655 | } |
@@ -709,7 +709,7 @@ discard block |
||
709 | 709 | $cookies = []; |
710 | 710 | |
711 | 711 | foreach ($this->cookies as $key => $value) { |
712 | - $cookies[]= is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
712 | + $cookies[] = is_array($value) ? http_build_query([$key => $value], '', '; ', PHP_QUERY_RFC3986) : "$key=$value"; |
|
713 | 713 | } |
714 | 714 | |
715 | 715 | if ($cookies) { |
@@ -41,7 +41,7 @@ |
||
41 | 41 | */ |
42 | 42 | public function render($exception): string |
43 | 43 | { |
44 | - return take(new GDebug, function ($debug) { |
|
44 | + return take(new GDebug, function($debug) { |
|
45 | 45 | |
46 | 46 | $debug->appendHandler($this->DebugHandler()); |
47 | 47 |
@@ -143,7 +143,7 @@ |
||
143 | 143 | $seconds = (int) ($duration - $hours * 60 * 60 - $minutes * 60); |
144 | 144 | |
145 | 145 | if ($seconds <= 0) { |
146 | - return ' ms'; |
|
146 | + return ' ms'; |
|
147 | 147 | } elseif ($seconds > 0) { |
148 | 148 | return ' s'; |
149 | 149 | } |