httpsoft / http-runner
运行PSR-7组件和构建PSR-15中间件管道
1.1.0
2023-05-05 21:47 UTC
Requires
- php: ^7.4|^8.0
- httpsoft/http-emitter: ^1.1
- psr/container: ^1.0|^2.0
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- devanych/di-container: ^2.1
- httpsoft/http-message: ^1.1
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7
- vimeo/psalm: ^4.9|^5.2
README
此包负责运行 PSR-7 组件并构建 PSR-15 中间件管道。
文档
安装
此包需要PHP版本7.4或更高。
composer require httpsoft/http-runner
使用方法
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!'