cormy/server-middleware

Common interfaces for Cormy PSR-7 server middlewares

0.1.0 2016-11-20 20:39 UTC

This package is not auto-updated.

Last update: 2024-11-10 01:02:34 UTC


README

SensioLabsInsight

🔩 Common interfaces for Cormy PSR-7 server middlewares

Install

composer require cormy/server-middleware

Usage

use Generator;
use Cormy\Server\MiddlewareInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

class Middleware implements MiddlewareInterface
{
    /**
     * Process an incoming server request and return the response, optionally delegating
     * to the next request handler.
     *
     * @param ServerRequestInterface $request
     *
     * @return Generator yields PSR `ServerRequestInterface` instances and returns a PSR `ResponseInterface` instance
     */
    public function __invoke(ServerRequestInterface $request):Generator
    {
        // delegate $request to the next request handler
        $response = yield $request;

        // mofify the response
        $response = $response->withHeader('X-PoweredBy', 'Unicorns');

        return $response;
    }
}

License

MIT © Michael Mayer