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

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.  * @method void configureRoutes(RoutingConfigurator $routes)
  28.  * @method void configureContainer(ContainerConfigurator $container)
  29.  */
  30. trait MicroKernelTrait
  31. {
  32.     /**
  33.      * Adds or imports routes into your application.
  34.      *
  35.      *     $routes->import($this->getProjectDir().'/config/*.{yaml,php}');
  36.      *     $routes
  37.      *         ->add('admin_dashboard', '/admin')
  38.      *         ->controller('App\Controller\AdminController::dashboard')
  39.      *     ;
  40.      */
  41.     //abstract protected function configureRoutes(RoutingConfigurator $routes): void;
  42.     /**
  43.      * Configures the container.
  44.      *
  45.      * You can register extensions:
  46.      *
  47.      *     $c->extension('framework', [
  48.      *         'secret' => '%secret%'
  49.      *     ]);
  50.      *
  51.      * Or services:
  52.      *
  53.      *     $c->services()->set('halloween', 'FooBundle\HalloweenProvider');
  54.      *
  55.      * Or parameters:
  56.      *
  57.      *     $c->parameters()->set('halloween', 'lot of fun');
  58.      */
  59.     //abstract protected function configureContainer(ContainerConfigurator $container): void;
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function getCacheDir(): string
  64.     {
  65.         if (isset($_SERVER['APP_CACHE_DIR'])) {
  66.             return $_SERVER['APP_CACHE_DIR'].'/'.$this->environment;
  67.         }
  68.         return parent::getCacheDir();
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getLogDir(): string
  74.     {
  75.         return $_SERVER['APP_LOG_DIR'] ?? parent::getLogDir();
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function registerBundles(): iterable
  81.     {
  82.         $contents = require $this->getProjectDir().'/config/bundles.php';
  83.         foreach ($contents as $class => $envs) {
  84.             if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  85.                 yield new $class();
  86.             }
  87.         }
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function registerContainerConfiguration(LoaderInterface $loader)
  93.     {
  94.         $loader->load(function (ContainerBuilder $container) use ($loader) {
  95.             $container->loadFromExtension('framework', [
  96.                 'router' => [
  97.                     'resource' => 'kernel::loadRoutes',
  98.                     'type' => 'service',
  99.                 ],
  100.             ]);
  101.             $kernelClass false !== strpos(static::class, "@anonymous\0") ? parent::class : static::class;
  102.             if (!$container->hasDefinition('kernel')) {
  103.                 $container->register('kernel'$kernelClass)
  104.                     ->addTag('controller.service_arguments')
  105.                     ->setAutoconfigured(true)
  106.                     ->setSynthetic(true)
  107.                     ->setPublic(true)
  108.                 ;
  109.             }
  110.             $kernelDefinition $container->getDefinition('kernel');
  111.             $kernelDefinition->addTag('routing.route_loader');
  112.             $container->addObjectResource($this);
  113.             $container->fileExists($this->getProjectDir().'/config/bundles.php');
  114.             try {
  115.                 $configureContainer = new \ReflectionMethod($this'configureContainer');
  116.             } catch (\ReflectionException $e) {
  117.                 throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureContainer(ContainerConfigurator $container): void".'get_debug_type($this), MicroKernelTrait::class), 0$e);
  118.             }
  119.             $configuratorClass $configureContainer->getNumberOfParameters() > && ($type $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  120.             if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClasstrue)) {
  121.                 $this->configureContainer($container$loader);
  122.                 return;
  123.             }
  124.             // the user has opted into using the ContainerConfigurator
  125.             /* @var ContainerPhpFileLoader $kernelLoader */
  126.             $kernelLoader $loader->getResolver()->resolve($file $configureContainer->getFileName());
  127.             $kernelLoader->setCurrentDir(\dirname($file));
  128.             $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader$kernelLoader)();
  129.             $valuePreProcessor AbstractConfigurator::$valuePreProcessor;
  130.             AbstractConfigurator::$valuePreProcessor = function ($value) {
  131.                 return $this === $value ? new Reference('kernel') : $value;
  132.             };
  133.             try {
  134.                 $this->configureContainer(new ContainerConfigurator($container$kernelLoader$instanceof$file$file), $loader);
  135.             } finally {
  136.                 $instanceof = [];
  137.                 $kernelLoader->registerAliasesForSinglyImplementedInterfaces();
  138.                 AbstractConfigurator::$valuePreProcessor $valuePreProcessor;
  139.             }
  140.             $container->setAlias($kernelClass'kernel')->setPublic(true);
  141.         });
  142.     }
  143.     /**
  144.      * @internal
  145.      *
  146.      * @return RouteCollection
  147.      */
  148.     public function loadRoutes(LoaderInterface $loader)
  149.     {
  150.         $file = (new \ReflectionObject($this))->getFileName();
  151.         /* @var RoutingPhpFileLoader $kernelLoader */
  152.         $kernelLoader $loader->getResolver()->resolve($file'php');
  153.         $kernelLoader->setCurrentDir(\dirname($file));
  154.         $collection = new RouteCollection();
  155.         try {
  156.             $configureRoutes = new \ReflectionMethod($this'configureRoutes');
  157.         } catch (\ReflectionException $e) {
  158.             throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".'get_debug_type($this), MicroKernelTrait::class), 0$e);
  159.         }
  160.         $configuratorClass $configureRoutes->getNumberOfParameters() > && ($type $configureRoutes->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  161.         if ($configuratorClass && !is_a(RoutingConfigurator::class, $configuratorClasstrue)) {
  162.             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);
  163.             $routes = new RouteCollectionBuilder($loader);
  164.             $this->configureRoutes($routes);
  165.             return $routes->build();
  166.         }
  167.         $this->configureRoutes(new RoutingConfigurator($collection$kernelLoader$file$file));
  168.         foreach ($collection as $route) {
  169.             $controller $route->getDefault('_controller');
  170.             if (\is_array($controller) && [01] === array_keys($controller) && $this === $controller[0]) {
  171.                 $route->setDefault('_controller', ['kernel'$controller[1]]);
  172.             }
  173.         }
  174.         return $collection;
  175.     }
  176. }