Ciao Symfony2, welcome StackPHP Middlewares Part II – Series

Ok so remembering #desymfony there was a talk regarding optimizations by splitting the kernels so we can have decoupled environments and yet still retain performance. Now with StackPHP Middlewares we don’t have the need to do this anymore, I mean not to split into kernels but I mean to create duplicate code on both kernel apps. You ask for performance? Our answer is with a lazy loaded kernel app! Here me again:

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
 
require_once __DIR__.'/../app/AppKernel.php';
 
$app = new AppKernel('prod', false);
$app->loadClassCache();
$lazyFactoryApp = function() { return require __DIR__.'/admin.php'; };
$stack = (new Stack\Builder)
    ->push('Stack\UrlMap', ['/dev_mode' => new LazyHttpKernel($lazyFactoryApp)]
);
 
$app = $stack->resolve($app);
 
$request = Request::createFromGlobals();
$response = $app->handle($request);
$response->send();
$app->terminate($request, $response);

What we have done is to basically lazy load the admin app via a LazyHttpKernel which is not a vertical way of stacking a kernel but a horizontal stacking which is why it is embedded within the UrlMap kernel mapper array. We can now further stack vertically with more middleware separately the admin app or the main app.

//admin.php
<?php
 
use Stack\LazyHttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
 
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
Debug::enable();
 
require_once __DIR__.'/../app/AppKernel.php';
 
$app = new AppKernel('dev', true);
$app->loadClassCache();
 
return $app;

Next step is to play with perhaps creating a new stack middleware from scratch. Very thankful!

Leave a Reply

Your email address will not be published. Required fields are marked *