Middleware PHP
Walter Dal Mut
@walterdalmut - github.com/wdalmut
About Middleware
function($request,$response){}
Something that does something with a Request and a Response
Thanks for listening
Why middleware is interesting?
Service Oriented Architecture
Micro Services
Something that we can change in couple of weeks
etc. etc.
General principle
Wire components that interacts with the
request and the response
$app = new MiddlewareRunner();
$app­>add(new Versioning());
$app­>add(new Router());
$app­>add(new Authentication());
$app­>add(new Options());
$app­>add(new Authorization());
$app­>add(new Accepts());
$app­>add(new ContentType());
$app­>add(new Parser());
$app­>add(new Params());
$app­>add(new Query());
$app­>add(new Body());
$app­>add(new Dispatcher());
$app­>add(new ProblemHandler());
$app­>run($request, $response);
A picture is worth a thousand words
from StackPHP
We need a good
implementation for Request
and Response
Existing middleware frameworks
StackPHP
Slim Framework
...
Frankie
A Frankenstein framework
https://github.com/wdalmut/frankie
Different framework components
Frankie is my third framework
Simple-MVC
Push & Pull MVC
UpCloo Web Framework
Event-Driven framework with ZF components
Frankie
Middleware with different framework components
https://github.com/wdalmut/simple-mvc
https://github.com/wdalmut/upcloo-web-framework
https://github.com/wdalmut/frankie
Components
acclimate/container
doctrine/annotations
doctrine/cache
doctrine/common
mnapoli/php-di
symfony/routing
symfony/http-foundation
symfony/config
Ideas
Middeware approach
Composition (DiC)
Readability (Annotations)
Not event-driven (state-machine approach)
TDD & Code Design
Middleware
class MyController
{
    public function index(Request $request, Response $response, ...$params) { }
}
Composition
Via acclimate (mixing containers)
$sfContainer = new DicBuilder();
$loader = new XmlFileLoader(
    $sfContainer, new FileLocator(realpath(__DIR__  . '/../'))
);
$loader­>load(realpath(__DIR__ . '/../configs/services.xml'));
$container­>addContainer($acclimate­>acclimate($sfContainer));
//­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­­
$conf = include __DIR__ . '/../configs/services.php';
$serviceManager = new ServiceManager();
$serviceManagerConfigurator = new ServiceManagerConfig($conf["services"]);
$serviceManagerConfigurator­>configureServiceManager($serviceManager);
$serviceManager­>setService("Config", $conf);
$container­>addContainer($acclimate­>acclimate($serviceManager));
Symfony2 DiC and Zend Framework ServiceManager together
All is resolved by
Acclimate/PHP-Di
class User
{
    /**
     * @Inject("orm")
     */
    private $entityManager;
    //...
}
Readability via annotations
/**
 * @Route(name="/v1")
 */
class User
{
    /**
     * @Route(name="/user/{id}", methods={"GET"})
     */
    public function get(Request $request, Response $response, $id)
    {
        //...
    }
}
http://domain.tld/v1/user/12
State-Machine
Middle-out - Read the action in order to compose other steps
/**
 * @Route(name="/v1")
 * @Before(targetClass="One", targetMethod="one")
 * @After(targetClass="Two", targetMethod="two")
 * @After(targetClass="Five", targetMethod="five")
 */
class User
{
    /**
     * @Route(name="/user/{id}", methods={"GET"})
     * @Before(targetClass="Three", targetMethod="three")
     * @After(targetClass="Four", targetMethod="four")
     */
    public function get(Request $request, Response $response, $id) { }
}
State-Machine: (other objects can chain more steps)
One::one => Three::three
User::get
Four::four => Two::two => Five::five
Shortcutting
class Auth
{
    public function basic(Request $request, Response $response)
    {
        $auth = $request­>headers­>get("Authorization");
        //...
        if (!$isValid) {
            $response­>setStatusCode(401);
            return $response;
        }
    }
}
Or more readable
return new ApiProblem(401, "Unauthorized"); //ApiProblem extends Response
Discover more on:
https://github.com/wdalmut/frankie
http://frankie.readthedocs.org/en/develop/
https://github.com/wdalmut/frankie-api
https://github.com/wdalmut/frankie-app
About PHP and Middleware
https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-
7.html
Check-out an example
Thanks for listening

Middleware PHP - A simple micro-framework