ztsu/reacon

PSR-7 middleware runner compatible with PSR-15

1.0.0 2018-03-07 08:03 UTC

This package is not auto-updated.

Last update: 2021-05-09 15:25:00 UTC


README

Build Status Coverage Status

The simplest PSR-15 complaint middleware runner.

The PSR-15 providers a standard for recommendations that defines the interface for server middleware-component compatible with PSR-7 HTTP Messages.

There are a few of another PSR-15 compatible dispatchers. Middleman is the best known. Also there are a lot of PSR-15 middleware-components collected in the middlewares/psr15-middlewares.

Installation

Via Composer:

$ composer require ztsu/reacon

Usage

<?php

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

class CreateRequestMiddleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        return new Zend\Diactoros\Response();
    }
}

class HelloMiddleware implements MiddlewareInterface
{
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        $response = $handler->handle($request);

        $response->getBody()->write("Hello, World!");

        return $response;
    }
}

$reacon = new Ztsu\Reacon\Reacon(
    new HelloMiddleware(),
    new CreateRequestMiddleware()
);

$request = Zend\Diactoros\ServerRequestFactory::fromGlobals();

$response = $reacon->handle($request);

(new Zend\Diactoros\Response\SapiEmitter)->emit($response);

License

MIT.