vendor/symfony/framework-bundle/CacheWarmer/ConfigBuilderCacheWarmer.php line 34

  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\CacheWarmer;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
  13. use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
  14. use Symfony\Component\Config\Definition\ConfigurationInterface;
  15. use Symfony\Component\DependencyInjection\ContainerBuilder;
  16. use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
  17. use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
  18. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  19. use Symfony\Component\HttpKernel\KernelInterface;
  20. /**
  21.  * Generate all config builders.
  22.  *
  23.  * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  24.  */
  25. class ConfigBuilderCacheWarmer implements CacheWarmerInterface
  26. {
  27.     private KernelInterface $kernel;
  28.     private ?LoggerInterface $logger;
  29.     public function __construct(KernelInterface $kernelLoggerInterface $logger null)
  30.     {
  31.         $this->kernel $kernel;
  32.         $this->logger $logger;
  33.     }
  34.     /**
  35.      * @return string[]
  36.      */
  37.     public function warmUp(string $cacheDir): array
  38.     {
  39.         $generator = new ConfigBuilderGenerator($cacheDir);
  40.         foreach ($this->kernel->getBundles() as $bundle) {
  41.             $extension $bundle->getContainerExtension();
  42.             if (null === $extension) {
  43.                 continue;
  44.             }
  45.             try {
  46.                 $this->dumpExtension($extension$generator);
  47.             } catch (\Exception $e) {
  48.                 $this->logger?->warning('Failed to generate ConfigBuilder for extension {extensionClass}: '.$e->getMessage(), ['exception' => $e'extensionClass' => $extension::class]);
  49.             }
  50.         }
  51.         // No need to preload anything
  52.         return [];
  53.     }
  54.     private function dumpExtension(ExtensionInterface $extensionConfigBuilderGeneratorInterface $generator): void
  55.     {
  56.         $configuration null;
  57.         if ($extension instanceof ConfigurationInterface) {
  58.             $configuration $extension;
  59.         } elseif ($extension instanceof ConfigurationExtensionInterface) {
  60.             $configuration $extension->getConfiguration([], new ContainerBuilder($this->kernel->getContainer()->getParameterBag()));
  61.         }
  62.         if (!$configuration) {
  63.             return;
  64.         }
  65.         $generator->build($configuration);
  66.     }
  67.     public function isOptional(): bool
  68.     {
  69.         return true;
  70.     }
  71. }