Skip to content

Commit 902f29d

Browse files
Programiestecman
authored andcommitted
Do not complete hidden commands (#75)
- Extract logic for getting commands - Hide _completion command (only if setHidden is supported) - Use existing behaviour where setHidden isn't supported (pre Symfony 3.2) Resolves #74
1 parent f239b51 commit 902f29d

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

src/CompletionCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ protected function configure()
3333
3434
END
3535
);
36+
37+
// setHidden() method was not available before Symfony 3.2
38+
if (method_exists($this, 'setHidden')) {
39+
$this->setHidden(true);
40+
}
3641
}
3742

3843
/**

src/CompletionHandler.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(Application $application, CompletionContext $context
4444
'help',
4545
'command_name',
4646
Completion::TYPE_ARGUMENT,
47-
array_keys($application->all())
47+
$this->getCommandNames()
4848
)
4949
);
5050

@@ -256,14 +256,7 @@ protected function completeForOptionValues()
256256
protected function completeForCommandName()
257257
{
258258
if (!$this->command || (count($this->context->getWords()) == 2 && $this->context->getWordIndex() == 1)) {
259-
$commands = $this->application->all();
260-
$names = array_keys($commands);
261-
262-
if ($key = array_search('_completion', $names)) {
263-
unset($names[$key]);
264-
}
265-
266-
return $names;
259+
return $this->getCommandNames();
267260
}
268261

269262
return false;
@@ -442,4 +435,29 @@ protected function getAllOptions()
442435
$this->application->getDefinition()->getOptions()
443436
);
444437
}
438+
439+
protected function getCommandNames()
440+
{
441+
$commands = array();
442+
443+
if (method_exists('\Symfony\Component\Console\Command\Command', 'isHidden')) {
444+
foreach ($this->application->all() as $name => $command) {
445+
if ($command->isHidden()) {
446+
continue;
447+
}
448+
449+
$commands[] = $name;
450+
}
451+
} else {
452+
foreach ($this->application->all() as $name => $command) {
453+
if ($name === '_completion') {
454+
continue;
455+
}
456+
457+
$commands[] = $name;
458+
}
459+
}
460+
461+
return $commands;
462+
}
445463
}

tests/Stecman/Component/Symfony/Console/BashCompletion/Common/CompletionHandlerTestCase.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class CompletionHandlerTestCase extends \PHPUnit_Framework_TestCase
1919
public static function setUpBeforeClass()
2020
{
2121
require_once __DIR__ . '/../Fixtures/CompletionAwareCommand.php';
22+
require_once __DIR__ . '/../Fixtures/HiddenCommand.php';
2223
require_once __DIR__ . '/../Fixtures/TestBasicCommand.php';
2324
require_once __DIR__ . '/../Fixtures/TestSymfonyStyleCommand.php';
2425
}
@@ -31,6 +32,10 @@ protected function setUp()
3132
new \TestBasicCommand(),
3233
new \TestSymfonyStyleCommand()
3334
));
35+
36+
if (method_exists('\HiddenCommand', 'setHidden')) {
37+
$this->application->add(new \HiddenCommand());
38+
}
3439
}
3540

3641
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
4+
class HiddenCommand extends \Symfony\Component\Console\Command\Command
5+
{
6+
protected function configure()
7+
{
8+
$this->setName('internals')
9+
->setHidden(true);
10+
}
11+
}

0 commit comments

Comments
 (0)