Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Allow using their name without added suffix when using `#[Target]` for custom services
* Deprecate `Symfony\Bundle\FrameworkBundle\Console\Application::add()` in favor of `Symfony\Bundle\FrameworkBundle\Console\Application::addCommand()`
* Add `assertEmailAddressNotContains()` to the `MailerAssertionsTrait`
* Generate JSON schema for YAML configuration

7.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;

use Psr\Log\LoggerInterface;
use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\JsonSchemaGenerator;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Generate config json schema.
*/
final readonly class ConfigSchemaCacheWarmer implements CacheWarmerInterface
{
public function __construct(
private KernelInterface $kernel,
private ?LoggerInterface $logger = null,
) {
}

public function warmUp(string $cacheDir, ?string $buildDir = null): array
{
if (!$buildDir || !$this->kernel->isDebug() || !class_exists(JsonSchemaGenerator::class)) {
return [];
}

$generator = new JsonSchemaGenerator(\dirname($this->kernel->getBuildDir()).'/config.schema.json');

if ($this->kernel instanceof Kernel) {
/** @var ContainerBuilder $container */
$container = \Closure::bind(function (Kernel $kernel) {
$containerBuilder = $kernel->getContainerBuilder();

Check failure on line 49 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:49:46: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::getContainerBuilder from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

Check failure on line 49 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:49:46: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::getContainerBuilder from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)
$kernel->prepareContainer($containerBuilder);

Check failure on line 50 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:50:26: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::prepareContainer from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

Check failure on line 50 in src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php

View workflow job for this annotation

GitHub Actions / Psalm

InaccessibleMethod

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigSchemaCacheWarmer.php:50:26: InaccessibleMethod: Cannot access protected method Symfony\Component\HttpKernel\Kernel::prepareContainer from context Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer (see https://psalm.dev/003)

return $containerBuilder;
}, null, $this->kernel)($this->kernel);

$extensions = $container->getExtensions();
} else {
$extensions = [];
foreach ($this->kernel->getBundles() as $bundle) {
$extension = $bundle->getContainerExtension();
if (null !== $extension) {
$extensions[] = $extension;
}
}
}

$tree = new ArrayNode($this->kernel->getEnvironment());
foreach ($extensions as $extension) {
try {
$configuration = null;
if ($extension instanceof ConfigurationInterface) {
$configuration = $extension;
} elseif ($extension instanceof ConfigurationExtensionInterface) {
$container = $this->kernel->getContainer();
$configuration = $extension->getConfiguration([], new ContainerBuilder($container instanceof Container ? new ContainerBag($container) : new ParameterBag()));
}

if (!$extensionConfigNode = $configuration?->getConfigTreeBuilder()->buildTree()) {
continue;
}

$tree->addChild($extensionConfigNode);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate JSON schema for extension {extensionClass}: '.$e->getMessage(), ['exception' => $e, 'extensionClass' => $extension::class]);
}
}

try {
$generator->merge($tree, (object) [
'description' => 'Symfony configuration',
'properties' => (object) [
'when@'.$tree->getName() => ['$ref' => '#/definitions/'.$tree->getName()],
],
'patternProperties' => (object) [
'when@[a-zA-Z0-9]+' => ['$ref' => '#/'],
],
]);
} catch (\Exception $e) {
$this->logger?->warning('Failed to generate JSON schema for the configuration: '.$e->getMessage(), ['exception' => $e]);
}

// No need to preload anything
return [];
}

public function isOptional(): bool
{
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Clock\ClockInterface as PsrClockInterface;
use Psr\EventDispatcher\EventDispatcherInterface as PsrEventDispatcherInterface;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigSchemaCacheWarmer;
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\ClockInterface;
Expand Down Expand Up @@ -238,6 +239,10 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
->tag('kernel.cache_warmer')

->set('config_schema.warmer', ConfigSchemaCacheWarmer::class)
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
->tag('kernel.cache_warmer')

->set('clock', Clock::class)
->alias(ClockInterface::class, 'clock')
->alias(PsrClockInterface::class, 'clock')
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add TagAwareAdapterInterface to NullAdapter
* Add `JsonSchemaGenerator`

7.3
---
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Config/Definition/BaseNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ public function addEquivalentValue(mixed $originalValue, mixed $equivalentValue)
$this->equivalentValues[] = [$originalValue, $equivalentValue];
}

/**
* @internal
*/
public function getEquivalentValues(): array
{
return $this->equivalentValues;
}

/**
* Set this node as required.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/Config/Definition/BooleanNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public function __construct(
parent::__construct($name, $parent, $pathSeparator);
}

/**
* @internal
*/
public function isNullable(): bool
{
return $this->nullable;
}

protected function validateType(mixed $value): void
{
if (!\is_bool($value)) {
Expand Down
Loading
Loading