From d5a7608036c520ce73711575309716dc4d21746f Mon Sep 17 00:00:00 2001 From: Roland Franssen Date: Sat, 17 Sep 2016 10:32:34 +0000 Subject: [PATCH] [Console] Exclude empty namespaces in text descriptor --- .../Console/Descriptor/TextDescriptor.php | 52 +++++++---- .../Descriptor/AbstractDescriptorTest.php | 4 +- .../Tests/Descriptor/JsonDescriptorTest.php | 4 +- .../Tests/Descriptor/TextDescriptorTest.php | 8 ++ .../Tests/Fixtures/DescriptorApplication2.php | 1 + .../Tests/Fixtures/DescriptorCommand4.php | 25 ++++++ .../Console/Tests/Fixtures/application_2.json | 89 ++++++++++++++++++- .../Console/Tests/Fixtures/application_2.md | 81 +++++++++++++++++ .../Console/Tests/Fixtures/application_2.txt | 1 + .../Console/Tests/Fixtures/application_2.xml | 38 ++++++++ .../application_filtered_namespace.txt | 16 ++++ 11 files changed, 298 insertions(+), 21 deletions(-) create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/DescriptorCommand4.php create mode 100644 src/Symfony/Component/Console/Tests/Fixtures/application_filtered_namespace.txt diff --git a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php index 81710046c610..1829405c98e0 100644 --- a/src/Symfony/Component/Console/Descriptor/TextDescriptor.php +++ b/src/Symfony/Component/Console/Descriptor/TextDescriptor.php @@ -191,7 +191,20 @@ protected function describeApplication(Application $application, array $options $this->writeText("\n"); $this->writeText("\n"); - $width = $this->getColumnWidth($description->getCommands()); + $commands = $description->getCommands(); + $namespaces = $description->getNamespaces(); + if ($describedNamespace && $namespaces) { + // make sure all alias commands are included when describing a specific namespace + $describedNamespaceInfo = reset($namespaces); + foreach ($describedNamespaceInfo['commands'] as $name) { + $commands[$name] = $description->getCommand($name); + } + } + + // calculate max. width based on available commands per namespace + $width = $this->getColumnWidth(call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) { + return array_intersect($namespace['commands'], array_keys($commands)); + }, $namespaces))); if ($describedNamespace) { $this->writeText(sprintf('Available commands for the "%s" namespace:', $describedNamespace), $options); @@ -199,23 +212,26 @@ protected function describeApplication(Application $application, array $options $this->writeText('Available commands:', $options); } - // add commands by namespace - $commands = $description->getCommands(); + foreach ($namespaces as $namespace) { + $namespace['commands'] = array_filter($namespace['commands'], function ($name) use ($commands) { + return isset($commands[$name]); + }); + + if (!$namespace['commands']) { + continue; + } - foreach ($description->getNamespaces() as $namespace) { if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) { $this->writeText("\n"); $this->writeText(' '.$namespace['id'].'', $options); } foreach ($namespace['commands'] as $name) { - if (isset($commands[$name])) { - $this->writeText("\n"); - $spacingWidth = $width - Helper::strlen($name); - $command = $commands[$name]; - $commandAliases = $this->getCommandAliasesText($command); - $this->writeText(sprintf(' %s%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options); - } + $this->writeText("\n"); + $spacingWidth = $width - Helper::strlen($name); + $command = $commands[$name]; + $commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : ''; + $this->writeText(sprintf(' %s%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options); } } @@ -276,7 +292,7 @@ private function formatDefaultValue($default) } /** - * @param Command[] $commands + * @param (Command|string)[] $commands * * @return int */ @@ -285,13 +301,17 @@ private function getColumnWidth(array $commands) $widths = array(); foreach ($commands as $command) { - $widths[] = Helper::strlen($command->getName()); - foreach ($command->getAliases() as $alias) { - $widths[] = Helper::strlen($alias); + if ($command instanceof Command) { + $widths[] = Helper::strlen($command->getName()); + foreach ($command->getAliases() as $alias) { + $widths[] = Helper::strlen($alias); + } + } else { + $widths[] = Helper::strlen($command); } } - return max($widths) + 2; + return $widths ? max($widths) + 2 : 0; } /** diff --git a/src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php b/src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php index fcbb719b6cd9..3686e7543b6b 100644 --- a/src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php +++ b/src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php @@ -98,10 +98,10 @@ protected function getDescriptionTestData(array $objects) return $data; } - protected function assertDescription($expectedDescription, $describedObject) + protected function assertDescription($expectedDescription, $describedObject, array $options = array()) { $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true); - $this->getDescriptor()->describe($output, $describedObject, array('raw_output' => true)); + $this->getDescriptor()->describe($output, $describedObject, $options + array('raw_output' => true)); $this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch()))); } } diff --git a/src/Symfony/Component/Console/Tests/Descriptor/JsonDescriptorTest.php b/src/Symfony/Component/Console/Tests/Descriptor/JsonDescriptorTest.php index f9a15612b3f9..dffcaf6648ce 100644 --- a/src/Symfony/Component/Console/Tests/Descriptor/JsonDescriptorTest.php +++ b/src/Symfony/Component/Console/Tests/Descriptor/JsonDescriptorTest.php @@ -26,10 +26,10 @@ protected function getFormat() return 'json'; } - protected function assertDescription($expectedDescription, $describedObject) + protected function assertDescription($expectedDescription, $describedObject, array $options = array()) { $output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true); - $this->getDescriptor()->describe($output, $describedObject, array('raw_output' => true)); + $this->getDescriptor()->describe($output, $describedObject, $options + array('raw_output' => true)); $this->assertEquals(json_decode(trim($expectedDescription), true), json_decode(trim(str_replace(PHP_EOL, "\n", $output->fetch())), true)); } } diff --git a/src/Symfony/Component/Console/Tests/Descriptor/TextDescriptorTest.php b/src/Symfony/Component/Console/Tests/Descriptor/TextDescriptorTest.php index 364e29c02664..c024a522efad 100644 --- a/src/Symfony/Component/Console/Tests/Descriptor/TextDescriptorTest.php +++ b/src/Symfony/Component/Console/Tests/Descriptor/TextDescriptorTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Tests\Descriptor; use Symfony\Component\Console\Descriptor\TextDescriptor; +use Symfony\Component\Console\Tests\Fixtures\DescriptorApplication2; use Symfony\Component\Console\Tests\Fixtures\DescriptorApplicationMbString; use Symfony\Component\Console\Tests\Fixtures\DescriptorCommandMbString; @@ -33,6 +34,13 @@ public function getDescribeApplicationTestData() )); } + public function testDescribeApplicationWithFilteredNamespace() + { + $application = new DescriptorApplication2(); + + $this->assertDescription(file_get_contents(__DIR__.'/../Fixtures/application_filtered_namespace.txt'), $application, array('namespace' => 'command4')); + } + protected function getDescriptor() { return new TextDescriptor(); diff --git a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php index 0fe87a18e436..7bb02fa54c1f 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php +++ b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php @@ -21,5 +21,6 @@ public function __construct() $this->add(new DescriptorCommand1()); $this->add(new DescriptorCommand2()); $this->add(new DescriptorCommand3()); + $this->add(new DescriptorCommand4()); } } diff --git a/src/Symfony/Component/Console/Tests/Fixtures/DescriptorCommand4.php b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorCommand4.php new file mode 100644 index 000000000000..13e8e5ba049f --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/DescriptorCommand4.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\Console\Tests\Fixtures; + +use Symfony\Component\Console\Command\Command; + +class DescriptorCommand4 extends Command +{ + protected function configure() + { + $this + ->setName('descriptor:command4') + ->setAliases(array('descriptor:alias_command4', 'command4:descriptor')) + ; + } +} diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.json b/src/Symfony/Component/Console/Tests/Fixtures/application_2.json index 6b31fd6cf49d..4777a60b52a3 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.json +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.json @@ -398,6 +398,85 @@ } } } + }, + { + "name": "descriptor:command4", + "hidden": false, + "usage": [ + "descriptor:command4", + "descriptor:alias_command4", + "command4:descriptor" + ], + "description": null, + "help": "", + "definition": { + "arguments": {}, + "options": { + "help": { + "name": "--help", + "shortcut": "-h", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Display this help message", + "default": false + }, + "quiet": { + "name": "--quiet", + "shortcut": "-q", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Do not output any message", + "default": false + }, + "verbose": { + "name": "--verbose", + "shortcut": "-v|-vv|-vvv", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug", + "default": false + }, + "version": { + "name": "--version", + "shortcut": "-V", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Display this application version", + "default": false + }, + "ansi": { + "name": "--ansi", + "shortcut": "", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Force ANSI output", + "default": false + }, + "no-ansi": { + "name": "--no-ansi", + "shortcut": "", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Disable ANSI output", + "default": false + }, + "no-interaction": { + "name": "--no-interaction", + "shortcut": "-n", + "accept_value": false, + "is_value_required": false, + "is_multiple": false, + "description": "Do not ask any interactive question", + "default": false + } + } + } } ], "namespaces": [ @@ -410,12 +489,20 @@ "list" ] }, + { + "id": "command4", + "commands": [ + "command4:descriptor" + ] + }, { "id": "descriptor", "commands": [ + "descriptor:alias_command4", "descriptor:command1", "descriptor:command2", - "descriptor:command3" + "descriptor:command3", + "descriptor:command4" ] } ] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.md b/src/Symfony/Component/Console/Tests/Fixtures/application_2.md index 14da75e45866..5b4896c0825c 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.md +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.md @@ -6,10 +6,16 @@ My Symfony application v1.0 * [`help`](#help) * [`list`](#list) +**command4:** + +* [`command4:descriptor`](#descriptorcommand4) + **descriptor:** +* [`descriptor:alias_command4`](#descriptorcommand4) * [`descriptor:command1`](#descriptorcommand1) * [`descriptor:command2`](#descriptorcommand2) +* [`descriptor:command4`](#descriptorcommand4) `help` ------ @@ -348,3 +354,78 @@ Do not ask any interactive question * Is value required: no * Is multiple: no * Default: `false` + +`descriptor:command4` +--------------------- + +### Usage + +* `descriptor:command4` +* `descriptor:alias_command4` +* `command4:descriptor` + + +### Options + +#### `--help|-h` + +Display this help message + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--quiet|-q` + +Do not output any message + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--verbose|-v|-vv|-vvv` + +Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--version|-V` + +Display this application version + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--ansi` + +Force ANSI output + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--no-ansi` + +Disable ANSI output + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` + +#### `--no-interaction|-n` + +Do not ask any interactive question + +* Accept value: no +* Is value required: no +* Is multiple: no +* Default: `false` diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_2.txt index b28036599642..d624f1946622 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.txt +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.txt @@ -18,3 +18,4 @@ My Symfony application v1.0 descriptor descriptor:command1 [alias1|alias2] command 1 description descriptor:command2 command 2 description + descriptor:command4 [descriptor:alias_command4|command4:descriptor] diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml index a76c0e24e0a7..5f0f98bd9f15 100644 --- a/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_2.xml @@ -199,6 +199,39 @@ + @@ -207,10 +240,15 @@ help list + + command4:descriptor + + descriptor:alias_command4 descriptor:command1 descriptor:command2 descriptor:command3 + descriptor:command4 diff --git a/src/Symfony/Component/Console/Tests/Fixtures/application_filtered_namespace.txt b/src/Symfony/Component/Console/Tests/Fixtures/application_filtered_namespace.txt new file mode 100644 index 000000000000..3bca270061d5 --- /dev/null +++ b/src/Symfony/Component/Console/Tests/Fixtures/application_filtered_namespace.txt @@ -0,0 +1,16 @@ +My Symfony application v1.0 + +Usage: + command [options] [arguments] + +Options: + -h, --help Display this help message + -q, --quiet Do not output any message + -V, --version Display this application version + --ansi Force ANSI output + --no-ansi Disable ANSI output + -n, --no-interaction Do not ask any interactive question + -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug + +Available commands for the "command4" namespace: + command4:descriptor