diff --git a/lib/Catcher.php b/lib/Catcher.php index fbc56be..29fdf49 100644 --- a/lib/Catcher.php +++ b/lib/Catcher.php @@ -105,8 +105,4 @@ class Catcher { } } } - - public static function isHTTPRequest() { - return (\PHP_SAPI !== 'cli' && isset($_SERVER['REQUEST_URI']) && isset($_SERVER['REQUEST_METHOD'])); - } } \ No newline at end of file diff --git a/lib/Catcher/HTMLHandler.php b/lib/Catcher/HTMLHandler.php index f20486b..8136879 100644 --- a/lib/Catcher/HTMLHandler.php +++ b/lib/Catcher/HTMLHandler.php @@ -13,7 +13,6 @@ class HTMLHandler extends Handler { public const CONTENT_TYPE = 'text/html'; protected ?\DOMDocument $_document = null; - protected static array $bullshit = []; /** If true the handler will output times to the output; defaults to true */ protected bool $_outputTime = true; /** The PHP-standard date format which to use for times printed to output */ @@ -85,12 +84,7 @@ class HTMLHandler extends Handler { $body->appendChild($o->output); } - $output = $this->_document->saveHTML(); - if (\PHP_SAPI === 'CLI') { - fprintf(\STDERR, "$output\n"); - } else { - echo $output; - } + $this->print($this->_document->saveHTML()); } protected function handleCallback(ThrowableController $controller): HandlerOutput { @@ -126,7 +120,7 @@ class HTMLHandler extends Handler { if (count($frames) > 0) { $ol = $this->_document->createElement('ol'); - $p->appendChild($ol); + $frag->appendChild($ol); $num = 1; foreach ($frames as $frame) { $li = $this->_document->createElement('li'); diff --git a/lib/Catcher/Handler.php b/lib/Catcher/Handler.php index c667f0f..3d32e82 100644 --- a/lib/Catcher/Handler.php +++ b/lib/Catcher/Handler.php @@ -176,6 +176,13 @@ abstract class Handler { abstract protected function handleCallback(ThrowableController $controller): HandlerOutput; + protected function print(string $string): void { + if (strtolower(\PHP_SAPI) === 'cli') { + fprintf(\STDERR, "$string\n"); + } else { + echo $string; + } + } /*public function __get(string $name): mixed { $name = "_$name"; diff --git a/lib/Catcher/JSONHandler.php b/lib/Catcher/JSONHandler.php new file mode 100644 index 0000000..4694e7d --- /dev/null +++ b/lib/Catcher/JSONHandler.php @@ -0,0 +1,115 @@ + (string)$this->_httpCode + ]; + + $errors = []; + foreach ($this->outputBuffer as $o) { + if ($o->outputCode & self::SILENT) { + continue; + } + + $errors[] = $o->output; + } + $output['errors'] = $errors; + + $this->print(json_encode($output, \JSON_PARTIAL_OUTPUT_ON_ERROR)); + } + + protected function handleCallback(ThrowableController $controller): HandlerOutput { + $output = $this->buildThrowableArray($controller); + if ($this->_outputPrevious) { + $target = $output; + $prevController = $controller->getPrevious(); + while ($prevController) { + $prev = $this->buildThrowableArray($prevController); + $target['previous'] = $prev; + $target = $prev; + $prevController = $prevController->getPrevious(); + } + } + + if ($this->_outputBacktrace) { + $output['frames'] = $controller->getFrames(); + } + + if ($this->_outputTime && $this->_timeFormat !== '') { + $output['timestamp'] = (new \DateTime())->format($this->_timeFormat); + } + + return new HandlerOutput($this->getControlCode(), $this->getOutputCode(), $output); + } + + + + protected function log(\Throwable $throwable, string $message): void { + if ($throwable instanceof \Error) { + switch ($throwable->getCode()) { + case \E_NOTICE: + case \E_USER_NOTICE: + case \E_STRICT: + $this->_logger->notice($message); + break; + case \E_WARNING: + case \E_COMPILE_WARNING: + case \E_USER_WARNING: + case \E_DEPRECATED: + case \E_USER_DEPRECATED: + $this->_logger->warning($message); + break; + case \E_RECOVERABLE_ERROR: + $this->_logger->error($message); + break; + case \E_PARSE: + case \E_CORE_ERROR: + case \E_COMPILE_ERROR: + $this->_logger->alert($message); + break; + default: $this->_logger->critical($message); + } + } elseif ($throwable instanceof \Exception && ($throwable instanceof \PharException || $throwable instanceof \RuntimeException)) { + $this->_logger->alert($message); + } else { + $this->_logger->critical($message); + } + } + + protected function buildThrowableArray(ThrowableController $controller): array { + $throwable = $controller->getThrowable(); + $type = $throwable::class; + if ($throwable instanceof Error) { + $t = $controller->getErrorType(); + $t = ($throwable instanceof Error) ? $controller->getErrorType() : null; + $type = ($t !== null) ? "$t (" . $type . ")" : $type; + } + + return [ + 'code' => $throwable->getCode(), + 'file' => $throwable->getFile(), + 'line' => $throwable->getLine(), + 'message' => $throwable->getMessage(), + 'type' => $type + ]; + } +} \ No newline at end of file diff --git a/lib/Catcher/PlainTextHandler.php b/lib/Catcher/PlainTextHandler.php index 21081c9..d18b7ee 100644 --- a/lib/Catcher/PlainTextHandler.php +++ b/lib/Catcher/PlainTextHandler.php @@ -27,11 +27,7 @@ class PlainTextHandler extends Handler { continue; } - if (\PHP_SAPI === 'CLI') { - fprintf(\STDERR, "{$o->output}\n"); - } else { - echo "{$o->output}\n"; - } + $this->print($o->output); } } @@ -76,8 +72,6 @@ class PlainTextHandler extends Handler { $frame['line'], $args ); - - die($output); } }