Kernel.php 5.56 KB
Newer Older
Julien Jorry committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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\Dotenv\Dotenv;
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}';

    public function getCacheDir()
    {
21
        return $this->getProjectDir() . '/var/cache/' . $this->environment;
Julien Jorry committed
22 23 24 25
    }

    public function getLogDir()
    {
26
        return $this->getProjectDir() . '/var/log';
Julien Jorry committed
27 28 29 30
    }

    public function registerBundles()
    {
31
        $contents = require $this->getProjectDir() . '/config/bundles.php';
Julien Jorry committed
32 33 34 35 36 37 38 39 40
        foreach ($contents as $class => $envs) {
            if (isset($envs['all']) || isset($envs[$this->environment])) {
                yield new $class();
            }
        }
    }

    protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
    {
41
        $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
Julien Jorry committed
42 43 44 45
        // 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);
46
        $confDir = $this->getProjectDir() . '/config';
Julien Jorry committed
47

48 49 50 51
        $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
52 53 54 55
    }

    protected function configureRoutes(RouteCollectionBuilder $routes)
    {
56
        $confDir = $this->getProjectDir() . '/config';
Julien Jorry committed
57

58 59 60
        $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');
Julien Jorry committed
61 62 63 64 65 66 67 68 69 70 71
    }

    public static function bootstrapCli(array &$argv)
    {
        // consume --env and --no-debug from the command line

        // when using symfony/console v4.2 or higher, this should
        // be replaced by a call to Application::bootstrapEnv()

        for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
            if ('--no-debug' === $v) {
72
                putenv('APP_DEBUG=' . $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
Julien Jorry committed
73 74 75 76 77 78 79 80 81 82
                $argvUnset[$i] = true;
                break;
            }
        }

        for ($i = 0; $i < \count($argv) && '--' !== $v = $argv[$i]; ++$i) {
            if (!$v || '-' !== $v[0] || !preg_match('/^-(?:-env(?:=|$)|e=?)(.*)$/D', $v, $v)) {
                continue;
            }
            if (!empty($v[1]) || !empty($argv[1 + $i])) {
83
                putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = empty($v[1]) ? $argv[1 + $i] : $v[1]);
Julien Jorry committed
84 85 86 87 88 89 90 91 92 93 94 95 96
                $argvUnset[$i] = $argvUnset[$i + empty($v[1])] = true;
            }
            break;
        }

        if (!empty($argvUnset)) {
            $argv = array_values(array_diff_key($argv, $argvUnset));
        }
    }

    public static function bootstrapEnv($env = null)
    {
        if (null !== $env) {
97
            putenv('APP_ENV=' . $_SERVER['APP_ENV'] = $env);
Julien Jorry committed
98 99 100 101 102 103 104 105 106
        }

        if ('prod' !== $_SERVER['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) {
            if (!class_exists(Dotenv::class)) {
                throw new \RuntimeException('The "APP_ENV" environment variable is not defined. You need to set it or run "composer require symfony/dotenv" to load it from a ".env" file.');
            }

            // when using symfony/dotenv v4.2 or higher, this call and the related methods
            // below should be replaced by a call to the new Dotenv::loadEnv() method
107
            self::loadEnv(new Dotenv(), \dirname(__DIR__) . '/.env');
Julien Jorry committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        }

        $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
        $_SERVER['APP_DEBUG'] = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : (isset($_ENV['APP_DEBUG']) ? $_ENV['APP_DEBUG'] : 'prod' !== $_SERVER['APP_ENV']);
        $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' : '0';
    }

    private static function loadEnv(Dotenv $dotenv, $path)
    {
        if (file_exists($path) || !file_exists($p = "$path.dist")) {
            $dotenv->load($path);
        } else {
            $dotenv->load($p);
        }

        if (null === $env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) {
124
            $dotenv->populate(['APP_ENV' => $env = 'dev']);
Julien Jorry committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        }

        if ('test' !== $env && file_exists($p = "$path.local")) {
            $dotenv->load($p);
            $env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : $env);
        }

        if (file_exists($p = "$path.$env")) {
            $dotenv->load($p);
        }

        if (file_exists($p = "$path.$env.local")) {
            $dotenv->load($p);
        }
    }
}