ds / middleware
该包已被弃用且不再维护。未建议替换包。
PSR 中间件运行器
v1.0.0
2017-03-14 13:15 UTC
Requires
- container-interop/container-interop: 1.*.*
- psr/log: ^1.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: 5.1.6
This package is not auto-updated.
Last update: 2018-01-29 11:24:30 UTC
README
中间件可以是
-
`Ds\Middleware\MiddlewareInterface` 的实例
-
扩展 `Ds/Middleware/Middleware`
-
闭包,以下签名
function(ServerRequest $request, ResponseInterface $response, callable $next = null){
//code before calling next middleware. if ($next){ $next($request, $response) } //code after calling next middleware. return $response;
}
堆栈
堆栈是中间件的命名集合
$stack = new Ds\Middleware\Stack();
$stack->withNamespace('My\Class\Namespace');
//The following with create a new stack called 'stackId'
//'MyClass::MyMethod' has been added to the following collection: 'stackName'
//'MyClass2::MyMethod2' has been added to both collections: 'stackName', 'altName'
$stack->withMiddleware('MyClass::MyMethod', 'stackId', ['stackName'])
$stack->withMiddleware('MyClass2::MyMethod2', 'stackId', ['stackName','altName'])
管道
管道执行堆栈并返回 PSR ResponseInterface
$pipe = new Ds\Middleware\Pipe();
$pipe = $pipe->withContainer($container)
$middlewareQueue = $pipe->fromStack($stack, 'stackId', 'stackName');
//call using invoke
$response = $middlewareQueue($request, $response);
//call using method 'execute'
$response = $middlewareQueue->execute($reqest, $response);