StackPHP – HttpKernelInterface based middlewares

onion

The HttpKernelInterface models web request and response as PHP objects, giving them value semantics. Stack is a convention for composing HttpKernelInterface middlewares. By wrapping your application in decorators you can add new behaviour from the outside.

In the screenshot above Session is a decorator wrapped around Authentication which itself is a decorator wrapped around the original $app. The incoming $request is passed through these three layers, one after the other.

The key part of StackPHP is \Symfony\Component\HttpKernel\HttpKernelInterface: each layer must implement it.

<?php namespace Cartisan\Middleware;

use Cartisan\Core\App;  
use Symfony\Component\HttpFoundation\Request;  
use Symfony\Component\HttpKernel\HttpKernelInterface;

class Logger implements HttpKernelInterface  
{
    protected $app;

    public function __construct(HttpKernelInterface $app)
    {
        $this->app = $app;
    }

    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
    {

        // Add logic here to be placed in the request response cycle
        // ...

        // Call handle on inner layer
        return $this->app->handle($request);

    }
}

Stack — HttpKernelInterface based middlewares →
StackPHP Middleware →

(via)