Browse Source

Added getLastOutputThrowable to Handler

main
Dustin Wilson 6 months ago
parent
commit
03b10e697a
  1. 5
      README.md
  2. 12
      lib/Catcher/Handler.php
  3. 9
      lib/Catcher/JSONHandler.php
  4. 7
      tests/cases/TestHandler.php
  5. 11
      tests/cases/TestJSONHandler.php

5
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

12
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")) {

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

7
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 */

11
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);

Loading…
Cancel
Save