Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d47868

Browse files
committed
[Console] Fix commands description with numeric namespaces
1 parent 84b5db3 commit 4d47868

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
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 <[email protected]>
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)