Security update to Drupal 8.4.6
[yaffs-website] / vendor / stack / builder / README.md
1 # Stack/Builder
2
3 Builder for stack middlewares based on HttpKernelInterface.
4
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.
7
8 ## Example
9
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:
12
13 ```php
14 use Symfony\Component\HttpKernel\HttpCache\Store;
15
16 $app = new Silex\Application();
17
18 $app->get('/', function () {
19     return 'Hello World!';
20 });
21
22 $app = new Stack\Session(
23     new Symfony\Component\HttpKernel\HttpCache\HttpCache(
24         $app,
25         new Store(__DIR__.'/cache')
26     )
27 );
28 ```
29
30 This can get quite annoying indeed. Stack/Builder simplifies that:
31
32 ```php
33 $stack = (new Stack\Builder())
34     ->push('Stack\Session')
35     ->push('Symfony\Component\HttpKernel\HttpCache\HttpCache', new Store(__DIR__.'/cache'));
36
37 $app = $stack->resolve($app);
38 ```
39 As you can see, by arranging the layers as a stack, they become a lot easier
40 to work with.
41
42 In the front controller, you need to serve the request:
43
44 ```php
45 use Symfony\Component\HttpFoundation\Request;
46
47 $request = Request::createFromGlobals();
48 $response = $app->handle($request)->send();
49 $app->terminate($request, $response);
50 ```
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:
55
56 ```php
57 $stack = (new Stack\Builder())
58     ->push('Stack\Session')
59     ->push(function ($app) {
60         $cache = new HttpCache($app, new Store(__DIR__.'/cache'));
61         return $cache;
62     })
63 ;
64 ```
65
66 ## Inspiration
67
68 * [Rack::Builder](http://rack.rubyforge.org/doc/Rack/Builder.html)
69 * [HttpKernel middlewares](https://igor.io/2013/02/02/http-kernel-middlewares.html)