From 62ceedeccb1c00c5ffc9f081a795a4a0d17a91b5 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Fri, 9 Aug 2013 23:27:35 +0200 Subject: [PATCH 1/4] Start work on example page --- source/_views/default.html | 3 +++ source/example.html | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 source/example.html diff --git a/source/_views/default.html b/source/_views/default.html index f1977ef..fbd18a2 100644 --- a/source/_views/default.html +++ b/source/_views/default.html @@ -21,6 +21,9 @@
  • Home
  • +
  • + Example +
  • Middlewares
  • diff --git a/source/example.html b/source/example.html new file mode 100644 index 0000000..70655df --- /dev/null +++ b/source/example.html @@ -0,0 +1,46 @@ +--- +layout: default +title: Example +nav_name: example +--- +{% block content %} +
    +

    Creating middlewares

    +

    Creating a new middleware is generally quite easy. The hardest part is grasping the decorator pattern.

    +

    A decorator is simply an object that wraps around an other object. In this case a middleware is a decorator for HttpKernelInterface. The decorator wraps around a kernel, and exposes the same interface.

    +

    Let's take a look at an Append middleware, that appends a pre-configured string to all response bodies.

    +
    {%- filter escape -%}use Symfony\Component\HttpFoundation\Request;
    +use Symfony\Component\HttpKernel\HttpKernelInterface;
    +
    +class Append implements HttpKernelInterface
    +{
    +    protected $app;
    +    protected $append;
    +
    +    public function __construct(HttpKernelInterface $app, $append)
    +    {
    +        $this->app = $app;
    +        $this->append = $append;
    +    }
    +
    +    public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
    +    {
    +        $response = $this->app->handle($request, $type, $catch);
    +        $response->setContent($response->getContent().$this->append);
    +        return $response;
    +    }
    +}{%- endfilter -%}
    +

    This middleware takes a kernel in the constructor and delegates to this kernel from within handle. Then it augments the response. It decorates the handle call.

    +

    Here is an example of how you could use this middleware with Silex. Note that you can replace Silex with any kernel implementation.

    +
    {%- filter escape -%}$app = new Silex\Application();
    +
    +$app->get('/', function () {
    +    return 'Hello World!';
    +});
    +
    +$app = new Append($app, ' FOO');
    +
    +Stack\run($app);{%- endfilter -%}
    +

    This will return a response body of "Hello World! FOO" when accessing the / resource.

    +
    +{% endblock %} From 0df5599a56935f3e207681ab4446aa2e3d6a5ad9 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 14 Aug 2013 11:53:54 +0200 Subject: [PATCH 2/4] Merge toolbox with middlewares page --- source/_views/default.html | 3 --- source/middlewares.html | 52 ++++++++++++++++++++++++++++++++++++-- source/toolbox.html | 46 --------------------------------- 3 files changed, 50 insertions(+), 51 deletions(-) delete mode 100644 source/toolbox.html diff --git a/source/_views/default.html b/source/_views/default.html index fbd18a2..0b1d849 100644 --- a/source/_views/default.html +++ b/source/_views/default.html @@ -27,9 +27,6 @@
  • Middlewares
  • -
  • - Toolbox -
  • Conventions
  • diff --git a/source/middlewares.html b/source/middlewares.html index dbb5f48..f241508 100644 --- a/source/middlewares.html +++ b/source/middlewares.html @@ -1,11 +1,35 @@ --- layout: default -title: Community Middlewares +title: Middlewares nav_name: middlewares --- {% block content %}
    -

    Community Middlewares

    +

    Middlewares

    +
    +
    +

    Session

    +

    + by stack +

    +

    Provides a request session for subsequent middlewares. Based on the Silex SessionServiceProvider.

    + +
    +
    +

    UrlMap

    +

    + by stack +

    +

    Provides the ability to map paths to specific HttpKernelInterface applications and dispatches accordingly.

    + +
    +

    HttpCache

    @@ -103,4 +127,28 @@

    + +
    + +
    +

    Toolbox

    +
    +
    +

    Builder

    +

    Builder constructs a nested HttpKernelInterface decorator tree. It models it as a stack of middlewares.

    + +
    +
    +

    Lazy Kernel

    +

    A lazy proxy for HttpKernelInterface, allows an app to be loaded on demand when combined with URL Map.

    + +
    +
    +
    {% endblock %} diff --git a/source/toolbox.html b/source/toolbox.html deleted file mode 100644 index 276d650..0000000 --- a/source/toolbox.html +++ /dev/null @@ -1,46 +0,0 @@ ---- -layout: default -title: The Toolbox -nav_name: toolbox ---- -{% block content %} -
    -

    The Toolbox

    -
    -
    -

    Builder

    -

    Builder constructs a nested HttpKernelInterface decorator tree. It models it as a stack of middlewares.

    - -
    -
    -

    Session

    -

    Provides a request session for subsequent middlewares. Based on the Silex SessionServiceProvider.

    - -
    -
    -
    -
    -

    URL Map

    -

    Provides the ability to map paths to specific HttpKernelInterface applications and dispatches accordingly.

    - -
    -
    -

    Lazy Kernel

    -

    A lazy proxy for HttpKernelInterface, allows an app to be loaded on demand when combined with URL Map.

    - -
    -
    -
    -{% endblock %} From ed4e68168e68323897bcf4704d7dfff2b5e38211 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 14 Aug 2013 12:04:33 +0200 Subject: [PATCH 3/4] Use simpler Powered-By header --- source/example.html | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/source/example.html b/source/example.html index 70655df..467c7b4 100644 --- a/source/example.html +++ b/source/example.html @@ -4,29 +4,27 @@ nav_name: example --- {% block content %} -
    -

    Creating middlewares

    +
    +

    Middlewares

    Creating a new middleware is generally quite easy. The hardest part is grasping the decorator pattern.

    A decorator is simply an object that wraps around an other object. In this case a middleware is a decorator for HttpKernelInterface. The decorator wraps around a kernel, and exposes the same interface.

    -

    Let's take a look at an Append middleware, that appends a pre-configured string to all response bodies.

    +

    Let's take a look at an PoweredByStack middleware, that adds a `X-Powered-By` header to all responses.

    {%- filter escape -%}use Symfony\Component\HttpFoundation\Request;
     use Symfony\Component\HttpKernel\HttpKernelInterface;
     
    -class Append implements HttpKernelInterface
    +class PoweredByStack implements HttpKernelInterface
     {
    -    protected $app;
    -    protected $append;
    +    private $app;
     
    -    public function __construct(HttpKernelInterface $app, $append)
    +    public function __construct(HttpKernelInterface $app)
         {
             $this->app = $app;
    -        $this->append = $append;
         }
     
         public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
         {
             $response = $this->app->handle($request, $type, $catch);
    -        $response->setContent($response->getContent().$this->append);
    +        $response->headers->set('X-Powered-By', 'Stack');
             return $response;
         }
     }{%- endfilter -%}
    @@ -38,9 +36,9 @@

    Creating middlewares

    return 'Hello World!'; }); -$app = new Append($app, ' FOO'); +$app = new PoweredByStack($app); Stack\run($app);{%- endfilter -%} -

    This will return a response body of "Hello World! FOO" when accessing the / resource.

    +

    This will return a "Hello World" body, but have the extra `X-Powered-By: Stack` header.

    {% endblock %} From 08d85851e3b36ba8d2e87f506bb9f3b325dff32d Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 14 Aug 2013 12:44:05 +0200 Subject: [PATCH 4/4] Add stack/run --- source/middlewares.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/source/middlewares.html b/source/middlewares.html index f241508..98c64dc 100644 --- a/source/middlewares.html +++ b/source/middlewares.html @@ -141,6 +141,16 @@

    Builder

    Packagist
    +
    +

    Run

    +

    Shortcut function for handling HttpKernel front-controller boilerplate.

    + +
    + +

    Lazy Kernel

    A lazy proxy for HttpKernelInterface, allows an app to be loaded on demand when combined with URL Map.