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