idiosyncratic/http-runner

PSR-15 HTTP Request runner

0.2.0 2019-11-30 08:47 UTC

This package is auto-updated.

Last update: 2024-03-29 03:58:18 UTC


README

Library for running an HTTP request and emitting the generated response.

Inspired by zend-httphandlerrunner, this library provides a class for executing PSR-15 request handlers and emitting the generated PSR-7 response back to the waiting client. Differences from
the Zend library include:

  • Requires an implementation of Idiosyncratic\Http\Runner\ServerRequestFactory to creates the initial PSR-7 ServerRequestInterface instance instead of accepting a callable
  • Internally, uses output buffering to suppress output to the client, including headers, until the final response is emitted. As a result, all headers and cookies (including for instance the PHP session cookie) must be set in the final ResponseInterface instance or they will not be sent to the client
  • The interface for the response emitter (Idiosyncratic\Http\Runner\ResponseEmitter) is not stackable

Installation

Install using Composer:

composer require idiosyncratic/http-runner

Usage

In order to user this library, you will need to provide:

  • An implementation of Idiosyncratic\Http\Runner\ServerRequestFactory to create the initial PSR-7 ServerRequestInterface instance
  • An implementation of Psr\Http\Server\RequestHandlerInterface to handle generating a response
  • An implementation of Idiosyncratic\Http\Runner\ResponseEmitter to emit a PSR-7 ResponseInterface to the client. A basic Idiosyncratic\Http\Runner\PhpSapiResponseEmitter is included.
  • An implementation of Psr\Log\LoggerInterface to log uncaught exceptions
use Idiosyncratic\Http\Runner\Runner;
use Idiosyncratic\Http\Runner\PhpSapiResponseEmitter;
use Psr\Log\NullLogger();

$runner = new Runner(
    $serverRequestFactoryImplementation,
    $requestHandlerImplementation,
    new PhpSapiResponseEmitter(),
    new NullLogger()
);

$runner->run();