From 24f2958a3fc7fac464ff712dfac1cfd0d88a8ea6 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 25 Jun 2024 08:08:32 +0200 Subject: [PATCH] Unify how --format is handle by commands --- .../Bridge/Twig/Command/DebugCommand.php | 15 +++++++--- .../Bridge/Twig/Command/LintCommand.php | 5 +++- src/Symfony/Bridge/Twig/composer.json | 1 + .../Command/ConfigDebugCommand.php | 7 ++--- .../Command/ConfigDumpReferenceCommand.php | 9 ++---- .../Command/ContainerDebugCommand.php | 4 +++ .../Command/EventDispatcherDebugCommand.php | 5 ++++ .../Command/RouterDebugCommand.php | 4 +++ .../Command/ImportMapAuditCommand.php | 29 +++++++++++++++---- .../Command/ImportMapOutdatedCommand.php | 14 +++++++++ .../Component/Form/Command/DebugCommand.php | 1 + .../Messenger/Command/StatsCommand.php | 26 ++++++++++++----- src/Symfony/Component/Messenger/composer.json | 3 +- .../Translation/Command/XliffLintCommand.php | 4 +++ .../Uid/Command/GenerateUlidCommand.php | 1 + .../Uid/Command/GenerateUuidCommand.php | 1 + .../Component/Yaml/Command/LintCommand.php | 4 +++ 17 files changed, 103 insertions(+), 30 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index 42b456c207223..0e92e3e57acb2 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -57,7 +57,7 @@ protected function configure(): void ->setDefinition([ new InputArgument('name', InputArgument::OPTIONAL, 'The template name'), new InputOption('filter', null, InputOption::VALUE_REQUIRED, 'Show details for all entries matching this filter'), - new InputOption('format', null, InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'text'), + new InputOption('format', null, InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions())), 'txt'), ]) ->setHelp(<<<'EOF' The %command.name% command outputs a list of twig functions, @@ -93,8 +93,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int throw new InvalidArgumentException(\sprintf('Argument "name" not supported, it requires the Twig loader "%s".', FilesystemLoader::class)); } - match ($input->getOption('format')) { - 'text' => $name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter), + $format = $input->getOption('format'); + if ('text' === $format) { + trigger_deprecation('symfony/twig-bridge', '7.2', 'The "text" format is deprecated, use "txt" instead.'); + + $format = 'txt'; + } + match ($format) { + 'txt' => $name ? $this->displayPathsText($io, $name) : $this->displayGeneralText($io, $filter), 'json' => $name ? $this->displayPathsJson($io, $name) : $this->displayGeneralJson($io, $filter), default => throw new InvalidArgumentException(\sprintf('Supported formats are "%s".', implode('", "', $this->getAvailableFormatOptions()))), }; @@ -582,8 +588,9 @@ private function getFileLink(string $absolutePath): string return (string) $this->fileLinkFormatter?->format($absolutePath, 1); } + /** @return string[] */ private function getAvailableFormatOptions(): array { - return ['text', 'json']; + return ['txt', 'json']; } } diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 456c186430bbd..96b37c77ce9d9 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -71,8 +71,10 @@ protected function configure(): void Or of a whole directory: php %command.full_name% dirname - php %command.full_name% dirname --format=json +The --format option specifies the format of the command output: + + php %command.full_name% dirname --format=json EOF ) ; @@ -280,6 +282,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti } } + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'json', 'github']; diff --git a/src/Symfony/Bridge/Twig/composer.json b/src/Symfony/Bridge/Twig/composer.json index f7f8d32d620ea..b707dab25277d 100644 --- a/src/Symfony/Bridge/Twig/composer.json +++ b/src/Symfony/Bridge/Twig/composer.json @@ -17,6 +17,7 @@ ], "require": { "php": ">=8.2", + "symfony/deprecation-contracts": "^2.5|^3", "symfony/translation-contracts": "^2.5|^3", "twig/twig": "^3.9" }, diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php index fb79b39bf47b7..55c101e9c29e3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDebugCommand.php @@ -41,9 +41,6 @@ class ConfigDebugCommand extends AbstractConfigCommand { protected function configure(): void { - $commentedHelpFormats = array_map(fn ($format) => \sprintf('%s', $format), $this->getAvailableFormatOptions()); - $helpFormats = implode('", "', $commentedHelpFormats); - $this ->setDefinition([ new InputArgument('name', InputArgument::OPTIONAL, 'The bundle name or the extension alias'), @@ -60,8 +57,7 @@ protected function configure(): void php %command.full_name% framework php %command.full_name% FrameworkBundle -The --format option specifies the format of the configuration, -these are "{$helpFormats}". +The --format option specifies the format of the command output: php %command.full_name% framework --format=json @@ -268,6 +264,7 @@ private static function buildPathsCompletion(array $paths, string $prefix = ''): return $completionPaths; } + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'yaml', 'json']; diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php index 27dc01b112bcb..7e5cd765fd2d3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ConfigDumpReferenceCommand.php @@ -39,9 +39,6 @@ class ConfigDumpReferenceCommand extends AbstractConfigCommand { protected function configure(): void { - $commentedHelpFormats = array_map(fn ($format) => \sprintf('%s', $format), $this->getAvailableFormatOptions()); - $helpFormats = implode('", "', $commentedHelpFormats); - $this ->setDefinition([ new InputArgument('name', InputArgument::OPTIONAL, 'The Bundle name or the extension alias'), @@ -57,10 +54,9 @@ protected function configure(): void php %command.full_name% framework php %command.full_name% FrameworkBundle -The --format option specifies the format of the configuration, -these are "{$helpFormats}". +The --format option specifies the format of the command output: - php %command.full_name% FrameworkBundle --format=xml + php %command.full_name% FrameworkBundle --format=json For dumping a specific option, add its path as second argument (only available for the yaml format): @@ -181,6 +177,7 @@ private function getAvailableBundles(): array return $bundles; } + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['yaml', 'xml']; diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php index 9619d19726caa..46cdca9abf1de 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/ContainerDebugCommand.php @@ -106,6 +106,9 @@ protected function configure(): void php %command.full_name% --show-hidden +The --format option specifies the format of the command output: + + php %command.full_name% --format=json EOF ) ; @@ -358,6 +361,7 @@ public function filterToServiceTypes(string $serviceId): bool return class_exists($serviceId) || interface_exists($serviceId, false); } + /** @return string[] */ private function getAvailableFormatOptions(): array { return (new DescriptorHelper())->getFormats(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php index a3531d800e374..3c51cb1b71103 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/EventDispatcherDebugCommand.php @@ -60,6 +60,10 @@ protected function configure(): void To get specific listeners for an event, specify its name: php %command.full_name% kernel.request + +The --format option specifies the format of the command output: + + php %command.full_name% --format=json EOF ) ; @@ -153,6 +157,7 @@ private function searchForEvent(EventDispatcherInterface $dispatcher, string $ne return $output; } + /** @return string[] */ private function getAvailableFormatOptions(): array { return (new DescriptorHelper())->getFormats(); diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php index d57c0623f1392..13a6f75d01230 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/RouterDebugCommand.php @@ -61,6 +61,9 @@ protected function configure(): void php %command.full_name% +The --format option specifies the format of the command output: + + php %command.full_name% --format=json EOF ) ; @@ -164,6 +167,7 @@ private function findRouteContaining(string $name, RouteCollection $routes): Rou return $foundRoutes; } + /** @return string[] */ private function getAvailableFormatOptions(): array { return (new DescriptorHelper())->getFormats(); diff --git a/src/Symfony/Component/AssetMapper/Command/ImportMapAuditCommand.php b/src/Symfony/Component/AssetMapper/Command/ImportMapAuditCommand.php index 369377afd9489..c4620752c1eb6 100644 --- a/src/Symfony/Component/AssetMapper/Command/ImportMapAuditCommand.php +++ b/src/Symfony/Component/AssetMapper/Command/ImportMapAuditCommand.php @@ -15,6 +15,8 @@ use Symfony\Component\AssetMapper\ImportMap\ImportMapPackageAuditVulnerability; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -41,12 +43,19 @@ public function __construct( protected function configure(): void { - $this->addOption( - name: 'format', - mode: InputOption::VALUE_REQUIRED, - description: \sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())), - default: 'txt', - ); + $this + ->addOption( + name: 'format', + mode: InputOption::VALUE_REQUIRED, + description: \sprintf('The output format ("%s")', implode(', ', $this->getAvailableFormatOptions())), + default: 'txt', + ) + ->setHelp(<<<'EOT' +The --format option specifies the format of the command output: + + php %command.full_name% --format=json +EOT + ); } protected function initialize(InputInterface $input, OutputInterface $output): void @@ -180,6 +189,14 @@ private function displayJson(array $audit): int return 0 < array_sum($json['summary']) ? self::FAILURE : self::SUCCESS; } + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestOptionValuesFor('format')) { + $suggestions->suggestValues($this->getAvailableFormatOptions()); + } + } + + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'json']; diff --git a/src/Symfony/Component/AssetMapper/Command/ImportMapOutdatedCommand.php b/src/Symfony/Component/AssetMapper/Command/ImportMapOutdatedCommand.php index 14b76157190bc..39b5d669c5ce9 100644 --- a/src/Symfony/Component/AssetMapper/Command/ImportMapOutdatedCommand.php +++ b/src/Symfony/Component/AssetMapper/Command/ImportMapOutdatedCommand.php @@ -15,6 +15,8 @@ use Symfony\Component\AssetMapper\ImportMap\PackageUpdateInfo; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -59,6 +61,10 @@ protected function configure(): void Or specific packages only: php %command.full_name% + +The --format option specifies the format of the command output: + + php %command.full_name% --format=json EOT ); } @@ -99,6 +105,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int return Command::FAILURE; } + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestOptionValuesFor('format')) { + $suggestions->suggestValues($this->getAvailableFormatOptions()); + } + } + + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'json']; diff --git a/src/Symfony/Component/Form/Command/DebugCommand.php b/src/Symfony/Component/Form/Command/DebugCommand.php index 18d208cbe812c..91db6f1a91365 100644 --- a/src/Symfony/Component/Form/Command/DebugCommand.php +++ b/src/Symfony/Component/Form/Command/DebugCommand.php @@ -272,6 +272,7 @@ private function completeOptions(string $class, CompletionSuggestions $suggestio $suggestions->suggestValues($resolvedType->getOptionsResolver()->getDefinedOptions()); } + /** @return string[] */ private function getAvailableFormatOptions(): array { return (new DescriptorHelper())->getFormats(); diff --git a/src/Symfony/Component/Messenger/Command/StatsCommand.php b/src/Symfony/Component/Messenger/Command/StatsCommand.php index 9a6a62c334b28..1f0753d598016 100644 --- a/src/Symfony/Component/Messenger/Command/StatsCommand.php +++ b/src/Symfony/Component/Messenger/Command/StatsCommand.php @@ -14,6 +14,8 @@ use Psr\Container\ContainerInterface; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Completion\CompletionInput; +use Symfony\Component\Console\Completion\CompletionSuggestions; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -38,10 +40,9 @@ public function __construct( protected function configure(): void { - $outputFormats = implode(', ', $this->getAvailableFormatOptions()); $this ->addArgument('transport_names', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'List of transports\' names') - ->addOption('format', '', InputOption::VALUE_REQUIRED, 'The output format, e.g.: '.$outputFormats, 'text', $this->getAvailableFormatOptions()) + ->addOption('format', '', InputOption::VALUE_REQUIRED, \sprintf('The output format ("%s")', implode('", "', $this->getAvailableFormatOptions()))) ->setHelp(<<%command.name% command counts the messages for all the transports: @@ -51,8 +52,7 @@ protected function configure(): void php %command.full_name% -The --format option specifies the format of command output, -these are "{$outputFormats}". +The --format option specifies the format of the command output: php %command.full_name% --format=json EOF @@ -65,6 +65,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io = new SymfonyStyle($input, $output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output); $format = $input->getOption('format'); + if ('text' === $format) { + trigger_deprecation('symfony/messenger', '7.2', 'The "text" format is deprecated, use "txt" instead.'); + + $format = 'txt'; + } if (!\in_array($format, $this->getAvailableFormatOptions(), true)) { throw new InvalidArgumentException('Invalid output format.'); } @@ -94,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int } match ($format) { - 'text' => $this->outputText($io, $outputTable, $uncountableTransports), + 'txt' => $this->outputText($io, $outputTable, $uncountableTransports), 'json' => $this->outputJson($io, $outputTable, $uncountableTransports), }; @@ -127,14 +132,21 @@ private function outputJson(SymfonyStyle $io, array $outputTable, array $uncount private function formatSupportsWarnings(string $format): bool { return match ($format) { - 'text' => true, + 'txt' => true, 'json' => false, }; } + public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void + { + if ($input->mustSuggestOptionValuesFor('format')) { + $suggestions->suggestValues($this->getAvailableFormatOptions()); + } + } + /** @return string[] */ private function getAvailableFormatOptions(): array { - return ['text', 'json']; + return ['txt', 'json']; } } diff --git a/src/Symfony/Component/Messenger/composer.json b/src/Symfony/Component/Messenger/composer.json index c51fdbfb58161..3fdfe4a55ee26 100644 --- a/src/Symfony/Component/Messenger/composer.json +++ b/src/Symfony/Component/Messenger/composer.json @@ -18,7 +18,8 @@ "require": { "php": ">=8.2", "psr/log": "^1|^2|^3", - "symfony/clock": "^6.4|^7.0" + "symfony/clock": "^6.4|^7.0", + "symfony/deprecation-contracts": "^2.5|^3" }, "require-dev": { "psr/cache": "^1.0|^2.0|^3.0", diff --git a/src/Symfony/Component/Translation/Command/XliffLintCommand.php b/src/Symfony/Component/Translation/Command/XliffLintCommand.php index 26bda4c608d2b..82a9571ce8c21 100644 --- a/src/Symfony/Component/Translation/Command/XliffLintCommand.php +++ b/src/Symfony/Component/Translation/Command/XliffLintCommand.php @@ -72,6 +72,9 @@ protected function configure(): void Or of a whole directory: php %command.full_name% dirname + +The --format option specifies the format of the command output: + php %command.full_name% dirname --format=json EOF @@ -277,6 +280,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti } } + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'json', 'github']; diff --git a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php index c7cac11c572ad..da46bc5c1d05e 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUlidCommand.php @@ -105,6 +105,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti } } + /** @return string[] */ private function getAvailableFormatOptions(): array { return [ diff --git a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php index 14df7f5206aa4..2117eb753e30c 100644 --- a/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php +++ b/src/Symfony/Component/Uid/Command/GenerateUuidCommand.php @@ -194,6 +194,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti } } + /** @return string[] */ private function getAvailableFormatOptions(): array { return [ diff --git a/src/Symfony/Component/Yaml/Command/LintCommand.php b/src/Symfony/Component/Yaml/Command/LintCommand.php index 0a8dbc1c4ad65..0fab77c569b67 100644 --- a/src/Symfony/Component/Yaml/Command/LintCommand.php +++ b/src/Symfony/Component/Yaml/Command/LintCommand.php @@ -72,6 +72,9 @@ protected function configure(): void Or of a whole directory: php %command.full_name% dirname + +The --format option specifies the format of the command output: + php %command.full_name% dirname --format=json You can also exclude one or more specific files: @@ -266,6 +269,7 @@ public function complete(CompletionInput $input, CompletionSuggestions $suggesti } } + /** @return string[] */ private function getAvailableFormatOptions(): array { return ['txt', 'json', 'github'];