|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\FrameworkBundle\CacheWarmer; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Command\BuildDebugContainerTrait; |
| 16 | +use Symfony\Component\Config\Builder\ConfigBuilderGenerator; |
| 17 | +use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface; |
| 18 | +use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 19 | +use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; |
| 20 | +use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
| 21 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 22 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Generate all config builders. |
| 26 | + * |
| 27 | + * @author Tobias Nyholm <tobias.nyholm@gmail.com> |
| 28 | + */ |
| 29 | +class ConfigBuilderCacheWarmer implements CacheWarmerInterface |
| 30 | +{ |
| 31 | + use BuildDebugContainerTrait; |
| 32 | + |
| 33 | + private $kernel; |
| 34 | + private $logger; |
| 35 | + |
| 36 | + public function __construct(KernelInterface $kernel, LoggerInterface $logger = null) |
| 37 | + { |
| 38 | + $this->kernel = $kernel; |
| 39 | + $this->logger = $logger; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + * |
| 45 | + * @return string[] |
| 46 | + */ |
| 47 | + public function warmUp(string $cacheDir) |
| 48 | + { |
| 49 | + $generator = new ConfigBuilderGenerator($cacheDir); |
| 50 | + |
| 51 | + foreach ($this->kernel->getBundles() as $bundle) { |
| 52 | + $extension = $bundle->getContainerExtension(); |
| 53 | + if (null === $extension) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + try { |
| 58 | + $this->dumpExtension($extension, $generator); |
| 59 | + } catch (\Exception $e) { |
| 60 | + if ($this->logger) { |
| 61 | + $this->logger->warning('Failed to generate ConfigBuilder for extension {extensionClass}.', ['exception' => $e, 'extensionClass' => \get_class($extension)]); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // No need to preload anything |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + private function dumpExtension(ExtensionInterface $extension, ConfigBuilderGeneratorInterface $generator): void |
| 71 | + { |
| 72 | + if ($extension instanceof ConfigurationInterface) { |
| 73 | + $configuration = $extension; |
| 74 | + } elseif ($extension instanceof ConfigurationExtensionInterface) { |
| 75 | + $configuration = $extension->getConfiguration([], $this->getContainerBuilder($this->kernel)); |
| 76 | + } else { |
| 77 | + throw new \LogicException(sprintf('Could not get configuration for extension "%s".', \get_class($extension))); |
| 78 | + } |
| 79 | + |
| 80 | + $generator->build($configuration); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * {@inheritdoc} |
| 85 | + */ |
| 86 | + public function isOptional() |
| 87 | + { |
| 88 | + return true; |
| 89 | + } |
| 90 | +} |
0 commit comments