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

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 $containerLoaderInterface $loaderContainerBuilder $builder): void
  46.     {
  47.         $configDir $this->getConfigDir();
  48.         $container->import($configDir.'/{packages}/*.{php,yaml}');
  49.         $container->import($configDir.'/{packages}/'.$this->environment.'/*.{php,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.'/*.{php,yaml}');
  70.         $routes->import($configDir.'/{routes}/*.{php,yaml}');
  71.         if (is_file($configDir.'/routes.yaml')) {
  72.             $routes->import($configDir.'/routes.yaml');
  73.         } else {
  74.             $routes->import($configDir.'/{routes}.php');
  75.         }
  76.         if (false !== ($fileName = (new \ReflectionObject($this))->getFileName())) {
  77.             $routes->import($fileName'annotation');
  78.         }
  79.     }
  80.     /**
  81.      * Gets the path to the configuration directory.
  82.      */
  83.     private function getConfigDir(): string
  84.     {
  85.         return $this->getProjectDir().'/config';
  86.     }
  87.     /**
  88.      * Gets the path to the bundles configuration file.
  89.      */
  90.     private function getBundlesPath(): string
  91.     {
  92.         return $this->getConfigDir().'/bundles.php';
  93.     }
  94.     /**
  95.      * {@inheritdoc}
  96.      */
  97.     public function getCacheDir(): string
  98.     {
  99.         if (isset($_SERVER['APP_CACHE_DIR'])) {
  100.             return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
  101.         }
  102.         return parent::getCacheDir();
  103.     }
  104.     /**
  105.      * {@inheritdoc}
  106.      */
  107.     public function getLogDir(): string
  108.     {
  109.         return $_SERVER['APP_LOG_DIR'] ?? parent::getLogDir();
  110.     }
  111.     /**
  112.      * {@inheritdoc}
  113.      */
  114.     public function registerBundles(): iterable
  115.     {
  116.         $contents = require $this->getBundlesPath();
  117.         foreach ($contents as $class => $envs) {
  118.             if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  119.                 yield new $class();
  120.             }
  121.         }
  122.     }
  123.     /**
  124.      * {@inheritdoc}
  125.      */
  126.     public function registerContainerConfiguration(LoaderInterface $loader)
  127.     {
  128.         $loader->load(function (ContainerBuilder $container) use ($loader) {
  129.             $container->loadFromExtension('framework', [
  130.                 'router' => [
  131.                     'resource' => 'kernel::loadRoutes',
  132.                     'type' => 'service',
  133.                 ],
  134.             ]);
  135.             $kernelClass str_contains(static::class, "@anonymous\0") ? parent::class : static::class;
  136.             if (!$container->hasDefinition('kernel')) {
  137.                 $container->register('kernel'$kernelClass)
  138.                     ->addTag('controller.service_arguments')
  139.                     ->setAutoconfigured(true)
  140.                     ->setSynthetic(true)
  141.                     ->setPublic(true)
  142.                 ;
  143.             }
  144.             $kernelDefinition $container->getDefinition('kernel');
  145.             $kernelDefinition->addTag('routing.route_loader');
  146.             $container->addObjectResource($this);
  147.             $container->fileExists($this->getBundlesPath());
  148.             $configureContainer = new \ReflectionMethod($this'configureContainer');
  149.             $configuratorClass $configureContainer->getNumberOfParameters() > && ($type $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  150.             if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClasstrue)) {
  151.                 $configureContainer->getClosure($this)($container$loader);
  152.                 return;
  153.             }
  154.             $file = (new \ReflectionObject($this))->getFileName();
  155.             /* @var ContainerPhpFileLoader $kernelLoader */
  156.             $kernelLoader $loader->getResolver()->resolve($file);
  157.             $kernelLoader->setCurrentDir(\dirname($file));
  158.             $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader$kernelLoader)();
  159.             $valuePreProcessor AbstractConfigurator::$valuePreProcessor;
  160.             AbstractConfigurator::$valuePreProcessor = function ($value) {
  161.                 return $this === $value ? new Reference('kernel') : $value;
  162.             };
  163.             try {
  164.                 $configureContainer->getClosure($this)(new ContainerConfigurator($container$kernelLoader$instanceof$file$file$this->getEnvironment()), $loader$container);
  165.             } finally {
  166.                 $instanceof = [];
  167.                 $kernelLoader->registerAliasesForSinglyImplementedInterfaces();
  168.                 AbstractConfigurator::$valuePreProcessor $valuePreProcessor;
  169.             }
  170.             $container->setAlias($kernelClass'kernel')->setPublic(true);
  171.         });
  172.     }
  173.     /**
  174.      * @internal
  175.      */
  176.     public function loadRoutes(LoaderInterface $loader): RouteCollection
  177.     {
  178.         $file = (new \ReflectionObject($this))->getFileName();
  179.         /* @var RoutingPhpFileLoader $kernelLoader */
  180.         $kernelLoader $loader->getResolver()->resolve($file'php');
  181.         $kernelLoader->setCurrentDir(\dirname($file));
  182.         $collection = new RouteCollection();
  183.         $configureRoutes = new \ReflectionMethod($this'configureRoutes');
  184.         $configureRoutes->getClosure($this)(new RoutingConfigurator($collection$kernelLoader$file$file$this->getEnvironment()));
  185.         foreach ($collection as $route) {
  186.             $controller $route->getDefault('_controller');
  187.             if (\is_array($controller) && [01] === array_keys($controller) && $this === $controller[0]) {
  188.                 $route->setDefault('_controller', ['kernel'$controller[1]]);
  189.             } elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !str_contains($r->name'{closure}')) {
  190.                 $route->setDefault('_controller', ['kernel'$r->name]);
  191.             }
  192.         }
  193.         return $collection;
  194.     }
  195. }