@@ -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; |
@@ -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 | |
@@ -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 = []): static |
|
| 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 = []): static |
|
| 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 |
@@ -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 | } |
@@ -274,7 +274,7 @@ |
||
| 274 | 274 | |
| 275 | 275 | // unless the domain already starts with a dot |
| 276 | 276 | if ($domain[0] !== '.') { |
| 277 | - $domain = '.' . $domain; |
|
| 277 | + $domain = '.'.$domain; |
|
| 278 | 278 | } |
| 279 | 279 | |
| 280 | 280 | // return the normalized domain |
@@ -85,6 +85,6 @@ |
||
| 85 | 85 | public static function longVersion(): string |
| 86 | 86 | { |
| 87 | 87 | return self::COPY.' '.self::YEAR.' '.self::COPYRIGHT.' - '.self::NAME.' ' .self::RELEASE. ' '. |
| 88 | - self::STATUS.' [ '.self::CODENAME.' ] '.self::RELEASEDATE; |
|
| 88 | + self::STATUS.' [ '.self::CODENAME.' ] '.self::RELEASEDATE; |
|
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | \ No newline at end of file |
@@ -84,7 +84,7 @@ |
||
| 84 | 84 | */ |
| 85 | 85 | public static function longVersion(): string |
| 86 | 86 | { |
| 87 | - return self::COPY.' '.self::YEAR.' '.self::COPYRIGHT.' - '.self::NAME.' ' .self::RELEASE. ' '. |
|
| 87 | + return self::COPY.' '.self::YEAR.' '.self::COPYRIGHT.' - '.self::NAME.' '.self::RELEASE.' '. |
|
| 88 | 88 | self::STATUS.' [ '.self::CODENAME.' ] '.self::RELEASEDATE; |
| 89 | 89 | } |
| 90 | 90 | } |
| 91 | 91 | \ No newline at end of file |
@@ -301,7 +301,7 @@ |
||
| 301 | 301 | */ |
| 302 | 302 | public function getDefaultDriver(): string |
| 303 | 303 | { |
| 304 | - return $this->app['config']['cache.default']; |
|
| 304 | + return $this->app['config']['cache.default']; |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | /** |
@@ -472,7 +472,7 @@ |
||
| 472 | 472 | */ |
| 473 | 473 | public function offsetSet($offset, $value): void |
| 474 | 474 | { |
| 475 | - $this->put($offset, $value, $this->cacheTime); |
|
| 475 | + $this->put($offset, $value, $this->cacheTime); |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | 478 | /** |
@@ -279,7 +279,7 @@ discard block |
||
| 279 | 279 | * |
| 280 | 280 | * @return int|bool |
| 281 | 281 | */ |
| 282 | - public function increment(string $key, mixed $value = 1): int|bool |
|
| 282 | + public function increment(string $key, mixed $value = 1): int | bool |
|
| 283 | 283 | { |
| 284 | 284 | return $this->store->increment($key, $value); |
| 285 | 285 | } |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | * |
| 293 | 293 | * @return int|bool |
| 294 | 294 | */ |
| 295 | - public function decrement(string $key, mixed $value = 1): int|bool |
|
| 295 | + public function decrement(string $key, mixed $value = 1): int | bool |
|
| 296 | 296 | { |
| 297 | 297 | return $this->store->decrement($key, $value); |
| 298 | 298 | } |