[Console] Fix disabling lazy commands

This commit is contained in:
Robin Chalas 2017-11-19 13:43:28 +01:00
parent 8d7f6ede84
commit 6787d8e5ab
2 changed files with 38 additions and 9 deletions

View File

@ -455,15 +455,12 @@ class Application
{ {
$this->init(); $this->init();
if (isset($this->commands[$name])) { if (!$this->has($name)) {
$command = $this->commands[$name];
} elseif ($this->commandLoader && $this->commandLoader->has($name)) {
$command = $this->commandLoader->get($name);
$this->add($command);
} else {
throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name)); throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
} }
$command = $this->commands[$name];
if ($this->wantHelps) { if ($this->wantHelps) {
$this->wantHelps = false; $this->wantHelps = false;
@ -487,7 +484,7 @@ class Application
{ {
$this->init(); $this->init();
return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name)); return isset($this->commands[$name]) || ($this->commandLoader && $this->commandLoader->has($name) && $this->add($this->commandLoader->get($name)));
} }
/** /**
@ -649,7 +646,7 @@ class Application
$commands = $this->commands; $commands = $this->commands;
foreach ($this->commandLoader->getNames() as $name) { foreach ($this->commandLoader->getNames() as $name) {
if (!isset($commands[$name])) { if (!isset($commands[$name]) && $this->has($name)) {
$commands[$name] = $this->get($name); $commands[$name] = $this->get($name);
} }
} }
@ -666,7 +663,7 @@ class Application
if ($this->commandLoader) { if ($this->commandLoader) {
foreach ($this->commandLoader->getNames() as $name) { foreach ($this->commandLoader->getNames() as $name) {
if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) { if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1) && $this->has($name)) {
$commands[$name] = $this->get($name); $commands[$name] = $this->get($name);
} }
} }

View File

@ -1532,6 +1532,30 @@ class ApplicationTest extends TestCase
$this->assertSame(array('lazy:alias', 'lazy:alias2'), $command->getAliases()); $this->assertSame(array('lazy:alias', 'lazy:alias2'), $command->getAliases());
} }
/**
* @expectedException \Symfony\Component\Console\Exception\CommandNotFoundException
*/
public function testGetDisabledLazyCommand()
{
$application = new Application();
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
$application->get('disabled');
}
public function testHasReturnsFalseForDisabledLazyCommand()
{
$application = new Application();
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
$this->assertFalse($application->has('disabled'));
}
public function testAllExcludesDisabledLazyCommand()
{
$application = new Application();
$application->setCommandLoader(new FactoryCommandLoader(array('disabled' => function () { return new DisabledCommand(); })));
$this->assertArrayNotHasKey('disabled', $application->all());
}
protected function getDispatcher($skipCommand = false) protected function getDispatcher($skipCommand = false)
{ {
$dispatcher = new EventDispatcher(); $dispatcher = new EventDispatcher();
@ -1634,3 +1658,11 @@ class LazyCommand extends Command
$output->writeln('lazy-command called'); $output->writeln('lazy-command called');
} }
} }
class DisabledCommand extends Command
{
public function isEnabled()
{
return false;
}
}