src/Kernel.php line 29

Open in your IDE?
  1. <?php
  2. namespace App;
  3. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  4. use Symfony\Component\Config\Loader\LoaderInterface;
  5. use Symfony\Component\Config\Resource\FileResource;
  6. use Symfony\Component\DependencyInjection\ContainerBuilder;
  7. use Symfony\Component\Dotenv\Dotenv;
  8. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  9. use Symfony\Component\Routing\RouteCollectionBuilder;
  10. class Kernel extends BaseKernel
  11. {
  12.     use MicroKernelTrait;
  13.     const CONFIG_EXTS '.{php,xml,yaml,yml}';
  14.     public function getCacheDir()
  15.     {
  16.         return $this->getProjectDir().'/var/cache/'.$this->environment;
  17.     }
  18.     public function getLogDir()
  19.     {
  20.         return $this->getProjectDir().'/var/log';
  21.     }
  22.     public function registerBundles()
  23.     {
  24.         $contents = require $this->getProjectDir().'/config/bundles.php';
  25.         foreach ($contents as $class => $envs) {
  26.             if (isset($envs['all']) || isset($envs[$this->environment])) {
  27.                 yield new $class();
  28.             }
  29.         }
  30.     }
  31.     protected function configureContainer(ContainerBuilder $containerLoaderInterface $loader)
  32.     {
  33.         $container->addResource(new FileResource($this->getProjectDir().'/config/bundles.php'));
  34.         // Feel free to remove the "container.autowiring.strict_mode" parameter
  35.         // if you are using symfony/dependency-injection 4.0+ as it's the default behavior
  36.         $container->setParameter('container.autowiring.strict_mode'true);
  37.         $container->setParameter('container.dumper.inline_class_loader'true);
  38.         $confDir $this->getProjectDir().'/config';
  39.         $loader->load($confDir.'/{packages}/*'.self::CONFIG_EXTS'glob');
  40.         $loader->load($confDir.'/{packages}/'.$this->environment.'/**/*'.self::CONFIG_EXTS'glob');
  41.         $loader->load($confDir.'/{services}'.self::CONFIG_EXTS'glob');
  42.         $loader->load($confDir.'/{services}_'.$this->environment.self::CONFIG_EXTS'glob');
  43.     }
  44.     protected function configureRoutes(RouteCollectionBuilder $routes)
  45.     {
  46.         $confDir $this->getProjectDir().'/config';
  47.         $routes->import($confDir.'/{routes}/*'.self::CONFIG_EXTS'/''glob');
  48.         $routes->import($confDir.'/{routes}/'.$this->environment.'/**/*'.self::CONFIG_EXTS'/''glob');
  49.         $routes->import($confDir.'/{routes}'.self::CONFIG_EXTS'/''glob');
  50.     }
  51.     public static function bootstrapCli(array &$argv)
  52.     {
  53.         // consume --env and --no-debug from the command line
  54.         // when using symfony/console v4.2 or higher, this should
  55.         // be replaced by a call to Application::bootstrapEnv()
  56.         for ($i 0$i \count($argv) && '--' !== $v $argv[$i]; ++$i) {
  57.             if ('--no-debug' === $v) {
  58.                 putenv('APP_DEBUG='.$_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = '0');
  59.                 $argvUnset[$i] = true;
  60.                 break;
  61.             }
  62.         }
  63.         for ($i 0$i \count($argv) && '--' !== $v $argv[$i]; ++$i) {
  64.             if (!$v || '-' !== $v[0] || !preg_match('/^-(?:-env(?:=|$)|e=?)(.*)$/D'$v$v)) {
  65.                 continue;
  66.             }
  67.             if (!empty($v[1]) || !empty($argv[$i])) {
  68.                 putenv('APP_ENV='.$_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = empty($v[1]) ? $argv[$i] : $v[1]);
  69.                 $argvUnset[$i] = $argvUnset[$i + empty($v[1])] = true;
  70.             }
  71.             break;
  72.         }
  73.         if (!empty($argvUnset)) {
  74.             $argv array_values(array_diff_key($argv$argvUnset));
  75.         }
  76.     }
  77.     public static function bootstrapEnv($env null)
  78.     {
  79.         if (null !== $env) {
  80.             putenv('APP_ENV='.$_SERVER['APP_ENV'] = $env);
  81.         }
  82.         if ('prod' !== $_SERVER['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) {
  83.             if (!class_exists(Dotenv::class)) {
  84.                 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.');
  85.             }
  86.             // when using symfony/dotenv v4.2 or higher, this call and the related methods
  87.             // below should be replaced by a call to the new Dotenv::loadEnv() method
  88.             self::loadEnv(new Dotenv(), \dirname(__DIR__).'/.env');
  89.         }
  90.         $_SERVER['APP_ENV'] = $_ENV['APP_ENV'] = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : 'dev';
  91.         $_SERVER['APP_DEBUG'] = isset($_SERVER['APP_DEBUG']) ? $_SERVER['APP_DEBUG'] : (isset($_ENV['APP_DEBUG']) ? $_ENV['APP_DEBUG'] : 'prod' !== $_SERVER['APP_ENV']);
  92.         $_SERVER['APP_DEBUG'] = $_ENV['APP_DEBUG'] = (int) $_SERVER['APP_DEBUG'] || filter_var($_SERVER['APP_DEBUG'], FILTER_VALIDATE_BOOLEAN) ? '1' '0';
  93.     }
  94.     private static function loadEnv(Dotenv $dotenv$path)
  95.     {
  96.         if (file_exists($path) || !file_exists($p "$path.dist")) {
  97.             $dotenv->load($path);
  98.         } else {
  99.             $dotenv->load($p);
  100.         }
  101.         if (null === $env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : null)) {
  102.             $dotenv->populate(array('APP_ENV' => $env 'dev'));
  103.         }
  104.         if ('test' !== $env && file_exists($p "$path.local")) {
  105.             $dotenv->load($p);
  106.             $env = isset($_SERVER['APP_ENV']) ? $_SERVER['APP_ENV'] : (isset($_ENV['APP_ENV']) ? $_ENV['APP_ENV'] : $env);
  107.         }
  108.         if (file_exists($p "$path.$env")) {
  109.             $dotenv->load($p);
  110.         }
  111.         if (file_exists($p "$path.$env.local")) {
  112.             $dotenv->load($p);
  113.         }
  114.     }
  115. }