# PHP package to run the PSR-7 and PSR-15 components
The HttpSoft\Runner package is responsible for running PSR-7 components and building PSR-15 middleware pipelines.
This package requires PHP version 7.4 or later.
Package installation:
composer require httpsoft/http-runner
# API
- HttpSoft\Runner\MiddlewarePipeline — class, that implements HttpSoft\Runner\MiddlewarePipelineInterface.
- HttpSoft\Runner\MiddlewareResolver — class, that implements HttpSoft\Runner\MiddlewareResolverInterface.
- HttpSoft\Runner\ServerRequestRunner — class, that runs the Psr\Http\Message\ServerRequestInterface implementation and emits the client an instance of Psr\Http\Message\ResponseInterface.
Exceptions:
- HttpSoft\Runner\Exception\EmptyMiddlewarePipelineException.
- HttpSoft\Runner\Exception\InvalidMiddlewareResolverHandlerException.
# Usage
use HttpSoft\Runner\MiddlewarePipeline;
use HttpSoft\Runner\MiddlewareResolver;
use HttpSoft\Runner\ServerRequestRunner;
use Psr\Http\Message\ResponseInterface;
/**
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
* @var Psr\Http\Server\MiddlewareInterface $siteMiddleware
* @var Psr\Http\Server\MiddlewareInterface $userMiddleware
*/
// Basic usage
$runner = new ServerRequestRunner();
$runner->run($request, $handler);
// Using `MiddlewarePipeline`
$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');
$runner = new ServerRequestRunner($pipeline);
$runner->run($request, $handler);
// Using `MiddlewareResolver`
$resolver = new MiddlewareResolver();
$handlerMiddleware = $resolver->resolve(function (): ResponseInterface {
$response = new HttpSoft\Message\Response();
$response->getBody()->write('Hello world!');
return $response;
});
$pipeline = new MiddlewarePipeline();
$pipeline->pipe($siteMiddleware);
$pipeline->pipe($userMiddleware, '/user');
$pipeline->pipe($handlerMiddleware);
$runner = new ServerRequestRunner($pipeline);
$runner->run($request); // Output result: 'Hello world!'
Detailed description of using HttpSoft\Runner\MiddlewarePipeline
see here.
Detailed description of using HttpSoft\Runner\MiddlewareResolver
see here.
Detailed description of using HttpSoft\Runner\ServerRequestRunner
see here.