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

  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.     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.     public function getLogDir(): string
  102.     {
  103.         return $_SERVER['APP_LOG_DIR'] ?? parent::getLogDir();
  104.     }
  105.     public function registerBundles(): iterable
  106.     {
  107.         $contents = require $this->getBundlesPath();
  108.         foreach ($contents as $class => $envs) {
  109.             if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  110.                 yield new $class();
  111.             }
  112.         }
  113.     }
  114.     public function registerContainerConfiguration(LoaderInterface $loader)
  115.     {
  116.         $loader->load(function (ContainerBuilder $container) use ($loader) {
  117.             $container->loadFromExtension('framework', [
  118.                 'router' => [
  119.                     'resource' => 'kernel::loadRoutes',
  120.                     'type' => 'service',
  121.                 ],
  122.             ]);
  123.             $kernelClass str_contains(static::class, "@anonymous\0") ? parent::class : static::class;
  124.             if (!$container->hasDefinition('kernel')) {
  125.                 $container->register('kernel'$kernelClass)
  126.                     ->addTag('controller.service_arguments')
  127.                     ->setAutoconfigured(true)
  128.                     ->setSynthetic(true)
  129.                     ->setPublic(true)
  130.                 ;
  131.             }
  132.             $kernelDefinition $container->getDefinition('kernel');
  133.             $kernelDefinition->addTag('routing.route_loader');
  134.             $container->addObjectResource($this);
  135.             $container->fileExists($this->getBundlesPath());
  136.             $configureContainer = new \ReflectionMethod($this'configureContainer');
  137.             $configuratorClass $configureContainer->getNumberOfParameters() > && ($type $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  138.             if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClasstrue)) {
  139.                 $configureContainer->getClosure($this)($container$loader);
  140.                 return;
  141.             }
  142.             $file = (new \ReflectionObject($this))->getFileName();
  143.             /* @var ContainerPhpFileLoader $kernelLoader */
  144.             $kernelLoader $loader->getResolver()->resolve($file);
  145.             $kernelLoader->setCurrentDir(\dirname($file));
  146.             $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader$kernelLoader)();
  147.             $valuePreProcessor AbstractConfigurator::$valuePreProcessor;
  148.             AbstractConfigurator::$valuePreProcessor = function ($value) {
  149.                 return $this === $value ? new Reference('kernel') : $value;
  150.             };
  151.             try {
  152.                 $configureContainer->getClosure($this)(new ContainerConfigurator($container$kernelLoader$instanceof$file$file$this->getEnvironment()), $loader$container);
  153.             } finally {
  154.                 $instanceof = [];
  155.                 $kernelLoader->registerAliasesForSinglyImplementedInterfaces();
  156.                 AbstractConfigurator::$valuePreProcessor $valuePreProcessor;
  157.             }
  158.             $container->setAlias($kernelClass'kernel')->setPublic(true);
  159.         });
  160.     }
  161.     /**
  162.      * @internal
  163.      */
  164.     public function loadRoutes(LoaderInterface $loader): RouteCollection
  165.     {
  166.         $file = (new \ReflectionObject($this))->getFileName();
  167.         /* @var RoutingPhpFileLoader $kernelLoader */
  168.         $kernelLoader $loader->getResolver()->resolve($file'php');
  169.         $kernelLoader->setCurrentDir(\dirname($file));
  170.         $collection = new RouteCollection();
  171.         $configureRoutes = new \ReflectionMethod($this'configureRoutes');
  172.         $configureRoutes->getClosure($this)(new RoutingConfigurator($collection$kernelLoader$file$file$this->getEnvironment()));
  173.         foreach ($collection as $route) {
  174.             $controller $route->getDefault('_controller');
  175.             if (\is_array($controller) && [01] === array_keys($controller) && $this === $controller[0]) {
  176.                 $route->setDefault('_controller', ['kernel'$controller[1]]);
  177.             } elseif ($controller instanceof \Closure && $this === ($r = new \ReflectionFunction($controller))->getClosureThis() && !str_contains($r->name'{closure}')) {
  178.                 $route->setDefault('_controller', ['kernel'$r->name]);
  179.             }
  180.         }
  181.         return $collection;
  182.     }
  183. }