mehr-it / eli-middleware-chain
Chaining for PSR-15 middleware
1.0.1
2020-01-09 06:06 UTC
Requires
- php: >=7.1.0
- psr/http-server-handler: ^1.0
- psr/http-server-middleware: ^1.0
Requires (Dev)
- phpunit/phpunit: ^7.4|^8.0
This package is auto-updated.
Last update: 2024-10-26 08:38:09 UTC
README
When defining a request processing chain, usually more than one middleware is involved into the
processing chain. The ChainHandler
allows to define the PSR-15 middleware processing stack as
array or iterator:
$chain = new ChainHandler([
new MiddlewareA(),
new MiddlewareB(),
], $next);
This makes code much more readable and allows easy dynamic configuration of the middleware chain.
To create middleware instances on the fly - only when needed - resolver functions may be used:
$chain = new ChainHandler([
function() { return new MiddlewareA(); },
function() { return new MiddlewareB(); },
], $next);
Middleware instead of handler
Sometimes a middleware chain is required as middleware itself. The ChainMiddleware
can be used
for such purposes. It's usage is straightforward as the ChainHandler
:
$chain = new ChainMiddleware([
new MiddlewareA(),
new MiddlewareB(),
]);