8000 feature #19954 [Console] Exclude empty namespaces in text descriptor … · symfony/symfony@c97407e · GitHub
[go: up one dir, main page]

Skip to content

Commit c97407e

Browse files
committed
feature #19954 [Console] Exclude empty namespaces in text descriptor (ro0NL)
This PR was merged into the 3.3-dev branch. Discussion ---------- [Console] Exclude empty namespaces in text descriptor | Q | A | | --- | --- | | Branch? | "master" | | Bug fix? | yes | | New feature? | no | | BC breaks? | no | | Deprecations? | no | | Tests pass? | yes | | Fixed tickets | comma-separated list of tickets fixed by the PR, if any | | License | MIT | | Doc PR | reference to the documentation PR, if any | Before: ``` $ bin/console doctrine doctrine:mapping:convert [orm:convert:mapping] Convert mapping information between supported formats. orm <---- router router:match Helps debug routes by simulating a path info match $ bin/console list orm [Symfony\Component\Debug\Exception\ContextErrorException] Warning: max(): Array must contain at least one element $ bin/console list generate Available commands for the "generate" namespace: generate:bundle Generates a bundle generate:command Generates a console command generate:controller Generates a controller ``` After: ``` $ bin/console doctrine doctrine:mapping:convert [orm:convert:mapping] Convert mapping information between supported formats. router router:match Helps debug routes by simulating a path info match $ bin/console list orm Available commands for the "orm" namespace: orm:convert:mapping Convert mapping information between supported formats. $ bin/console list generate Available commands for the "generate" namespace: generate:bundle Generates a bundle generate:command Generates a console command generate:controller Generates a controller generate:doctrine:crud Generates a CRUD based on a Doctrine entity generate:doctrine:entities Generates entity classes and method stubs from your mapping information generate:doctrine:entity Generates a new Doctrine entity inside a bundle generate:doctrine:form Generates a form type class based on a Doctrine entity ``` Overrules #19776 but also includes other fixes related to aliases that popped up when writing tests 👍 Commits ------- d5a7608 [Console] Exclude empty namespaces in text descriptor
2 parents 858af71 + d5a7608 commit c97407e

11 files changed

+298
-21
lines changed

