1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-07-05 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Cli\Readline; |
8
|
|
|
|
9
|
|
|
class Autocomplete |
10
|
|
|
{ |
11
|
|
|
/** array */ |
12
|
|
|
private $configuration; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string $input |
16
|
|
|
* @param int $index |
17
|
|
|
* @return array|bool |
18
|
|
|
*/ |
19
|
|
|
public function __invoke($input, $index) |
20
|
|
|
{ |
21
|
|
|
return $this->complete($input, $index); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $input |
26
|
|
|
* @param int $index |
27
|
|
|
* @return array|bool |
28
|
|
|
*/ |
29
|
|
|
public function complete($input, $index) |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$configuration = $this->configuration; |
32
|
|
|
|
33
|
|
|
if ($index == 0) { |
34
|
|
|
$completion = array_keys($configuration); |
35
|
|
|
} else { |
36
|
|
|
$buffer = preg_replace('/\s+/', ' ', trim(readline_info('line_buffer'))); |
37
|
|
|
$tokens = explode(' ', $buffer); |
38
|
|
|
$completion = $this->fetchCompletion($configuration, $tokens); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $completion; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $configuration |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setConfiguration(array $configuration) |
49
|
|
|
{ |
50
|
|
|
$this->configuration = $configuration; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $configuration |
57
|
|
|
* @param array $tokens |
58
|
|
|
* @return array|bool |
59
|
|
|
*/ |
60
|
|
|
private function fetchCompletion(array $configuration, array &$tokens) |
61
|
|
|
{ |
62
|
|
|
$completion = false; |
63
|
|
|
$index = current($tokens); |
64
|
|
|
|
65
|
|
|
if (isset($configuration[$index])) { |
66
|
|
|
if (next($tokens) !== false) { |
67
|
|
|
$completion = $this->fetchCompletion($configuration[$index], $tokens); |
68
|
|
|
} else { |
69
|
|
|
$arrayOrExecutable = $configuration[$index]; |
70
|
|
|
|
71
|
|
|
if (is_array($arrayOrExecutable)) { |
72
|
|
|
$completion = array_keys($arrayOrExecutable); |
73
|
|
|
} else { |
74
|
|
|
$completion = false; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$indexLengthIsGreaterZero = (strlen($index) > 0); |
79
|
|
|
|
80
|
|
|
if ($indexLengthIsGreaterZero) { |
81
|
|
|
$position = strlen($index); |
82
|
|
|
$values = array_keys($configuration); |
83
|
|
|
$completion = []; |
84
|
|
|
|
85
|
|
|
foreach ($values as $value) { |
86
|
|
|
if (substr($value, 0, $position) === $index) { |
87
|
|
|
$completion[] = $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (empty($completion)) { |
92
|
|
|
$completion = false; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $completion; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.