Skip to content

Commit

Permalink
appendHandler and prependHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sokolov committed Dec 25, 2019
1 parent 8957f6f commit f2eb7d6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 25 deletions.
49 changes: 36 additions & 13 deletions src/Whoops/Run.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,33 @@ public function __construct(SystemFacade $system = null)
}

/**
* Pushes a handler to the end of the stack
* Explicitly request your handler runs as the last of all currently registered handlers
*/
public function appendHandler($handler)
{
array_unshift($this->handlerStack, $this->resolveHandler($handler));
return $this;
}

/**
* Explicitly request your handler runs as the first of all currently registered handlers
*/
public function prependHandler($handler)
{
return $this->pushHandler($handler);
}

/**
* Register your handler as the last of all currently registered handlers.
* Prefer using appendHandler and prependHandler for clarity.
*
* @throws InvalidArgumentException If argument is not callable or instance of HandlerInterface
* @param Callable|HandlerInterface $handler
* @return Run
*/
public function pushHandler($handler)
{
if (is_callable($handler)) {
$handler = new CallbackHandler($handler);
}

if (!$handler instanceof HandlerInterface) {
throw new InvalidArgumentException(
"Argument to " . __METHOD__ . " must be a callable, or instance of "
. "Whoops\\Handler\\HandlerInterface"
);
}

$this->handlerStack[] = $handler;
$this->handlerStack[] = $this->resolveHandler($handler);
return $this;
}

Expand Down Expand Up @@ -393,6 +400,22 @@ public function handleShutdown()
*/
private $canThrowExceptions = true;

private function resolveHandler($handler)
{
if (is_callable($handler)) {
$handler = new CallbackHandler($handler);
}

if (!$handler instanceof HandlerInterface) {
throw new InvalidArgumentException(
"Handler must be a callable, or instance of "
. "Whoops\\Handler\\HandlerInterface"
);
}

return $handler;
}

/**
* Echo something to the browser
* @param string $output
Expand Down
42 changes: 30 additions & 12 deletions tests/Whoops/RunTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,16 @@ public function testHandlerHoldsOrder()
$handlerFour = $this->getHandler();

$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
$run->pushHandler($handlerThree);
$run->pushHandler($handlerFour);
$run->prependHandler($handlerTwo);
$run->appendHandler($handlerThree);
$run->appendHandler($handlerFour);

$handlers = $run->getHandlers();

$this->assertSame($handlers[0], $handlerOne);
$this->assertSame($handlers[1], $handlerTwo);
$this->assertSame($handlers[2], $handlerThree);
$this->assertSame($handlers[3], $handlerFour);
$this->assertSame($handlers[0], $handlerFour);
$this->assertSame($handlers[1], $handlerThree);
$this->assertSame($handlers[2], $handlerOne);
$this->assertSame($handlers[3], $handlerTwo);
}

/**
Expand Down Expand Up @@ -238,21 +238,39 @@ public function testLastHandler()

$handlerOne = $this->getHandler();
$handlerTwo = $this->getHandler();
$handlerThree = $this->getHandler();
$handlerFour = $this->getHandler();

$run->pushHandler($handlerOne);
$run->pushHandler($handlerTwo);
$run->prependHandler($handlerTwo);
$run->appendHandler($handlerThree);
$run->appendHandler($handlerFour);

$test = $this;
$handlerOne
$handlerFour
->shouldReceive('handle')
->andReturnUsing(function () use ($test) {
$test->fail('$handlerOne should not be called');
$test->fail('$handlerFour should not be called');
});

$handlerTwo
$handlerThree
->shouldReceive('handle')
->andReturn(Handler::LAST_HANDLER);

$twoRan = false;

$handlerOne
->shouldReceive('handle')
->andReturnUsing(function () use ($test, &$twoRan) {
$test->assertTrue($twoRan);
});

$handlerTwo
->shouldReceive('handle')
->andReturnUsing(function () use (&$twoRan) {
$twoRan = true;
});

$run->handleException($this->getException());

// Reached the end without errors
Expand Down Expand Up @@ -394,7 +412,7 @@ public function testErrorWrappedInException()
$this->assertSame(99, $e->getLine());
}
}

/**
* @covers Whoops\Run::handleException
* @covers Whoops\Run::writeToOutput
Expand Down

0 comments on commit f2eb7d6

Please sign in to comment.