@@ -246,9 +246,9 @@ discard block |
||
246 | 246 | protected function buildNotInstantiable(string $class) |
247 | 247 | { |
248 | 248 | if ( ! empty($this->buildStack)) { |
249 | - $reset = implode(', ', $this->buildStack); |
|
249 | + $reset = implode(', ', $this->buildStack); |
|
250 | 250 | |
251 | - $message = "Target [ {$class} ] is not instantiable while building [ {$reset} ]."; |
|
251 | + $message = "Target [ {$class} ] is not instantiable while building [ {$reset} ]."; |
|
252 | 252 | } else { |
253 | 253 | $message = "Target [ {$class} ] is not instantiable."; |
254 | 254 | } |
@@ -679,7 +679,7 @@ discard block |
||
679 | 679 | |
680 | 680 | array_pop($this->across); |
681 | 681 | |
682 | - return $this->resolveObject($id, $object); |
|
682 | + return $this->resolveObject($id, $object); |
|
683 | 683 | } |
684 | 684 | |
685 | 685 | /** |
@@ -184,7 +184,7 @@ discard block |
||
184 | 184 | */ |
185 | 185 | protected function getClosure($id, string $value) |
186 | 186 | { |
187 | - return function ($container, $parameters = []) use ($id, $value) { |
|
187 | + return function($container, $parameters = []) use ($id, $value) { |
|
188 | 188 | if ($id == $value) { |
189 | 189 | return $container->build($value); |
190 | 190 | } |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | */ |
423 | 423 | public function factory($id) |
424 | 424 | { |
425 | - return function () use ($id) { |
|
425 | + return function() use ($id) { |
|
426 | 426 | return $this->make($id); |
427 | 427 | }; |
428 | 428 | } |
@@ -810,7 +810,7 @@ discard block |
||
810 | 810 | */ |
811 | 811 | public function bound($id) |
812 | 812 | { |
813 | - return isset($this->bindings[$id]) || |
|
813 | + return isset($this->bindings[$id]) || |
|
814 | 814 | isset($this->instances[$id]) || |
815 | 815 | $this->isAlias($id); |
816 | 816 | } |
@@ -151,8 +151,8 @@ discard block |
||
151 | 151 | */ |
152 | 152 | protected function call() |
153 | 153 | { |
154 | - return function ($stack, $pipe) { |
|
155 | - return function ($passable) use ($stack, $pipe) { |
|
154 | + return function($stack, $pipe) { |
|
155 | + return function($passable) use ($stack, $pipe) { |
|
156 | 156 | try { |
157 | 157 | if (is_callable($pipe)) { |
158 | 158 | return $pipe($passable, $stack); |
@@ -205,10 +205,10 @@ discard block |
||
205 | 205 | */ |
206 | 206 | protected function prepareDestination(Closure $destination) |
207 | 207 | { |
208 | - return function ($passable) use ($destination) { |
|
208 | + return function($passable) use ($destination) { |
|
209 | 209 | try { |
210 | 210 | return $destination($passable); |
211 | - } catch(Throwable $e) { |
|
211 | + } catch (Throwable $e) { |
|
212 | 212 | return $this->handleException($passable, $e); |
213 | 213 | } |
214 | 214 | }; |
@@ -99,13 +99,13 @@ |
||
99 | 99 | } |
100 | 100 | |
101 | 101 | /** |
102 | - * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
103 | - * Can redirect via a Location header. |
|
104 | - * |
|
105 | - * @param string $url The url |
|
106 | - * |
|
107 | - * @return $this |
|
108 | - */ |
|
102 | + * Redirects to another url. Sets the redirect header, sends the headers and exits. |
|
103 | + * Can redirect via a Location header. |
|
104 | + * |
|
105 | + * @param string $url The url |
|
106 | + * |
|
107 | + * @return $this |
|
108 | + */ |
|
109 | 109 | public function setTargetUrl($url) |
110 | 110 | { |
111 | 111 | if ('' === ($url ?? '')) { |
@@ -39,297 +39,297 @@ |
||
39 | 39 | */ |
40 | 40 | class Response extends Status |
41 | 41 | { |
42 | - use ResponseTrait; |
|
43 | - |
|
44 | - /** |
|
45 | - * Sets up the response with a content and a status code. |
|
46 | - * |
|
47 | - * @param mixed $content The response content |
|
48 | - * @param int $status The response status (200 by default) |
|
49 | - * @param array $headers Array of HTTP headers for this response |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function __construct($content = '', int $status = 200, array $headers = []) |
|
54 | - { |
|
55 | - $this->setContent($content); |
|
56 | - $this->setStatusCode($status); |
|
42 | + use ResponseTrait; |
|
43 | + |
|
44 | + /** |
|
45 | + * Sets up the response with a content and a status code. |
|
46 | + * |
|
47 | + * @param mixed $content The response content |
|
48 | + * @param int $status The response status (200 by default) |
|
49 | + * @param array $headers Array of HTTP headers for this response |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function __construct($content = '', int $status = 200, array $headers = []) |
|
54 | + { |
|
55 | + $this->setContent($content); |
|
56 | + $this->setStatusCode($status); |
|
57 | 57 | |
58 | - $this->server = new Server($_SERVER); |
|
59 | - $this->headers = new Headers($headers); |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * Creates an instance of the same response class for rendering contents to the content, |
|
64 | - * status code and headers. |
|
65 | - * |
|
66 | - * @param mixed $content The response content |
|
67 | - * @param int $status The HTTP response status for this response (200 by default) |
|
68 | - * @param array $headers Array of HTTP headers for this response |
|
69 | - * |
|
70 | - * @return static |
|
71 | - */ |
|
72 | - public static function render($content = '', $status = 200, $headers = []) |
|
73 | - { |
|
74 | - return new static($content, $status, $headers); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Gets the current response content. |
|
79 | - * |
|
80 | - * @return string |
|
81 | - */ |
|
82 | - public function getContent() |
|
83 | - { |
|
84 | - return $this->content; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Gets the response status code. |
|
89 | - * |
|
90 | - * The status code is a 3-digit code to specify server response results to the browser. |
|
91 | - * |
|
92 | - * @return int |
|
93 | - * |
|
94 | - * @throws \BadMethodCallException |
|
95 | - */ |
|
96 | - public function getStatusCode() |
|
97 | - { |
|
98 | - if (empty($this->status)) { |
|
99 | - throw new BadMethodCallException('HTTP Response is missing a status code.'); |
|
100 | - } |
|
101 | - |
|
102 | - return $this->status; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Sends the headers if they haven't already been sent. |
|
107 | - * Returns whether they were sent or not. |
|
108 | - * |
|
109 | - * @return bool |
|
110 | - * |
|
111 | - * @uses \Syscodes\Http\Http |
|
112 | - */ |
|
113 | - public function sendHeaders() |
|
114 | - { |
|
115 | - // Have the headers already been sent? |
|
116 | - if (headers_sent()) { |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - // Headers |
|
121 | - foreach ($this->headers->allPreserveCase() as $name => $values) { |
|
122 | - $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
123 | - |
|
124 | - foreach ($values as $value) { |
|
125 | - header($name.': '. $value, $replace, $this->status); |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - // Status |
|
130 | - if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
131 | - // Send the protocol/status line first, FCGI servers need different status header |
|
132 | - header(sprintf('Status: %s %s', $this->status, $this->statusText)); |
|
133 | - } else { |
|
134 | - $this->protocol = (string) $this->server->get('SERVER_PROTOCOL') ?: 'HTTP/1.1'; |
|
135 | - header(sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText), true, $this->status); |
|
136 | - } |
|
137 | - |
|
138 | - return $this; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Sends content for the current web response. |
|
143 | - * |
|
144 | - * @return $this |
|
145 | - */ |
|
146 | - public function sendContent() |
|
147 | - { |
|
148 | - echo $this->content; |
|
149 | - |
|
150 | - return $this; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sends the response to the output buffer. Optionally, headers will be sent. |
|
155 | - * |
|
156 | - * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
157 | - * |
|
158 | - * @return $this |
|
159 | - */ |
|
160 | - public function send($sendHeader = false) |
|
161 | - { |
|
162 | - if ($sendHeader) { |
|
163 | - $this->sendHeaders(); |
|
164 | - } |
|
165 | - |
|
166 | - if (null !== $this->content) { |
|
167 | - $this->sendContent(); |
|
168 | - } |
|
169 | - |
|
170 | - return $this; |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * Sends the content of the message to the browser. |
|
175 | - * |
|
176 | - * @param mixed $content The response content |
|
177 | - * |
|
178 | - * @return $this |
|
179 | - */ |
|
180 | - public function setContent($content) |
|
181 | - { |
|
182 | - if ($content !== null && ! is_string($content) && ! is_numeric($content) && ! is_callable([$content, '__toString'])) { |
|
183 | - throw new UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content))); |
|
184 | - } |
|
185 | - |
|
186 | - if ($content instanceof JsonSerializable || is_array($content)) { |
|
187 | - $this->header('Content-Type', 'application/json'); |
|
188 | - |
|
189 | - $content = json_encode($content); |
|
190 | - } elseif ($content instanceof Renderable) { |
|
191 | - $content = $content->render(); |
|
192 | - } |
|
58 | + $this->server = new Server($_SERVER); |
|
59 | + $this->headers = new Headers($headers); |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * Creates an instance of the same response class for rendering contents to the content, |
|
64 | + * status code and headers. |
|
65 | + * |
|
66 | + * @param mixed $content The response content |
|
67 | + * @param int $status The HTTP response status for this response (200 by default) |
|
68 | + * @param array $headers Array of HTTP headers for this response |
|
69 | + * |
|
70 | + * @return static |
|
71 | + */ |
|
72 | + public static function render($content = '', $status = 200, $headers = []) |
|
73 | + { |
|
74 | + return new static($content, $status, $headers); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Gets the current response content. |
|
79 | + * |
|
80 | + * @return string |
|
81 | + */ |
|
82 | + public function getContent() |
|
83 | + { |
|
84 | + return $this->content; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Gets the response status code. |
|
89 | + * |
|
90 | + * The status code is a 3-digit code to specify server response results to the browser. |
|
91 | + * |
|
92 | + * @return int |
|
93 | + * |
|
94 | + * @throws \BadMethodCallException |
|
95 | + */ |
|
96 | + public function getStatusCode() |
|
97 | + { |
|
98 | + if (empty($this->status)) { |
|
99 | + throw new BadMethodCallException('HTTP Response is missing a status code.'); |
|
100 | + } |
|
101 | + |
|
102 | + return $this->status; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Sends the headers if they haven't already been sent. |
|
107 | + * Returns whether they were sent or not. |
|
108 | + * |
|
109 | + * @return bool |
|
110 | + * |
|
111 | + * @uses \Syscodes\Http\Http |
|
112 | + */ |
|
113 | + public function sendHeaders() |
|
114 | + { |
|
115 | + // Have the headers already been sent? |
|
116 | + if (headers_sent()) { |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + // Headers |
|
121 | + foreach ($this->headers->allPreserveCase() as $name => $values) { |
|
122 | + $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
123 | + |
|
124 | + foreach ($values as $value) { |
|
125 | + header($name.': '. $value, $replace, $this->status); |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + // Status |
|
130 | + if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
131 | + // Send the protocol/status line first, FCGI servers need different status header |
|
132 | + header(sprintf('Status: %s %s', $this->status, $this->statusText)); |
|
133 | + } else { |
|
134 | + $this->protocol = (string) $this->server->get('SERVER_PROTOCOL') ?: 'HTTP/1.1'; |
|
135 | + header(sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText), true, $this->status); |
|
136 | + } |
|
137 | + |
|
138 | + return $this; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Sends content for the current web response. |
|
143 | + * |
|
144 | + * @return $this |
|
145 | + */ |
|
146 | + public function sendContent() |
|
147 | + { |
|
148 | + echo $this->content; |
|
149 | + |
|
150 | + return $this; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sends the response to the output buffer. Optionally, headers will be sent. |
|
155 | + * |
|
156 | + * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
157 | + * |
|
158 | + * @return $this |
|
159 | + */ |
|
160 | + public function send($sendHeader = false) |
|
161 | + { |
|
162 | + if ($sendHeader) { |
|
163 | + $this->sendHeaders(); |
|
164 | + } |
|
165 | + |
|
166 | + if (null !== $this->content) { |
|
167 | + $this->sendContent(); |
|
168 | + } |
|
169 | + |
|
170 | + return $this; |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * Sends the content of the message to the browser. |
|
175 | + * |
|
176 | + * @param mixed $content The response content |
|
177 | + * |
|
178 | + * @return $this |
|
179 | + */ |
|
180 | + public function setContent($content) |
|
181 | + { |
|
182 | + if ($content !== null && ! is_string($content) && ! is_numeric($content) && ! is_callable([$content, '__toString'])) { |
|
183 | + throw new UnexpectedValueException(sprintf('The Response content must be a string or object implementing __toString(), "%s" given.', gettype($content))); |
|
184 | + } |
|
185 | + |
|
186 | + if ($content instanceof JsonSerializable || is_array($content)) { |
|
187 | + $this->header('Content-Type', 'application/json'); |
|
188 | + |
|
189 | + $content = json_encode($content); |
|
190 | + } elseif ($content instanceof Renderable) { |
|
191 | + $content = $content->render(); |
|
192 | + } |
|
193 | 193 | |
194 | - $this->content = $content ?? ''; |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Prepares the Response before it is sent to the client. |
|
201 | - * |
|
202 | - * @param \Syscodes\Http\Request $request |
|
203 | - * |
|
204 | - * @return $this |
|
205 | - */ |
|
206 | - public function prepare($request) |
|
207 | - { |
|
208 | - $headers = $this->headers; |
|
209 | - |
|
210 | - if ($this->isInformational() || $this->isEmpty()) { |
|
211 | - $this->setContent(null); |
|
212 | - $headers->remove('Content-Type'); |
|
213 | - $headers->remove('Content-Length'); |
|
214 | - } |
|
215 | - |
|
216 | - return $this; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Sets the response status code. |
|
221 | - * |
|
222 | - * @param int $code The status code |
|
223 | - * @param string|null $text The status text |
|
224 | - * |
|
225 | - * @return $this |
|
226 | - * |
|
227 | - * @throws \InvalidArgumentException |
|
228 | - */ |
|
229 | - public function setStatusCode(int $code, $text = null) |
|
230 | - { |
|
231 | - $this->status = $code; |
|
232 | - |
|
233 | - // Valid range? |
|
234 | - if ($this->isInvalid()) { |
|
235 | - throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
236 | - } |
|
237 | - |
|
238 | - // Check if you have an accepted status code if not shows to a message of unknown status |
|
239 | - if (null === $text) { |
|
240 | - $this->statusText = isset($this->statusCodes[$code]) ? $this->statusCodes[$code] : __('response.UnknownStatus'); |
|
241 | - |
|
242 | - return $this; |
|
243 | - } |
|
244 | - |
|
245 | - if (false === $text) { |
|
246 | - $this->statusText = ''; |
|
247 | - |
|
248 | - return $this; |
|
249 | - } |
|
250 | - |
|
251 | - $this->statusText = $text; |
|
252 | - |
|
253 | - return $this; |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Is response invalid? |
|
258 | - * |
|
259 | - * @final |
|
260 | - * |
|
261 | - * @return void |
|
262 | - */ |
|
263 | - public function isInvalid(): bool |
|
264 | - { |
|
265 | - return $this->status < 100 || $this->status >= 600; |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * Is response informative? |
|
270 | - * |
|
271 | - * @final |
|
272 | - * |
|
273 | - * @return void |
|
274 | - */ |
|
275 | - public function isInformational() |
|
276 | - { |
|
277 | - return $this->status >= 100 && $this->status < 200; |
|
278 | - } |
|
194 | + $this->content = $content ?? ''; |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Prepares the Response before it is sent to the client. |
|
201 | + * |
|
202 | + * @param \Syscodes\Http\Request $request |
|
203 | + * |
|
204 | + * @return $this |
|
205 | + */ |
|
206 | + public function prepare($request) |
|
207 | + { |
|
208 | + $headers = $this->headers; |
|
209 | + |
|
210 | + if ($this->isInformational() || $this->isEmpty()) { |
|
211 | + $this->setContent(null); |
|
212 | + $headers->remove('Content-Type'); |
|
213 | + $headers->remove('Content-Length'); |
|
214 | + } |
|
215 | + |
|
216 | + return $this; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Sets the response status code. |
|
221 | + * |
|
222 | + * @param int $code The status code |
|
223 | + * @param string|null $text The status text |
|
224 | + * |
|
225 | + * @return $this |
|
226 | + * |
|
227 | + * @throws \InvalidArgumentException |
|
228 | + */ |
|
229 | + public function setStatusCode(int $code, $text = null) |
|
230 | + { |
|
231 | + $this->status = $code; |
|
232 | + |
|
233 | + // Valid range? |
|
234 | + if ($this->isInvalid()) { |
|
235 | + throw new InvalidArgumentException(__('response.statusCodeNotValid', ['code' => $code])); |
|
236 | + } |
|
237 | + |
|
238 | + // Check if you have an accepted status code if not shows to a message of unknown status |
|
239 | + if (null === $text) { |
|
240 | + $this->statusText = isset($this->statusCodes[$code]) ? $this->statusCodes[$code] : __('response.UnknownStatus'); |
|
241 | + |
|
242 | + return $this; |
|
243 | + } |
|
244 | + |
|
245 | + if (false === $text) { |
|
246 | + $this->statusText = ''; |
|
247 | + |
|
248 | + return $this; |
|
249 | + } |
|
250 | + |
|
251 | + $this->statusText = $text; |
|
252 | + |
|
253 | + return $this; |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Is response invalid? |
|
258 | + * |
|
259 | + * @final |
|
260 | + * |
|
261 | + * @return void |
|
262 | + */ |
|
263 | + public function isInvalid(): bool |
|
264 | + { |
|
265 | + return $this->status < 100 || $this->status >= 600; |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * Is response informative? |
|
270 | + * |
|
271 | + * @final |
|
272 | + * |
|
273 | + * @return void |
|
274 | + */ |
|
275 | + public function isInformational() |
|
276 | + { |
|
277 | + return $this->status >= 100 && $this->status < 200; |
|
278 | + } |
|
279 | 279 | |
280 | - /** |
|
281 | - * Is the response a redirect? |
|
282 | - * |
|
283 | - * @final |
|
284 | - * |
|
285 | - * @return void |
|
286 | - */ |
|
287 | - public function isRedirection() |
|
288 | - { |
|
289 | - return $this->status >= 300 && $this->status < 400; |
|
290 | - } |
|
280 | + /** |
|
281 | + * Is the response a redirect? |
|
282 | + * |
|
283 | + * @final |
|
284 | + * |
|
285 | + * @return void |
|
286 | + */ |
|
287 | + public function isRedirection() |
|
288 | + { |
|
289 | + return $this->status >= 300 && $this->status < 400; |
|
290 | + } |
|
291 | 291 | |
292 | - /** |
|
293 | - * Is the response empty? |
|
294 | - * |
|
295 | - * @final |
|
296 | - * |
|
297 | - * @return void |
|
298 | - */ |
|
299 | - public function isEmpty() |
|
300 | - { |
|
301 | - return in_array($this->status, [204, 304]); |
|
302 | - } |
|
292 | + /** |
|
293 | + * Is the response empty? |
|
294 | + * |
|
295 | + * @final |
|
296 | + * |
|
297 | + * @return void |
|
298 | + */ |
|
299 | + public function isEmpty() |
|
300 | + { |
|
301 | + return in_array($this->status, [204, 304]); |
|
302 | + } |
|
303 | 303 | |
304 | - /** |
|
305 | - * Is the response a redirect of some form? |
|
306 | - * |
|
307 | - * @return bool |
|
308 | - */ |
|
309 | - public function isRedirect() |
|
310 | - { |
|
311 | - return in_array($this->status, [301, 302, 303, 307, 308]); |
|
312 | - } |
|
304 | + /** |
|
305 | + * Is the response a redirect of some form? |
|
306 | + * |
|
307 | + * @return bool |
|
308 | + */ |
|
309 | + public function isRedirect() |
|
310 | + { |
|
311 | + return in_array($this->status, [301, 302, 303, 307, 308]); |
|
312 | + } |
|
313 | 313 | |
314 | - /** |
|
315 | - * Returns the Response as an HTTP string. |
|
316 | - * |
|
317 | - * @return string |
|
318 | - */ |
|
319 | - public function __toString() |
|
320 | - { |
|
321 | - return sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText)."\r\n". |
|
322 | - $this->headers."\r\n". |
|
323 | - $this->getContent(); |
|
324 | - } |
|
314 | + /** |
|
315 | + * Returns the Response as an HTTP string. |
|
316 | + * |
|
317 | + * @return string |
|
318 | + */ |
|
319 | + public function __toString() |
|
320 | + { |
|
321 | + return sprintf('%s %s %s', $this->protocol, $this->status, $this->statusText)."\r\n". |
|
322 | + $this->headers."\r\n". |
|
323 | + $this->getContent(); |
|
324 | + } |
|
325 | 325 | |
326 | - /** |
|
327 | - * Clone the current Response instance. |
|
328 | - * |
|
329 | - * @return void |
|
330 | - */ |
|
331 | - public function __clone() |
|
332 | - { |
|
333 | - $this->headers = clone $this->headers; |
|
334 | - } |
|
326 | + /** |
|
327 | + * Clone the current Response instance. |
|
328 | + * |
|
329 | + * @return void |
|
330 | + */ |
|
331 | + public function __clone() |
|
332 | + { |
|
333 | + $this->headers = clone $this->headers; |
|
334 | + } |
|
335 | 335 | } |
336 | 336 | \ No newline at end of file |
@@ -122,7 +122,7 @@ |
||
122 | 122 | $replace = 0 === strcasecmp($name, 'Content-Type'); |
123 | 123 | |
124 | 124 | foreach ($values as $value) { |
125 | - header($name.': '. $value, $replace, $this->status); |
|
125 | + header($name.': '.$value, $replace, $this->status); |
|
126 | 126 | } |
127 | 127 | } |
128 | 128 |
@@ -31,275 +31,275 @@ |
||
31 | 31 | */ |
32 | 32 | class Http |
33 | 33 | { |
34 | - /** |
|
35 | - * Determines if this request was made from the command line (CLI). |
|
36 | - * |
|
37 | - * @return bool |
|
38 | - */ |
|
39 | - public function isCli() |
|
40 | - { |
|
41 | - return (PHP_SAPI === 'cli' || defined('STDIN')); |
|
42 | - } |
|
43 | - |
|
44 | - /** |
|
45 | - * Return's the protocol that the request was made with. |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function protocol() |
|
50 | - { |
|
51 | - if ($this->server('HTTPS') == 'on' || |
|
52 | - $this->server('HTTPS') == 1 || |
|
53 | - $this->server('SERVER_PORT') == 443 || |
|
54 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
55 | - (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
56 | - { |
|
57 | - return 'https'; |
|
58 | - } |
|
59 | - |
|
60 | - return 'http'; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Fetch an item from the COOKIE array. |
|
65 | - * |
|
66 | - * @param string $index The index key |
|
67 | - * @param mixed $default The default value |
|
68 | - * |
|
69 | - * @return string|array |
|
70 | - */ |
|
71 | - public function cookie($index = null, $default = null) |
|
72 | - { |
|
73 | - return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Fetch an item from the FILE array. |
|
78 | - * |
|
79 | - * @param string $index The index key |
|
80 | - * @param mixed $default The default value |
|
81 | - * |
|
82 | - * @return string|array |
|
83 | - */ |
|
84 | - public function file($index = null, $default = null) |
|
85 | - { |
|
86 | - return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * Fetch an item from the SERVER array. |
|
91 | - * |
|
92 | - * @param string $index The index key |
|
93 | - * @param mixed $default The default value |
|
94 | - * |
|
95 | - * @return string|array |
|
96 | - */ |
|
97 | - public function server($index = null, $default = null) |
|
98 | - { |
|
99 | - return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
100 | - } |
|
34 | + /** |
|
35 | + * Determines if this request was made from the command line (CLI). |
|
36 | + * |
|
37 | + * @return bool |
|
38 | + */ |
|
39 | + public function isCli() |
|
40 | + { |
|
41 | + return (PHP_SAPI === 'cli' || defined('STDIN')); |
|
42 | + } |
|
43 | + |
|
44 | + /** |
|
45 | + * Return's the protocol that the request was made with. |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function protocol() |
|
50 | + { |
|
51 | + if ($this->server('HTTPS') == 'on' || |
|
52 | + $this->server('HTTPS') == 1 || |
|
53 | + $this->server('SERVER_PORT') == 443 || |
|
54 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PROTO') == 'https') || |
|
55 | + (config('security.allow-x-headers', false) && $this->server('HTTP_X_FORWARDED_PORT') == 443)) |
|
56 | + { |
|
57 | + return 'https'; |
|
58 | + } |
|
59 | + |
|
60 | + return 'http'; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Fetch an item from the COOKIE array. |
|
65 | + * |
|
66 | + * @param string $index The index key |
|
67 | + * @param mixed $default The default value |
|
68 | + * |
|
69 | + * @return string|array |
|
70 | + */ |
|
71 | + public function cookie($index = null, $default = null) |
|
72 | + { |
|
73 | + return (func_num_args() === 0) ? $_COOKIE : Arr::get($_COOKIE, strtoupper($index), $default); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Fetch an item from the FILE array. |
|
78 | + * |
|
79 | + * @param string $index The index key |
|
80 | + * @param mixed $default The default value |
|
81 | + * |
|
82 | + * @return string|array |
|
83 | + */ |
|
84 | + public function file($index = null, $default = null) |
|
85 | + { |
|
86 | + return (func_num_args() === 0) ? $_FILES : Arr::get($_FILES, strtoupper($index), $default); |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * Fetch an item from the SERVER array. |
|
91 | + * |
|
92 | + * @param string $index The index key |
|
93 | + * @param mixed $default The default value |
|
94 | + * |
|
95 | + * @return string|array |
|
96 | + */ |
|
97 | + public function server($index = null, $default = null) |
|
98 | + { |
|
99 | + return (func_num_args() === 0) ? $_SERVER : Arr::get($_SERVER, strtoupper($index), $default); |
|
100 | + } |
|
101 | 101 | |
102 | - /** |
|
103 | - * Gets the URI Protocol based setting, will attempt to detect the path |
|
104 | - * portion of the current URI. |
|
105 | - * |
|
106 | - * @param string $protocol |
|
107 | - * |
|
108 | - * @return string |
|
109 | - */ |
|
110 | - public function detectPath(string $protocol = '') |
|
111 | - { |
|
112 | - if (empty($protocol)) { |
|
113 | - $protocol = 'REQUEST_URI'; |
|
114 | - } |
|
115 | - |
|
116 | - switch($protocol) { |
|
117 | - case 'REQUEST_URI': |
|
118 | - $path = $this->parseRequestUri(); |
|
119 | - break; |
|
120 | - case 'QUERY_STRING': |
|
121 | - $path = $this->parseQueryString(); |
|
122 | - break; |
|
123 | - case 'PATH_INFO': |
|
124 | - default: |
|
125 | - $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
126 | - break; |
|
127 | - } |
|
128 | - |
|
129 | - return $path; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Filters a value from the start of a string in this case the passed URI string. |
|
134 | - * |
|
135 | - * @return string |
|
136 | - */ |
|
137 | - protected function parseRequestUri() |
|
138 | - { |
|
139 | - if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
140 | - return ''; |
|
141 | - } |
|
142 | - |
|
143 | - $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
144 | - $components = parse_url($requestURI); |
|
145 | - $query = $components['query'] ?? ''; |
|
146 | - $uri = $components['path'] ?? ''; |
|
147 | - |
|
148 | - // If the search value is at the start |
|
149 | - if (isset($this->server('SCRIPT_NAME')[0])) { |
|
150 | - if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
151 | - $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
152 | - } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
153 | - $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
154 | - } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
155 | - $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - // This section ensures that even on servers that require the URI to contain |
|
160 | - // the query string (Nginx) is the correctly |
|
161 | - if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
162 | - $query = explode('?', $query, 2); |
|
163 | - $uri = $query[0]; |
|
164 | - $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
165 | - } else { |
|
166 | - $_SERVER['QUERY_STRING'] = $query; |
|
167 | - } |
|
168 | - |
|
169 | - // Parses the string into variables |
|
170 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
171 | - |
|
172 | - if ('/' === $uri || '' === $uri) { |
|
173 | - return ''; |
|
174 | - } |
|
175 | - |
|
176 | - return $this->filterDecode($uri); |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Will parse QUERY_STRING and automatically detect the URI from it. |
|
181 | - * |
|
182 | - * @return string |
|
183 | - */ |
|
184 | - protected function parseQueryString() |
|
185 | - { |
|
186 | - $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
187 | - |
|
188 | - if (trim($uri, '/') === '') { |
|
189 | - return ''; |
|
190 | - } elseif (0 === strncmp($uri, '/', 1)) { |
|
191 | - $uri = explode('?', $uri, 2); |
|
192 | - $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
193 | - $uri = $uri[0] ?? ''; |
|
194 | - } |
|
195 | - |
|
196 | - parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
197 | - |
|
198 | - return $this->filterDecode($uri); |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
203 | - * |
|
204 | - * @param string $uri |
|
205 | - * |
|
206 | - * @return string |
|
207 | - */ |
|
208 | - protected function filterDecode($uri) |
|
209 | - { |
|
210 | - // Remove all characters except letters, |
|
211 | - // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
212 | - $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
102 | + /** |
|
103 | + * Gets the URI Protocol based setting, will attempt to detect the path |
|
104 | + * portion of the current URI. |
|
105 | + * |
|
106 | + * @param string $protocol |
|
107 | + * |
|
108 | + * @return string |
|
109 | + */ |
|
110 | + public function detectPath(string $protocol = '') |
|
111 | + { |
|
112 | + if (empty($protocol)) { |
|
113 | + $protocol = 'REQUEST_URI'; |
|
114 | + } |
|
115 | + |
|
116 | + switch($protocol) { |
|
117 | + case 'REQUEST_URI': |
|
118 | + $path = $this->parseRequestUri(); |
|
119 | + break; |
|
120 | + case 'QUERY_STRING': |
|
121 | + $path = $this->parseQueryString(); |
|
122 | + break; |
|
123 | + case 'PATH_INFO': |
|
124 | + default: |
|
125 | + $path = $this->server($protocol) ?? $this->parseRequestUri(); |
|
126 | + break; |
|
127 | + } |
|
128 | + |
|
129 | + return $path; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Filters a value from the start of a string in this case the passed URI string. |
|
134 | + * |
|
135 | + * @return string |
|
136 | + */ |
|
137 | + protected function parseRequestUri() |
|
138 | + { |
|
139 | + if ( ! isset($_SERVER['REQUEST_URI'], $_SERVER['SCRIPT_NAME'])) { |
|
140 | + return ''; |
|
141 | + } |
|
142 | + |
|
143 | + $requestURI = $this->server('REQUEST_URI') ?? '/'; |
|
144 | + $components = parse_url($requestURI); |
|
145 | + $query = $components['query'] ?? ''; |
|
146 | + $uri = $components['path'] ?? ''; |
|
147 | + |
|
148 | + // If the search value is at the start |
|
149 | + if (isset($this->server('SCRIPT_NAME')[0])) { |
|
150 | + if (0 === strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
151 | + $uri = (string) substr($uri, strlen($this->server('SCRIPT_NAME'))); |
|
152 | + } elseif (0 < strpos($uri, $this->server('SCRIPT_NAME'))) { |
|
153 | + $uri = (string) substr($uri, strpos($uri, $this->server('SCRIPT_NAME')) + strlen($this->server('SCRIPT_NAME'))); |
|
154 | + } elseif (0 === strpos($uri, dirname($this->server('SCRIPT_NAME')))) { |
|
155 | + $uri = (string) substr($uri, strlen(dirname($this->server('SCRIPT_NAME')))); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + // This section ensures that even on servers that require the URI to contain |
|
160 | + // the query string (Nginx) is the correctly |
|
161 | + if ('' === trim($uri, '/') && 0 === strncmp($query, '/', 1)) { |
|
162 | + $query = explode('?', $query, 2); |
|
163 | + $uri = $query[0]; |
|
164 | + $_SERVER['QUERY_STRING'] = $query[1] ?? ''; |
|
165 | + } else { |
|
166 | + $_SERVER['QUERY_STRING'] = $query; |
|
167 | + } |
|
168 | + |
|
169 | + // Parses the string into variables |
|
170 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
171 | + |
|
172 | + if ('/' === $uri || '' === $uri) { |
|
173 | + return ''; |
|
174 | + } |
|
175 | + |
|
176 | + return $this->filterDecode($uri); |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Will parse QUERY_STRING and automatically detect the URI from it. |
|
181 | + * |
|
182 | + * @return string |
|
183 | + */ |
|
184 | + protected function parseQueryString() |
|
185 | + { |
|
186 | + $uri = $_SERVER['QUERY_STRING'] ?? @getenv('QUERY_STRING'); |
|
187 | + |
|
188 | + if (trim($uri, '/') === '') { |
|
189 | + return ''; |
|
190 | + } elseif (0 === strncmp($uri, '/', 1)) { |
|
191 | + $uri = explode('?', $uri, 2); |
|
192 | + $_SERVER['QUERY_STRING'] = $uri[1] ?? ''; |
|
193 | + $uri = $uri[0] ?? ''; |
|
194 | + } |
|
195 | + |
|
196 | + parse_str($_SERVER['QUERY_STRING'], $_GET); |
|
197 | + |
|
198 | + return $this->filterDecode($uri); |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Filters the uri string remove any malicious characters and inappropriate slashes. |
|
203 | + * |
|
204 | + * @param string $uri |
|
205 | + * |
|
206 | + * @return string |
|
207 | + */ |
|
208 | + protected function filterDecode($uri) |
|
209 | + { |
|
210 | + // Remove all characters except letters, |
|
211 | + // digits and $-_.+!*'(),{}|\\^~[]`<>#%";/?:@&=. |
|
212 | + $uri = filter_var(rawurldecode($uri), FILTER_SANITIZE_URL); |
|
213 | 213 | |
214 | - // Return argument if not empty or return a single slash |
|
215 | - return trim($uri, '/') ?: '/'; |
|
216 | - } |
|
214 | + // Return argument if not empty or return a single slash |
|
215 | + return trim($uri, '/') ?: '/'; |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
219 | - * Parse the base URL. |
|
220 | - * |
|
221 | - * @return string |
|
222 | - */ |
|
223 | - public function parseBaseUrl() |
|
224 | - { |
|
225 | - $filename = basename($this->server('SCRIPT_FILENAME')); |
|
218 | + /** |
|
219 | + * Parse the base URL. |
|
220 | + * |
|
221 | + * @return string |
|
222 | + */ |
|
223 | + public function parseBaseUrl() |
|
224 | + { |
|
225 | + $filename = basename($this->server('SCRIPT_FILENAME')); |
|
226 | 226 | |
227 | - if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
228 | - $baseUrl = $this->server('SCRIPT_NAME'); |
|
229 | - } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
230 | - $baseUrl = $this->server('PHP_SELF'); |
|
231 | - } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
232 | - $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
233 | - } else { |
|
234 | - $path = $this->server('PHP_SELF', ''); |
|
235 | - $file = $this->server('SCRIPT_FILENAME', ''); |
|
236 | - $segs = explode('/', trim($file, '/')); |
|
237 | - $segs = array_reverse($segs); |
|
238 | - $index = 0; |
|
239 | - $last = count($segs); |
|
240 | - $baseUrl = ''; |
|
227 | + if ($filename === basename($this->server('SCRIPT_NAME'))) { |
|
228 | + $baseUrl = $this->server('SCRIPT_NAME'); |
|
229 | + } elseif ($filename === basename($this->server('PHP_SELF'))) { |
|
230 | + $baseUrl = $this->server('PHP_SELF'); |
|
231 | + } elseif ($filename === basename($this->server('ORIG_SCRIPT_NAME'))) { |
|
232 | + $baseUrl = $this->server('ORIG_SCRIPT_NAME'); |
|
233 | + } else { |
|
234 | + $path = $this->server('PHP_SELF', ''); |
|
235 | + $file = $this->server('SCRIPT_FILENAME', ''); |
|
236 | + $segs = explode('/', trim($file, '/')); |
|
237 | + $segs = array_reverse($segs); |
|
238 | + $index = 0; |
|
239 | + $last = count($segs); |
|
240 | + $baseUrl = ''; |
|
241 | 241 | |
242 | - do { |
|
243 | - $seg = $segs[$index]; |
|
244 | - $baseUrl = '/'.$seg.$baseUrl; |
|
245 | - ++$index; |
|
246 | - } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
247 | - } |
|
248 | - |
|
249 | - // Does the baseUrl have anything in common with the request_uri? |
|
250 | - $requestUri = $this->parseRequestUri(); |
|
251 | - |
|
252 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
253 | - $requestUri = '/'.$requestUri; |
|
254 | - } |
|
255 | - |
|
256 | - $baseUrl = dirname($baseUrl); |
|
257 | - |
|
258 | - if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
259 | - // no match whatsoever; set it blank |
|
260 | - return ''; |
|
261 | - } |
|
242 | + do { |
|
243 | + $seg = $segs[$index]; |
|
244 | + $baseUrl = '/'.$seg.$baseUrl; |
|
245 | + ++$index; |
|
246 | + } while ($last > $index && (false !== $pos = strpos($path, $baseUrl)) && 0 != $pos); |
|
247 | + } |
|
248 | + |
|
249 | + // Does the baseUrl have anything in common with the request_uri? |
|
250 | + $requestUri = $this->parseRequestUri(); |
|
251 | + |
|
252 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
253 | + $requestUri = '/'.$requestUri; |
|
254 | + } |
|
255 | + |
|
256 | + $baseUrl = dirname($baseUrl); |
|
257 | + |
|
258 | + if (empty($baseUrl) || false !== strpos(rawurldecode($requestUri), $baseUrl)) { |
|
259 | + // no match whatsoever; set it blank |
|
260 | + return ''; |
|
261 | + } |
|
262 | 262 | |
263 | - // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
264 | - // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
265 | - // from PATH_INFO or QUERY_STRING |
|
266 | - if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
267 | - $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
268 | - } |
|
263 | + // If using mod_rewrite or ISAPI_Rewrite strip the script filename |
|
264 | + // out of baseUrl. $pos !== 0 makes sure it is not matching a value |
|
265 | + // from PATH_INFO or QUERY_STRING |
|
266 | + if (strlen($requestUri) >= strlen($baseUrl) && (false !== $pos = strpos($requestUri, $baseUrl)) && 0 !== $pos) { |
|
267 | + $baseUrl = substr($requestUri, 0, $pos + strlen($baseUrl)); |
|
268 | + } |
|
269 | 269 | |
270 | - return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Parse the path info. |
|
275 | - * |
|
276 | - * @return string |
|
277 | - */ |
|
278 | - public function parsePathInfo() |
|
279 | - { |
|
280 | - if (null === ($requestUri = $this->parseRequestUri())) { |
|
281 | - return '/'; |
|
282 | - } |
|
283 | - |
|
284 | - // Remove the query string from REQUEST_URI |
|
285 | - if (false !== $pos = strpos($requestUri, '?')) { |
|
286 | - $requestUri = substr($requestUri, 0, $pos); |
|
287 | - } |
|
288 | - |
|
289 | - if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
290 | - $requestUri = '/'.$requestUri; |
|
291 | - } |
|
292 | - |
|
293 | - if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
294 | - return $requestUri; |
|
295 | - } |
|
296 | - |
|
297 | - $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
298 | - |
|
299 | - if (false === $pathInfo && '' === $pathInfo) { |
|
300 | - return '/'; |
|
301 | - } |
|
270 | + return rtrim($baseUrl, '/'.DIRECTORY_SEPARATOR); |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Parse the path info. |
|
275 | + * |
|
276 | + * @return string |
|
277 | + */ |
|
278 | + public function parsePathInfo() |
|
279 | + { |
|
280 | + if (null === ($requestUri = $this->parseRequestUri())) { |
|
281 | + return '/'; |
|
282 | + } |
|
283 | + |
|
284 | + // Remove the query string from REQUEST_URI |
|
285 | + if (false !== $pos = strpos($requestUri, '?')) { |
|
286 | + $requestUri = substr($requestUri, 0, $pos); |
|
287 | + } |
|
288 | + |
|
289 | + if ('' !== $requestUri && '/' !== $requestUri[0]) { |
|
290 | + $requestUri = '/'.$requestUri; |
|
291 | + } |
|
292 | + |
|
293 | + if (null === ($baseUrl = $this->parseBaseUrl())) { |
|
294 | + return $requestUri; |
|
295 | + } |
|
296 | + |
|
297 | + $pathInfo = substr($requestUri, strlen($baseUrl)); |
|
298 | + |
|
299 | + if (false === $pathInfo && '' === $pathInfo) { |
|
300 | + return '/'; |
|
301 | + } |
|
302 | 302 | |
303 | - return (string) $pathInfo; |
|
304 | - } |
|
303 | + return (string) $pathInfo; |
|
304 | + } |
|
305 | 305 | } |
306 | 306 | \ No newline at end of file |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | $protocol = 'REQUEST_URI'; |
114 | 114 | } |
115 | 115 | |
116 | - switch($protocol) { |
|
116 | + switch ($protocol) { |
|
117 | 117 | case 'REQUEST_URI': |
118 | 118 | $path = $this->parseRequestUri(); |
119 | 119 | break; |
@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $last = count($segs); |
240 | 240 | $baseUrl = ''; |
241 | 241 | |
242 | - do { |
|
242 | + do { |
|
243 | 243 | $seg = $segs[$index]; |
244 | 244 | $baseUrl = '/'.$seg.$baseUrl; |
245 | 245 | ++$index; |
@@ -33,156 +33,156 @@ |
||
33 | 33 | */ |
34 | 34 | class Parameters implements IteratorAggregate, Countable |
35 | 35 | { |
36 | - /** |
|
37 | - * Array parameters from the Server global. |
|
38 | - * |
|
39 | - * @var array $parameters |
|
40 | - */ |
|
41 | - protected $parameters = []; |
|
36 | + /** |
|
37 | + * Array parameters from the Server global. |
|
38 | + * |
|
39 | + * @var array $parameters |
|
40 | + */ |
|
41 | + protected $parameters = []; |
|
42 | 42 | |
43 | - /** |
|
44 | - * Parameter Object Constructor. |
|
45 | - * |
|
46 | - * @param array $parameters |
|
47 | - * |
|
48 | - * @return array |
|
49 | - */ |
|
50 | - public function __construct(array $parameters = []) |
|
51 | - { |
|
52 | - $this->parameters = $parameters; |
|
53 | - } |
|
43 | + /** |
|
44 | + * Parameter Object Constructor. |
|
45 | + * |
|
46 | + * @param array $parameters |
|
47 | + * |
|
48 | + * @return array |
|
49 | + */ |
|
50 | + public function __construct(array $parameters = []) |
|
51 | + { |
|
52 | + $this->parameters = $parameters; |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Returns the parameters. |
|
57 | - * |
|
58 | - * @return array |
|
59 | - */ |
|
60 | - public function all() |
|
61 | - { |
|
62 | - return $this->parameters; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Returns the parameters. |
|
57 | + * |
|
58 | + * @return array |
|
59 | + */ |
|
60 | + public function all() |
|
61 | + { |
|
62 | + return $this->parameters; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Returns the parameter keys. |
|
67 | - * |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - public function keys() |
|
71 | - { |
|
72 | - return array_keys($this->parameters); |
|
73 | - } |
|
65 | + /** |
|
66 | + * Returns the parameter keys. |
|
67 | + * |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + public function keys() |
|
71 | + { |
|
72 | + return array_keys($this->parameters); |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Replaces the current parameters. |
|
77 | - * |
|
78 | - * @param array $parameters |
|
79 | - * |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - public function replace(array $parameters = []) |
|
83 | - { |
|
84 | - $this->parameters = $parameters; |
|
85 | - } |
|
75 | + /** |
|
76 | + * Replaces the current parameters. |
|
77 | + * |
|
78 | + * @param array $parameters |
|
79 | + * |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + public function replace(array $parameters = []) |
|
83 | + { |
|
84 | + $this->parameters = $parameters; |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * Adds parameters. |
|
89 | - * |
|
90 | - * @param array $parameters |
|
91 | - * |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function add(array $parameters = []) |
|
95 | - { |
|
96 | - $this->parameters = array_replace($this->parameters, $parameters); |
|
97 | - } |
|
87 | + /** |
|
88 | + * Adds parameters. |
|
89 | + * |
|
90 | + * @param array $parameters |
|
91 | + * |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function add(array $parameters = []) |
|
95 | + { |
|
96 | + $this->parameters = array_replace($this->parameters, $parameters); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * Get a parameter array item. |
|
101 | - * |
|
102 | - * @param string $key |
|
103 | - * @param string|null $default (null by default) |
|
104 | - * |
|
105 | - * @return mixed |
|
106 | - */ |
|
107 | - public function get($key, $default = null) |
|
108 | - { |
|
109 | - if ($this->has($key)) { |
|
110 | - return $this->parameters[$key]; |
|
111 | - } |
|
99 | + /** |
|
100 | + * Get a parameter array item. |
|
101 | + * |
|
102 | + * @param string $key |
|
103 | + * @param string|null $default (null by default) |
|
104 | + * |
|
105 | + * @return mixed |
|
106 | + */ |
|
107 | + public function get($key, $default = null) |
|
108 | + { |
|
109 | + if ($this->has($key)) { |
|
110 | + return $this->parameters[$key]; |
|
111 | + } |
|
112 | 112 | |
113 | - return $default; |
|
114 | - } |
|
113 | + return $default; |
|
114 | + } |
|
115 | 115 | |
116 | - /** |
|
117 | - * Check if a parameter array item exists. |
|
118 | - * |
|
119 | - * @param string $key |
|
120 | - * |
|
121 | - * @return mixed |
|
122 | - */ |
|
123 | - public function has($key) |
|
124 | - { |
|
125 | - return Arr::exists($this->parameters, $key); |
|
126 | - } |
|
116 | + /** |
|
117 | + * Check if a parameter array item exists. |
|
118 | + * |
|
119 | + * @param string $key |
|
120 | + * |
|
121 | + * @return mixed |
|
122 | + */ |
|
123 | + public function has($key) |
|
124 | + { |
|
125 | + return Arr::exists($this->parameters, $key); |
|
126 | + } |
|
127 | 127 | |
128 | - /** |
|
129 | - * Set a parameter array item. |
|
130 | - * |
|
131 | - * @param string $key |
|
132 | - * @param string $value |
|
133 | - * |
|
134 | - * @return mixed |
|
135 | - */ |
|
136 | - public function set($key, $value) |
|
137 | - { |
|
138 | - $this->parameters[$key] = $value; |
|
139 | - } |
|
128 | + /** |
|
129 | + * Set a parameter array item. |
|
130 | + * |
|
131 | + * @param string $key |
|
132 | + * @param string $value |
|
133 | + * |
|
134 | + * @return mixed |
|
135 | + */ |
|
136 | + public function set($key, $value) |
|
137 | + { |
|
138 | + $this->parameters[$key] = $value; |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * Remove a parameter array item. |
|
143 | - * |
|
144 | - * @param string $key |
|
145 | - * |
|
146 | - * @return void |
|
147 | - */ |
|
148 | - public function remove($key) |
|
149 | - { |
|
150 | - if ($this->has($key)) { |
|
151 | - unset($this->parameters[$key]); |
|
152 | - } |
|
153 | - } |
|
141 | + /** |
|
142 | + * Remove a parameter array item. |
|
143 | + * |
|
144 | + * @param string $key |
|
145 | + * |
|
146 | + * @return void |
|
147 | + */ |
|
148 | + public function remove($key) |
|
149 | + { |
|
150 | + if ($this->has($key)) { |
|
151 | + unset($this->parameters[$key]); |
|
152 | + } |
|
153 | + } |
|
154 | 154 | |
155 | - /* |
|
155 | + /* |
|
156 | 156 | |----------------------------------------------------------------- |
157 | 157 | | IteratorAggregate Method |
158 | 158 | |----------------------------------------------------------------- |
159 | 159 | */ |
160 | 160 | |
161 | - /** |
|
162 | - * Retrieve an external iterator. |
|
163 | - * |
|
164 | - * @see \IteratorAggregate::getIterator |
|
165 | - * |
|
166 | - * @return new \ArrayIterator |
|
167 | - */ |
|
168 | - public function getIterator() |
|
169 | - { |
|
170 | - return new ArrayIterator($this->parameters); |
|
171 | - } |
|
161 | + /** |
|
162 | + * Retrieve an external iterator. |
|
163 | + * |
|
164 | + * @see \IteratorAggregate::getIterator |
|
165 | + * |
|
166 | + * @return new \ArrayIterator |
|
167 | + */ |
|
168 | + public function getIterator() |
|
169 | + { |
|
170 | + return new ArrayIterator($this->parameters); |
|
171 | + } |
|
172 | 172 | |
173 | - /* |
|
173 | + /* |
|
174 | 174 | |----------------------------------------------------------------- |
175 | 175 | | Countable Method |
176 | 176 | |----------------------------------------------------------------- |
177 | 177 | */ |
178 | 178 | |
179 | - /** |
|
180 | - * Returns the number of parameters. |
|
181 | - * |
|
182 | - * @return int The number of parameters |
|
183 | - */ |
|
184 | - public function count() |
|
185 | - { |
|
186 | - return count($this->parameters); |
|
187 | - } |
|
179 | + /** |
|
180 | + * Returns the number of parameters. |
|
181 | + * |
|
182 | + * @return int The number of parameters |
|
183 | + */ |
|
184 | + public function count() |
|
185 | + { |
|
186 | + return count($this->parameters); |
|
187 | + } |
|
188 | 188 | } |
189 | 189 | \ No newline at end of file |
@@ -33,46 +33,46 @@ discard block |
||
33 | 33 | final class Inputs extends Parameters |
34 | 34 | { |
35 | 35 | /** |
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - public function all(string $key = null) |
|
39 | - { |
|
40 | - return parent::all($key); |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + public function all(string $key = null) |
|
39 | + { |
|
40 | + return parent::all($key); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | /** |
44 | - * {@inheritdoc} |
|
45 | - */ |
|
46 | - public function replace(array $inputs = []) |
|
47 | - { |
|
48 | - $this->parameters = []; |
|
44 | + * {@inheritdoc} |
|
45 | + */ |
|
46 | + public function replace(array $inputs = []) |
|
47 | + { |
|
48 | + $this->parameters = []; |
|
49 | 49 | $this->add($inputs); |
50 | - } |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * Adds input values. |
|
52 | + /** |
|
53 | + * Adds input values. |
|
54 | 54 | * |
55 | 55 | * @param array $inputs |
56 | 56 | * |
57 | 57 | * @return mixed |
58 | - */ |
|
59 | - public function add(array $inputs = []) |
|
60 | - { |
|
58 | + */ |
|
59 | + public function add(array $inputs = []) |
|
60 | + { |
|
61 | 61 | foreach ($inputs as $key => $file) { |
62 | 62 | $this->set($key, $file); |
63 | 63 | } |
64 | 64 | } |
65 | 65 | |
66 | 66 | /** |
67 | - * Gets a string input value by name. |
|
68 | - * |
|
69 | - * @param string $key |
|
70 | - * @param string|null $default (null by default) |
|
71 | - * |
|
72 | - * @return string|null |
|
73 | - */ |
|
74 | - public function get($key, $default = null) |
|
75 | - { |
|
67 | + * Gets a string input value by name. |
|
68 | + * |
|
69 | + * @param string $key |
|
70 | + * @param string|null $default (null by default) |
|
71 | + * |
|
72 | + * @return string|null |
|
73 | + */ |
|
74 | + public function get($key, $default = null) |
|
75 | + { |
|
76 | 76 | if (null !== $default && ! is_scalar($default) && ! (is_object($default)) && ! method_exist($default, '__toString')) { |
77 | 77 | throw new BadRequestHttpException(sprintf('Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead', __METHOD__)); |
78 | 78 | } |
@@ -87,19 +87,19 @@ discard block |
||
87 | 87 | } |
88 | 88 | |
89 | 89 | /** |
90 | - * Sets an input by name. |
|
91 | - * |
|
92 | - * @param string $key |
|
93 | - * @param string|array|null $value |
|
94 | - * |
|
95 | - * @return mixed |
|
96 | - */ |
|
97 | - public function set($key, $value) |
|
98 | - { |
|
90 | + * Sets an input by name. |
|
91 | + * |
|
92 | + * @param string $key |
|
93 | + * @param string|array|null $value |
|
94 | + * |
|
95 | + * @return mixed |
|
96 | + */ |
|
97 | + public function set($key, $value) |
|
98 | + { |
|
99 | 99 | if (null !== $value && ! is_scalar($value) && ! is_array($value) && ! method_exist($value, '__toString')) { |
100 | 100 | throw new BadRequestHttpException(sprintf('Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string, array, or null instead', get_debug_type($value), __METHOD__)); |
101 | 101 | } |
102 | 102 | |
103 | - $this->parameters[$key] = $value; |
|
104 | - } |
|
103 | + $this->parameters[$key] = $value; |
|
104 | + } |
|
105 | 105 | } |
106 | 106 | \ No newline at end of file |
@@ -42,21 +42,21 @@ |
||
42 | 42 | } |
43 | 43 | |
44 | 44 | /** |
45 | - * {@inheritdoc} |
|
46 | - */ |
|
47 | - public function replace(array $files = []) |
|
48 | - { |
|
49 | - $this->parameters = []; |
|
45 | + * {@inheritdoc} |
|
46 | + */ |
|
47 | + public function replace(array $files = []) |
|
48 | + { |
|
49 | + $this->parameters = []; |
|
50 | 50 | $this->add($files); |
51 | - } |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - public function add(array $files = []) |
|
57 | - { |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + public function add(array $files = []) |
|
57 | + { |
|
58 | 58 | foreach ($files as $key => $file) { |
59 | 59 | $this->set($key, $file); |
60 | 60 | } |
61 | - } |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | \ No newline at end of file |
@@ -41,406 +41,406 @@ |
||
41 | 41 | */ |
42 | 42 | class GDebug implements DebugContract |
43 | 43 | { |
44 | - /** |
|
45 | - * Allow Handlers to force the script to quit. |
|
46 | - * |
|
47 | - * @var bool $allowQuit |
|
48 | - */ |
|
49 | - protected $allowQuit = true; |
|
44 | + /** |
|
45 | + * Allow Handlers to force the script to quit. |
|
46 | + * |
|
47 | + * @var bool $allowQuit |
|
48 | + */ |
|
49 | + protected $allowQuit = true; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Benchmark instance. |
|
53 | - * |
|
54 | - * @var string $benchmark |
|
55 | - */ |
|
56 | - protected $benchmark; |
|
57 | - |
|
58 | - /** |
|
59 | - * The handler stack. |
|
60 | - * |
|
61 | - * @var array $handlerStack |
|
62 | - */ |
|
63 | - protected $handlerStack = []; |
|
64 | - |
|
65 | - /** |
|
66 | - * The send Http code by default: 500 Internal Server Error. |
|
67 | - * |
|
68 | - * @var bool $sendHttpCode |
|
69 | - */ |
|
70 | - protected $sendHttpCode = 500; |
|
71 | - |
|
72 | - /** |
|
73 | - * The send output. |
|
74 | - * |
|
75 | - * @var bool $sendOutput |
|
76 | - */ |
|
77 | - protected $sendOutput = true; |
|
78 | - |
|
79 | - /** |
|
80 | - * The functions of system what control errors and exceptions. |
|
81 | - * |
|
82 | - * @var string $system |
|
83 | - */ |
|
84 | - protected $system; |
|
85 | - |
|
86 | - /** |
|
87 | - * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
88 | - * |
|
89 | - * @var bool $throwExceptions |
|
90 | - */ |
|
91 | - protected $throwExceptions = true; |
|
92 | - |
|
93 | - /** |
|
94 | - * Constructor. The Debug class instance. |
|
95 | - * |
|
96 | - * @param \Syscodes\Debug\Util\System|null $system |
|
97 | - * |
|
98 | - * @return void |
|
99 | - */ |
|
100 | - public function __construct(System $system = null) |
|
101 | - { |
|
102 | - $this->system = $system ?: new System; |
|
103 | - $this->benchmark = new Benchmark; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the |
|
108 | - * error, display it if display_errors is on, and fire an event that allows custom actions |
|
109 | - * to be taken at this point. |
|
110 | - * |
|
111 | - * @param \Throwable $exception |
|
112 | - * |
|
113 | - * @return string |
|
114 | - */ |
|
115 | - public function handleException(Throwable $exception) |
|
116 | - { |
|
117 | - // The start benchmark |
|
118 | - $this->benchmark->start('total_execution', LENEVOR_START); |
|
119 | - |
|
120 | - $supervisor = $this->getSupervisor($exception); |
|
121 | - |
|
122 | - // Start buffer |
|
123 | - $this->system->startOutputBuferring(); |
|
124 | - |
|
125 | - $handlerResponse = null; |
|
126 | - $handlerContentType = null; |
|
51 | + /** |
|
52 | + * Benchmark instance. |
|
53 | + * |
|
54 | + * @var string $benchmark |
|
55 | + */ |
|
56 | + protected $benchmark; |
|
57 | + |
|
58 | + /** |
|
59 | + * The handler stack. |
|
60 | + * |
|
61 | + * @var array $handlerStack |
|
62 | + */ |
|
63 | + protected $handlerStack = []; |
|
64 | + |
|
65 | + /** |
|
66 | + * The send Http code by default: 500 Internal Server Error. |
|
67 | + * |
|
68 | + * @var bool $sendHttpCode |
|
69 | + */ |
|
70 | + protected $sendHttpCode = 500; |
|
71 | + |
|
72 | + /** |
|
73 | + * The send output. |
|
74 | + * |
|
75 | + * @var bool $sendOutput |
|
76 | + */ |
|
77 | + protected $sendOutput = true; |
|
78 | + |
|
79 | + /** |
|
80 | + * The functions of system what control errors and exceptions. |
|
81 | + * |
|
82 | + * @var string $system |
|
83 | + */ |
|
84 | + protected $system; |
|
85 | + |
|
86 | + /** |
|
87 | + * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
88 | + * |
|
89 | + * @var bool $throwExceptions |
|
90 | + */ |
|
91 | + protected $throwExceptions = true; |
|
92 | + |
|
93 | + /** |
|
94 | + * Constructor. The Debug class instance. |
|
95 | + * |
|
96 | + * @param \Syscodes\Debug\Util\System|null $system |
|
97 | + * |
|
98 | + * @return void |
|
99 | + */ |
|
100 | + public function __construct(System $system = null) |
|
101 | + { |
|
102 | + $this->system = $system ?: new System; |
|
103 | + $this->benchmark = new Benchmark; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the |
|
108 | + * error, display it if display_errors is on, and fire an event that allows custom actions |
|
109 | + * to be taken at this point. |
|
110 | + * |
|
111 | + * @param \Throwable $exception |
|
112 | + * |
|
113 | + * @return string |
|
114 | + */ |
|
115 | + public function handleException(Throwable $exception) |
|
116 | + { |
|
117 | + // The start benchmark |
|
118 | + $this->benchmark->start('total_execution', LENEVOR_START); |
|
119 | + |
|
120 | + $supervisor = $this->getSupervisor($exception); |
|
121 | + |
|
122 | + // Start buffer |
|
123 | + $this->system->startOutputBuferring(); |
|
124 | + |
|
125 | + $handlerResponse = null; |
|
126 | + $handlerContentType = null; |
|
127 | 127 | |
128 | - try { |
|
129 | - foreach ($this->handlerStack as $handler) { |
|
130 | - $handler->setDebug($this); |
|
131 | - $handler->setException($exception); |
|
132 | - $handler->setSupervisor($supervisor); |
|
128 | + try { |
|
129 | + foreach ($this->handlerStack as $handler) { |
|
130 | + $handler->setDebug($this); |
|
131 | + $handler->setException($exception); |
|
132 | + $handler->setSupervisor($supervisor); |
|
133 | 133 | |
134 | - $handlerResponse = $handler->handle(); |
|
134 | + $handlerResponse = $handler->handle(); |
|
135 | 135 | |
136 | - // Collect the content type for possible sending in the headers |
|
137 | - $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
136 | + // Collect the content type for possible sending in the headers |
|
137 | + $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
138 | 138 | |
139 | - if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
140 | - break; |
|
141 | - } |
|
142 | - } |
|
139 | + if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
140 | + break; |
|
141 | + } |
|
142 | + } |
|
143 | 143 | |
144 | - $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
145 | - } |
|
146 | - finally { |
|
147 | - // Returns the contents of the output buffer |
|
148 | - $output = $this->system->CleanOutputBuffer(); |
|
149 | - } |
|
150 | - |
|
151 | - // Returns the contents of the output buffer for loading time of page |
|
152 | - $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
153 | - $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
154 | - |
|
155 | - if ($this->writeToOutput()) { |
|
156 | - if ($Quit) { |
|
157 | - while ($this->system->getOutputBufferLevel() > 0) { |
|
158 | - // Cleanes the output buffer |
|
159 | - $this->system->endOutputBuffering(); |
|
160 | - } |
|
161 | - |
|
162 | - if (Misc::sendHeaders() && $handlerContentType) { |
|
163 | - header("Content-Type: {$handlerContentType}"); |
|
164 | - } |
|
165 | - } |
|
166 | - |
|
167 | - $this->writeToOutputBuffer($output); |
|
168 | - } |
|
169 | - |
|
170 | - if ($Quit) { |
|
171 | - $this->system->flushOutputBuffer(); |
|
172 | - $this->system->stopException(1); |
|
173 | - } |
|
174 | - |
|
175 | - return $output; |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Allow Handlers to force the script to quit. |
|
180 | - * |
|
181 | - * @param bool|int $exit |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - public function allowQuit($exit = null) |
|
186 | - { |
|
187 | - if (func_num_args() == 0) { |
|
188 | - return $this->allowQuit; |
|
189 | - } |
|
190 | - |
|
191 | - return $this->allowQuit = (bool) $exit; |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Lenevor Exception push output directly to the client it the data |
|
196 | - * if they are true, but if it is false, the output will be returned |
|
197 | - * by exception. |
|
198 | - * |
|
199 | - * @param bool|int $send |
|
200 | - * |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function writeToOutput($send = null) |
|
204 | - { |
|
205 | - if (func_num_args() == 0) { |
|
206 | - return $this->sendOutput; |
|
207 | - } |
|
144 | + $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
145 | + } |
|
146 | + finally { |
|
147 | + // Returns the contents of the output buffer |
|
148 | + $output = $this->system->CleanOutputBuffer(); |
|
149 | + } |
|
150 | + |
|
151 | + // Returns the contents of the output buffer for loading time of page |
|
152 | + $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
153 | + $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
154 | + |
|
155 | + if ($this->writeToOutput()) { |
|
156 | + if ($Quit) { |
|
157 | + while ($this->system->getOutputBufferLevel() > 0) { |
|
158 | + // Cleanes the output buffer |
|
159 | + $this->system->endOutputBuffering(); |
|
160 | + } |
|
161 | + |
|
162 | + if (Misc::sendHeaders() && $handlerContentType) { |
|
163 | + header("Content-Type: {$handlerContentType}"); |
|
164 | + } |
|
165 | + } |
|
166 | + |
|
167 | + $this->writeToOutputBuffer($output); |
|
168 | + } |
|
169 | + |
|
170 | + if ($Quit) { |
|
171 | + $this->system->flushOutputBuffer(); |
|
172 | + $this->system->stopException(1); |
|
173 | + } |
|
174 | + |
|
175 | + return $output; |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Allow Handlers to force the script to quit. |
|
180 | + * |
|
181 | + * @param bool|int $exit |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + public function allowQuit($exit = null) |
|
186 | + { |
|
187 | + if (func_num_args() == 0) { |
|
188 | + return $this->allowQuit; |
|
189 | + } |
|
190 | + |
|
191 | + return $this->allowQuit = (bool) $exit; |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Lenevor Exception push output directly to the client it the data |
|
196 | + * if they are true, but if it is false, the output will be returned |
|
197 | + * by exception. |
|
198 | + * |
|
199 | + * @param bool|int $send |
|
200 | + * |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function writeToOutput($send = null) |
|
204 | + { |
|
205 | + if (func_num_args() == 0) { |
|
206 | + return $this->sendOutput; |
|
207 | + } |
|
208 | 208 | |
209 | - return $this->sendOutput = (bool) $send; |
|
210 | - } |
|
209 | + return $this->sendOutput = (bool) $send; |
|
210 | + } |
|
211 | 211 | |
212 | - /** |
|
213 | - * Generate output to the browser. |
|
214 | - * |
|
215 | - * @param string $output |
|
216 | - * |
|
217 | - * @return $this |
|
218 | - */ |
|
219 | - protected function writeToOutputBuffer($output) |
|
220 | - { |
|
221 | - if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
222 | - $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
223 | - } |
|
212 | + /** |
|
213 | + * Generate output to the browser. |
|
214 | + * |
|
215 | + * @param string $output |
|
216 | + * |
|
217 | + * @return $this |
|
218 | + */ |
|
219 | + protected function writeToOutputBuffer($output) |
|
220 | + { |
|
221 | + if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
222 | + $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
223 | + } |
|
224 | 224 | |
225 | - echo $output; |
|
225 | + echo $output; |
|
226 | 226 | |
227 | - return $this; |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Error handler |
|
232 | - * |
|
233 | - * This will catch the php native error and treat it as a exception which will |
|
234 | - * provide a full back trace on all errors. |
|
235 | - * |
|
236 | - * @param int $level |
|
237 | - * @param string $message |
|
238 | - * @param string|null $file |
|
239 | - * @param int|null $line |
|
240 | - * |
|
241 | - * @throws \ErrorException |
|
242 | - */ |
|
243 | - public function handleError(int $level, string $message, string $file = null, int $line = null) |
|
244 | - { |
|
245 | - if ($level & $this->system->getErrorReportingLevel()) { |
|
246 | - $exception = new ErrorException($message, $level, $level, $file, $line); |
|
247 | - |
|
248 | - if ($this->throwExceptions) { |
|
249 | - throw $exception; |
|
250 | - } else { |
|
251 | - $this->handleException($exception); |
|
252 | - } |
|
253 | - |
|
254 | - return true; |
|
255 | - } |
|
256 | - |
|
257 | - return false; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Pushes a handler to the end of the stack. |
|
262 | - * |
|
263 | - * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
264 | - * |
|
265 | - * @return \Syscodes\Contracts\Debug\Handler |
|
266 | - */ |
|
267 | - public function pushHandler($handler) |
|
268 | - { |
|
269 | - return $this->prependHandler($handler); |
|
270 | - } |
|
271 | - |
|
272 | - /** |
|
273 | - * Appends a handler to the end of the stack. |
|
274 | - * |
|
275 | - * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
276 | - * |
|
277 | - * @return $this |
|
278 | - */ |
|
279 | - public function appendHandler($handler) |
|
280 | - { |
|
281 | - array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
282 | - |
|
283 | - return $this; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * Prepends a handler to the start of the stack. |
|
288 | - * |
|
289 | - * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
290 | - * |
|
291 | - * @return $this |
|
292 | - */ |
|
293 | - public function prependHandler($handler) |
|
294 | - { |
|
295 | - array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
296 | - |
|
297 | - return $this; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * Create a CallbackHandler from callable and throw if handler is invalid. |
|
302 | - * |
|
303 | - * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
304 | - * |
|
305 | - * @return \Syscodes\Contracts\Debug\Handler |
|
306 | - * |
|
307 | - * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Contracts\Debug\Handler |
|
308 | - */ |
|
309 | - protected function resolveHandler($handler) |
|
310 | - { |
|
311 | - if (is_callable($handler)) { |
|
312 | - $handler = new CallbackHandler($handler); |
|
313 | - } |
|
314 | - |
|
315 | - if ( ! $handler instanceof MainHandler) { |
|
316 | - throw new InvalidArgumentException( |
|
317 | - "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
318 | - "Syscodes\\Contracts\\Debug\\Handler" |
|
319 | - ); |
|
320 | - } |
|
321 | - |
|
322 | - return $handler; |
|
323 | - } |
|
324 | - |
|
325 | - /** |
|
326 | - * Returns an array with all handlers, in the order they were added to the stack. |
|
327 | - * |
|
328 | - * @return array |
|
329 | - */ |
|
330 | - public function getHandlers() |
|
331 | - { |
|
332 | - return $this->handlerStack; |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Clears all handlers in the handlerStack, including the default PleasingPage handler. |
|
337 | - * |
|
338 | - * @return $this |
|
339 | - */ |
|
340 | - public function clearHandlers() |
|
341 | - { |
|
342 | - $this->handlerStack = []; |
|
343 | - |
|
344 | - return $this; |
|
345 | - } |
|
346 | - |
|
347 | - /** |
|
348 | - * Removes the last handler in the stack and returns it. |
|
349 | - * |
|
350 | - * @return array|null |
|
351 | - */ |
|
352 | - public function popHandler() |
|
353 | - { |
|
354 | - return array_pop($this->handlerStack); |
|
355 | - } |
|
356 | - |
|
357 | - /** |
|
358 | - * Gets supervisor already specified. |
|
359 | - * |
|
360 | - * @param \Throwable $exception |
|
361 | - * |
|
362 | - * @return \Syscodes\Debug\Engine\Supervisor |
|
363 | - */ |
|
364 | - protected function getSupervisor(Throwable $exception) |
|
365 | - { |
|
366 | - return new Supervisor($exception); |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Unregisters all handlers registered by this Debug instance. |
|
371 | - * |
|
372 | - * @return void |
|
373 | - */ |
|
374 | - public function off() |
|
375 | - { |
|
376 | - $this->system->restoreExceptionHandler(); |
|
377 | - $this->system->restoreErrorHandler(); |
|
378 | - } |
|
227 | + return $this; |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Error handler |
|
232 | + * |
|
233 | + * This will catch the php native error and treat it as a exception which will |
|
234 | + * provide a full back trace on all errors. |
|
235 | + * |
|
236 | + * @param int $level |
|
237 | + * @param string $message |
|
238 | + * @param string|null $file |
|
239 | + * @param int|null $line |
|
240 | + * |
|
241 | + * @throws \ErrorException |
|
242 | + */ |
|
243 | + public function handleError(int $level, string $message, string $file = null, int $line = null) |
|
244 | + { |
|
245 | + if ($level & $this->system->getErrorReportingLevel()) { |
|
246 | + $exception = new ErrorException($message, $level, $level, $file, $line); |
|
247 | + |
|
248 | + if ($this->throwExceptions) { |
|
249 | + throw $exception; |
|
250 | + } else { |
|
251 | + $this->handleException($exception); |
|
252 | + } |
|
253 | + |
|
254 | + return true; |
|
255 | + } |
|
256 | + |
|
257 | + return false; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Pushes a handler to the end of the stack. |
|
262 | + * |
|
263 | + * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
264 | + * |
|
265 | + * @return \Syscodes\Contracts\Debug\Handler |
|
266 | + */ |
|
267 | + public function pushHandler($handler) |
|
268 | + { |
|
269 | + return $this->prependHandler($handler); |
|
270 | + } |
|
271 | + |
|
272 | + /** |
|
273 | + * Appends a handler to the end of the stack. |
|
274 | + * |
|
275 | + * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
276 | + * |
|
277 | + * @return $this |
|
278 | + */ |
|
279 | + public function appendHandler($handler) |
|
280 | + { |
|
281 | + array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
282 | + |
|
283 | + return $this; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * Prepends a handler to the start of the stack. |
|
288 | + * |
|
289 | + * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
290 | + * |
|
291 | + * @return $this |
|
292 | + */ |
|
293 | + public function prependHandler($handler) |
|
294 | + { |
|
295 | + array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
296 | + |
|
297 | + return $this; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * Create a CallbackHandler from callable and throw if handler is invalid. |
|
302 | + * |
|
303 | + * @param \Callable|\Syscodes\Contracts\Debug\Handler $handler |
|
304 | + * |
|
305 | + * @return \Syscodes\Contracts\Debug\Handler |
|
306 | + * |
|
307 | + * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Contracts\Debug\Handler |
|
308 | + */ |
|
309 | + protected function resolveHandler($handler) |
|
310 | + { |
|
311 | + if (is_callable($handler)) { |
|
312 | + $handler = new CallbackHandler($handler); |
|
313 | + } |
|
314 | + |
|
315 | + if ( ! $handler instanceof MainHandler) { |
|
316 | + throw new InvalidArgumentException( |
|
317 | + "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
318 | + "Syscodes\\Contracts\\Debug\\Handler" |
|
319 | + ); |
|
320 | + } |
|
321 | + |
|
322 | + return $handler; |
|
323 | + } |
|
324 | + |
|
325 | + /** |
|
326 | + * Returns an array with all handlers, in the order they were added to the stack. |
|
327 | + * |
|
328 | + * @return array |
|
329 | + */ |
|
330 | + public function getHandlers() |
|
331 | + { |
|
332 | + return $this->handlerStack; |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Clears all handlers in the handlerStack, including the default PleasingPage handler. |
|
337 | + * |
|
338 | + * @return $this |
|
339 | + */ |
|
340 | + public function clearHandlers() |
|
341 | + { |
|
342 | + $this->handlerStack = []; |
|
343 | + |
|
344 | + return $this; |
|
345 | + } |
|
346 | + |
|
347 | + /** |
|
348 | + * Removes the last handler in the stack and returns it. |
|
349 | + * |
|
350 | + * @return array|null |
|
351 | + */ |
|
352 | + public function popHandler() |
|
353 | + { |
|
354 | + return array_pop($this->handlerStack); |
|
355 | + } |
|
356 | + |
|
357 | + /** |
|
358 | + * Gets supervisor already specified. |
|
359 | + * |
|
360 | + * @param \Throwable $exception |
|
361 | + * |
|
362 | + * @return \Syscodes\Debug\Engine\Supervisor |
|
363 | + */ |
|
364 | + protected function getSupervisor(Throwable $exception) |
|
365 | + { |
|
366 | + return new Supervisor($exception); |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Unregisters all handlers registered by this Debug instance. |
|
371 | + * |
|
372 | + * @return void |
|
373 | + */ |
|
374 | + public function off() |
|
375 | + { |
|
376 | + $this->system->restoreExceptionHandler(); |
|
377 | + $this->system->restoreErrorHandler(); |
|
378 | + } |
|
379 | 379 | |
380 | - /** |
|
381 | - * Registers this instance as an error handler. |
|
382 | - * |
|
383 | - * @return void |
|
384 | - */ |
|
385 | - public function on() |
|
386 | - { |
|
387 | - // Set the exception handler |
|
388 | - $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
389 | - // Set the error handler |
|
390 | - $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
391 | - // Set the handler for shutdown to catch Parse errors |
|
392 | - $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Lenevor Exception will by default send HTTP code 500, but you may wish |
|
397 | - * to use 502, 503, or another 5xx family code. |
|
398 | - * |
|
399 | - * @param bool|int $code |
|
400 | - * |
|
401 | - * @return int|false |
|
402 | - * |
|
403 | - * @throws \InvalidArgumentException |
|
404 | - */ |
|
405 | - public function sendHttpCode($code = null) |
|
406 | - { |
|
407 | - if (func_num_args() == 0) { |
|
408 | - return $this->sendHttpCode; |
|
409 | - } |
|
380 | + /** |
|
381 | + * Registers this instance as an error handler. |
|
382 | + * |
|
383 | + * @return void |
|
384 | + */ |
|
385 | + public function on() |
|
386 | + { |
|
387 | + // Set the exception handler |
|
388 | + $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
389 | + // Set the error handler |
|
390 | + $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
391 | + // Set the handler for shutdown to catch Parse errors |
|
392 | + $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Lenevor Exception will by default send HTTP code 500, but you may wish |
|
397 | + * to use 502, 503, or another 5xx family code. |
|
398 | + * |
|
399 | + * @param bool|int $code |
|
400 | + * |
|
401 | + * @return int|false |
|
402 | + * |
|
403 | + * @throws \InvalidArgumentException |
|
404 | + */ |
|
405 | + public function sendHttpCode($code = null) |
|
406 | + { |
|
407 | + if (func_num_args() == 0) { |
|
408 | + return $this->sendHttpCode; |
|
409 | + } |
|
410 | 410 | |
411 | - if ( ! $code) { |
|
412 | - return $this->sendHttpCode = false; |
|
413 | - } |
|
411 | + if ( ! $code) { |
|
412 | + return $this->sendHttpCode = false; |
|
413 | + } |
|
414 | 414 | |
415 | - if ($code === true) { |
|
416 | - $code = 500; |
|
417 | - } |
|
415 | + if ($code === true) { |
|
416 | + $code = 500; |
|
417 | + } |
|
418 | 418 | |
419 | - if ($code < 400 || 600 <= $code) { |
|
420 | - throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
421 | - } |
|
419 | + if ($code < 400 || 600 <= $code) { |
|
420 | + throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
421 | + } |
|
422 | 422 | |
423 | - return $this->sendHttpCode = $code; |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * This will catch errors that are generated at the shutdown level of execution. |
|
428 | - * |
|
429 | - * @return void |
|
430 | - * |
|
431 | - * @throws \ErrorException |
|
432 | - */ |
|
433 | - public function handleShutdown() |
|
434 | - { |
|
435 | - $this->throwExceptions = false; |
|
436 | - |
|
437 | - $error = $this->system->getLastError(); |
|
438 | - |
|
439 | - // If we've got an error that hasn't been displayed, then convert |
|
440 | - // it to an Exception and use the Exception handler to display it |
|
441 | - // to the user |
|
442 | - if ($error && Misc::isFatalError($error['type'])) { |
|
443 | - $this->errorHandler($error['type'], $error['message'], $error['file'], $error['line']); |
|
444 | - } |
|
445 | - } |
|
423 | + return $this->sendHttpCode = $code; |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * This will catch errors that are generated at the shutdown level of execution. |
|
428 | + * |
|
429 | + * @return void |
|
430 | + * |
|
431 | + * @throws \ErrorException |
|
432 | + */ |
|
433 | + public function handleShutdown() |
|
434 | + { |
|
435 | + $this->throwExceptions = false; |
|
436 | + |
|
437 | + $error = $this->system->getLastError(); |
|
438 | + |
|
439 | + // If we've got an error that hasn't been displayed, then convert |
|
440 | + // it to an Exception and use the Exception handler to display it |
|
441 | + // to the user |
|
442 | + if ($error && Misc::isFatalError($error['type'])) { |
|
443 | + $this->errorHandler($error['type'], $error['message'], $error['file'], $error['line']); |
|
444 | + } |
|
445 | + } |
|
446 | 446 | } |
447 | 447 | \ No newline at end of file |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | $this->system->endOutputBuffering(); |
160 | 160 | } |
161 | 161 | |
162 | - if (Misc::sendHeaders() && $handlerContentType) { |
|
162 | + if (Misc::sendHeaders() && $handlerContentType) { |
|
163 | 163 | header("Content-Type: {$handlerContentType}"); |
164 | 164 | } |
165 | 165 | } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | |
315 | 315 | if ( ! $handler instanceof MainHandler) { |
316 | 316 | throw new InvalidArgumentException( |
317 | - "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
317 | + "Argument to ".__METHOD__." must be a callable, or instance of ". |
|
318 | 318 | "Syscodes\\Contracts\\Debug\\Handler" |
319 | 319 | ); |
320 | 320 | } |
@@ -142,8 +142,7 @@ |
||
142 | 142 | } |
143 | 143 | |
144 | 144 | $Quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
145 | - } |
|
146 | - finally { |
|
145 | + } finally { |
|
147 | 146 | // Returns the contents of the output buffer |
148 | 147 | $output = $this->system->CleanOutputBuffer(); |
149 | 148 | } |