andrewdyer/shutdown-handler

A Slim library for converting fatal errors into HTTP responses using configurable responders and emitters

Maintainers

Package info

github.com/andrewdyer/shutdown-handler

pkg:composer/andrewdyer/shutdown-handler

Statistics

Installs: 81

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

0.1.3 2026-06-02 22:52 UTC

This package is auto-updated.

Last update: 2026-06-02 22:53:33 UTC


README

A Slim library for converting fatal errors into HTTP responses using configurable responders and emitters.

Latest Stable Version Total Downloads License PHP Version Require

Introduction

This library provides a shutdown handler for Slim Framework applications by intercepting fatal errors and converting them into consistent HTTP responses using configurable responder and emitter strategies. The handler is fully composable, supports multiple implementations, and integrates 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.