From ec518e1db06c23a429f092696b10b26eb3446a43 Mon Sep 17 00:00:00 2001 From: Nyholm Date: Sun, 28 Mar 2021 12:52:27 +0200 Subject: [PATCH] Adding command to dump builders --- .../Command/ConfigDumpBuilderCommand.php | 110 ++++++++++++++++++ .../FrameworkExtension.php | 1 + .../Resources/config/config.php | 25 ++++ .../Resources/config/console.php | 8 ++ 4 files changed, 144 insertions(+) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpBuilderCommand.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Resources/config/config.php diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpBuilderCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpBuilderCommand.php new file mode 100644 index 0000000000000..faf49a31ca79d --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpBuilderCommand.php @@ -0,0 +1,110 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Command; + +use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface; +use Symfony\Component\Config\Definition\ConfigurationInterface; +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\Extension\ExtensionInterface; +use Symfony\Component\HttpKernel\Bundle\BundleInterface; + +/** + * A command for dumping all config builder for your bundles. + * + * @author Tobias Nyholm + * + * @final + */ +class ConfigDumpBuilderCommand extends AbstractConfigCommand +{ + protected static $defaultName = 'config:dump-builders'; + protected static $defaultDescription = 'Dump the config builder for an extension'; + + private $generator; + + public function __construct(ConfigBuilderGeneratorInterface $generator) + { + $this->generator = $generator; + parent::__construct(); + } + + /** + * {@inheritdoc} + */ + protected function configure() + { + $this + ->setDefinition([ + new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'), + ]) + ->setDescription(self::$defaultDescription) + ->setHelp(<<<'EOF' +The %command.name% command dumps "configuration bundles" that help +writing configuration for an extension/bundle. + +Either the extension alias or bundle name can be used: + + php %command.full_name% framework + php %command.full_name% FrameworkBundle + +EOF + ) + ; + } + + /** + * {@inheritdoc} + * + * @throws \LogicException + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $errorIo = $io->getErrorStyle(); + + if (null !== $name = $input->getArgument('name')) { + $extensions = [$this->findExtension($name)]; + } else { + $extensions = array_map(function (BundleInterface $a) { + return $a->getContainerExtension(); + }, $this->getApplication()->getKernel()->getBundles()); + } + + foreach ($extensions as $extension) { + if (null === $extension) { + continue; + } + + try { + $this->dumpExtension($extension); + } catch (\Throwable $e) { + $errorIo->error(sprintf('Could not dump configuration for "%s". Exception: %s', \get_class($extension), $e->getMessage())); + } + } + + return 0; + } + + private function dumpExtension(ExtensionInterface $extension): void + { + if ($extension instanceof ConfigurationInterface) { + $configuration = $extension; + } else { + $configuration = $extension->getConfiguration([], $this->getContainerBuilder()); + } + + $this->generator->build($configuration); + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 7d76a17ee5154..263ea76d454c2 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -425,6 +425,7 @@ public function load(array $configs, ContainerBuilder $container) $this->registerAnnotationsConfiguration($config['annotations'], $container, $loader); $this->registerPropertyAccessConfiguration($config['property_access'], $container, $loader); $this->registerSecretsConfiguration($config['secrets'], $container, $loader); + $loader->load('config.php'); if ($this->isConfigEnabled($container, $config['serializer'])) { if (!class_exists(\Symfony\Component\Serializer\Serializer::class)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/config.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/config.php new file mode 100644 index 0000000000000..55da989444497 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/config.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\DependencyInjection\Loader\Configurator; + +use Symfony\Component\Config\Builder\ConfigBuilderGenerator; +use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface; + +return static function (ContainerConfigurator $container) { + $container->services() + ->set('config.builder_generator', ConfigBuilderGenerator::class) + ->args([ + param('kernel.build_dir'), + ]) + ->alias(ConfigBuilderGeneratorInterface::class, 'config.builder_generator') + ; +}; diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php index 1c05d8760e614..407890d42a6a8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/console.php @@ -20,6 +20,7 @@ use Symfony\Bundle\FrameworkBundle\Command\CachePoolPruneCommand; use Symfony\Bundle\FrameworkBundle\Command\CacheWarmupCommand; use Symfony\Bundle\FrameworkBundle\Command\ConfigDebugCommand; +use Symfony\Bundle\FrameworkBundle\Command\ConfigDumpBuilderCommand; use Symfony\Bundle\FrameworkBundle\Command\ConfigDumpReferenceCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerDebugCommand; use Symfony\Bundle\FrameworkBundle\Command\ContainerLintCommand; @@ -38,6 +39,7 @@ use Symfony\Bundle\FrameworkBundle\Command\WorkflowDumpCommand; use Symfony\Bundle\FrameworkBundle\Command\YamlLintCommand; use Symfony\Bundle\FrameworkBundle\EventListener\SuggestMissingPackageSubscriber; +use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface; use Symfony\Component\Console\EventListener\ErrorListener; use Symfony\Component\Messenger\Command\ConsumeMessagesCommand; use Symfony\Component\Messenger\Command\DebugCommand; @@ -111,6 +113,12 @@ ->set('console.command.config_debug', ConfigDebugCommand::class) ->tag('console.command') + ->set('console.command.config_dump_builder', ConfigDumpBuilderCommand::class) + ->args([ + service(ConfigBuilderGeneratorInterface::class), + ]) + ->tag('console.command') + ->set('console.command.config_dump_reference', ConfigDumpReferenceCommand::class) ->tag('console.command')