@@ -38,412 +38,412 @@ |
||
| 38 | 38 | */ |
| 39 | 39 | class GDebug implements DebugContract |
| 40 | 40 | { |
| 41 | - /** |
|
| 42 | - * Allow Handlers to force the script to quit. |
|
| 43 | - * |
|
| 44 | - * @var bool $allowQuit |
|
| 45 | - */ |
|
| 46 | - protected $allowQuit = true; |
|
| 41 | + /** |
|
| 42 | + * Allow Handlers to force the script to quit. |
|
| 43 | + * |
|
| 44 | + * @var bool $allowQuit |
|
| 45 | + */ |
|
| 46 | + protected $allowQuit = true; |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Benchmark instance. |
|
| 50 | - * |
|
| 51 | - * @var string|object $benchmark |
|
| 52 | - */ |
|
| 53 | - protected $benchmark; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * The handler stack. |
|
| 57 | - * |
|
| 58 | - * @var array $handlerStack |
|
| 59 | - */ |
|
| 60 | - protected $handlerStack = []; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The send Http code by default: 500 Internal Server Error. |
|
| 64 | - * |
|
| 65 | - * @var bool $sendHttpCode |
|
| 66 | - */ |
|
| 67 | - protected $sendHttpCode = 500; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The send output. |
|
| 71 | - * |
|
| 72 | - * @var bool $sendOutput |
|
| 73 | - */ |
|
| 74 | - protected $sendOutput = true; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * The functions of system what control errors and exceptions. |
|
| 78 | - * |
|
| 79 | - * @var string|object $system |
|
| 80 | - */ |
|
| 81 | - protected $system; |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
| 85 | - * |
|
| 86 | - * @var bool $throwExceptions |
|
| 87 | - */ |
|
| 88 | - protected $throwExceptions = true; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Constructor. The Debug class instance. |
|
| 92 | - * |
|
| 93 | - * @param \Syscodes\Components\Debug\Util\System|null $system |
|
| 94 | - * |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - public function __construct(System $system = null) |
|
| 98 | - { |
|
| 99 | - $this->system = $system ?: new System; |
|
| 100 | - $this->benchmark = new Benchmark; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the |
|
| 105 | - * error, display it if display_errors is on, and fire an event that allows custom actions |
|
| 106 | - * to be taken at this point. |
|
| 107 | - * |
|
| 108 | - * @param \Throwable $exception |
|
| 109 | - * |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - public function handleException(Throwable $exception): string |
|
| 113 | - { |
|
| 114 | - // The start benchmark |
|
| 115 | - $this->benchmark->start('total_execution', LENEVOR_START); |
|
| 116 | - |
|
| 117 | - $supervisor = $this->getSupervisor($exception); |
|
| 118 | - |
|
| 119 | - // Start buffer |
|
| 120 | - $this->system->startOutputBuferring(); |
|
| 121 | - |
|
| 122 | - $handlerResponse = null; |
|
| 123 | - $handlerContentType = null; |
|
| 48 | + /** |
|
| 49 | + * Benchmark instance. |
|
| 50 | + * |
|
| 51 | + * @var string|object $benchmark |
|
| 52 | + */ |
|
| 53 | + protected $benchmark; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * The handler stack. |
|
| 57 | + * |
|
| 58 | + * @var array $handlerStack |
|
| 59 | + */ |
|
| 60 | + protected $handlerStack = []; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The send Http code by default: 500 Internal Server Error. |
|
| 64 | + * |
|
| 65 | + * @var bool $sendHttpCode |
|
| 66 | + */ |
|
| 67 | + protected $sendHttpCode = 500; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The send output. |
|
| 71 | + * |
|
| 72 | + * @var bool $sendOutput |
|
| 73 | + */ |
|
| 74 | + protected $sendOutput = true; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * The functions of system what control errors and exceptions. |
|
| 78 | + * |
|
| 79 | + * @var string|object $system |
|
| 80 | + */ |
|
| 81 | + protected $system; |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * In certain scenarios, like in shutdown handler, we can not throw exceptions. |
|
| 85 | + * |
|
| 86 | + * @var bool $throwExceptions |
|
| 87 | + */ |
|
| 88 | + protected $throwExceptions = true; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Constructor. The Debug class instance. |
|
| 92 | + * |
|
| 93 | + * @param \Syscodes\Components\Debug\Util\System|null $system |
|
| 94 | + * |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + public function __construct(System $system = null) |
|
| 98 | + { |
|
| 99 | + $this->system = $system ?: new System; |
|
| 100 | + $this->benchmark = new Benchmark; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Catches any uncaught errors and exceptions, including most Fatal errors. Will log the |
|
| 105 | + * error, display it if display_errors is on, and fire an event that allows custom actions |
|
| 106 | + * to be taken at this point. |
|
| 107 | + * |
|
| 108 | + * @param \Throwable $exception |
|
| 109 | + * |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + public function handleException(Throwable $exception): string |
|
| 113 | + { |
|
| 114 | + // The start benchmark |
|
| 115 | + $this->benchmark->start('total_execution', LENEVOR_START); |
|
| 116 | + |
|
| 117 | + $supervisor = $this->getSupervisor($exception); |
|
| 118 | + |
|
| 119 | + // Start buffer |
|
| 120 | + $this->system->startOutputBuferring(); |
|
| 121 | + |
|
| 122 | + $handlerResponse = null; |
|
| 123 | + $handlerContentType = null; |
|
| 124 | 124 | |
| 125 | - try { |
|
| 126 | - foreach ($this->handlerStack as $handler) { |
|
| 127 | - $handler->setDebug($this); |
|
| 128 | - $handler->setException($exception); |
|
| 129 | - $handler->setSupervisor($supervisor); |
|
| 125 | + try { |
|
| 126 | + foreach ($this->handlerStack as $handler) { |
|
| 127 | + $handler->setDebug($this); |
|
| 128 | + $handler->setException($exception); |
|
| 129 | + $handler->setSupervisor($supervisor); |
|
| 130 | 130 | |
| 131 | - $handlerResponse = $handler->handle(); |
|
| 131 | + $handlerResponse = $handler->handle(); |
|
| 132 | 132 | |
| 133 | - // Collect the content type for possible sending in the headers |
|
| 134 | - $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
| 133 | + // Collect the content type for possible sending in the headers |
|
| 134 | + $handlerContentType = method_exists($handler, 'contentType') ? $handler->contentType() : null; |
|
| 135 | 135 | |
| 136 | - if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
| 137 | - break; |
|
| 138 | - } |
|
| 139 | - } |
|
| 136 | + if (in_array($handlerResponse, [MainHandler::LAST_HANDLER, MainHandler::QUIT])) { |
|
| 137 | + break; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | 140 | |
| 141 | - $quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
| 142 | - } |
|
| 143 | - finally { |
|
| 144 | - // Returns the contents of the output buffer |
|
| 145 | - $output = $this->system->CleanOutputBuffer(); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Returns the contents of the output buffer for loading time of page |
|
| 149 | - $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
| 150 | - $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
| 151 | - |
|
| 152 | - if ($this->writeToOutput()) { |
|
| 153 | - if ($quit) { |
|
| 154 | - while ($this->system->getOutputBufferLevel() > 0) { |
|
| 155 | - // Cleanes the output buffer |
|
| 156 | - $this->system->endOutputBuffering(); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - if (Misc::sendHeaders() && $handlerContentType) { |
|
| 160 | - header("Content-Type: {$handlerContentType}"); |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - $this->writeToOutputBuffer($output); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - if ($quit) { |
|
| 168 | - $this->system->flushOutputBuffer(); |
|
| 169 | - $this->system->stopException($this->sendHttpCode()); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - return $output; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Allow Handlers to force the script to quit. |
|
| 177 | - * |
|
| 178 | - * @param bool|int|null $exit |
|
| 179 | - * |
|
| 180 | - * @return bool |
|
| 181 | - */ |
|
| 182 | - public function allowQuit($exit = null): bool |
|
| 183 | - { |
|
| 184 | - if (func_num_args() == 0) { |
|
| 185 | - return $this->allowQuit; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return $this->allowQuit = (bool) $exit; |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - /** |
|
| 192 | - * Lenevor Exception push output directly to the client it the data |
|
| 193 | - * if they are true, but if it is false, the output will be returned |
|
| 194 | - * by exception. |
|
| 195 | - * |
|
| 196 | - * @param bool|int|null $send |
|
| 197 | - * |
|
| 198 | - * @return bool |
|
| 199 | - */ |
|
| 200 | - public function writeToOutput($send = null): bool |
|
| 201 | - { |
|
| 202 | - if (func_num_args() == 0) { |
|
| 203 | - return $this->sendOutput; |
|
| 204 | - } |
|
| 141 | + $quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
|
| 142 | + } |
|
| 143 | + finally { |
|
| 144 | + // Returns the contents of the output buffer |
|
| 145 | + $output = $this->system->CleanOutputBuffer(); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Returns the contents of the output buffer for loading time of page |
|
| 149 | + $totalTime = $this->benchmark->getElapsedTime('total_execution'); |
|
| 150 | + $output = str_replace('{elapsed_time}', $totalTime, $output); |
|
| 151 | + |
|
| 152 | + if ($this->writeToOutput()) { |
|
| 153 | + if ($quit) { |
|
| 154 | + while ($this->system->getOutputBufferLevel() > 0) { |
|
| 155 | + // Cleanes the output buffer |
|
| 156 | + $this->system->endOutputBuffering(); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + if (Misc::sendHeaders() && $handlerContentType) { |
|
| 160 | + header("Content-Type: {$handlerContentType}"); |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + $this->writeToOutputBuffer($output); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + if ($quit) { |
|
| 168 | + $this->system->flushOutputBuffer(); |
|
| 169 | + $this->system->stopException($this->sendHttpCode()); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + return $output; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Allow Handlers to force the script to quit. |
|
| 177 | + * |
|
| 178 | + * @param bool|int|null $exit |
|
| 179 | + * |
|
| 180 | + * @return bool |
|
| 181 | + */ |
|
| 182 | + public function allowQuit($exit = null): bool |
|
| 183 | + { |
|
| 184 | + if (func_num_args() == 0) { |
|
| 185 | + return $this->allowQuit; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return $this->allowQuit = (bool) $exit; |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + /** |
|
| 192 | + * Lenevor Exception push output directly to the client it the data |
|
| 193 | + * if they are true, but if it is false, the output will be returned |
|
| 194 | + * by exception. |
|
| 195 | + * |
|
| 196 | + * @param bool|int|null $send |
|
| 197 | + * |
|
| 198 | + * @return bool |
|
| 199 | + */ |
|
| 200 | + public function writeToOutput($send = null): bool |
|
| 201 | + { |
|
| 202 | + if (func_num_args() == 0) { |
|
| 203 | + return $this->sendOutput; |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - return $this->sendOutput = (bool) $send; |
|
| 207 | - } |
|
| 206 | + return $this->sendOutput = (bool) $send; |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Generate output to the browser. |
|
| 211 | - * |
|
| 212 | - * @param string $output |
|
| 213 | - * |
|
| 214 | - * @return static |
|
| 215 | - */ |
|
| 216 | - protected function writeToOutputBuffer($output): static |
|
| 217 | - { |
|
| 218 | - if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
| 219 | - $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
| 220 | - } |
|
| 209 | + /** |
|
| 210 | + * Generate output to the browser. |
|
| 211 | + * |
|
| 212 | + * @param string $output |
|
| 213 | + * |
|
| 214 | + * @return static |
|
| 215 | + */ |
|
| 216 | + protected function writeToOutputBuffer($output): static |
|
| 217 | + { |
|
| 218 | + if ($this->sendHttpCode() && Misc::sendHeaders()) { |
|
| 219 | + $this->system->setHttpResponseCode($this->sendHttpCode()); |
|
| 220 | + } |
|
| 221 | 221 | |
| 222 | - echo $output; |
|
| 222 | + echo $output; |
|
| 223 | 223 | |
| 224 | - return $this; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * Error handler |
|
| 229 | - * |
|
| 230 | - * This will catch the php native error and treat it as a exception which will |
|
| 231 | - * provide a full back trace on all errors. |
|
| 232 | - * |
|
| 233 | - * @param int $level |
|
| 234 | - * @param string $message |
|
| 235 | - * @param string|null $file |
|
| 236 | - * @param int|null $line |
|
| 237 | - * |
|
| 238 | - * @return bool |
|
| 239 | - * |
|
| 240 | - * @throws \ErrorException |
|
| 241 | - */ |
|
| 242 | - public function handleError( |
|
| 243 | - int $level, |
|
| 244 | - string $message, |
|
| 245 | - string $file = null, |
|
| 246 | - int $line = null |
|
| 247 | - ): bool { |
|
| 248 | - if ($level & $this->system->getErrorReportingLevel()) { |
|
| 249 | - $exception = new ErrorException($message, $level, $level, $file, $line); |
|
| 250 | - |
|
| 251 | - if ($this->throwExceptions) { |
|
| 252 | - throw $exception; |
|
| 253 | - } else { |
|
| 254 | - $this->handleException($exception); |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return true; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return false; |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * Appends a handler to the end of the stack. |
|
| 265 | - * |
|
| 266 | - * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 267 | - * |
|
| 268 | - * @return static |
|
| 269 | - */ |
|
| 270 | - public function appendHandler($handler): static |
|
| 271 | - { |
|
| 272 | - array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
| 273 | - |
|
| 274 | - return $this; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * Prepends a handler to the start of the stack. |
|
| 279 | - * |
|
| 280 | - * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 281 | - * |
|
| 282 | - * @return static |
|
| 283 | - */ |
|
| 284 | - public function prependHandler($handler): static |
|
| 285 | - { |
|
| 286 | - return $this->pushHandler($handler); |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Pushes a handler to the end of the stack. |
|
| 291 | - * |
|
| 292 | - * @param string|callable $handler |
|
| 293 | - * |
|
| 294 | - * @return static |
|
| 295 | - */ |
|
| 296 | - public function pushHandler($handler): static |
|
| 297 | - { |
|
| 298 | - $this->handlerStack[] = $this->resolveHandler($handler); |
|
| 299 | - |
|
| 300 | - return $this; |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Create a CallbackHandler from callable and throw if handler is invalid. |
|
| 305 | - * |
|
| 306 | - * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 307 | - * |
|
| 308 | - * @return \Syscodes\Components\Contracts\Debug\Handler |
|
| 309 | - * |
|
| 310 | - * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler |
|
| 311 | - */ |
|
| 312 | - protected function resolveHandler($handler) |
|
| 313 | - { |
|
| 314 | - if (is_callable($handler)) { |
|
| 315 | - $handler = new CallbackHandler($handler); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - if ( ! $handler instanceof MainHandler) { |
|
| 319 | - throw new InvalidArgumentException( |
|
| 320 | - "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
| 321 | - "Syscodes\Components\\Contracts\\Debug\\Handler" |
|
| 322 | - ); |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - return $handler; |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - /** |
|
| 329 | - * Returns an array with all handlers, in the order they were added to the stack. |
|
| 330 | - * |
|
| 331 | - * @return array |
|
| 332 | - */ |
|
| 333 | - public function getHandlers(): array |
|
| 334 | - { |
|
| 335 | - return $this->handlerStack; |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Clears all handlers in the handlerStack, including the default PleasingPage handler. |
|
| 340 | - * |
|
| 341 | - * @return static |
|
| 342 | - */ |
|
| 343 | - public function clearHandlers(): static |
|
| 344 | - { |
|
| 345 | - $this->handlerStack = []; |
|
| 346 | - |
|
| 347 | - return $this; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Removes the last handler in the stack and returns it. |
|
| 352 | - * |
|
| 353 | - * @return array|null |
|
| 354 | - */ |
|
| 355 | - public function popHandler() |
|
| 356 | - { |
|
| 357 | - return array_pop($this->handlerStack); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * Gets supervisor already specified. |
|
| 362 | - * |
|
| 363 | - * @param \Throwable $exception |
|
| 364 | - * |
|
| 365 | - * @return \Syscodes\Components\Debug\Engine\Supervisor |
|
| 366 | - */ |
|
| 367 | - protected function getSupervisor(Throwable $exception) |
|
| 368 | - { |
|
| 369 | - return new Supervisor($exception); |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * Unregisters all handlers registered by this Debug instance. |
|
| 374 | - * |
|
| 375 | - * @return void |
|
| 376 | - */ |
|
| 377 | - public function off(): void |
|
| 378 | - { |
|
| 379 | - $this->system->restoreExceptionHandler(); |
|
| 380 | - $this->system->restoreErrorHandler(); |
|
| 381 | - } |
|
| 224 | + return $this; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * Error handler |
|
| 229 | + * |
|
| 230 | + * This will catch the php native error and treat it as a exception which will |
|
| 231 | + * provide a full back trace on all errors. |
|
| 232 | + * |
|
| 233 | + * @param int $level |
|
| 234 | + * @param string $message |
|
| 235 | + * @param string|null $file |
|
| 236 | + * @param int|null $line |
|
| 237 | + * |
|
| 238 | + * @return bool |
|
| 239 | + * |
|
| 240 | + * @throws \ErrorException |
|
| 241 | + */ |
|
| 242 | + public function handleError( |
|
| 243 | + int $level, |
|
| 244 | + string $message, |
|
| 245 | + string $file = null, |
|
| 246 | + int $line = null |
|
| 247 | + ): bool { |
|
| 248 | + if ($level & $this->system->getErrorReportingLevel()) { |
|
| 249 | + $exception = new ErrorException($message, $level, $level, $file, $line); |
|
| 250 | + |
|
| 251 | + if ($this->throwExceptions) { |
|
| 252 | + throw $exception; |
|
| 253 | + } else { |
|
| 254 | + $this->handleException($exception); |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return true; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return false; |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * Appends a handler to the end of the stack. |
|
| 265 | + * |
|
| 266 | + * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 267 | + * |
|
| 268 | + * @return static |
|
| 269 | + */ |
|
| 270 | + public function appendHandler($handler): static |
|
| 271 | + { |
|
| 272 | + array_unshift($this->handlerStack, $this->resolveHandler($handler)); |
|
| 273 | + |
|
| 274 | + return $this; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * Prepends a handler to the start of the stack. |
|
| 279 | + * |
|
| 280 | + * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 281 | + * |
|
| 282 | + * @return static |
|
| 283 | + */ |
|
| 284 | + public function prependHandler($handler): static |
|
| 285 | + { |
|
| 286 | + return $this->pushHandler($handler); |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Pushes a handler to the end of the stack. |
|
| 291 | + * |
|
| 292 | + * @param string|callable $handler |
|
| 293 | + * |
|
| 294 | + * @return static |
|
| 295 | + */ |
|
| 296 | + public function pushHandler($handler): static |
|
| 297 | + { |
|
| 298 | + $this->handlerStack[] = $this->resolveHandler($handler); |
|
| 299 | + |
|
| 300 | + return $this; |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Create a CallbackHandler from callable and throw if handler is invalid. |
|
| 305 | + * |
|
| 306 | + * @param \Callable|\Syscodes\Components\Contracts\Debug\Handler $handler |
|
| 307 | + * |
|
| 308 | + * @return \Syscodes\Components\Contracts\Debug\Handler |
|
| 309 | + * |
|
| 310 | + * @throws \InvalidArgumentException If argument is not callable or instance of \Syscodes\Components\Contracts\Debug\Handler |
|
| 311 | + */ |
|
| 312 | + protected function resolveHandler($handler) |
|
| 313 | + { |
|
| 314 | + if (is_callable($handler)) { |
|
| 315 | + $handler = new CallbackHandler($handler); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + if ( ! $handler instanceof MainHandler) { |
|
| 319 | + throw new InvalidArgumentException( |
|
| 320 | + "Argument to " . __METHOD__ . " must be a callable, or instance of ". |
|
| 321 | + "Syscodes\Components\\Contracts\\Debug\\Handler" |
|
| 322 | + ); |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + return $handler; |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + /** |
|
| 329 | + * Returns an array with all handlers, in the order they were added to the stack. |
|
| 330 | + * |
|
| 331 | + * @return array |
|
| 332 | + */ |
|
| 333 | + public function getHandlers(): array |
|
| 334 | + { |
|
| 335 | + return $this->handlerStack; |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Clears all handlers in the handlerStack, including the default PleasingPage handler. |
|
| 340 | + * |
|
| 341 | + * @return static |
|
| 342 | + */ |
|
| 343 | + public function clearHandlers(): static |
|
| 344 | + { |
|
| 345 | + $this->handlerStack = []; |
|
| 346 | + |
|
| 347 | + return $this; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Removes the last handler in the stack and returns it. |
|
| 352 | + * |
|
| 353 | + * @return array|null |
|
| 354 | + */ |
|
| 355 | + public function popHandler() |
|
| 356 | + { |
|
| 357 | + return array_pop($this->handlerStack); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * Gets supervisor already specified. |
|
| 362 | + * |
|
| 363 | + * @param \Throwable $exception |
|
| 364 | + * |
|
| 365 | + * @return \Syscodes\Components\Debug\Engine\Supervisor |
|
| 366 | + */ |
|
| 367 | + protected function getSupervisor(Throwable $exception) |
|
| 368 | + { |
|
| 369 | + return new Supervisor($exception); |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * Unregisters all handlers registered by this Debug instance. |
|
| 374 | + * |
|
| 375 | + * @return void |
|
| 376 | + */ |
|
| 377 | + public function off(): void |
|
| 378 | + { |
|
| 379 | + $this->system->restoreExceptionHandler(); |
|
| 380 | + $this->system->restoreErrorHandler(); |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - /** |
|
| 384 | - * Registers this instance as an error handler. |
|
| 385 | - * |
|
| 386 | - * @return void |
|
| 387 | - */ |
|
| 388 | - public function on() : void |
|
| 389 | - { |
|
| 390 | - // Set the exception handler |
|
| 391 | - $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
| 392 | - // Set the error handler |
|
| 393 | - $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
| 394 | - // Set the handler for shutdown to catch Parse errors |
|
| 395 | - $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Lenevor Exception will by default send HTTP code 500, but you may wish |
|
| 400 | - * to use 502, 503, or another 5xx family code. |
|
| 401 | - * |
|
| 402 | - * @param bool|int $code |
|
| 403 | - * |
|
| 404 | - * @return int|false |
|
| 405 | - * |
|
| 406 | - * @throws \InvalidArgumentException |
|
| 407 | - */ |
|
| 408 | - public function sendHttpCode($code = null) |
|
| 409 | - { |
|
| 410 | - if (func_num_args() == 0) { |
|
| 411 | - return $this->sendHttpCode; |
|
| 412 | - } |
|
| 383 | + /** |
|
| 384 | + * Registers this instance as an error handler. |
|
| 385 | + * |
|
| 386 | + * @return void |
|
| 387 | + */ |
|
| 388 | + public function on() : void |
|
| 389 | + { |
|
| 390 | + // Set the exception handler |
|
| 391 | + $this->system->setExceptionHandler([$this, self::EXCEPTION_HANDLER]); |
|
| 392 | + // Set the error handler |
|
| 393 | + $this->system->setErrorHandler([$this, self::ERROR_HANDLER]); |
|
| 394 | + // Set the handler for shutdown to catch Parse errors |
|
| 395 | + $this->system->registerShutdownFunction([$this, self::SHUTDOWN_HANDLER]); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Lenevor Exception will by default send HTTP code 500, but you may wish |
|
| 400 | + * to use 502, 503, or another 5xx family code. |
|
| 401 | + * |
|
| 402 | + * @param bool|int $code |
|
| 403 | + * |
|
| 404 | + * @return int|false |
|
| 405 | + * |
|
| 406 | + * @throws \InvalidArgumentException |
|
| 407 | + */ |
|
| 408 | + public function sendHttpCode($code = null) |
|
| 409 | + { |
|
| 410 | + if (func_num_args() == 0) { |
|
| 411 | + return $this->sendHttpCode; |
|
| 412 | + } |
|
| 413 | 413 | |
| 414 | - if ( ! $code) { |
|
| 415 | - return $this->sendHttpCode = false; |
|
| 416 | - } |
|
| 414 | + if ( ! $code) { |
|
| 415 | + return $this->sendHttpCode = false; |
|
| 416 | + } |
|
| 417 | 417 | |
| 418 | - if ($code === true) { |
|
| 419 | - $code = 500; |
|
| 420 | - } |
|
| 418 | + if ($code === true) { |
|
| 419 | + $code = 500; |
|
| 420 | + } |
|
| 421 | 421 | |
| 422 | - if ($code < 400 || 600 <= $code) { |
|
| 423 | - throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
| 424 | - } |
|
| 422 | + if ($code < 400 || 600 <= $code) { |
|
| 423 | + throw new InvalidArgumentException("Invalid status code {$code}, must be 4xx or 5xx"); |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - return $this->sendHttpCode = $code; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * This will catch errors that are generated at the shutdown level of execution. |
|
| 431 | - * |
|
| 432 | - * @return void |
|
| 433 | - * |
|
| 434 | - * @throws \ErrorException |
|
| 435 | - */ |
|
| 436 | - public function handleShutdown() |
|
| 437 | - { |
|
| 438 | - $this->throwExceptions = false; |
|
| 439 | - |
|
| 440 | - $error = $this->system->getLastError(); |
|
| 441 | - |
|
| 442 | - // If we've got an error that hasn't been displayed, then convert |
|
| 443 | - // it to an Exception and use the Exception handler to display it |
|
| 444 | - // to the user |
|
| 445 | - if ($error && Misc::isFatalError($error['type'])) { |
|
| 446 | - $this->handleError($error['type'], $error['message'], $error['file'], $error['line']); |
|
| 447 | - } |
|
| 448 | - } |
|
| 426 | + return $this->sendHttpCode = $code; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * This will catch errors that are generated at the shutdown level of execution. |
|
| 431 | + * |
|
| 432 | + * @return void |
|
| 433 | + * |
|
| 434 | + * @throws \ErrorException |
|
| 435 | + */ |
|
| 436 | + public function handleShutdown() |
|
| 437 | + { |
|
| 438 | + $this->throwExceptions = false; |
|
| 439 | + |
|
| 440 | + $error = $this->system->getLastError(); |
|
| 441 | + |
|
| 442 | + // If we've got an error that hasn't been displayed, then convert |
|
| 443 | + // it to an Exception and use the Exception handler to display it |
|
| 444 | + // to the user |
|
| 445 | + if ($error && Misc::isFatalError($error['type'])) { |
|
| 446 | + $this->handleError($error['type'], $error['message'], $error['file'], $error['line']); |
|
| 447 | + } |
|
| 448 | + } |
|
| 449 | 449 | } |
| 450 | 450 | \ No newline at end of file |
@@ -139,8 +139,7 @@ |
||
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | $quit = $handlerResponse == MainHandler::QUIT && $this->allowQuit(); |
| 142 | - } |
|
| 143 | - finally { |
|
| 142 | + } finally { |
|
| 144 | 143 | // Returns the contents of the output buffer |
| 145 | 144 | $output = $this->system->CleanOutputBuffer(); |
| 146 | 145 | } |
@@ -41,7 +41,7 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | public function render($exception): string |
| 43 | 43 | { |
| 44 | - return take(new GDebug, function ($debug) { |
|
| 44 | + return take(new GDebug, function($debug) { |
|
| 45 | 45 | |
| 46 | 46 | $debug->appendHandler($this->DebugHandler()); |
| 47 | 47 | |