Developers/Custom Kernel

From PartKeepr Wiki
Jump to: navigation, search

Sometimes it's required to use additional bundles. Since the bundle list in the AppKernel is pretty much static and there's no plugin system yet, the recommended way is to create a custom kernel:

Basics

# app/MyCustomAppKernel.php
use Symfony\Component\HttpKernel\Kernel;

class MyCustomAppKernel extends AppKernel {
    public function getCustomBundles () {
        $customBundles = [];
        $customBundles[] = new My\Custom\Bundle();
        return $customBundles;
    }
}

Then create a custom app.php which uses the custom app kernel:

<?php

use Symfony\Component\ClassLoader\ApcClassLoader;
use Symfony\Component\HttpFoundation\Request;

$loader = require_once __DIR__.'/../app/bootstrap.php.cache';

// Use APC for autoloading to improve performance.
// Change 'sf2' to a unique prefix in order to prevent cache key conflicts
// with other applications also using APC.
/*
$apcLoader = new ApcClassLoader('sf2', $loader);
$loader->unregister();
$apcLoader->register(true);
*/
require_once __DIR__.'/../app/AppKernel.php';
require_once __DIR__.'/../app/MyCustomAppKernel.php';
//require_once __DIR__.'/../app/AppCache.php';

$kernel = new MyCustomAppKernel('prod', false);
$kernel->loadClassCache();
//$kernel = new AppCache($kernel);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

To avoid overwriting, you can create a file like app_custom.php and set your DirectoryIndex to that file.

Custom Kernel Use Case

A good use case for a custom kernel is if you wish to operate PartKeepr with sessions stored in redis. To do that, add the Redis Bundle to the overridden kernel:

# app/MyCustomAppKernel.php
use Symfony\Component\HttpKernel\Kernel;

class MyCustomAppKernel extends Kernel {
    public function getCustomBundles () {
        $customBundles = [];
        $customBundles[] = new Snc\RedisBundle\SncRedisBundle();
        return $customBundles;
    }
}

Then adjust your app.php and configure the bundle.