@@ -4,9 +4,12 @@ |
||
4 | 4 | <?php foreach ($class as $i => $name) : ?> |
5 | 5 | <?php if ($i == count($class) - 1): ?> |
6 | 6 | <h1><?= $template->escape($name) ?></h1> |
7 | - <?php else: ?> |
|
7 | + <?php else { |
|
8 | + : ?> |
|
8 | 9 | <?= $template->escape($name).'\\' ?> |
9 | - <?php endif; ?> |
|
10 | + <?php endif; |
|
11 | +} |
|
12 | +?> |
|
10 | 13 | <?php endforeach; ?> |
11 | 14 | <?php if ($code): ?> |
12 | 15 | <span class="subtitle" title="Exception Code">(<?= $template->escape($code) ?>)</span> |
@@ -31,156 +31,156 @@ |
||
31 | 31 | */ |
32 | 32 | class Supervisor |
33 | 33 | { |
34 | - /** |
|
35 | - * Get exception. |
|
36 | - * |
|
37 | - * @var \Throwable $exception |
|
38 | - */ |
|
39 | - protected $exception; |
|
40 | - |
|
41 | - /** |
|
42 | - * The frame execute errors. |
|
43 | - * |
|
44 | - * @var string $frames |
|
45 | - */ |
|
46 | - protected $frames; |
|
47 | - |
|
48 | - /** |
|
49 | - * Constructor. The Supervisor class instance. |
|
50 | - * |
|
51 | - * @param \Throwable $exception |
|
52 | - * |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - public function __construct($exception) |
|
56 | - { |
|
57 | - $this->exception = $exception; |
|
58 | - } |
|
34 | + /** |
|
35 | + * Get exception. |
|
36 | + * |
|
37 | + * @var \Throwable $exception |
|
38 | + */ |
|
39 | + protected $exception; |
|
40 | + |
|
41 | + /** |
|
42 | + * The frame execute errors. |
|
43 | + * |
|
44 | + * @var string $frames |
|
45 | + */ |
|
46 | + protected $frames; |
|
47 | + |
|
48 | + /** |
|
49 | + * Constructor. The Supervisor class instance. |
|
50 | + * |
|
51 | + * @param \Throwable $exception |
|
52 | + * |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + public function __construct($exception) |
|
56 | + { |
|
57 | + $this->exception = $exception; |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * Returns an iterator for the inspected exception's frames. |
|
62 | - * |
|
63 | - * @param \Throwable $exception |
|
64 | - * |
|
65 | - * @return array |
|
66 | - */ |
|
67 | - public function getFrames() |
|
68 | - { |
|
69 | - if ($this->frames === null) { |
|
70 | - $frames = $this->getTrace($this->exception); |
|
71 | - |
|
72 | - // Fill empty line/file info for call_user_func_array usages |
|
73 | - foreach ($frames as $k => $frame) { |
|
74 | - if (empty($frame['file'])) { |
|
75 | - // Default values when file and line are missing |
|
76 | - $file = '[PHP internal Code]'; |
|
77 | - $line = 0; |
|
78 | - $next_frame = ! empty($frames[$k + 1]) ? $frames[$k + 1] : []; |
|
79 | - $frames[$k]['file'] = $file; |
|
80 | - $frames[$k]['line'] = $line; |
|
81 | - } |
|
82 | - } |
|
60 | + /** |
|
61 | + * Returns an iterator for the inspected exception's frames. |
|
62 | + * |
|
63 | + * @param \Throwable $exception |
|
64 | + * |
|
65 | + * @return array |
|
66 | + */ |
|
67 | + public function getFrames() |
|
68 | + { |
|
69 | + if ($this->frames === null) { |
|
70 | + $frames = $this->getTrace($this->exception); |
|
71 | + |
|
72 | + // Fill empty line/file info for call_user_func_array usages |
|
73 | + foreach ($frames as $k => $frame) { |
|
74 | + if (empty($frame['file'])) { |
|
75 | + // Default values when file and line are missing |
|
76 | + $file = '[PHP internal Code]'; |
|
77 | + $line = 0; |
|
78 | + $next_frame = ! empty($frames[$k + 1]) ? $frames[$k + 1] : []; |
|
79 | + $frames[$k]['file'] = $file; |
|
80 | + $frames[$k]['line'] = $line; |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - // Find latest non-error handling frame index ($i) used to remove error handling frames |
|
85 | - $i = 0; |
|
86 | - |
|
87 | - foreach ($frames as $k => $frame) { |
|
88 | - if ($frame['file'] == $this->exception->getFile() && $frame['line'] == $this->exception->getLine()) { |
|
89 | - $i = $k; |
|
90 | - } |
|
91 | - } |
|
92 | - // Remove error handling frames |
|
93 | - if ($i > 0) { |
|
94 | - array_splice($frames, 0, $i); |
|
95 | - } |
|
84 | + // Find latest non-error handling frame index ($i) used to remove error handling frames |
|
85 | + $i = 0; |
|
86 | + |
|
87 | + foreach ($frames as $k => $frame) { |
|
88 | + if ($frame['file'] == $this->exception->getFile() && $frame['line'] == $this->exception->getLine()) { |
|
89 | + $i = $k; |
|
90 | + } |
|
91 | + } |
|
92 | + // Remove error handling frames |
|
93 | + if ($i > 0) { |
|
94 | + array_splice($frames, 0, $i); |
|
95 | + } |
|
96 | 96 | |
97 | - $firstFrame = $this->getFrameFromException($this->exception); |
|
98 | - array_unshift($frames, $firstFrame); |
|
99 | - |
|
100 | - $this->frames = new Collection($frames); |
|
101 | - } |
|
102 | - |
|
103 | - return $this->frames; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Given an exception, generates an array in the format generated by Exception::getTrace(). |
|
108 | - * |
|
109 | - * @param \Throwable $exception |
|
110 | - * |
|
111 | - * @return array |
|
112 | - */ |
|
113 | - protected function getFrameFromException(Throwable $exception): array |
|
114 | - { |
|
115 | - return [ |
|
116 | - 'file' => $exception->getFile(), |
|
117 | - 'line' => $exception->getLine(), |
|
118 | - 'class' => get_class($exception), |
|
119 | - 'code' => $exception->getCode(), |
|
120 | - 'args' => [ |
|
121 | - $exception->getMessage(), |
|
122 | - ], |
|
123 | - ]; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Gets exception already specified. |
|
128 | - * |
|
129 | - * @return \Throwable |
|
130 | - */ |
|
131 | - public function getException() |
|
132 | - { |
|
133 | - return $this->exception; |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Gets the message of exception. |
|
138 | - * |
|
139 | - * @return string |
|
140 | - */ |
|
141 | - public function getExceptionMessage(): string |
|
142 | - { |
|
143 | - return $this->exception->getMessage(); |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Gets the class name of exception. |
|
148 | - * |
|
149 | - * @return mixed |
|
150 | - */ |
|
151 | - public function getExceptionName() |
|
152 | - { |
|
153 | - return getClass($this->exception); |
|
154 | - } |
|
97 | + $firstFrame = $this->getFrameFromException($this->exception); |
|
98 | + array_unshift($frames, $firstFrame); |
|
99 | + |
|
100 | + $this->frames = new Collection($frames); |
|
101 | + } |
|
102 | + |
|
103 | + return $this->frames; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Given an exception, generates an array in the format generated by Exception::getTrace(). |
|
108 | + * |
|
109 | + * @param \Throwable $exception |
|
110 | + * |
|
111 | + * @return array |
|
112 | + */ |
|
113 | + protected function getFrameFromException(Throwable $exception): array |
|
114 | + { |
|
115 | + return [ |
|
116 | + 'file' => $exception->getFile(), |
|
117 | + 'line' => $exception->getLine(), |
|
118 | + 'class' => get_class($exception), |
|
119 | + 'code' => $exception->getCode(), |
|
120 | + 'args' => [ |
|
121 | + $exception->getMessage(), |
|
122 | + ], |
|
123 | + ]; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Gets exception already specified. |
|
128 | + * |
|
129 | + * @return \Throwable |
|
130 | + */ |
|
131 | + public function getException() |
|
132 | + { |
|
133 | + return $this->exception; |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Gets the message of exception. |
|
138 | + * |
|
139 | + * @return string |
|
140 | + */ |
|
141 | + public function getExceptionMessage(): string |
|
142 | + { |
|
143 | + return $this->exception->getMessage(); |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Gets the class name of exception. |
|
148 | + * |
|
149 | + * @return mixed |
|
150 | + */ |
|
151 | + public function getExceptionName() |
|
152 | + { |
|
153 | + return getClass($this->exception); |
|
154 | + } |
|
155 | 155 | |
156 | - /** |
|
157 | - * Gets the backtrace from an exception. |
|
158 | - * |
|
159 | - * @param \Throwable $exception |
|
160 | - * |
|
161 | - * @return array |
|
162 | - */ |
|
163 | - protected function getTrace($exception): array |
|
164 | - { |
|
165 | - $traces = $exception->getTrace(); |
|
166 | - |
|
167 | - if ( ! $exception instanceof ErrorException) { |
|
168 | - return $traces; |
|
169 | - } |
|
170 | - |
|
171 | - if ( ! Misc::isFatalError($exception->getSeverity())) { |
|
172 | - return $traces; |
|
173 | - } |
|
174 | - |
|
175 | - if ( ! extension_loaded('xdebug') || ! function_exists('xdebug_is_enabled') || ! xdebug_is_enabled()) { |
|
176 | - return $traces; |
|
177 | - } |
|
156 | + /** |
|
157 | + * Gets the backtrace from an exception. |
|
158 | + * |
|
159 | + * @param \Throwable $exception |
|
160 | + * |
|
161 | + * @return array |
|
162 | + */ |
|
163 | + protected function getTrace($exception): array |
|
164 | + { |
|
165 | + $traces = $exception->getTrace(); |
|
166 | + |
|
167 | + if ( ! $exception instanceof ErrorException) { |
|
168 | + return $traces; |
|
169 | + } |
|
170 | + |
|
171 | + if ( ! Misc::isFatalError($exception->getSeverity())) { |
|
172 | + return $traces; |
|
173 | + } |
|
174 | + |
|
175 | + if ( ! extension_loaded('xdebug') || ! function_exists('xdebug_is_enabled') || ! xdebug_is_enabled()) { |
|
176 | + return $traces; |
|
177 | + } |
|
178 | 178 | |
179 | - // Use xdebug to get the full stack trace and remove the shutdown handler stack trace |
|
180 | - $stack = array_reverse(xdebug_get_function_stack()); |
|
181 | - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
182 | - $traces = array_diff_key($stack, $trace); |
|
179 | + // Use xdebug to get the full stack trace and remove the shutdown handler stack trace |
|
180 | + $stack = array_reverse(xdebug_get_function_stack()); |
|
181 | + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
|
182 | + $traces = array_diff_key($stack, $trace); |
|
183 | 183 | |
184 | - return $traces; |
|
185 | - } |
|
184 | + return $traces; |
|
185 | + } |
|
186 | 186 | } |
187 | 187 | \ No newline at end of file |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | $exception->getFile(), |
56 | 56 | $exception->getLine(), |
57 | 57 | $this->getTraceOutput() |
58 | - ); |
|
58 | + ); |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | /** |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | $frame->getFunction(), |
88 | 88 | $frame->getFile(), |
89 | 89 | $frame->getLine() |
90 | - ); |
|
90 | + ); |
|
91 | 91 | |
92 | 92 | $line--; |
93 | 93 | } |
@@ -213,13 +213,13 @@ |
||
213 | 213 | return $this->file; |
214 | 214 | } |
215 | 215 | |
216 | - /** |
|
217 | - * Sets the file path. |
|
218 | - * |
|
219 | - * @param string $file |
|
220 | - * |
|
221 | - * @return static |
|
222 | - */ |
|
216 | + /** |
|
217 | + * Sets the file path. |
|
218 | + * |
|
219 | + * @param string $file |
|
220 | + * |
|
221 | + * @return static |
|
222 | + */ |
|
223 | 223 | public function setFile($file): static |
224 | 224 | { |
225 | 225 | $this->file = $file; |
@@ -34,199 +34,199 @@ |
||
34 | 34 | */ |
35 | 35 | class Response |
36 | 36 | { |
37 | - use HttpResponse, |
|
37 | + use HttpResponse, |
|
38 | 38 | HttpStatusCode; |
39 | 39 | |
40 | - /** |
|
41 | - * Sets up the response with a content and a status code. |
|
42 | - * |
|
43 | - * @param mixed $content The response content |
|
44 | - * @param int $status The response status |
|
45 | - * @param array $headers Array of HTTP headers for this response |
|
46 | - * |
|
47 | - * @return string |
|
48 | - */ |
|
49 | - public function __construct($content = '', int $status = 200, array $headers = []) |
|
50 | - { |
|
51 | - $this->headers = new ResponseHeaders($headers); |
|
40 | + /** |
|
41 | + * Sets up the response with a content and a status code. |
|
42 | + * |
|
43 | + * @param mixed $content The response content |
|
44 | + * @param int $status The response status |
|
45 | + * @param array $headers Array of HTTP headers for this response |
|
46 | + * |
|
47 | + * @return string |
|
48 | + */ |
|
49 | + public function __construct($content = '', int $status = 200, array $headers = []) |
|
50 | + { |
|
51 | + $this->headers = new ResponseHeaders($headers); |
|
52 | 52 | |
53 | - $this->setContent($content); |
|
54 | - $this->setStatusCode($status); |
|
55 | - $this->setProtocolVersion('1.0'); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Creates an instance of the same response class for rendering contents to the content, |
|
60 | - * status code and headers. |
|
61 | - * |
|
62 | - * @param mixed $content The response content |
|
63 | - * @param int $status The HTTP response status for this response |
|
64 | - * @param array $headers Array of HTTP headers for this response |
|
65 | - * |
|
66 | - * @return static |
|
67 | - */ |
|
68 | - public static function render($content = '', $status = 200, $headers = []) |
|
69 | - { |
|
70 | - return new static($content, $status, $headers); |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Gets the current response content. |
|
75 | - * |
|
76 | - * @return string |
|
77 | - */ |
|
78 | - public function getContent(): string |
|
79 | - { |
|
80 | - return $this->content; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Sends the headers if they haven't already been sent. |
|
85 | - * Returns whether they were sent or not. |
|
86 | - * |
|
87 | - * @return static |
|
88 | - */ |
|
89 | - public function sendHeaders(): static |
|
90 | - { |
|
91 | - // Have the headers already been sent? |
|
92 | - if (headers_sent()) { |
|
93 | - return $this; |
|
94 | - } |
|
95 | - |
|
96 | - // Headers |
|
97 | - foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { |
|
98 | - $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
99 | - |
|
100 | - foreach ($values as $value) { |
|
101 | - header($name.': '. $value, $replace, $this->statusCode); |
|
102 | - } |
|
103 | - } |
|
53 | + $this->setContent($content); |
|
54 | + $this->setStatusCode($status); |
|
55 | + $this->setProtocolVersion('1.0'); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Creates an instance of the same response class for rendering contents to the content, |
|
60 | + * status code and headers. |
|
61 | + * |
|
62 | + * @param mixed $content The response content |
|
63 | + * @param int $status The HTTP response status for this response |
|
64 | + * @param array $headers Array of HTTP headers for this response |
|
65 | + * |
|
66 | + * @return static |
|
67 | + */ |
|
68 | + public static function render($content = '', $status = 200, $headers = []) |
|
69 | + { |
|
70 | + return new static($content, $status, $headers); |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Gets the current response content. |
|
75 | + * |
|
76 | + * @return string |
|
77 | + */ |
|
78 | + public function getContent(): string |
|
79 | + { |
|
80 | + return $this->content; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Sends the headers if they haven't already been sent. |
|
85 | + * Returns whether they were sent or not. |
|
86 | + * |
|
87 | + * @return static |
|
88 | + */ |
|
89 | + public function sendHeaders(): static |
|
90 | + { |
|
91 | + // Have the headers already been sent? |
|
92 | + if (headers_sent()) { |
|
93 | + return $this; |
|
94 | + } |
|
95 | + |
|
96 | + // Headers |
|
97 | + foreach ($this->headers->allPreserveCaseWithoutCookies() as $name => $values) { |
|
98 | + $replace = 0 === strcasecmp($name, 'Content-Type'); |
|
99 | + |
|
100 | + foreach ($values as $value) { |
|
101 | + header($name.': '. $value, $replace, $this->statusCode); |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - // Cookies |
|
106 | - foreach ($this->headers->getCookies() as $cookie) { |
|
107 | - header('Set-Cookie: '.$cookie, false, $this->statusCode); |
|
108 | - } |
|
105 | + // Cookies |
|
106 | + foreach ($this->headers->getCookies() as $cookie) { |
|
107 | + header('Set-Cookie: '.$cookie, false, $this->statusCode); |
|
108 | + } |
|
109 | 109 | |
110 | - // Status |
|
111 | - if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
112 | - // Send the protocol/status line first, FCGI servers need different status header |
|
113 | - header(sprintf('Status: %s %s', $this->statusCode, $this->statusText)); |
|
114 | - } else { |
|
115 | - header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); |
|
116 | - } |
|
117 | - |
|
118 | - return $this; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Sends content for the current web response. |
|
123 | - * |
|
124 | - * @return static |
|
125 | - */ |
|
126 | - public function sendContent(): static |
|
127 | - { |
|
128 | - echo $this->content; |
|
129 | - |
|
130 | - return $this; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Sends the response to the output buffer. Optionally, headers will be sent. |
|
135 | - * |
|
136 | - * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
137 | - * |
|
138 | - * @return static |
|
139 | - */ |
|
140 | - public function send($sendHeader = false): static |
|
141 | - { |
|
142 | - if ($sendHeader) { |
|
143 | - $this->sendHeaders(); |
|
144 | - } |
|
145 | - |
|
146 | - if (null !== $this->content) { |
|
147 | - $this->sendContent(); |
|
148 | - } |
|
149 | - |
|
150 | - return $this; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Sends the content of the message to the browser. |
|
155 | - * |
|
156 | - * @param mixed $content The response content |
|
157 | - * |
|
158 | - * @return static |
|
159 | - */ |
|
160 | - public function setContent($content): static |
|
161 | - { |
|
162 | - if (null !== $content && ! is_string($content) && ! is_numeric($content) && |
|
163 | - ! is_bool($content) && ! is_object($content) && ! is_callable([$content, '__toString'])) { |
|
164 | - throw new UnexpectedValueException( |
|
165 | - sprintf('The Response content must be a string or object implementing __toString(), "%s" given', gettype($content) |
|
166 | - )); |
|
167 | - } |
|
168 | - |
|
169 | - if ($content instanceof JsonSerializable || is_array($content)) { |
|
170 | - $this->header('Content-Type', 'application/json'); |
|
171 | - |
|
172 | - $content = json_encode($content); |
|
173 | - } elseif ($content instanceof Renderable) { |
|
174 | - $content = $content->render(); |
|
175 | - } |
|
110 | + // Status |
|
111 | + if ( ! empty($_SERVER['FCGI_SERVER_VERSION'])) { |
|
112 | + // Send the protocol/status line first, FCGI servers need different status header |
|
113 | + header(sprintf('Status: %s %s', $this->statusCode, $this->statusText)); |
|
114 | + } else { |
|
115 | + header(sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText), true, $this->statusCode); |
|
116 | + } |
|
117 | + |
|
118 | + return $this; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Sends content for the current web response. |
|
123 | + * |
|
124 | + * @return static |
|
125 | + */ |
|
126 | + public function sendContent(): static |
|
127 | + { |
|
128 | + echo $this->content; |
|
129 | + |
|
130 | + return $this; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Sends the response to the output buffer. Optionally, headers will be sent. |
|
135 | + * |
|
136 | + * @param bool $sendHeader Whether or not to send the defined HTTP headers |
|
137 | + * |
|
138 | + * @return static |
|
139 | + */ |
|
140 | + public function send($sendHeader = false): static |
|
141 | + { |
|
142 | + if ($sendHeader) { |
|
143 | + $this->sendHeaders(); |
|
144 | + } |
|
145 | + |
|
146 | + if (null !== $this->content) { |
|
147 | + $this->sendContent(); |
|
148 | + } |
|
149 | + |
|
150 | + return $this; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Sends the content of the message to the browser. |
|
155 | + * |
|
156 | + * @param mixed $content The response content |
|
157 | + * |
|
158 | + * @return static |
|
159 | + */ |
|
160 | + public function setContent($content): static |
|
161 | + { |
|
162 | + if (null !== $content && ! is_string($content) && ! is_numeric($content) && |
|
163 | + ! is_bool($content) && ! is_object($content) && ! is_callable([$content, '__toString'])) { |
|
164 | + throw new UnexpectedValueException( |
|
165 | + sprintf('The Response content must be a string or object implementing __toString(), "%s" given', gettype($content) |
|
166 | + )); |
|
167 | + } |
|
168 | + |
|
169 | + if ($content instanceof JsonSerializable || is_array($content)) { |
|
170 | + $this->header('Content-Type', 'application/json'); |
|
171 | + |
|
172 | + $content = json_encode($content); |
|
173 | + } elseif ($content instanceof Renderable) { |
|
174 | + $content = $content->render(); |
|
175 | + } |
|
176 | 176 | |
177 | - $this->content = $content ?? ''; |
|
178 | - |
|
179 | - return $this; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Prepares the Response before it is sent to the client. |
|
184 | - * |
|
185 | - * @param \Syscodes\Components\Http\Request $request |
|
186 | - * |
|
187 | - * @return static |
|
188 | - */ |
|
189 | - public function prepare($request): static |
|
190 | - { |
|
191 | - $headers = $this->headers; |
|
192 | - |
|
193 | - if ($this->isInformational() || $this->isEmpty()) { |
|
194 | - $this->setContent(null); |
|
195 | - $headers->remove('Content-Type'); |
|
196 | - $headers->remove('Content-Length'); |
|
197 | - } |
|
177 | + $this->content = $content ?? ''; |
|
178 | + |
|
179 | + return $this; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Prepares the Response before it is sent to the client. |
|
184 | + * |
|
185 | + * @param \Syscodes\Components\Http\Request $request |
|
186 | + * |
|
187 | + * @return static |
|
188 | + */ |
|
189 | + public function prepare($request): static |
|
190 | + { |
|
191 | + $headers = $this->headers; |
|
192 | + |
|
193 | + if ($this->isInformational() || $this->isEmpty()) { |
|
194 | + $this->setContent(null); |
|
195 | + $headers->remove('Content-Type'); |
|
196 | + $headers->remove('Content-Length'); |
|
197 | + } |
|
198 | 198 | |
199 | - // Fix protocol |
|
200 | - if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) { |
|
201 | - $this->setProtocolVersion('1.1'); |
|
202 | - } |
|
199 | + // Fix protocol |
|
200 | + if ('HTTP/1.0' != $request->server->get('SERVER_PROTOCOL')) { |
|
201 | + $this->setProtocolVersion('1.1'); |
|
202 | + } |
|
203 | 203 | |
204 | - return $this; |
|
205 | - } |
|
204 | + return $this; |
|
205 | + } |
|
206 | 206 | |
207 | - /** |
|
208 | - * Magic method. |
|
209 | - * |
|
210 | - * Returns the Response as an HTTP string. |
|
211 | - * |
|
212 | - * @return string |
|
213 | - */ |
|
214 | - public function __toString(): string |
|
215 | - { |
|
216 | - return sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". |
|
217 | - $this->headers."\r\n". |
|
218 | - $this->getContent(); |
|
219 | - } |
|
207 | + /** |
|
208 | + * Magic method. |
|
209 | + * |
|
210 | + * Returns the Response as an HTTP string. |
|
211 | + * |
|
212 | + * @return string |
|
213 | + */ |
|
214 | + public function __toString(): string |
|
215 | + { |
|
216 | + return sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". |
|
217 | + $this->headers."\r\n". |
|
218 | + $this->getContent(); |
|
219 | + } |
|
220 | 220 | |
221 | - /** |
|
222 | - * Magic method. |
|
223 | - * |
|
224 | - * Clone the current Response instance. |
|
225 | - * |
|
226 | - * @return void |
|
227 | - */ |
|
228 | - public function __clone() |
|
229 | - { |
|
230 | - $this->headers = clone $this->headers; |
|
231 | - } |
|
221 | + /** |
|
222 | + * Magic method. |
|
223 | + * |
|
224 | + * Clone the current Response instance. |
|
225 | + * |
|
226 | + * @return void |
|
227 | + */ |
|
228 | + public function __clone() |
|
229 | + { |
|
230 | + $this->headers = clone $this->headers; |
|
231 | + } |
|
232 | 232 | } |
233 | 233 | \ No newline at end of file |
@@ -98,7 +98,7 @@ |
||
98 | 98 | $replace = 0 === strcasecmp($name, 'Content-Type'); |
99 | 99 | |
100 | 100 | foreach ($values as $value) { |
101 | - header($name.': '. $value, $replace, $this->statusCode); |
|
101 | + header($name.': '.$value, $replace, $this->statusCode); |
|
102 | 102 | } |
103 | 103 | } |
104 | 104 |
@@ -51,7 +51,7 @@ |
||
51 | 51 | * |
52 | 52 | * @var \Syscodes\Components\Http\Headers|object $headers |
53 | 53 | */ |
54 | - public $headers; |
|
54 | + public $headers; |
|
55 | 55 | |
56 | 56 | /** |
57 | 57 | * 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 |
@@ -49,7 +49,7 @@ |
||
49 | 49 | * |
50 | 50 | * @var \Syscodes\Components\Http\Headers $headers |
51 | 51 | */ |
52 | - public $headers; |
|
52 | + public $headers; |
|
53 | 53 | |
54 | 54 | /** |
55 | 55 | * The HTTP protocol version. |
@@ -302,13 +302,13 @@ |
||
302 | 302 | } |
303 | 303 | |
304 | 304 | /** |
305 | - * Remove a parameter array item. |
|
306 | - * |
|
307 | - * @param string $key |
|
308 | - * |
|
309 | - * @return void |
|
310 | - */ |
|
311 | - public function remove($key) |
|
305 | + * Remove a parameter array item. |
|
306 | + * |
|
307 | + * @param string $key |
|
308 | + * |
|
309 | + * @return void |
|
310 | + */ |
|
311 | + public function remove($key) |
|
312 | 312 | { |
313 | 313 | return $this->getInputSource()->remove($key); |
314 | 314 | } |