diff --git a/README.md b/README.md index bc998a4..33af6bf 100644 --- a/README.md +++ b/README.md @@ -201,6 +201,7 @@ abstract class Handler { public function __construct(array $options = []); public function __invoke(): void; + public function getLastOutputThrowable(): ?array; public function getOption(string $name): mixed; public function setOption(string $name, mixed $value): void; @@ -253,6 +254,10 @@ _timeFormat_: The PHP-standard date format which to use for times in output. Def Outputs the stored throwable arrays in the output buffer. +#### MensBeam\Catcher\Handler::getLastOutputThrowable #### + +Returns the last output throwable array handled by this handler (or null if there hasn't been one). + #### MensBeam\Catcher\Handler::getOption #### Returns the value of the provided option name diff --git a/lib/Catcher/Handler.php b/lib/Catcher/Handler.php index 255cf56..3256879 100644 --- a/lib/Catcher/Handler.php +++ b/lib/Catcher/Handler.php @@ -21,6 +21,13 @@ abstract class Handler { public const NOW = 8; public const OUTPUT = 16; + /** + * The last throwable (as an output array) that was handled by this handler + * + * @var ?array[] + */ + protected ?array $lastOutputThrowable = null; + /** * Array of HandlerOutputs the handler creates * @@ -90,9 +97,14 @@ abstract class Handler { } $this->invokeCallback(); + $this->lastOutputThrowable = end($this->outputBuffer) ?: null; $this->outputBuffer = []; } + public function getLastOutputThrowable(): ?array { + return $this->lastOutputThrowable; + } + public function getOption(string $name): mixed { $class = get_class($this); if (!property_exists($class, "_$name")) { diff --git a/lib/Catcher/JSONHandler.php b/lib/Catcher/JSONHandler.php index dc1c819..34f3605 100644 --- a/lib/Catcher/JSONHandler.php +++ b/lib/Catcher/JSONHandler.php @@ -14,6 +14,8 @@ class JSONHandler extends Handler { /** If true the handler will pretty print JSON output; defaults to true */ protected bool $_prettyPrint = true; + /** If true the handler will pretty print JSON output when logging; defaults to false */ + protected bool $_prettyPrintWhenLogging = false; protected function handleCallback(array $output): array { @@ -40,9 +42,6 @@ class JSONHandler extends Handler { $controller = $outputThrowable['controller']; $output = $this->cleanOutputThrowable($outputThrowable); $jsonFlags = \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE; - if ($this->_prettyPrint) { - $jsonFlags |= \JSON_PRETTY_PRINT; - } if (isset($outputThrowable['previous'])) { $output['previous'] = $this->serializeOutputThrowable($outputThrowable['previous'], true); @@ -57,10 +56,10 @@ class JSONHandler extends Handler { if ($outputThrowable['code'] & self::LOG) { $o = $output; unset($o['time']); - $this->log($controller->getThrowable(), json_encode($o, $jsonFlags)); + $this->log($controller->getThrowable(), json_encode($o, ($this->_prettyPrintWhenLogging) ? $jsonFlags | \JSON_PRETTY_PRINT : $jsonFlags)); } - $output = json_encode($output, $jsonFlags); + $output = json_encode($output, ($this->_prettyPrint) ? $jsonFlags | \JSON_PRETTY_PRINT : $jsonFlags); } return $output; diff --git a/tests/cases/TestHandler.php b/tests/cases/TestHandler.php index 460d1c2..288ceb7 100644 --- a/tests/cases/TestHandler.php +++ b/tests/cases/TestHandler.php @@ -62,12 +62,15 @@ class TestHandler extends ErrorHandlingTestCase { $r = new \ReflectionProperty($this->handler::class, 'outputBuffer'); $r->setAccessible(true); $this->assertEquals(1, count($r->getValue($this->handler))); + $this->assertNull($this->handler->getLastOutputThrowable()); $h = $this->handler; $h(); - $this->assertEquals(0, count($r->getValue($this->handler))); + $this->assertEquals(0, count($r->getValue($h))); + $this->assertSame(\Exception::class, $h->getLastOutputThrowable()['class']); $h(); - $this->assertEquals(0, count($r->getValue($this->handler))); + $this->assertEquals(0, count($r->getValue($h))); + $this->assertSame(\Exception::class, $h->getLastOutputThrowable()['class']); } /** @dataProvider provideLogTests */ diff --git a/tests/cases/TestJSONHandler.php b/tests/cases/TestJSONHandler.php index a9aef94..080e16a 100644 --- a/tests/cases/TestJSONHandler.php +++ b/tests/cases/TestJSONHandler.php @@ -31,17 +31,6 @@ class TestJSONHandler extends \PHPUnit\Framework\TestCase { ]); } - ///** @dataProvider provideHandlingTests */ - /*public function testHandling(\Throwable $throwable, int $expectedCode, array $options = []): void { - foreach ($options as $k => $v) { - $this->handler->setOption($k, $v); - } - - $o = $this->handler->handle(new ThrowableController($throwable)); - $this->assertSame($throwable::class, $o['controller']->getThrowable()::class); - $this->assertEquals($expectedCode, $o['code']); - }*/ - /** @dataProvider provideInvocationTests */ public function testInvocation(\Throwable $throwable, bool $silent, bool $log, ?string $logMethodName, int $line): void { $this->handler->setOption('outputToStderr', false);