andrewdyer / shutdown-handler
A shutdown handler for Slim Framework applications that converts fatal errors into consistent HTTP responses through pluggable responder and emitter strategies
Requires
- php: ^8.3
- psr/http-message: ^2.0
- slim/slim: ^4.15
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.75
- phpunit/phpunit: ^10.5
- slim/psr7: ^1.8
README
Shutdown Handler
A shutdown handler for Slim Framework applications that converts fatal errors into consistent HTTP responses through pluggable responder and emitter strategies.
Introduction
This library provides a shutdown handler for Slim applications. It intercepts fatal errors and transforms them into consistent HTTP responses, maintaining predictable application behaviour. The handler is fully composable, allowing different responder and emitter implementations to be combined as required, and integrates seamlessly with existing Slim error handling and response emission workflows.
Prerequisites
- PHP: Version 8.3 or higher is required.
- Composer: Dependency management tool for PHP.
- Slim Framework: Version 4 is required.
Installation
composer require andrewdyer/shutdown-handler
Getting Started
An error responder and response emitter are required before registering the shutdown handler.
1. Create an error responder
Error responders define how errors are transformed into HTTP responses.
use AndrewDyer\ShutdownHandler\Contracts\ErrorResponderInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Throwable; final class MyErrorResponder implements ErrorResponderInterface { public function createResponse( ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails ): ResponseInterface { // Custom response logic } }
Alternatively, wrap existing logic using CallableErrorResponder:
use AndrewDyer\ShutdownHandler\Adapters\CallableErrorResponder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Throwable; $errorResponder = new CallableErrorResponder( static fn ( ServerRequestInterface $request, Throwable $exception, bool $displayErrorDetails ): ResponseInterface => $httpErrorHandler( $request, $exception, $displayErrorDetails, false, false ) );
The callable must accept a request, exception, and display flag, and return a PSR-7 response.
2. Create a response emitter
Response emitters are responsible for sending responses to the client.
use AndrewDyer\ShutdownHandler\Contracts\ResponseEmitterInterface; use Psr\Http\Message\ResponseInterface; final class MyResponseEmitter implements ResponseEmitterInterface { public function emit(ResponseInterface $response): void { // Custom emit logic } }
Alternatively, wrap an existing emitter using CallableResponseEmitter:
use AndrewDyer\ShutdownHandler\Adapters\CallableResponseEmitter; use Psr\Http\Message\ResponseInterface; $responseEmitter = new CallableResponseEmitter( static fn (ResponseInterface $response): void => $slimEmitter->emit($response) );
The adapter wraps an existing emitter implementation.
Usage
Register the shutdown handler to convert fatal errors into consistent HTTP responses:
use AndrewDyer\ShutdownHandler\ShutdownHandler; $shutdownHandler = new ShutdownHandler( $request, $errorResponder, $responseEmitter, $displayErrorDetails ); register_shutdown_function($shutdownHandler);
The $errorResponder and $responseEmitter values can come from custom implementations or the callable adapters shown in Getting Started.
License
Licensed under the MIT license and is free for private or commercial projects.