, (*1)
:nut_and_bolt: Cormy PSR-7 server middleware dispatcher, (*2)
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)
/**
* Process an incoming server request and return the response.
*
* @param ServerRequestInterface $request
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request):ResponseInterface;
License
MIT © Michael Mayer, (*3)