src/Kernel.php line 98

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App;
  12. use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
  13. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  14. use Symfony\Component\Config\Loader\DelegatingLoader;
  15. use Symfony\Component\Config\Loader\LoaderInterface;
  16. use Symfony\Component\Config\Loader\LoaderResolver;
  17. use Symfony\Component\Config\Resource\FileResource;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\DependencyInjection\ContainerInterface;
  20. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  21. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  22. use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
  23. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  24. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  25. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  26. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  27. use Symfony\Component\HttpKernel\Config\FileLocator;
  28. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  29. use Symfony\Component\Routing\RouteCollectionBuilder;
  30. use Webmozart\Assert\Assert;
  31. /** @final */
  32. class Kernel extends BaseKernel
  33. {
  34.     use MicroKernelTrait;
  35.     private const CONFIG_EXTS '.{php,xml,yaml,yml}';
  36.     public function getCacheDir(): string
  37.     {
  38.         return $this->getProjectDir() . '/var/cache/' $this->environment;
  39.     }
  40.     public function getLogDir(): string
  41.     {
  42.         return $this->getProjectDir() . '/var/log';
  43.     }
  44.     public function registerBundles(): iterable
  45.     {
  46.         $contents = require $this->getProjectDir() . '/config/bundles.php';
  47.         foreach ($contents as $class => $envs) {
  48.             if (isset($envs['all']) || isset($envs[$this->environment])) {
  49.                 yield new $class();
  50.             }
  51.         }
  52.     }
  53.     protected function configureContainer(ContainerBuilder $containerLoaderInterface $loader): void
  54.     {
  55.         $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
  56.         $container->setParameter('container.dumper.inline_class_loader'true);
  57.         $confDir $this->getProjectDir() . '/config';
  58.         $loader->load($confDir '/{packages}/*' self::CONFIG_EXTS'glob');
  59.         $loader->load($confDir '/{packages}/' $this->environment '/**/*' self::CONFIG_EXTS'glob');
  60.         $loader->load($confDir '/{services}' self::CONFIG_EXTS'glob');
  61.         $loader->load($confDir '/{services}_' $this->environment self::CONFIG_EXTS'glob');
  62.     }
  63.     protected function configureRoutes(RouteCollectionBuilder $routes): void
  64.     {
  65.         $confDir $this->getProjectDir() . '/config';
  66.         $routes->import($confDir '/{routes}/*' self::CONFIG_EXTS'/''glob');
  67.         $routes->import($confDir '/{routes}/' $this->environment '/**/*' self::CONFIG_EXTS'/''glob');
  68.         $routes->import($confDir '/{routes}' self::CONFIG_EXTS'/''glob');
  69.     }
  70.     protected function getContainerBaseClass(): string
  71.     {
  72.         if ($this->isTestEnvironment()) {
  73.             return MockerContainer::class;
  74.         }
  75.         return parent::getContainerBaseClass();
  76.     }
  77.     protected function getContainerLoader(ContainerInterface $container): LoaderInterface
  78.     {
  79.         /** @var ContainerBuilder $container */
  80.         Assert::isInstanceOf($containerContainerBuilder::class);
  81.         $locator = new FileLocator($this$this->getRootDir() . '/Resources');
  82.         $resolver = new LoaderResolver([
  83.             new XmlFileLoader($container$locator),
  84.             new YamlFileLoader($container$locator),
  85.             new IniFileLoader($container$locator),
  86.             new PhpFileLoader($container$locator),
  87.             new GlobFileLoader($container$locator),
  88.             new DirectoryLoader($container$locator),
  89.             new ClosureLoader($container),
  90.         ]);
  91.         return new DelegatingLoader($resolver);
  92.     }
  93.     private function isTestEnvironment(): bool
  94.     {
  95.         return === strpos($this->getEnvironment(), 'test');
  96.     }
  97. }