Skip to content

Commit 3b995e5

Browse files
author
Robin Chalas
committed
[Console] Fix disabling lazy commands
1 parent 8d7f6ed commit 3b995e5

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/Symfony/Component/Console/Application.php

+10-9
Original file line numberDiff line numberDiff line change
@@ -455,15 +455,12 @@ public function get($name)
455455
{
456456
$this->init();
457457

458-
if (isset($this->commands[$name])) {
459-
$command = $this->commands[$name];
460-
} elseif ($this->commandLoader && $this->commandLoader->has($name)) {
461-
$command = $this->commandLoader->get($name);
462-
$this->add($command);
463-
} else {
458+
if (!$this->has($name)) {
464459
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
465460
}
466461

462+
$command = $this->commands[$name];
463+
467464
if ($this->wantHelps) {
468465
$this->wantHelps = false;
469466

@@ -487,7 +484,7 @@ public function has($name)
487484
{
488485
$this->init();
489486

490-
return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name));
487+
return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name) && $this->add($this->commandLoader->get($name)));
491488
}
492489

493490
/**
@@ -650,7 +647,9 @@ public function all($namespace = null)
650647
$commands = $this->commands;
651648
foreach ($this->commandLoader->getNames() as $name) {
652649
if (!isset($commands[$name])) {
653-
$commands[$name] = $this->get($name);
650+
if ($this->commandLoader->get($name)->isEnabled()) {
651+
$commands[$name] = $this->get($name);
652+
}
654653
}
655654
}
656655

@@ -667,7 +666,9 @@ public function all($namespace = null)
667666
if ($this->commandLoader) {
668667
foreach ($this->commandLoader->getNames() as $name) {
669668
if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
670-
$commands[$name] = $this->get($name);
669+
if ($this->commandLoader->get($name)->isEnabled()) {
670+
$commands[$name] = $this->get($name);
671+
}
671672
}
672673
}
673674
}

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

+32
Original file line numberDiff line numberDiff line change
@@ -1532,6 +1532,30 @@ public function testRunLazyCommandService()
15321532
$this->assertSame(array('lazy:alias', 'lazy:alias2'), $command->getAliases());
15331533
}
15341534

1535+
/**
1536+
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
1537+
*/
1538+
public function testGetDisabledLazyCommand()
1539+
{
1540+
$application = new Application();
1541+
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
1542+
$application->get('disabled');
1543+
}
1544+
1545+
public function testHasReturnsFalseForDisabledLazyCommand()
1546+
{
1547+
$application = new Application();
1548+
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
1549+
$this->assertFalse($application->has('disabled'));
1550+
}
1551+
1552+
public function testAllExcludesDisabledLazyCommand()
1553+
{
1554+
$application = new Application();
1555+
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
1556+
$this->assertArrayNotHasKey('disabled', $application->all());
1557+
}
1558+
15351559
protected function getDispatcher($skipCommand = false)
15361560
{
15371561
$dispatcher = new EventDispatcher();
@@ -1634,3 +1658,11 @@ public function execute(InputInterface $input, OutputInterface $output)
16341658
$output->writeln('lazy-command called');
16351659
}
16361660
}
1661+
1662+
class DisabledCommand extends Command
1663+
{
1664+
public function isEnabled()
1665+
{
1666+
return false;
1667+
}
1668+
}

0 commit comments

Comments
 (0)