Skip to content

[Console] Fix ability to disable lazy commands #25030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/Symfony/Component/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,15 +455,12 @@ public function get($name)
{
$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;

Expand All @@ -487,7 +484,7 @@ public function has($name)
{
$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)));
}

/**
Expand Down Expand Up @@ -649,7 +646,7 @@ public function all($namespace = null)

$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);
}
}
Expand All @@ -666,7 +663,7 @@ public function all($namespace = null)

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);
}
}
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,30 @@ public function testRunLazyCommandService()
$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();
Expand Down Expand Up @@ -1634,3 +1658,11 @@ public function execute(InputInterface $input, OutputInterface $output)
$output->writeln('lazy-command called');
}
}

class DisabledCommand extends Command
{
public function isEnabled()
{
return false;
}
}