diff --git a/src/Symfony/Component/Console/Application.php b/src/Symfony/Component/Console/Application.php index 4ecabaafd5..98f6662f36 100644 --- a/src/Symfony/Component/Console/Application.php +++ b/src/Symfony/Component/Console/Application.php @@ -455,15 +455,12 @@ class Application { $this->init(); - if (isset($this->commands[$name])) { - $command = $this->commands[$name]; - } elseif ($this->commandLoader && $this->commandLoader->has($name)) { - $command = $this->commandLoader->get($name); - $this->add($command); - } else { + if (!$this->has($name)) { throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name)); } + $command = $this->commands[$name]; + if ($this->wantHelps) { $this->wantHelps = false; @@ -487,7 +484,7 @@ class Application { $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; foreach ($this->commandLoader->getNames() as $name) { - if (!isset($commands[$name])) { + if (!isset($commands[$name]) && $this->has($name)) { $commands[$name] = $this->get($name); } } @@ -666,7 +663,7 @@ class Application if ($this->commandLoader) { 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); } } diff --git a/src/Symfony/Component/Console/Tests/ApplicationTest.php b/src/Symfony/Component/Console/Tests/ApplicationTest.php index 396c35c733..4e1f8811b2 100644 --- a/src/Symfony/Component/Console/Tests/ApplicationTest.php +++ b/src/Symfony/Component/Console/Tests/ApplicationTest.php @@ -1532,6 +1532,30 @@ class ApplicationTest extends TestCase $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) { $dispatcher = new EventDispatcher(); @@ -1634,3 +1658,11 @@ class LazyCommand extends Command $output->writeln('lazy-command called'); } } + +class DisabledCommand extends Command +{ + public function isEnabled() + { + return false; + } +}