Skip to content

Commit 0ce2f58

Browse files
committed
Initial commit
0 parents  commit 0ce2f58

File tree

6 files changed

+713
-0
lines changed

6 files changed

+713
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
.idea

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "stecman/symfony-console-completion",
3+
"description": "Automatic BASH completion for the Symfony Console Component.",
4+
"minimum-stability": "dev",
5+
"authors": [
6+
{
7+
"name": "Stephen Holdaway",
8+
"email": "[email protected]"
9+
}
10+
],
11+
"require": {
12+
"symfony/console": "~2.2.0"
13+
},
14+
"require-dev": {
15+
"phpunit/phpunit": "~3.7"
16+
},
17+
"autoload": {
18+
"psr-0": {
19+
"Stecman\\Component": "src/"
20+
}
21+
}
22+
}

composer.lock

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
4+
namespace Stecman\Component\Symfony\Console\BashCompletion;
5+
6+
7+
class Completion {
8+
9+
const ALL_COMMANDS = null;
10+
11+
const TYPE_OPTION = 'option';
12+
const TYPE_ARGUMENT = 'argument';
13+
14+
/**
15+
* The option/argument name this helper should be run for
16+
* @var string
17+
*/
18+
protected $type;
19+
20+
/**
21+
* The command name the helper applies to.
22+
* Helper will apply to all commands if this is not set
23+
* @var string
24+
*/
25+
protected $commandName;
26+
27+
/**
28+
* The option/argument name the helper should be run for
29+
* @var string
30+
*/
31+
protected $targetName;
32+
33+
/**
34+
* Array or Closure
35+
* @var mixed
36+
*/
37+
protected $completion;
38+
39+
public static function makeGlobalHandler($targetName, $type, $completion)
40+
{
41+
return new Completion(self::ALL_COMMANDS, $targetName, $type, $completion);
42+
}
43+
44+
public function __construct($commandName, $targetName, $type, $completion)
45+
{
46+
$this->commandName = $commandName;
47+
$this->targetName = $targetName;
48+
$this->type = $type;
49+
$this->completion = $completion;
50+
}
51+
52+
public function isGlobal()
53+
{
54+
return $this->commandName == '';
55+
}
56+
57+
/**
58+
* Return the result of the completion helper
59+
* @return array|mixed
60+
*/
61+
public function run()
62+
{
63+
if (is_array($this->completion)) {
64+
return $this->completion;
65+
}
66+
67+
if ($this->isCallable()) {
68+
return call_user_func($this->completion);
69+
}
70+
}
71+
72+
/**
73+
* @return string
74+
*/
75+
public function getType()
76+
{
77+
return $this->type;
78+
}
79+
80+
/**
81+
* @param string $type
82+
*/
83+
public function setType($type)
84+
{
85+
$this->type = $type;
86+
}
87+
88+
/**
89+
* @return string
90+
*/
91+
public function getCommandName()
92+
{
93+
return $this->commandName;
94+
}
95+
96+
/**
97+
* @param string $commandName
98+
*/
99+
public function setCommandName($commandName)
100+
{
101+
$this->commandName = $commandName;
102+
}
103+
104+
/**
105+
* @return string
106+
*/
107+
public function getTargetName()
108+
{
109+
return $this->targetName;
110+
}
111+
112+
/**
113+
* @param string $targetName
114+
*/
115+
public function setTargetName($targetName)
116+
{
117+
$this->targetName = $targetName;
118+
}
119+
120+
/**
121+
* @return mixed
122+
*/
123+
public function getCompletion()
124+
{
125+
return $this->completion;
126+
}
127+
128+
/**
129+
* @param mixed $completion
130+
*/
131+
public function setCompletion($completion)
132+
{
133+
$this->completion = $completion;
134+
}
135+
136+
public function isCallable()
137+
{
138+
return is_callable($this->completion);
139+
}
140+
141+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Stecman\Component\Symfony\Console\BashCompletion;
4+
5+
use Stecman\Component\Symfony\Console\BashCompletion\CompletionHandler;
6+
use Symfony\Component\Console\Command\Command as SymfonyCommand;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class CompletionCommand extends SymfonyCommand {
12+
13+
/**
14+
* @var CompletionHandler
15+
*/
16+
protected $handler;
17+
18+
protected function configure()
19+
{
20+
$this
21+
->setName('_completion')
22+
->setDescription('BASH completion hook.')
23+
->setHelp(<<<END
24+
To enable BASH completion, run: <comment>eval `beam completion --genhook`</comment>
25+
END
26+
)
27+
->addOption(
28+
'genhook',
29+
null,
30+
InputOption::VALUE_NONE,
31+
'Generate BASH script to enable completion using this command'
32+
);
33+
}
34+
35+
protected function execute(InputInterface $input, OutputInterface $output)
36+
{
37+
$this->handler = new CompletionHandler( $this->getApplication() );
38+
$handler = $this->handler;
39+
40+
if ($input->getOption('genhook')) {
41+
$bash = $handler->generateBashCompletionHook();
42+
$output->write($bash, true);
43+
} else {
44+
$handler->configureFromEnvironment();
45+
$output->write($this->runCompletion(), true);
46+
}
47+
}
48+
49+
protected function runCompletion()
50+
{
51+
return $this->handler->runCompletion();
52+
}
53+
54+
}

0 commit comments

Comments
 (0)