3 Builder for stack middlewares based on HttpKernelInterface.
5 Stack/Builder is a small library that helps you construct a nested
6 HttpKernelInterface decorator tree. It models it as a stack of middlewares.
10 If you want to decorate a [silex](https://github.com/fabpot/Silex) app with
11 session and cache middlewares, you'll have to do something like this:
14 use Symfony\Component\HttpKernel\HttpCache\Store;
16 $app = new Silex\Application();
18 $app->get('/', function () {
19 return 'Hello World!';
22 $app = new Stack\Session(
23 new Symfony\Component\HttpKernel\HttpCache\HttpCache(
25 new Store(__DIR__.'/cache')
30 This can get quite annoying indeed. Stack/Builder simplifies that:
33 $stack = (new Stack\Builder())
34 ->push('Stack\Session')
35 ->push('Symfony\Component\HttpKernel\HttpCache\HttpCache', new Store(__DIR__.'/cache'));
37 $app = $stack->resolve($app);
39 As you can see, by arranging the layers as a stack, they become a lot easier
42 In the front controller, you need to serve the request:
45 use Symfony\Component\HttpFoundation\Request;
47 $request = Request::createFromGlobals();
48 $response = $app->handle($request)->send();
49 $app->terminate($request, $response);
51 Stack/Builder also supports pushing a `callable` on to the stack, for situations
52 where instantiating middlewares might be more complicated. The `callable` should
53 accept a `HttpKernelInterface` as the first argument and should also return a
54 `HttpKernelInterface`. The example above could be rewritten as:
57 $stack = (new Stack\Builder())
58 ->push('Stack\Session')
59 ->push(function ($app) {
60 $cache = new HttpCache($app, new Store(__DIR__.'/cache'));
68 * [Rack::Builder](http://rack.rubyforge.org/doc/Rack/Builder.html)
69 * [HttpKernel middlewares](https://igor.io/2013/02/02/http-kernel-middlewares.html)