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';
require_once __DIR__.'/../app/AppKernel.php';
$app = new AppKernel('prod', false);
$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);
$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.

use Stack\LazyHttpKernel;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Debug\Debug;
$loader = require_once __DIR__.'/../app/bootstrap.php.cache';
require_once __DIR__.'/../app/AppKernel.php';
$app = new AppKernel('dev', true);
return $app;

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

One thought on “Ciao Symfony2, welcome StackPHP Middlewares Part II – Series

Leave a Reply

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