stack/builder

This package is abandoned and no longer maintained. No replacement package was suggested.

Builder for stack middleware based on HttpKernelInterface.

Maintainers

👁 igorw

Package info

github.com/stackphp/builder

pkg:composer/stack/builder

Statistics

Installs: 43 582 685

Dependents: 51

Suggesters: 3

Stars: 295

Open Issues: 1

v1.0.6 2020-01-30 12:17 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT a4faaa6f532c6086bc66c29e1bc6c29593e1ca7c

  • Igor Wiedler <igor.woop@wiedler.ch>

stack

This package is not auto-updated.

Last update: 2024-08-09 13:33:01 UTC


README

Builder for stack middlewares based on HttpKernelInterface.

Stack/Builder is a small library that helps you construct a nested HttpKernelInterface decorator tree. It models it as a stack of middlewares.

Example

If you want to decorate a silex app with session and cache middlewares, you'll have to do something like this:

use Symfony\Component\HttpKernel\HttpCache\Store;

$app = new Silex\Application();

$app->get('/', function () {
 return 'Hello World!';
});

$app = new Stack\Session(
 new Symfony\Component\HttpKernel\HttpCache\HttpCache(
 $app,
 new Store(__DIR__.'/cache')
 )
);

This can get quite annoying indeed. Stack/Builder simplifies that:

$stack = (new Stack\Builder())
 ->push('Stack\Session')
 ->push('Symfony\Component\HttpKernel\HttpCache\HttpCache', new Store(__DIR__.'/cache'));

$app = $stack->resolve($app);

As you can see, by arranging the layers as a stack, they become a lot easier to work with.

In the front controller, you need to serve the request:

use Symfony\Component\HttpFoundation\Request;

$request = Request::createFromGlobals();
$response = $app->handle($request)->send();
$app->terminate($request, $response);

Stack/Builder also supports pushing a callable on to the stack, for situations where instantiating middlewares might be more complicated. The callable should accept a HttpKernelInterface as the first argument and should also return a HttpKernelInterface. The example above could be rewritten as:

$stack = (new Stack\Builder())
 ->push('Stack\Session')
 ->push(function ($app) {
 $cache = new HttpCache($app, new Store(__DIR__.'/cache'));
 return $cache;
 })
;

Inspiration