|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stecman\Component\Symfony\Console\BashCompletion; |
|
5
|
|
|
|
|
6
|
|
|
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionInterface; |
|
7
|
|
|
|
|
8
|
|
|
class Completion implements CompletionInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The type of input (option/argument) the completion should be run for |
|
12
|
|
|
* |
|
13
|
|
|
* @see CompletionInterface::ALL_TYPES |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $type; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The command name the completion should be run for |
|
20
|
|
|
* |
|
21
|
|
|
* @see CompletionInterface::ALL_COMMANDS |
|
22
|
|
|
* @var string|null |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $commandName; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The option/argument name the completion should be run for |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $targetName; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Array of values to return, or a callback to generate completion results with |
|
35
|
|
|
* The callback can be in any form accepted by call_user_func. |
|
36
|
|
|
* |
|
37
|
|
|
* @var callable|array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $completion; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Create a Completion with the command name set to CompletionInterface::ALL_COMMANDS |
|
43
|
|
|
* |
|
44
|
|
|
* @deprecated - This will be removed in 1.0.0 as it is redundant and isn't any more concise than what it implements. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $targetName |
|
47
|
|
|
* @param string $type |
|
48
|
|
|
* @param array|callable $completion |
|
49
|
|
|
* @return Completion |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function makeGlobalHandler($targetName, $type, $completion) |
|
52
|
|
|
{ |
|
53
|
|
|
return new Completion(CompletionInterface::ALL_COMMANDS, $targetName, $type, $completion); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $commandName |
|
58
|
|
|
* @param string $targetName |
|
59
|
|
|
* @param string $type |
|
60
|
|
|
* @param array|callable $completion |
|
61
|
|
|
*/ |
|
62
|
|
|
public function __construct($commandName, $targetName, $type, $completion) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->commandName = $commandName; |
|
65
|
|
|
$this->targetName = $targetName; |
|
66
|
|
|
$this->type = $type; |
|
67
|
|
|
$this->completion = $completion; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return the stored completion, or the results returned from the completion callback |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function run() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->isCallable()) { |
|
78
|
|
|
return call_user_func($this->completion); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->completion; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get type of input (option/argument) the completion should be run for |
|
86
|
|
|
* |
|
87
|
|
|
* @see CompletionInterface::ALL_TYPES |
|
88
|
|
|
* @return string|null |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getType() |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->type; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Set type of input (option/argument) the completion should be run for |
|
97
|
|
|
* |
|
98
|
|
|
* @see CompletionInterface::ALL_TYPES |
|
99
|
|
|
* @param string|null $type |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setType($type) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->type = $type; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get the command name the completion should be run for |
|
108
|
|
|
* |
|
109
|
|
|
* @see CompletionInterface::ALL_COMMANDS |
|
110
|
|
|
* @return string|null |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getCommandName() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->commandName; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set the command name the completion should be run for |
|
119
|
|
|
* |
|
120
|
|
|
* @see CompletionInterface::ALL_COMMANDS |
|
121
|
|
|
* @param string|null $commandName |
|
122
|
|
|
*/ |
|
123
|
|
|
public function setCommandName($commandName) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->commandName = $commandName; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the option/argument name the completion should be run for |
|
130
|
|
|
* |
|
131
|
|
|
* @see setType() |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getTargetName() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->targetName; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get the option/argument name the completion should be run for |
|
141
|
|
|
* |
|
142
|
|
|
* @see getType() |
|
143
|
|
|
* @param string $targetName |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setTargetName($targetName) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->targetName = $targetName; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Return the array or callback configured for for the Completion |
|
152
|
|
|
* |
|
153
|
|
|
* @return array|callable |
|
154
|
|
|
*/ |
|
155
|
|
|
public function getCompletion() |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->completion; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Set the array or callback to return/run when Completion is run |
|
162
|
|
|
* |
|
163
|
|
|
* @see run() |
|
164
|
|
|
* @param array|callable $completion |
|
165
|
|
|
*/ |
|
166
|
|
|
public function setCompletion($completion) |
|
167
|
|
|
{ |
|
168
|
|
|
$this->completion = $completion; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Check if the configured completion value is a callback function |
|
173
|
|
|
* |
|
174
|
|
|
* @return bool |
|
175
|
|
|
*/ |
|
176
|
|
|
public function isCallable() |
|
177
|
|
|
{ |
|
178
|
|
|
return is_callable($this->completion); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|