|
| 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\Component\Config\Definition\ArrayNode; |
| 16 | +use Symfony\Component\Config\Definition\ConfigurationInterface; |
| 17 | +use Symfony\Component\Config\Definition\JsonSchemaGenerator; |
| 18 | +use Symfony\Component\DependencyInjection\Container; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | +use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface; |
| 21 | +use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag; |
| 22 | +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
| 23 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 24 | +use Symfony\Component\HttpKernel\Kernel; |
| 25 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * Generate config json schema. |
| 29 | + */ |
| 30 | +final readonly class ConfigSchemaCacheWarmer implements CacheWarmerInterface |
| 31 | +{ |
| 32 | + public function __construct( |
| 33 | + private KernelInterface $kernel, |
| 34 | + private ?LoggerInterface $logger = null, |
| 35 | + ) { |
| 36 | + } |
| 37 | + |
| 38 | + public function warmUp(string $cacheDir, ?string $buildDir = null): array |
| 39 | + { |
| 40 | + if (!$buildDir || !$this->kernel->isDebug() || !class_exists(JsonSchemaGenerator::class)) { |
| 41 | + return []; |
| 42 | + } |
| 43 | + |
| 44 | + $generator = new JsonSchemaGenerator(\dirname($this->kernel->getBuildDir()).'/config.schema.json'); |
| 45 | + |
| 46 | + if ($this->kernel instanceof Kernel) { |
| 47 | + /** @var ContainerBuilder $container */ |
| 48 | + $container = \Closure::bind(function (Kernel $kernel) { |
| 49 | + $containerBuilder = $kernel->getContainerBuilder(); |
| 50 | + $kernel->prepareContainer($containerBuilder); |
| 51 | + |
| 52 | + return $containerBuilder; |
| 53 | + }, null, $this->kernel)($this->kernel); |
| 54 | + |
| 55 | + $extensions = $container->getExtensions(); |
| 56 | + } else { |
| 57 | + $extensions = []; |
| 58 | + foreach ($this->kernel->getBundles() as $bundle) { |
| 59 | + $extension = $bundle->getContainerExtension(); |
| 60 | + if (null !== $extension) { |
| 61 | + $extensions[] = $extension; |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $tree = new ArrayNode($this->kernel->getEnvironment()); |
| 67 | + foreach ($extensions as $extension) { |
| 68 | + try { |
| 69 | + $configuration = null; |
| 70 | + if ($extension instanceof ConfigurationInterface) { |
| 71 | + $configuration = $extension; |
| 72 | + } elseif ($extension instanceof ConfigurationExtensionInterface) { |
| 73 | + $container = $this->kernel->getContainer(); |
| 74 | + $configuration = $extension->getConfiguration([], new ContainerBuilder($container instanceof Container ? new ContainerBag($container) : new ParameterBag())); |
| 75 | + } |
| 76 | + |
| 77 | + if (!$extensionConfigNode = $configuration?->getConfigTreeBuilder()->buildTree()) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $tree->addChild($extensionConfigNode); |
| 82 | + } catch (\Exception $e) { |
| 83 | + $this->logger?->warning('Failed to generate JSON schema for extension {extensionClass}: '.$e->getMessage(), ['exception' => $e, 'extensionClass' => $extension::class]); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + try { |
| 88 | + $generator->merge($tree, (object) [ |
| 89 | + 'description' => 'Symfony configuration', |
| 90 | + 'properties' => (object) [ |
| 91 | + 'when@'.$tree->getName() => ['$ref' => '#/definitions/'.$tree->getName()], |
| 92 | + ], |
| 93 | + 'patternProperties' => (object) [ |
| 94 | + 'when@[a-zA-Z0-9]+' => ['$ref' => '#/'], |
| 95 | + ], |
| 96 | + ]); |
| 97 | + } catch (\Exception $e) { |
| 98 | + $this->logger?->warning('Failed to generate JSON schema for the configuration: '.$e->getMessage(), ['exception' => $e]); |
| 99 | + } |
| 100 | + |
| 101 | + // No need to preload anything |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + public function isOptional(): bool |
| 106 | + { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + private function getRelativePath(string $path, string $projectDir): string |
| 111 | + { |
| 112 | + return './'.ltrim(substr($path, strspn($path ^ $projectDir, "\0")), '/'); |
| 113 | + } |
| 114 | +} |
0 commit comments