Kernel.php 2.04 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?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\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

class Kernel extends BaseKernel
{
    use MicroKernelTrait;

16
    private const CONFIG_EXTS = '.{php,xml,yaml,yml}';
Julien Jorry committed
17

18
    public function registerBundles(): iterable
Julien Jorry committed
19
    {
20
        $contents = require $this->getProjectDir() . '/config/bundles.php';
Julien Jorry committed
21
        foreach ($contents as $class => $envs) {
22
            if ($envs[$this->environment] ?? $envs['all'] ?? false) {
Julien Jorry committed
23 24 25 26 27
                yield new $class();
            }
        }
    }

28
    public function getProjectDir(): string
Julien Jorry committed
29
    {
30
        return \dirname(__DIR__);
Julien Jorry committed
31 32
    }

33
    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
Julien Jorry committed
34
    {
35 36 37 38
        $container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
        $container->setParameter('container.dumper.inline_class_loader', \PHP_VERSION_ID < 70400 || $this->debug);
        $container->setParameter('container.dumper.inline_factories', true);
        $confDir = $this->getProjectDir().'/config';
Julien Jorry committed
39

40 41 42 43
        $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');
Julien Jorry committed
44 45
    }

46
    protected function configureRoutes(RouteCollectionBuilder $routes): void
Julien Jorry committed
47
    {
48
        $confDir = $this->getProjectDir() . '/config';
Julien Jorry committed
49

50 51 52
        $routes->import($confDir.'/{routes}/'.$this->environment.'/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS, '/', 'glob');
        $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS, '/', 'glob');
Julien Jorry committed
53 54
    }
}