src/Symfony/Component/Console/Descriptor/TextDescriptor.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,31 +191,47 @@ protected function describeApplication(Application $application, array $options
191191
$this->writeText("\n");
192192
$this->writeText("\n");
193193

194-
$width = $this->getColumnWidth($description->getCommands());
194+
$commands = $description->getCommands();
195+
$namespaces = $description->getNamespaces();
196+
if ($describedNamespace && $namespaces) {
197+
// make sure all alias commands are included when describing a specific namespace
198+
$describedNamespaceInfo = reset($namespaces);
199+
foreach ($describedNamespaceInfo['commands'] as $name) {
200+
$commands[$name] = $description->getCommand($name);
201+
}
202+
}
203+
204+
// calculate max. width based on available commands per namespace
205+
$width = $this->getColumnWidth(call_user_func_array('array_merge', array_map(function ($namespace) use ($commands) {
206+
return array_intersect($namespace['commands'], array_keys($commands));
207+
}, $namespaces)));
195208

196209
if ($describedNamespace) {
197210
$this->writeText(sprintf('<comment>Available commands for the "%s" namespace:</comment>', $describedNamespace), $options);
198211
} else {
199212
$this->writeText('<comment>Available commands:</comment>', $options);
200213
}
201214

202-
// add commands by namespace
203-
$commands = $description->getCommands();
215+
foreach ($namespaces as $namespace) {
216+
$namespace['commands'] = array_filter($namespace['commands'], function ($name) use ($commands) {
217+
return isset($commands[$name]);
218+
});
219+
220+
if (!$namespace['commands']) {
221+
continue;
222+
}
204223

205-
foreach ($description->getNamespaces() as $namespace) {
206224
if (!$describedNamespace && ApplicationDescription::GLOBAL_NAMESPACE !== $namespace['id']) {
207225
$this->writeText("\n");
208226
$this->writeText(' <comment>'.$namespace['id'].'</comment>', $options);
209227
}
210228

211229
foreach ($namespace['commands'] as $name) {
212-
if (isset($commands[$name])) {
213-
$this->writeText("\n");
214-
$spacingWidth = $width - Helper::strlen($name);
215-
$command = $commands[$name];
216-
$commandAliases = $this->getCommandAliasesText($command);
217-
$this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
218-
}
230+
$this->writeText("\n");
231+
$spacingWidth = $width - Helper::strlen($name);
232+
$command = $commands[$name];
233+
$commandAliases = $name === $command->getName() ? $this->getCommandAliasesText($command) : '';
234+
$this->writeText(sprintf(' <info>%s</info>%s%s', $name, str_repeat(' ', $spacingWidth), $commandAliases.$command->getDescription()), $options);
219235
}
220236
}
221237

@@ -276,7 +292,7 @@ private function formatDefaultValue($default)
276292
}
277293

278294
/**
279-
* @param Command[] $commands
295+
* @param (Command|string)[] $commands
280296
*
281297
* @return int
282298
*/
@@ -285,13 +301,17 @@ private function getColumnWidth(array $commands)
285301
$widths = array();
286302

287303
foreach ($commands as $command) {
288-
$widths[] = Helper::strlen($command->getName());
289-
foreach ($command->getAliases() as $alias) {
290-
$widths[] = Helper::strlen($alias);
304+
if ($command instanceof Command) {
305+
$widths[] = Helper::strlen($command->getName());
306+
foreach ($command->getAliases() as $alias) {
307+
$widths[] = Helper::strlen($alias);
308+
}
309+
} else {
310+
$widths[] = Helper::strlen($command);
291311
}
292312
}
293313

294-
return max($widths) + 2;
314+
return $widths ? max($widths) + 2 : 0;
295315
}
296316

297317
/**

src/Symfony/Component/Console/Tests/Descriptor/AbstractDescriptorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ protected function getDescriptionTestData(array $objects)
9898
return $data;
9999
}
100100

101-
protected function assertDescription($expectedDescription, $describedObject)
101+
protected function assertDescription($expectedDescription, $describedObject, array $options = array())
102102
{
103103
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
104-
$this->getDescriptor()->describe($output, $describedObject, array('raw_output' => true));
104+
$this->getDescriptor()->describe($output, $describedObject, $options + array('raw_output' => true));
105105
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
106106
}
107107
}

src/Symfony/Component/Console/Tests/Descriptor/JsonDescriptorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ protected function getFormat()
2626
return 'json';
2727
}
2828

29-
protected function assertDescription($expectedDescription, $describedObject)
29+
protected function assertDescription($expectedDescription, $describedObject, array $options = array())
3030
{
3131
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
32-
$this->getDescriptor()->describe($output, $describedObject, array('raw_output' => true));
32+
$this->getDescriptor()->describe($output, $describedObject, $options + array('raw_output' => true));
3333
$this->assertEquals(json_decode(trim($expectedDescription), true), json_decode(trim(str_replace(PHP_EOL, "\n", $output->fetch())), true));
3434
}
3535
}

src/Symfony/Component/Console/Tests/Descriptor/TextDescriptorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Console\Tests\Descriptor;
1313

1414
use Symfony\Component\Console\Descriptor\TextDescriptor;
15+
use Symfony\Component\Console\Tests\Fixtures\DescriptorApplication2;
1516
use Symfony\Component\Console\Tests\Fixtures\DescriptorApplicationMbString;
1617
use Symfony\Component\Console\Tests\Fixtures\DescriptorCommandMbString;
1718

@@ -33,6 +34,13 @@ public function getDescribeApplicationTestData()
3334
));
3435
}
3536

37+
public function testDescribeApplicationWithFilteredNamespace()
38+
{
39+
$application = new DescriptorApplication2();
40+
41+
$this->assertDescription(file_get_contents(__DIR__.'/../Fixtures/application_filtered_namespace.txt'), $application, array('namespace' => 'command4'));
42+
}
43+
3644
protected function getDescriptor()
3745
{
3846
return new TextDescriptor();

src/Symfony/Component/Console/Tests/Fixtures/DescriptorApplication2.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public function __construct()
2121
$this->add(new DescriptorCommand1());
2222
$this->add(new DescriptorCommand2());
2323
$this->add(new DescriptorCommand3());
24+
$this->add(new DescriptorCommand4());
2425
}
2526
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Fixtures;
13+
14+
use Symfony\Component\Console\Command\Command;
15+
16+
class DescriptorCommand4 extends Command
17+
{
18+
protected function configure()
19+
{
20+
$this
21+
->setName('descriptor:command4')
22+
->setAliases(array('descriptor:alias_command4', 'command4:descriptor'))
23+
;
24+
}
25+
}

src/Symfony/Component/Console/Tests/Fixtures/application_2.json

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,85 @@
398398
}
399399
}
400400
}
401+
},
402+
{
403+
"name": "descriptor:command4",
404+
"hidden": false,
405+
"usage": [
406+
"descriptor:command4",
407+
"descriptor:alias_command4",
408+
"command4:descriptor"
409+
],
410+
"description": null,
411+
"help": "",
412+
"definition": {
413+
"arguments": {},
414+
"options": {
415+
"help": {
416+
"name": "--help",
417+
"shortcut": "-h",
418+
"accept_value": false,
419+
"is_value_required": false,
420+
"is_multiple": false,
421+
"description": "Display this help message",
422+
"default": false
423+
},
424+
"quiet": {
425+
"name": "--quiet",
426+
"shortcut": "-q",
427+
"accept_value": false,
428+
"is_value_required": false,
429+
"is_multiple": false,
430+
"description": "Do not output any message",
431+
"default": false
432+
},
433+
"verbose": {
434+
"name": "--verbose",
435+
"shortcut": "-v|-vv|-vvv",
436+
"accept_value": false,
437+
"is_value_required": false,
438+
"is_multiple": false,
439+
"description": "Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug",
440+
"default": false
441+
},
442+
"version": {
443+
"name": "--version",
444+
"shortcut": "-V",
445+
"accept_value": false,
446+
"is_value_required": false,
447+
"is_multiple": false,
448+
"description": "Display this application version",
449+
"default": false
450+
},
451+
"ansi": {
452+
"name": "--ansi",
453+
"shortcut": "",
454+
"accept_value": false,
455+
"is_value_required": false,
456+
"is_multiple": false,
457+
"description": "Force ANSI output",
458+
"default": false
459+
},
460+
"no-ansi": {
461+
"name": "--no-ansi",
462+
"shortcut": "",
463+
"accept_value": false,
464+
"is_value_required": false,
465+
"is_multiple": false,
466+
"description": "Disable ANSI output",
467+
"default": false
468+
},
469+
"no-interaction": {
470+
"name": "--no-interaction",
471+
"shortcut": "-n",
472+
"accept_value": false,
473+
"is_value_required": false,
474+
"is_multiple": false,
475+
"description": "Do not ask any interactive question",
476+
"default": false
477+
}
478+
}
479+
}
401480
}
402481
],
403482
"namespaces": [
@@ -410,12 +489,20 @@
410489
"list"
411490
]
412491
},
492+
{
493+
"id": "command4",
494+
"commands": [
495+
"command4:descriptor"
496+
]
497+
},
413498
{
414499
"id": "descriptor",
415500
"commands": [
501+
"descriptor:alias_command4",
416502
"descriptor:command1",
417503
"descriptor:command2",
418-
"descriptor:command3"
504+
"descriptor:command3",
505+
"descriptor:command4"
419506
]
420507
}
421508
]

src/Symfony/Component/Console/Tests/Fixtures/application_2.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ My Symfony application v1.0
66
* [`help`](#help)
77
* [`list`](#list)
88

9+
**command4:**
10+
11+
* [`command4:descriptor`](#descriptorcommand4)
12+
913
**descriptor:**
1014

15+
* [`descriptor:alias_command4`](#descriptorcommand4)
1116
* [`descriptor:command1`](#descriptorcommand1)
1217
* [`descriptor:command2`](#descriptorcommand2)
18+
* [`descriptor:command4`](#descriptorcommand4)
1319

1420
`help`
1521
------
@@ -348,3 +354,78 @@ Do not ask any interactive question
348354
* Is value required: no
349355
* Is multiple: no
350356
* Default: `false`
357+
358+
`descriptor:command4`
359+
---------------------
360+
361+
### Usage
362+
363+
* `descriptor:command4`
364+
* `descriptor:alias_command4`
365+
* `command4:descriptor`
366+
367+
368+
### Options
369+
370+
#### `--help|-h`
371+
372+
Display this help message
373+
374+
* Accept value: no
375+
* Is value required: no
376+
* Is multiple: no
377+
* Default: `false`
378+
379+
#### `--quiet|-q`
380+
381+
Do not output any message
382+
383+
* Accept value: no
384+
* Is value required: no
385+
* Is multiple: no
386+
* Default: `false`
387+
388+
#### `--verbose|-v|-vv|-vvv`
389+
390+
Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug
391+
392+
* Accept value: no
393+
* Is value required: no
394+
* Is multiple: no
395+
* Default: `false`
396+
397+
#### `--version|-V`
398+
399+
Display this application version
400+
401+
* Accept value: no
402+
* Is value required: no
403+
* Is multiple: no
404+
* Default: `false`
405+
406+
#### `--ansi`
407+
408+
Force ANSI output
409+
410+
* Accept value: no
411+
* Is value required: no
412+
* Is multiple: no
413+
* Default: `false`
414+
415+
#### `--no-ansi`
416+
417+
Disable ANSI output
418+
419+
* Accept value: no
420+
* Is value required: no
421+
* Is multiple: no
422+
* Default: `false`
423+
424+
#### `--no-interaction|-n`
425+
426+
Do not ask any interactive question
427+
428+
* Accept value: no
429+
* Is value required: no
430+
* Is multiple: no
431+
* Default: `false`

src/Symfony/Component/Console/Tests/Fixtures/application_2.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ My Symfony application <info>v1.0</info>
1818
<comment>descriptor</comment>
1919
<info>descriptor:command1</info> [alias1|alias2] command 1 description
2020
<info>descriptor:command2</info> command 2 description
21+
<info>descriptor:command4</info> [descriptor:alias_command4|command4:descriptor]

0 commit comments

Comments
 (0)
0