Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Config] Add NodeDefinition::docUrl() #59762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('debug');

$rootNode = $treeBuilder->getRootNode();
$rootNode->children()
$rootNode
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/debug.html', 'symfony/debug-bundle')
->children()
->integerNode('max_items')
->info('Max number of displayed items past the first level, -1 means no limit.')
->min(-1)
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/DebugBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"require": {
"php": ">=8.2",
"ext-xml": "*",
"composer-runtime-api": ">=2.1",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When installing the smallest possible Symfony app, we currently only require Symfony deps. So, that would be the first non-Symfony dependency that we require.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same thought until I realized the framework bundle already has this dependency:
https://github.com/symfony/symfony/blob/7.3/src/Symfony/Bundle/FrameworkBundle/composer.json

"symfony/dependency-injection": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/twig-bridge": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0"
},
"require-dev": {
"symfony/config": "^6.4|^7.0",
"symfony/config": "^7.3",
"symfony/web-profiler-bundle": "^6.4|^7.0"
},
"conflict": {
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@
$io->title(
\sprintf('Current configuration for %s', $name === $extensionAlias ? \sprintf('extension with alias "%s"', $extensionAlias) : \sprintf('"%s"', $name))
);

if ($docUrl = $this->getDocUrl($extension, $container)) {
$io->comment(\sprintf('Documentation at %s', $docUrl));
}
}

$io->writeln($this->convertToFormat([$extensionAlias => $config], $format));
Expand Down Expand Up @@ -269,4 +273,15 @@
{
return ['txt', 'yaml', 'json'];
}

private function getDocUrl(ExtensionInterface $extension, ContainerBuilder $container): ?string
{
$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($container->getExtensionConfig($extension->getAlias()), $container);

Check failure on line 279 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php:279:98: UndefinedInterfaceMethod: Method Symfony\Component\DependencyInjection\Extension\ExtensionInterface::getConfiguration does not exist (see https://psalm.dev/181)

Check failure on line 279 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php:279:98: UndefinedInterfaceMethod: Method Symfony\Component\DependencyInjection\Extension\ExtensionInterface::getConfiguration does not exist (see https://psalm.dev/181)

return $configuration
->getConfigTreeBuilder()
->getRootNode()
->getNode(true)
->getAttribute('docUrl');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -123,6 +124,10 @@
$message .= \sprintf(' at path "%s"', $path);
}

if ($docUrl = $this->getExtensionDocUrl($extension)) {

Check failure on line 127 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:127:49: InvalidArgument: Argument 1 of Symfony\Bundle\FrameworkBundle\Command\ConfigDumpReferenceCommand::getExtensionDocUrl expects Symfony\Component\Config\Definition\ConfigurationInterface|Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface, but Symfony\Component\DependencyInjection\Extension\ExtensionInterface provided (see https://psalm.dev/004)

Check failure on line 127 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

InvalidArgument

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:127:49: InvalidArgument: Argument 1 of Symfony\Bundle\FrameworkBundle\Command\ConfigDumpReferenceCommand::getExtensionDocUrl expects Symfony\Component\Config\Definition\ConfigurationInterface|Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface, but Symfony\Component\DependencyInjection\Extension\ExtensionInterface provided (see https://psalm.dev/004)
$message .= \sprintf(' (see %s)', $docUrl);
}

switch ($format) {
case 'yaml':
$io->writeln(\sprintf('# %s', $message));
Expand Down Expand Up @@ -182,4 +187,18 @@
{
return ['yaml', 'xml'];
}

private function getExtensionDocUrl(ConfigurationInterface|ConfigurationExtensionInterface $extension): ?string
{
$kernel = $this->getApplication()->getKernel();

Check failure on line 193 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:193:44: UndefinedMethod: Method Symfony\Component\Console\Application::getKernel does not exist (see https://psalm.dev/022)

Check failure on line 193 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:193:44: UndefinedMethod: Method Symfony\Component\Console\Application::getKernel does not exist (see https://psalm.dev/022)
$container = $this->getContainerBuilder($kernel);

$configuration = $extension instanceof ConfigurationInterface ? $extension : $extension->getConfiguration($container->getExtensionConfig($extension->getAlias()), $container);

Check failure on line 196 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:196:158: UndefinedInterfaceMethod: Method Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface::getAlias does not exist (see https://psalm.dev/181)

Check failure on line 196 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:196:158: UndefinedInterfaceMethod: Method Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface::getAlias does not exist (see https://psalm.dev/181)

return $configuration
->getConfigTreeBuilder()
->getRootNode()
->getNode(true)
->getAttribute('docUrl');

Check failure on line 202 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:202:15: UndefinedInterfaceMethod: Method Symfony\Component\Config\Definition\NodeInterface::getAttribute does not exist (see https://psalm.dev/181)

Check failure on line 202 in src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php

View workflow job for this annotation

GitHub Actions / Psalm

UndefinedInterfaceMethod

src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php:202:15: UndefinedInterfaceMethod: Method Symfony\Component\Config\Definition\NodeInterface::getAttribute does not exist (see https://psalm.dev/181)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $treeBuilder->getRootNode();

$rootNode
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/framework.html', 'symfony/framework-bundle')
->beforeNormalization()
->ifTrue(fn ($v) => !isset($v['assets']) && isset($v['templating']) && class_exists(Package::class))
->then(function ($v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function getConfigTreeBuilder(): TreeBuilder
$rootNode = $tb->getRootNode();

$rootNode
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/security.html', 'symfony/security-bundle')
->beforeNormalization()
->always()
->then(function ($v) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SecurityBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"composer-runtime-api": ">=2.1",
"ext-xml": "*",
"symfony/clock": "^6.4|^7.0",
"symfony/config": "^6.4|^7.0",
"symfony/config": "^7.3",
"symfony/dependency-injection": "^6.4.11|^7.1.4",
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public function getConfigTreeBuilder(): TreeBuilder
$treeBuilder = new TreeBuilder('twig');
$rootNode = $treeBuilder->getRootNode();

$rootNode->beforeNormalization()
$rootNode
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/twig.html', 'symfony/twig-bundle')
->beforeNormalization()
->ifTrue(fn ($v) => \is_array($v) && \array_key_exists('exception_controller', $v))
->then(function ($v) {
if (isset($v['exception_controller'])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"require": {
"php": ">=8.2",
"composer-runtime-api": ">=2.1",
"symfony/config": "^6.4|^7.0",
"symfony/config": "^7.3",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/twig-bridge": "^6.4|^7.0",
"symfony/http-foundation": "^6.4|^7.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('web_profiler');

$treeBuilder->getRootNode()
$treeBuilder
->getRootNode()
->docUrl('https://symfony.com/doc/{version:major}.{version:minor}/reference/configuration/web_profiler.html', 'symfony/web-profiler-bundle')
->children()
->arrayNode('toolbar')
->info('Profiler toolbar configuration')
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/WebProfilerBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
],
"require": {
"php": ">=8.2",
"symfony/config": "^6.4|^7.0",
"composer-runtime-api": ">=2.1",
"symfony/config": "^7.3",
"symfony/framework-bundle": "^6.4|^7.0",
"symfony/http-kernel": "^6.4|^7.0",
"symfony/routing": "^6.4|^7.0",
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 @@ -7,6 +7,7 @@ CHANGELOG
* Add `ExprBuilder::ifFalse()`
* Add support for info on `ArrayNodeDefinition::canBeEnabled()` and `ArrayNodeDefinition::canBeDisabled()`
* Allow using an enum FQCN with `EnumNode`
* Add `NodeDefinition::docUrl()`

7.2
---
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Config/Definition/Builder/NodeDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Config\Definition\Builder;

use Composer\InstalledVersions;
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Exception\InvalidDefinitionException;
use Symfony\Component\Config\Definition\NodeInterface;
Expand Down Expand Up @@ -76,6 +77,26 @@ public function example(string|array $example): static
return $this->attribute('example', $example);
}

/**
* Sets the documentation URI, as usually put in the "@see" tag of a doc block. This
* can either be a URL or a file path. You can use the placeholders {package},
* {version:major} and {version:minor} in the URI.
*
* @return $this
*/
public function docUrl(string $uri, ?string $package = null): static
{
if ($package) {
preg_match('/^(\d+)\.(\d+)\.(\d+)/', InstalledVersions::getVersion($package) ?? '', $m);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No $, because some packages may return a fourth number (in local, phpunit/phpunit returns 9.6.99.0 for example on my computer)

}

return $this->attribute('docUrl', strtr($uri, [
'{package}' => $package ?? '',
'{version:major}' => $m[1] ?? '',
'{version:minor}' => $m[2] ?? '',
]));
}

/**
* Sets an attribute on the node.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,35 @@ public function testSetPathSeparatorChangesChildren()

$parentNode->setPathSeparator('/');
}

public function testDocUrl()
{
$node = new ArrayNodeDefinition('node');
$node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/phpunit');

$r = new \ReflectionObject($node);
$p = $r->getProperty('attributes');

$this->assertMatchesRegularExpression('~^https://example.com/doc/phpunit/phpunit/\d+\.\d+$~', $p->getValue($node)['docUrl']);
}

public function testDocUrlWithoutPackage()
{
$node = new ArrayNodeDefinition('node');
$node->docUrl('https://example.com/doc/empty{version:major}.empty{version:minor}');

$r = new \ReflectionObject($node);
$p = $r->getProperty('attributes');

$this->assertSame('https://example.com/doc/empty.empty', $p->getValue($node)['docUrl']);
}

public function testUnknownPackageThrowsException()
{
$this->expectException(\OutOfBoundsException::class);
$this->expectExceptionMessage('Package "phpunit/invalid" is not installed');

$node = new ArrayNodeDefinition('node');
$node->docUrl('https://example.com/doc/{package}/{version:major}.{version:minor}', 'phpunit/invalid');
}
}
Loading