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

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 $c)
  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 $c): void;
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     public function registerBundles(): iterable
  64.     {
  65.         $contents = require $this->getProjectDir().'/config/bundles.php';
  66.         foreach ($contents as $class => $envs) {
  67.             if ($envs[$this->environment] ?? $envs['all'] ?? false) {
  68.                 yield new $class();
  69.             }
  70.         }
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function registerContainerConfiguration(LoaderInterface $loader)
  76.     {
  77.         $loader->load(function (ContainerBuilder $container) use ($loader) {
  78.             $container->loadFromExtension('framework', [
  79.                 'router' => [
  80.                     'resource' => 'kernel::loadRoutes',
  81.                     'type' => 'service',
  82.                 ],
  83.             ]);
  84.             $kernelClass false !== strpos(static::class, "@anonymous\0") ? parent::class : static::class;
  85.             if (!$container->hasDefinition('kernel')) {
  86.                 $container->register('kernel'$kernelClass)
  87.                     ->addTag('controller.service_arguments')
  88.                     ->setAutoconfigured(true)
  89.                     ->setSynthetic(true)
  90.                     ->setPublic(true)
  91.                 ;
  92.             }
  93.             $kernelDefinition $container->getDefinition('kernel');
  94.             $kernelDefinition->addTag('routing.route_loader');
  95.             $container->addObjectResource($this);
  96.             $container->fileExists($this->getProjectDir().'/config/bundles.php');
  97.             try {
  98.                 $configureContainer = new \ReflectionMethod($this'configureContainer');
  99.             } catch (\ReflectionException $e) {
  100.                 throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureContainer(ContainerConfigurator $c): void".'get_debug_type($this), MicroKernelTrait::class), 0$e);
  101.             }
  102.             $configuratorClass $configureContainer->getNumberOfParameters() > && ($type $configureContainer->getParameters()[0]->getType()) instanceof \ReflectionNamedType && !$type->isBuiltin() ? $type->getName() : null;
  103.             if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClasstrue)) {
  104.                 $this->configureContainer($container$loader);
  105.                 return;
  106.             }
  107.             // the user has opted into using the ContainerConfigurator
  108.             /* @var ContainerPhpFileLoader $kernelLoader */
  109.             $kernelLoader $loader->getResolver()->resolve($file $configureContainer->getFileName());
  110.             $kernelLoader->setCurrentDir(\dirname($file));
  111.             $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader$kernelLoader)();
  112.             $valuePreProcessor AbstractConfigurator::$valuePreProcessor;
  113.             AbstractConfigurator::$valuePreProcessor = function ($value) {
  114.                 return $this === $value ? new Reference('kernel') : $value;
  115.             };
  116.             try {
  117.                 $this->configureContainer(new ContainerConfigurator($container$kernelLoader$instanceof$file$file), $loader);
  118.             } finally {
  119.                 $instanceof = [];
  120.                 $kernelLoader->registerAliasesForSinglyImplementedInterfaces();
  121.                 AbstractConfigurator::$valuePreProcessor $valuePreProcessor;
  122.             }
  123.             $container->setAlias($kernelClass'kernel')->setPublic(true);
  124.         });
  125.     }
  126.     /**
  127.      * @internal
  128.      *
  129.      * @return RouteCollection
  130.      */
  131.     public function loadRoutes(LoaderInterface $loader)
  132.     {
  133.         $file = (new \ReflectionObject($this))->getFileName();
  134.         /* @var RoutingPhpFileLoader $kernelLoader */
  135.         $kernelLoader $loader->getResolver()->resolve($file);
  136.         $kernelLoader->setCurrentDir(\dirname($file));
  137.         $collection = new RouteCollection();
  138.         try {
  139.             $configureRoutes = new \ReflectionMethod($this'configureRoutes');
  140.         } catch (\ReflectionException $e) {
  141.             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);
  142.         }
  143.         $configuratorClass $configureRoutes->getNumberOfParameters() > && ($type $configureRoutes->getParameters()[0]->getType()) && !$type->isBuiltin() ? $type->getName() : null;
  144.         if ($configuratorClass && !is_a(RoutingConfigurator::class, $configuratorClasstrue)) {
  145.             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);
  146.             $routes = new RouteCollectionBuilder($loader);
  147.             $this->configureRoutes($routes);
  148.             return $routes->build();
  149.         }
  150.         $this->configureRoutes(new RoutingConfigurator($collection$kernelLoader$file$file));
  151.         foreach ($collection as $route) {
  152.             $controller $route->getDefault('_controller');
  153.             if (\is_array($controller) && [01] === array_keys($controller) && $this === $controller[0]) {
  154.                 $route->setDefault('_controller', ['kernel'$controller[1]]);
  155.             }
  156.         }
  157.         return $collection;
  158.     }
  159. }