Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Repository files navigation

Stack/Builder

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:

useSymfony\Component\HttpKernel\HttpCache\Store;
$app = newSilex\Application();
$app->get('/', function () {
return'Hello World!';
});
$app = newStack\Session(
newSymfony\Component\HttpKernel\HttpCache\HttpCache(
$app,
newStore(__DIR__.'/cache')
)
);

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

$stack = (newStack\Builder())
->push('Stack\Session')
->push('Symfony\Component\HttpKernel\HttpCache\HttpCache', newStore(__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:

useSymfony\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 = (newStack\Builder())
->push('Stack\Session')
->push(function ($app) {
$cache = newHttpCache($app, newStore(__DIR__.'/cache'));
return$cache;
})
;

Inspiration

About

Builder for stack middlewares based on HttpKernelInterface.

Resources

Stars

294 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages