Skip to content

Commit

Permalink
feature #36186 [FrameworkBundle] Dump kernel extension configuration …
Browse files Browse the repository at this point in the history
…(guillbdx)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[FrameworkBundle] Dump kernel extension configuration

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | #34756
| License       | MIT

If the kernel is a container extension and defines a configuration, the `config:dump-reference` will now be able to dump it.

Commits
-------

2ccafb1 [FrameworkBundle] Dump kernel extension configuration
  • Loading branch information
nicolas-grekas committed Mar 31, 2020
2 parents 0876480 + 2ccafb1 commit 0c74ff4
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ CHANGELOG
* Added tag `routing.expression_language_function` to define functions available in route conditions
* Added `debug:container --deprecations` option to see compile-time deprecations.
* Made `BrowserKitAssertionsTrait` report the original error message in case of a failure
* Added ability for `config:dump-reference` and `debug:config` to dump and debug kernel container extension configuration.

5.0.0
-----
Expand Down
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;

/**
Expand Down Expand Up @@ -64,6 +65,22 @@ protected function findExtension(string $name)
$bundles = $this->initializeBundles();
$minScore = INF;

$kernel = $this->getApplication()->getKernel();
if ($kernel instanceof ExtensionInterface && ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)) {
if ($name === $kernel->getAlias()) {
return $kernel;
}

if ($kernel->getAlias()) {
$distance = levenshtein($name, $kernel->getAlias());

if ($distance < $minScore) {
$guess = $kernel->getAlias();
$minScore = $distance;
}
}
}

foreach ($bundles as $bundle) {
if ($name === $bundle->getName()) {
if (!$bundle->getContainerExtension()) {
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php
Expand Up @@ -11,13 +11,16 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Compiler\ValidateEnvPlaceholdersPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Yaml\Yaml;

/**
Expand Down Expand Up @@ -70,6 +73,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (null === $name = $input->getArgument('name')) {
$this->listBundles($errorIo);

$kernel = $this->getApplication()->getKernel();
if ($kernel instanceof ExtensionInterface
&& ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)
&& $kernel->getAlias()
) {
$errorIo->table(['Kernel Extension'], [[$kernel->getAlias()]]);
}

$errorIo->comment('Provide the name of a bundle as the first argument of this command to dump its configuration. (e.g. <comment>debug:config FrameworkBundle</comment>)');
$errorIo->comment('For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>debug:config FrameworkBundle serializer</comment> to dump the <comment>framework.serializer</comment> configuration)');

Expand Down
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Command;

use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Dumper\XmlReferenceDumper;
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
use Symfony\Component\Console\Exception\InvalidArgumentException;
Expand All @@ -19,6 +20,8 @@
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\DependencyInjection\Extension\ExtensionInterface;

/**
* A console command for dumping available configuration reference.
Expand Down Expand Up @@ -81,6 +84,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (null === $name = $input->getArgument('name')) {
$this->listBundles($errorIo);

$kernel = $this->getApplication()->getKernel();
if ($kernel instanceof ExtensionInterface
&& ($kernel instanceof ConfigurationInterface || $kernel instanceof ConfigurationExtensionInterface)
&& $kernel->getAlias()
) {
$errorIo->table(['Kernel Extension'], [[$kernel->getAlias()]]);
}

$errorIo->comment([
'Provide the name of a bundle as the first argument of this command to dump its default configuration. (e.g. <comment>config:dump-reference FrameworkBundle</comment>)',
'For dumping a specific option, add its path as the second argument of this command. (e.g. <comment>config:dump-reference FrameworkBundle profiler.matcher</comment> to dump the <comment>framework.profiler.matcher</comment> configuration)',
Expand All @@ -91,7 +103,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$extension = $this->findExtension($name);

$configuration = $extension->getConfiguration([], $this->getContainerBuilder());
if ($extension instanceof ConfigurationInterface) {
$configuration = $extension;
} else {
$configuration = $extension->getConfiguration([], $this->getContainerBuilder());
}

$this->validateConfiguration($extension, $configuration);

Expand Down
Expand Up @@ -30,6 +30,14 @@ protected function setUp(): void
$this->application->doRun(new ArrayInput([]), new NullOutput());
}

public function testDumpKernelExtension()
{
$tester = $this->createCommandTester();
$ret = $tester->execute(['name' => 'foo']);
$this->assertStringContainsString('foo:', $tester->getDisplay());
$this->assertStringContainsString(' bar', $tester->getDisplay());
}

public function testDumpBundleName()
{
$tester = $this->createCommandTester();
Expand Down
Expand Up @@ -12,9 +12,12 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\app;

use Psr\Log\NullLogger;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;

Expand All @@ -23,7 +26,7 @@
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class AppKernel extends Kernel
class AppKernel extends Kernel implements ExtensionInterface, ConfigurationInterface
{
private $varDir;
private $testCase;
Expand Down Expand Up @@ -106,4 +109,32 @@ public function getContainer(): ContainerInterface

return parent::getContainer();
}

public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('foo');
$rootNode = $treeBuilder->getRootNode();
$rootNode->children()->scalarNode('foo')->defaultValue('bar')->end()->end();

return $treeBuilder;
}

public function load(array $configs, ContainerBuilder $container)
{
}

public function getNamespace()
{
return '';
}

public function getXsdValidationBasePath()
{
return false;
}

public function getAlias()
{
return 'foo';
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Compiler;

use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
Expand Down Expand Up @@ -68,14 +69,18 @@ public function process(ContainerBuilder $container)
$processor = new Processor();

foreach ($extensions as $name => $extension) {
if (!$extension instanceof ConfigurationExtensionInterface || !$config = array_filter($container->getExtensionConfig($name))) {
if (!($extension instanceof ConfigurationExtensionInterface || $extension instanceof ConfigurationInterface)
|| !$config = array_filter($container->getExtensionConfig($name))
) {
// this extension has no semantic configuration or was not called
continue;
}

$config = $resolvingBag->resolveValue($config);

if (null === $configuration = $extension->getConfiguration($config, $container)) {
if ($extension instanceof ConfigurationInterface) {
$configuration = $extension;
} elseif (null === $configuration = $extension->getConfiguration($config, $container)) {
continue;
}

Expand Down

0 comments on commit 0c74ff4

Please sign in to comment.