cormy / 洋葱
使用生成器的洋葱风格PSR-7中间件栈
0.2.0
2016-12-04 10:08 UTC
Requires
- php: >=7
- cormy/server-middleware: ^0.1.0
- cormy/server-request-handler: ^0.1.0
- psr/http-message: ^1.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-15 00:34:25 UTC
README
🌷 洋葱风格的 PSR-7 中间件栈,使用生成器
安装
composer require cormy/onion
用法
use Cormy\Server\Onion; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; // create the core of the onion, i.e. the innermost request handler $core = function (ServerRequestInterface $request):ResponseInterface { return new \Zend\Diactoros\Response(); }; // create some scales (aka middlewares) to wrap around the core $scales = []; $scales[] = function (ServerRequestInterface $request):\Generator { // delegate $request to the next request handler, i.e. $core $response = (yield $request); return $response->withHeader('content-type', 'application/json; charset=utf-8'); }; $scales[] = function (ServerRequestInterface $request):\Generator { // delegate $request to the next request handler, i.e. the middleware right above $response = (yield $request); return $response->withHeader('X-PoweredBy', 'Unicorns'); }; // create an onion style middleware stack $middlewareStack = new Onion($core, ...$scales); // and process an incoming server request $response = $middlewareStack(new \Zend\Diactoros\ServerRequest());
API
Cormy\Server\Onion实现了RequestHandlerInterface
Onion::__construct
/** * Constructs an onion style PSR-7 middleware stack. * * @param RequestHandlerInterface|callable $core the innermost request handler * @param (MiddlewareInterface|RequestHandlerInterface|callable)[] $scales the middlewares to wrap around the core */ public function __construct(callable $core, callable ...$scales)
继承自 RequestHandlerInterface::__invoke
/** * Process an incoming server request and return the response. * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request):ResponseInterface
相关
- Cormy\Server\Bamboo – 使用生成器的Bamboo风格PSR-7 中间件管道
- Cormy\Server\MiddlewareDispatcher – Cormy PSR-7服务器 中间件分发器
- Cormy\Server\RequestHandlerInterface – PSR-7服务器请求处理器的通用接口
- Cormy\Server\MiddlewareInterface – Cormy PSR-7服务器中间件的通用接口
许可证
MIT © Michael Mayer