Skip to content

Commit

Permalink
Make shutdown handler depth customizable
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jul 11, 2024
1 parent b7c933f commit 23a84f6
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ final class ErrorHandler
private bool $enabled = false;
private bool $initialized = false;

/**
* @param LoggerInterface $logger Logger to write errors to.
* @param ThrowableRendererInterface $defaultRenderer Default throwable renderer.
* @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events.
* @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last.
*/
public function __construct(
private LoggerInterface $logger,
private ThrowableRendererInterface $defaultRenderer,
private ?EventDispatcherInterface $eventDispatcher = null,
private int $exitShutdownHandlerDepth = 2

Check warning on line 54 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 2) + public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 1) { } /**

Check warning on line 54 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 2) + public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 3) { } /**

Check warning on line 54 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 2) + public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 1) { } /**

Check warning on line 54 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 2) + public function __construct(private LoggerInterface $logger, private ThrowableRendererInterface $defaultRenderer, private ?EventDispatcherInterface $eventDispatcher = null, private int $exitShutdownHandlerDepth = 3) { } /**
) {
}

Expand Down Expand Up @@ -207,13 +214,33 @@ private function renderThrowableAndTerminate(Throwable $t): void
echo $this->handle($t);
$this->eventDispatcher?->dispatch(new ApplicationError($t));

register_shutdown_function(static function (): void {
// Ensure exit(1) is called last after all other shutdown functions, even those added to the end.
register_shutdown_function(static function (): void {
register_shutdown_function(static function (): void {
exit(1);
});
});
});
$handler = $this->wrapShutdownHandler(
static function (): void {
exit(1);
},
$this->exitShutdownHandlerDepth
);

register_shutdown_function($handler);
}

/**
* Wraps shutdown handler into another shutdown handler to ensure it is called last after all other shutdown
* functions, even those added to the end.
*
* @param callable $handler Shutdown handler to wrap.
* @param int $depth Wrapping depth.
* @return callable Wrapped handler.
*/
private function wrapShutdownHandler(callable $handler, int $depth): callable
{
$currentDepth = 0;
while ($currentDepth < $depth) {
$handler = static function() use ($handler): void {
register_shutdown_function($handler);
};
$currentDepth++;
}
return $handler;
}
}

0 comments on commit 23a84f6

Please sign in to comment.