<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
// Those prefixes are used to determine EasyAdmin configuration and cache folder to use.
public const BACKENDS_PREFIXES = ['system', 'admin', 'manager', 'agent', 'customer'];
protected static $backendContext;
/**
* @override
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) {
if($type == HttpKernelInterface::MASTER_REQUEST && static::$backendContext == null) {
$request_uri = explode('/', $request->server->get('REQUEST_URI'));
$uri_context = $request_uri[1];
if(in_array($uri_context, self::BACKENDS_PREFIXES)) {
static::$backendContext = $uri_context;
}
}
return parent::handle($request, $type, $catch);
}
public function getCacheDir()
{
// Cache folders exemples : '/var/cache/dev/system' or '/var/cache/prod/agent' or, by default '/var/cache/dev/front'
return $this->getProjectDir().'/var/cache/' . $this->environment . (static::$backendContext != null ? '/' . static::$backendContext : '/front');
}
public function getLogDir()
{
return $this->getProjectDir().'/var/log';
}
public function registerBundles()
{
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
// Feel free to remove the "container.autowiring.strict_mode" parameter
// if you are using symfony/dependency-injection 4.0+ as it's the default behavior
$container->setParameter('container.autowiring.strict_mode', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir().'/config';
$loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS, 'glob');
// EasyAdmin dynamic configuration file
if(static::$backendContext != null) {
$loader->load($confDir . '/backends/' . static::$backendContext . '.yaml');
}
}
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$confDir = $this->getProjectDir().'/config';
$routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
$routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
// Easy admin dynamic route prefixing
if(static::$backendContext != null) {
$routes->import('App\Controller\Backend\Ea' . ucfirst(static::$backendContext) . 'Controller', static::$backendContext, 'annotation');
}
}
}