mehr-it/eli-middleware-chain

Chaining for PSR-15 middleware

1.0.1 2020-01-09 06:06 UTC

This package is auto-updated.

Last update: 2024-04-26 07:37:27 UTC


README

Latest Version on Packagist Build Status

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(),
]);