cormy/server-middleware-dispatcher

Cormy PSR-7 server middleware dispatcher

0.1.0 2016-11-23 10:13 UTC

This package is not auto-updated.

Last update: 2024-11-09 20:45:18 UTC


README

SensioLabsInsight

🔩 Cormy PSR-7 server middleware dispatcher

Install

composer require cormy/server-middleware-dispatcher

Usage

use Cormy\Server\MiddlewareDispatcher;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

$middleware = function (ServerRequestInterface $request):\Generator {
    // delegate $request to the next request handler, i.e. the $finalHandler below
    $response = (yield $request);

    return $response->withHeader('X-PoweredBy', 'Unicorns');
};

$finalHandler = function (ServerRequestInterface $request):ResponseInterface {
    return new \Zend\Diactoros\Response();
};

// create a dispatcher
$dispatcher = new MiddlewareDispatcher($middleware, $finalHandler);

// dispatch a request
$response = $dispatcher(new \Zend\Diactoros\ServerRequest());

API

Cormy\Server\MiddlewareDispatcher implements Cormy\Server\RequestHandlerInterface

MiddlewareDispatcher::__construct

/**
 * Create a Cormy PSR-7 server middleware dispatcher.
 *
 * @param callable|MiddlewareInterface     $middleware
 * @param callable|RequestHandlerInterface $finalHandler
 */
public function __construct(callable $middleware, callable $finalHandler)

Inherited from Cormy\Server\RequestHandlerInterface::__invoke

/**
 * Process an incoming server request and return the response.
 *
 * @param ServerRequestInterface $request
 *
 * @return ResponseInterface
 */
public function __invoke(ServerRequestInterface $request):ResponseInterface;

Related

License

MIT © Michael Mayer