8000 bug #25030 [Console] Fix ability to disable lazy commands (chalasr) · symfony/symfony@fa7dd8e · GitHub
[go: up one dir, main page]

Skip to content

Commit fa7dd8e

Browse files
bug #25030 [Console] Fix ability to disable lazy commands (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Fix ability to disable lazy commands | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Properly throw when running them and don't show them in the list, as for non lazy ones. Commits ------- 6787d8e [Console] Fix disabling lazy commands
2 parents a8afcba + 6787d8e commit fa7dd8e

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 6 additions & 9 deletions
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
/**
@@ -649,7 +646,7 @@ public function all($namespace = null)
649646

650647
$commands = $this->commands;
651648
foreach ($this->commandLoader->getNames() as $name) {
652-
if (!isset($commands[$name])) {
649+
if (!isset($commands[$name]) && $this->has($name)) {
653650
$commands[$name] = $this->get($name);
654651
}
655652
}
@@ -666,7 +663,7 @@ public function all($namespace = null)
666663

667664
if ($this->commandLoader) {
668665
foreach ($this->commandLoader->getNames() as $name) {
669-
if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
666+
if (!isset($commands[$name]) && $namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1) && $this->has($name)) {
670667
$commands[$name] = $this->get($name);
671668
}
672669
}

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

Lines changed: 32 additions & 0 deletions
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)
0