vendor/symfony/framework-bundle/Kernel/MicroKernelTrait.php line 169

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\Kernel;
  11. use Symfony\Component\Config\Loader\LoaderInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Loader\Configurator\AbstractConfigurator;
  14. use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
  15. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader as ContainerPhpFileLoader;
  16. use Symfony\Component\DependencyInjection\Reference;
  17. use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
  18. use Symfony\Component\Routing\Loader\PhpFileLoader as RoutingPhpFileLoader;
  19. use Symfony\Component\Routing\RouteCollection;
  20. /**
  21.  * A Kernel that provides configuration hooks.
  22.  *
  23.  * @author Ryan Weaver <ryan@knpuniversity.com>
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. trait MicroKernelTrait
  27. {
  28.     /**
  29.      * Configures the container.
  30.      *
  31.      * You can register extensions:
  32.      *
  33.      *     $container->extension('framework', [
  34.      *         'secret' => '%secret%'
  35.      *     ]);
  36.      *
  37.      * Or services:
  38.      *
  39.      *     $container->services()->set('halloween', 'FooBundle\HalloweenProvider');
  40.      *
  41.      * Or parameters:
  42.      *
  43.      *     $container->parameters()->set('halloween', 'lot of fun');
  44.      */
  45.     private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
  46.     {
  47.         $configDir = $this->getConfigDir();
  48.         $container->import($configDir.'/{packages}/*.yaml');
  49.         $container->import($configDir.'/{packages}/'.$this->environment.'/*.yaml');
  50.         if (is_file($configDir.'/services.yaml')) {
  51.             $container->import($configDir.'/services.yaml');
  52.             $container->import($configDir.'/{services}_'.$this->environment.'.yaml');
  53.         } else {
  54.             $container->import($configDir.'/{services}.php');
  55.         }
  56.     }
  57.     /**
  58.      * Adds or imports routes into your application.
  59.      *
  60.      *     $routes->import($this->getConfigDir().'/*.{yaml,php}');
  61.      *     $routes
  62.      *         ->add('admin_dashboard', '/admin')
  63.      *         ->controller('App\Controller\AdminController::dashboard')
  64.      *     ;
  65.      */
  66.     private function configureRoutes(RoutingConfigurator $routes): void
  67.     {
  68.         $configDir = $this->getConfigDir();
  69.         $routes->import($configDir.'/{routes}/'.$this->environment.'/*.yaml');
  70.         $routes->import($configDir.'/{routes}/*.yaml');
  71.         if (is_file($configDir.'/routes.yaml')) {
  72.             $routes->import($configDir.'/routes.yaml');
  73.         } else {
  74.             $routes->import($configDir.'/{routes}.php');
  75.         }
  76.     }
  77.     /**
  78.      * Gets the path to the configuration directory.
  79.      */
  80.     private function getConfigDir(): string
  81.     {
  82.         return $this->getProjectDir().'/config';
  83.     }
  84.     /**
  85.      * Gets the path to the bundles configuration file.
  86.      */
  87.     private function getBundlesPath(): string
  88.     {
  89.         return $this->getConfigDir().'/bundles.php';
  90.     }
  91.     /**
  92.      * {@inheritdoc}
  93.      */
  94.     public function getCacheDir(): string
  95.     {
  96.         if (isset($_SERVER['APP_CACHE_DIR'])) {
  97.             return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
  98.         }
  99.         return parent::getCacheDir();
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function getLogDir(): string
  105.     {
  106.         return $_SERVER['APP_LOG_DIR'] ?? parent::getLogDir();
  107.     }
  108.     /**
  109.      * {@inheritdoc}
  110.      */
  111.     public function registerBundles(): iterable
  112.     {
  113.         $contents = require $this->getBundlesPath();
  114.         foreach ($contents as $class => $envs) {
  115.             if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  116.                 yield new $class();
  117.             }
  118.         }
  119.     }
  120.     /**
  121.      * {@inheritdoc}
  122.      */
  123.     public function registerContainerConfiguration(LoaderInterface $loader)
  124.     {
  125.         $loader->load(function (ContainerBuilder $container) use ($loader) {
  126.             $container->loadFromExtension('framework', [
  127.                 'router' => [
  128.                     'resource' => 'kernel::loadRoutes',
  129.                     'type' => 'service',
  130.                 ],
  131.             ]);
  132.             $kernelClass = false !== strpos(static::class, "@anonymous\0") ? parent::class : static::class;
  133.             if (!$container->hasDefinition('kernel')) {
  134.                 $container->register('kernel', $kernelClass)
  135.                     ->addTag('controller.service_arguments')
  136.                     ->setAutoconfigured(true)
  137.                     ->setSynthetic(true)
  138.                     ->setPublic(true)
  139.                 ;
  140.             }
  141.             $kernelDefinition = $container->getDefinition('kernel');
  142.             $kernelDefinition->addTag('routing.route_loader');
  143.             $container->addObjectResource($this);
  144.             $container->fileExists($this->getBundlesPath());
  145.             $configureContainer = new \ReflectionMethod($this, 'configureContainer');
  146.             $configuratorClass = $configureContainer->getNumberOfParameters() > 0 && ($type = $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  147.             if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClass, true)) {
  148.                 $configureContainer->getClosure($this)($container, $loader);
  149.                 return;
  150.             }
  151.             $file = (new \ReflectionObject($this))->getFileName();
  152.             /* @var ContainerPhpFileLoader $kernelLoader */
  153.             $kernelLoader = $loader->getResolver()->resolve($file);
  154.             $kernelLoader->setCurrentDir(\dirname($file));
  155.             $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
  156.             $valuePreProcessor = AbstractConfigurator::$valuePreProcessor;
  157.             AbstractConfigurator::$valuePreProcessor = function ($value) {
  158.                 return $this === $value ? new Reference('kernel') : $value;
  159.             };
  160.             try {
  161.                 $configureContainer->getClosure($this)(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader, $container);
  162.             } finally {
  163.                 $instanceof = [];
  164.                 $kernelLoader->registerAliasesForSinglyImplementedInterfaces();
  165.                 AbstractConfigurator::$valuePreProcessor = $valuePreProcessor;
  166.             }
  167.             $container->setAlias($kernelClass, 'kernel')->setPublic(true);
  168.         });
  169.     }
  170.     /**
  171.      * @internal
  172.      */
  173.     public function loadRoutes(LoaderInterface $loader): RouteCollection
  174.     {
  175.         $file = (new \ReflectionObject($this))->getFileName();
  176.         /* @var RoutingPhpFileLoader $kernelLoader */
  177.         $kernelLoader = $loader->getResolver()->resolve($file, 'php');
  178.         $kernelLoader->setCurrentDir(\dirname($file));
  179.         $collection = new RouteCollection();
  180.         $configureRoutes = new \ReflectionMethod($this, 'configureRoutes');
  181.         $configureRoutes->getClosure($this)(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
  182.         foreach ($collection as $route) {
  183.             $controller = $route->getDefault('_controller');
  184.             if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) {
  185.                 $route->setDefault('_controller', ['kernel', $controller[1]]);
  186.             }
  187.         }
  188.         return $collection;
  189.     }
  190. }