Browse Source

Added JSONHandler

2.1.0
Dustin Wilson 2 years ago
parent
commit
0fc39573b6
  1. 4
      lib/Catcher.php
  2. 10
      lib/Catcher/HTMLHandler.php
  3. 7
      lib/Catcher/Handler.php
  4. 115
      lib/Catcher/JSONHandler.php
  5. 8
      lib/Catcher/PlainTextHandler.php

4
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']));
}
}

10
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');

7
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";

115
lib/Catcher/JSONHandler.php

@ -0,0 +1,115 @@
<?php
/**
* @license MIT
* Copyright 2022 Dustin Wilson, et al.
* See LICENSE and AUTHORS files for details
*/
declare(strict_types=1);
namespace MensBeam\Framework\Catcher;
use \Psr\Log\LoggerInterface;
class JSONHandler extends Handler {
public const CONTENT_TYPE = 'application/json';
/** 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 timestamps in output */
protected string $_timeFormat = 'c';
protected function dispatchCallback(): void {
$output = [
'status' => (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
];
}
}

8
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);
}
}

Loading…
Cancel
Save