-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
autocomplete.php
356 lines (313 loc) · 9.38 KB
/
autocomplete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/*
This file is part of the eQual framework <http://www.github.com/equalframework/equal>
Some Rights Reserved, Cedric Francoys, 2010-2023
Licensed under GNU LGPL 3 license <http://www.gnu.org/licenses/>
*/
/*
This script is meant to provide a list of possible values or completion, given a CLI call context for the ./equal command.
A single parameter is expected as $argv[1], holding command line fields separated with spaces.
Call sample: php autocomplete.php 'equal --get = core_model_c'
*/
define('QN_BASEDIR', realpath(dirname(__FILE__)));
$values = explode(' ', $argv[1]);
$count = count($values);
if(strlen($values[1]) == 0) {
echo '--'."\n";
exit();
}
if($count == 2) {
if(in_array($values[1], ['', '-'])) {
echo "--";
}
elseif($values[1] == '--') {
echo "do\nget\nshow\n";
}
elseif(in_array($values[1], ['--d', '--do'])) {
echo "--do=\n";
}
elseif(in_array($values[1], ['--g', '--ge', '--get'])) {
echo "--get=\n";
}
elseif(in_array($values[1], ['--s', '--sh', '--sho', '--show'])) {
echo "--show=\n";
}
exit();
}
/*
[0] => ./equal
[1] => --get
[2] => =
*/
if($count == 3) {
$results = choices_level($values[1], ['']);
$count_result = count($results);
foreach($results as $result) {
if($count_result > 1) {
echo trim($result, '_')."\n";
}
else {
echo $result."\n";
}
}
exit();
}
/*
[0] => ./equal
[1] => --get
[2] => =
[3] => co, core_
*/
if($count == 4) {
$output = [];
$parts = explode('_', $values[3]);
$count_parts = count($parts);
$results = choices_level($values[1], $parts);
// filter results
$map_results = [];
foreach($results as $i => $result) {
$val = trim($result, ' _');
if(strlen($val) <= 0 || isset($map_results[$val])) {
unset($results[$i]);
}
$map_results[$val] = true;
}
$count_results = count($results);
if($count_results > 1) {
foreach($results as $result) {
$output[] = rtrim($result, '_');
}
// display number of results
// #memo - this is a workaround for COMPREPLY not considering entries when count is lesser than 3 (observed under WIN env)
$output[] = "({$count_results})\n";
}
elseif($count_results > 0) {
if($count_parts > 1) {
array_pop($parts);
$output[] = implode('_', $parts).'_'.$results[0];
}
else {
$output[] = $results[0];
}
}
echo implode("\n", $output);
exit();
}
/*
[0] => ./equal
[1] => --get
[2] => =
[3] => core_model_collect
[4] => --, --fields
*/
// field name choices
if(in_array($count, range(5, 30, 3))) {
$results = [];
if(in_array($values[$count-2], ['', '-'])) {
echo '--'."\n";
exit();
}
$params = [];
$announcement = get_announcement(trim($values[1], '-'), $values[3]);
if(isset($announcement['params'])) {
$params = array_keys($announcement['params']);
}
$clue = trim($values[$count-1], "-'");
foreach($params as $param) {
if(!strlen($clue) || strpos($param, $clue) === 0) {
$results[] = $param;
}
}
// filter results
$map_results = [];
foreach($results as $i => $result) {
$val = trim($result);
if(strlen($val) <= 0 || isset($map_results[$val])) {
unset($results[$i]);
}
$map_results[$val] = true;
// withdraw fields already present in the command
foreach(range(4, 30, 3) as $index) {
if(!isset($values[$index])) {
break;
}
$field = trim($values[$index], '-');
if($field == $result && $index < $count-1) {
unset($results[$i]);
}
}
}
$count_results = count($results);
if($count_results > 0) {
if($count_results == 1) {
echo '--'.reset($results).'='."\n";
}
else {
foreach($results as $result) {
// #memo - if there are results beginning with same chars, will display the common part without leading dashes
echo '--'.$result."\n";
}
}
}
exit();
}
/*
[0] => ./equal
[1] => --get
[2] => =
[3] => core_model_collect
[4] => --entity
[5] => =
( [6] => aa )
*/
// value choices
if(in_array($count, range(6, 30, 3)) || in_array($count, range(7, 30, 3))) {
$clue = '';
$param = trim($values[$count-2], '-');
if(in_array($count, range(7, 30, 3))) {
$clue = $values[$count-1];
$param = trim($values[$count-3], '-');
}
$params = [];
$announcement = get_announcement(trim($values[1], '-'), $values[3]);
if(isset($announcement['params'])) {
$params = $announcement['params'];
}
if(isset($params[$param])) {
$type = $params[$param]['type'] ?? '';
$usage = $params[$param]['usage'] ?? '';
if($usage == 'orm/entity') {
$entities = choices_entities();
// filter
$results = [];
foreach($entities as $choice) {
if(!strlen($clue) || strpos($choice, $clue) === 0) {
$results[] = $choice;
}
}
// output
foreach($results as $choice) {
echo ($choice != $clue)? "'".$choice."'\n" : '';
}
exit();
}
if($usage == 'orm/package') {
$packages = choices_packages();
foreach($packages as $choice) {
if(!strlen($clue) || strpos($choice, $clue) === 0) {
echo ($choice != $clue)? $choice."\n" : '';
}
}
exit();
}
if($type == 'boolean') {
$choices = ['true', 'false'];
foreach($choices as $choice) {
if(!strlen($clue) || strpos($choice, $clue) === 0) {
echo ($choice != $clue)? $choice."\n" : '';
}
}
exit();
}
}
exit();
}
/**
* #memo - Utilities below are hoisted when script is parsed.
*/
function get_announcement($operation, $controller) {
$announcement = [];
$command = 'php '.QN_BASEDIR.'/run.php --'.$operation.'='.$controller.' --announce=1';
$output = null;
if(exec($command, $output) !== false) {
$announce = json_decode(implode("\n", $output), true);
if(isset($announce['announcement'])) {
$announcement = $announce['announcement'];
}
}
return $announcement;
}
function choices_entities() {
$entities = [];
$command = 'php '.QN_BASEDIR.'/run.php --get=config_classes';
$output = null;
if(exec($command, $output) !== false) {
$result = json_decode(implode("\n", $output), true);
foreach($result as $package => $classes) {
foreach($classes as $class) {
$entities[] = "$package\\$class";
}
}
}
return $entities;
}
function choices_packages() {
$choices = [];
foreach(glob(QN_BASEDIR.'/packages/*', GLOB_ONLYDIR) as $directory) {
$choices[] = basename($directory);
}
return $choices;
}
function choices_root($operation) {
$choices = choices_level($operation, ['core', '']);
foreach(choices_packages() as $choice) {
$choices[] = $choice.'_';
}
return $choices;
}
function choices_level(string $operation, array $parts) {
$choices = [];
$map_folders = [
'--do' => 'actions',
'--get' => 'data',
'--show' => 'apps'
];
$count_parts = count($parts);
// clue is always the last item, and can be empty
$clue = $parts[$count_parts-1];
if(in_array($operation, array_keys($map_folders))) {
// clue only
if($count_parts == 1) {
foreach(choices_root($operation) as $choice) {
// filter results
if(!strlen($clue) || strpos($choice, $clue) === 0) {
$choices[] = $choice;
}
}
}
// several parts
else {
$packages = choices_packages();
if(!in_array($parts[0], $packages)) {
array_unshift($parts, 'core');
}
$folder = $map_folders[$operation];
$package = array_shift($parts);
$path_test = QN_BASEDIR.'/packages/'.$package.'/'.$folder.'/'.implode('/', $parts);
if(!is_dir($path_test)) {
// last part is a clue, ignore it
array_pop($parts);
$path_test = dirname($path_test);
if(!is_dir($path_test)) {
// invalid path
return [];
}
}
// suggest all files and dirs within that folder
foreach(glob($path_test.'/*') as $node) {
$choice = '';
if(is_dir($node)) {
$choice = basename($node).'_';
}
elseif(strpos($node, '.php') > 0) {
$choice = basename($node, '.php');
}
// filter results
if(!strlen($clue) || strpos($choice, $clue) === 0) {
$choices[] = $choice;
}
}
}
}
return $choices;
}