8000 bug #13309 [Console] fixed 10531 (nacmartin) · symfony/symfony@5dd44b6 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 5dd44b6

Browse files
committed
bug #13309 [Console] fixed 10531 (nacmartin)
This PR was merged into the 2.3 branch. Discussion ---------- [Console] fixed 10531 | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10531 | License | MIT | Doc PR | - This is a fix for #10531. It works by extracting all the parent namespaces of a command. Commits ------- e6afff4 [Console] fixed #10531
2 parents 4b36893 + e6afff4 commit 5dd44b6

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,10 @@ public function getNamespaces()
469469
{
470470
$namespaces = array();
471471
foreach ($this->commands as $command) {
472-
$namespaces[] = $this->extractNamespace($command->getName());
472+
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
473473

474474
foreach ($command->getAliases() as $alias) {
475-
$namespaces[] = $this->extractNamespace($alias);
475+
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
476476
}
477477
}
478478

@@ -1169,4 +1169,28 @@ private function splitStringByWidth($string, $width)
11691169

11701170
return $lines;
11711171
}
1172+
1173+
/**
1174+
* Returns all namespaces of the command name.
1175+
*
1176+
* @param string $name The full name of the command
1177+
*
1178+
* @return array The namespaces of the command
1179+
*/
1180+
private function extractAllNamespaces($name)
1181+
{
1182+
// -1 as third argument is needed to skip the command short name when exploding
1183+
$parts = explode(':', $name, -1);
1184+
$namespaces = array();
1185+
1186+
foreach ($parts as $part) {
1187+
if (count($namespaces)) {
1188+
$namespaces[] = end($namespaces).':'.$part;
1189+
} else {
1190+
$namespaces[] = $part;
1191+
}
1192+
}
1193+
1194+
return $namespaces;
1195+
}
11721196
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ public static function setUpBeforeClass()
4040
require_once self::$fixturesPath.'/Foo2Command.php';
4141
require_once self::$fixturesPath.'/Foo3Command.php';
4242
require_once self::$fixturesPath.'/Foo4Command.php';
43+
require_once self::$fixturesPath.'/FooSubnamespaced1Command.php';
44+
require_once self::$fixturesPath.'/FooSubnamespaced2Command.php';
4345
}
4446

4547
protected function normalizeLineBreaks($text)
@@ -186,6 +188,14 @@ public function testFindNamespace()
186188
$this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns the given namespace if it exists');
187189
}
188190

191+
public function testFindNamespaceWithSubnamespaces()
192+
{
193+
$application = new Application();
194+
$application->add(new \FooSubnamespaced1Command());
195+
$application->add(new \FooSubnamespaced2Command());
196+
$this->assertEquals('foo', $application->findNamespace('foo'), '->findNamespace() returns commands even if the commands are only contained in subnamespaces');
197+
}
198+
189199
/**
190200
* @expectedException \InvalidArgumentException
191201
* @expectedExceptionMessage The namespace "f" is ambiguous (foo, foo1).
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class FooSubnamespaced1Command extends Command
8+
{
9+
public $input;
10+
public $output;
11+
12+
protected function configure()
13+
{
14+
$this
15+
->setName('foo:bar:baz')
16+
->setDescription('The foo:bar:baz command')
17+
->setAliases(array('foobarbaz'))
18+
;
19+
}
20+
21+
protected function execute(InputInterface $input, OutputInterface $output)
22+
{
23+
$this->input = $input;
24+
$this->output = $output;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Symfony\Component\Console\Command\Command;
4+
use Symfony\Component\Console\Input\InputInterface;
5+
use Symfony\Component\Console\Output\OutputInterface;
6+
7+
class FooSubnamespaced2Command extends Command
8+
{
9+
public $input;
10+
public $output;
11+
12+
protected function configure()
13+
{
14+
$this
15+
->setName('foo:go:bret')
16+
->setDescription('The foo:bar:go command')
17+
->setAliases(array('foobargo'))
18+
;
19+
}
20+
21+
protected function execute(InputInterface $input, OutputInterface $output)
22+
{
23+
$this->input = $input;
24+
$this->output = $output;
25+
}
26+
}

0 commit comments

Comments
 (0)
0