src/Kernel.php line 105

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 App\DependencyInjection\Compiler\DoctrineFunctionsPass;
  13. use App\DependencyInjection\Compiler\MailPass;
  14. use App\DependencyInjection\Compiler\RegisterExporterPass;
  15. use Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle;
  16. use PSS\SymfonyMockerContainer\DependencyInjection\MockerContainer;
  17. use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
  18. use Symfony\Component\Config\Loader\DelegatingLoader;
  19. use Symfony\Component\Config\Loader\LoaderInterface;
  20. use Symfony\Component\Config\Loader\LoaderResolver;
  21. use Symfony\Component\Config\Resource\FileResource;
  22. use Symfony\Component\DependencyInjection\ContainerBuilder;
  23. use Symfony\Component\DependencyInjection\ContainerInterface;
  24. use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
  25. use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
  26. use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
  27. use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
  28. use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
  29. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  30. use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
  31. use Symfony\Component\HttpKernel\Config\FileLocator;
  32. use Symfony\Component\HttpKernel\Kernel as BaseKernel;
  33. use Symfony\Component\Routing\RouteCollectionBuilder;
  34. use Webmozart\Assert\Assert;
  35. final class Kernel extends BaseKernel
  36. {
  37.     use MicroKernelTrait;
  38.     private const CONFIG_EXTS '.{php,xml,yaml,yml}';
  39.     public function getCacheDir(): string
  40.     {
  41.         return $this->getProjectDir() . '/var/cache/' $this->environment;
  42.     }
  43.     public function getLogDir(): string
  44.     {
  45.         return $this->getProjectDir() . '/var/log';
  46.     }
  47.     public function registerBundles(): iterable
  48.     {
  49.         $contents = require $this->getProjectDir() . '/config/bundles.php';
  50.         foreach ($contents as $class => $envs) {
  51.             if (isset($envs['all']) || isset($envs[$this->environment])) {
  52.                 yield new $class();
  53.             }
  54.         }
  55.         if (in_array($this->getEnvironment(), ['dev''test'], true)) {
  56.             $bundles[] = new DoctrineFixturesBundle();
  57.         }
  58.     }
  59.     protected function configureContainer(ContainerBuilder $containerLoaderInterface $loader): void
  60.     {
  61.         $container->addResource(new FileResource($this->getProjectDir() . '/config/bundles.php'));
  62.         $container->setParameter('container.dumper.inline_class_loader'true);
  63.         $confDir $this->getProjectDir() . '/config';
  64.         $loader->load($confDir '/{packages}/*' self::CONFIG_EXTS'glob');
  65.         $loader->load($confDir '/{packages}/' $this->environment '/**/*' self::CONFIG_EXTS'glob');
  66.         $loader->load($confDir '/{services}' self::CONFIG_EXTS'glob');
  67.         $loader->load($confDir '/{services}_' $this->environment self::CONFIG_EXTS'glob');
  68.     }
  69.     protected function configureRoutes(RouteCollectionBuilder $routes): void
  70.     {
  71.         $confDir $this->getProjectDir() . '/config';
  72.         $routes->import($confDir '/{routes}/*' self::CONFIG_EXTS'/''glob');
  73.         $routes->import($confDir '/{routes}/' $this->environment '/**/*' self::CONFIG_EXTS'/''glob');
  74.         $routes->import($confDir '/{routes}' self::CONFIG_EXTS'/''glob');
  75.     }
  76.     protected function getContainerBaseClass(): string
  77.     {
  78.         if ($this->isTestEnvironment()) {
  79.             return MockerContainer::class;
  80.         }
  81.         return parent::getContainerBaseClass();
  82.     }
  83.     protected function getContainerLoader(ContainerInterface $container): LoaderInterface
  84.     {
  85.         /** @var ContainerBuilder $container */
  86.         Assert::isInstanceOf($containerContainerBuilder::class);
  87.         $locator = new FileLocator($this$this->getRootDir() . '/Resources');
  88.         $resolver = new LoaderResolver([
  89.             new XmlFileLoader($container$locator),
  90.             new YamlFileLoader($container$locator),
  91.             new IniFileLoader($container$locator),
  92.             new PhpFileLoader($container$locator),
  93.             new GlobFileLoader($container$locator),
  94.             new DirectoryLoader($container$locator),
  95.             new ClosureLoader($container),
  96.         ]);
  97.         return new DelegatingLoader($resolver);
  98.     }
  99.     private function isTestEnvironment(): bool
  100.     {
  101.         return === strpos($this->getEnvironment(), 'test');
  102.     }
  103.     protected function build(ContainerBuilder $container): void
  104.     {
  105.         $container->addCompilerPass(new DoctrineFunctionsPass());
  106.         $container->addCompilerPass(new RegisterExporterPass());
  107.         $container->addCompilerPass(new MailPass());
  108.     }
  109. }