andrewdyer/shutdown-handler
最新稳定版本:0.1.0
Composer 安装命令:
composer require andrewdyer/shutdown-handler
包简介
A shutdown handler for Slim Framework applications that converts fatal errors into consistent HTTP responses through pluggable responder and emitter strategies
README 文档
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.
统计信息
- 总下载量: 39
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-07