This repository has been archived on 2025-01-19. You can view files and clone it, but cannot push or open issues or pull requests.
drupalcampbristol/vendor/stack/builder
2016-10-06 15:16:27 -07:00
..
src/Stack Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663 2015-10-08 15:13:01 -07:00
.gitignore Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663 2015-10-08 15:13:01 -07:00
.travis.yml Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663 2015-10-08 15:13:01 -07:00
CHANGELOG.md Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0 2016-10-06 15:16:27 -07:00
composer.json Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0 2016-10-06 15:16:27 -07:00
composer.lock Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0 2016-10-06 15:16:27 -07:00
LICENSE Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663 2015-10-08 15:13:01 -07:00
phpunit.xml.dist Update to drupal 8.0.0-rc1. For more information, see https://www.drupal.org/node/2582663 2015-10-08 15:13:01 -07:00
README.md Update to Drupal 8.2.0. For more information, see https://www.drupal.org/project/drupal/releases/8.2.0 2016-10-06 15:16:27 -07:00

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:

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