8000 bug #34130 [Console] Fix commands description with numeric namespaces… · symfony/symfony@fa783f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit fa783f9

Browse files
bug #34130 [Console] Fix commands description with numeric namespaces (fancyweb)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Fix commands description with numeric namespaces | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | #34111 | License | MIT | Doc PR | - This PR fixes the linked ticket case. It also changes the keys sorting to display the numeric namespaces first. It also fixes another bug if your command name starts with `_global:`. In this special case the command is considered global but its full name is still `_global:xxx`. We can't do better without more refactoring since the final array of namespaces and global commands is shared, `_global` just being a special key. Currently, if your command starts with `_global`, all global commands are not displayed at all so it's better like this anyway. It also fixes another bug if your command starts with `0:` (cf `'' ===` comparison). Commits ------- 4d47868 [Console] Fix commands description with numeric namespaces
2 parents 6e6ed9c + 4d47868 commit fa783f9

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,29 @@ private function sortCommands(array $commands)
129129
{
130130
$namespacedCommands = [];
131131
$globalCommands = [];
132+
$sortedCommands = [];
132133
foreach ($commands as $name => $command) {
133134
$key = $this->application->extractNamespace($name, 1);
134-
if (!$key) {
135-
$globalCommands['_global'][$name] = $command;
135+
if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) {
136+
$globalCommands[$name] = $command;
136137
} else {
137138
$namespacedCommands[$key][$name] = $command;
138139
}
139140
}
140-
ksort($namespacedCommands);
141-
$namespacedCommands = array_merge($globalCommands, $namespacedCommands);
142141

143-
foreach ($namespacedCommands as &$commandsSet) {
144-
ksort($commandsSet);
142+
if ($globalCommands) {
143+
ksort($globalCommands);
144+
$sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands;
145145
}
146-
// unset reference to keep scope clear
147-
unset($commandsSet);
148146

149-
return $namespacedCommands;
147+
if ($namespacedCommands) {
148+
ksort($namespacedCommands);
149+
foreach ($namespacedCommands as $key => $commandsSet) {
150+
ksort($commandsSet);
151+
$sortedCommands[$key] = $commandsSet;
152+
}
153+
}
154+
155+
return $sortedCommands;
150156
}
151157
}
Lines changed: 53 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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\Descriptor;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Application;
16+
use Symfony\Component\Console\Command\Command;
17+
use Symfony\Component\Console\Descriptor\ApplicationDescription;
18+
19+
final class ApplicationDescriptionTest extends TestCase
20+
{
21+
/**
22+
* @dataProvider getNamespacesProvider
23+
*/
24+
public function testGetNamespaces(array $expected, array $names)
25+
{
26+
$application = new TestApplication();
27+
foreach ($names as $name) {
28+
$application->add(new Command($name));
29+
}
30+
31+
$this->assertSame($expected, array_keys((new ApplicationDescription($application))->getNamespaces()));
32+
}
33+
34+
public function getNamespacesProvider()
35+
{
36+
return [
37+
[['_global'], ['foobar']],
38+
[['a', 'b'], ['b:foo', 'a:foo', 'b:bar']],
39+
[['_global', 'b', 'z', 22, 33], ['z:foo', '1', '33:foo', 'b:foo', '22:foo:bar']],
40+
];
41+
}
42+
}
43+
44+
final class TestApplication extends Application
45+
{
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
protected function getDefaultCommands()
50+
{
51+
return [];
52+
}
53+
}

0 commit comments

Comments
 (0)
0