-
Notifications
You must be signed in to change notification settings - Fork 1
/
interactive_runner.php
executable file
·643 lines (501 loc) · 20.2 KB
/
interactive_runner.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
class interactive_runner {
private $obj;
private $className;
private $commands_list;
private $current_command_completion = null;
const LOOP_IDL = 'LOOP_IDL'; //loop not running
const LOOP_RUN = 'LOOP_RUN'; //loop running
const LOOP_QUIT = 'LOOP_QUIT'; //loop end
private $command_pipe;
private $file;
private $output; //output mode, see below
public $current_call; //read only structure with current call args
const ns = "runner";
const OUTPUT_RBX = 'rbx';
const OUTPUT_JSON = 'json';
const OUTPUT_RAW = 'raw';
static function init(){
if(!classes::init_need(__CLASS__)) return;
classes::register_class_path("doc_parser", CLASS_PATH."/apis/doc/parse.php");
}
public function __construct(){
$this->output = self::OUTPUT_RBX;
if(isset(cli::$dict['ir://raw'])) {
$this->output = self::OUTPUT_RAW;
}
if(isset(cli::$dict['ir://json'])) {
$this->output = self::OUTPUT_JSON;
}
if(isset(cli::$dict['ir://output'])) {
if(!bool(cli::$dict['ir://output'])) $this->output = self::OUTPUT_RAW;
elseif(cli::$dict['ir://output'] === true) $this->output = self::OUTPUT_RBX;
else $this->output = cli::$dict['ir://output'];
}
rbx::$output_mode = $this->output == self::OUTPUT_RBX;
$this->file = getcwd().DIRECTORY_SEPARATOR.$GLOBALS['argv'][0];
cli::register_completion(array($this, 'completion_function'));
$this->reflection_scan($this, self::ns);
}
/**
* @interactive_runner disable
*/
public function completion_function($input, $index){
$infos = readline_info();
$completion = array();
if(!$index && !$this->current_command_completion){
foreach ($this->commands_list as $command) {
if($command['command_ns'] != self::ns){
$completion[] = $command['command_key'];
}
}
}
else {
$line = pick($this->current_command_completion, substr($infos['line_buffer'], 0, $infos['point']) );
$args = cli::parse_args($line);
$method_name = array_shift($args);
$command_infos = $this->lookup($method_name);
if(!$command_infos)
return false;
$command_args = array_keys(array_msort($command_infos['usage']['params'], array('position' => SORT_ASC)));
$arg_index = pick($this->mandatory_arg_index, max(0, (count($args) - (substr($line, -1) == " " ? 0 : 1))) );
$arg_name = $command_args[$arg_index];
if(!$this->mandatory_arg_index && count($args))
foreach($command_args as $k=>$v)
$this->current_call['args'][$v] = array_get($args, $k);
if($completion_callback = $command_infos['usage']['params'][$arg_name]['completion_callback'])
$completion = call_user_func($completion_callback, array_get($args, $arg_index), $completion_callback[0], $this, $args);
else if($completion_values = $command_infos['usage']['params'][$arg_name]['completion_values'])
$completion = $completion_values;
else
$completion = array();
}
$completion = array_filter($completion);
return empty($completion) ? array("") : $completion;
}
private function help_cmd($command){
if($command['usage']['cli'])
return "(none) -- ONLY AVAILABLE THROUGH CLI";
$str = "{$command['command_key']} ";
$parametred_aliases = array_filter($command['aliases']);
$aliases = array_keys(array_diff_key($command['aliases'], $parametred_aliases));
$aliases = array_diff($aliases, array($command['command_key'], $command['command_hash']));
if($aliases) $str.= "(".join(', ', $aliases).") ";
if($command['usage']['params']) {
$tmp_trailing_optionnal = 0; $tmp_str = array();
foreach($command['usage']['params'] as $param_name=>$param_infos){
$tmp_str [] = (isset($param_infos['optional']) ? '[':'')."\$$param_name";
if(isset($param_infos['optional'])) $tmp_trailing_optionnal++;
}
$str .= join(', ', $tmp_str).str_repeat("]", $tmp_trailing_optionnal);
}
return $str;
}
/**
* @alias y true
* @alias n false
* @interactive_runner hide
*/
function yes_mode($mode = true){
cli::$unattended = bool($mode);
rbx::ok("Unattended mode is ".(cli::$unattended ? "on" : "off"));
}
/**
* @alias ?
* @interactive_runner hide
*/
function list_commands(){
$msgs = array();
$rbx_msgs = array();
foreach($this->commands_list as $command_hash=>$command) {
if($command['usage']['hide']) continue;
if(!$command['usage']['cli']) $str = $this->help_cmd($command);
else $str = "{$command['command_key']} (cli-only)";
if(isset($command['usage']['doc'][0]) && $doc = trim($command['usage']['doc'][0]))
$str = $str . str_repeat(" ", max(1, cli::$cols - strlen($str) - strlen($doc) - 2)).$doc;
$msgs[$command['command_ns']][] = $str;
$parametred_aliases = array_filter($command['aliases']);
foreach($parametred_aliases as $alias_name=>$args)
$msgs[$command['command_ns']][] = "$alias_name (={$command['command_key']} ".join(" ", $args).")";
}
foreach($msgs as $command_ns=>$msgs) {
//title
$rbx_msgs[] = "`$command_ns` commands list";
$rbx_msgs[] = join(LF, $msgs);
}
call_user_func_array(array('rbx', 'box'), $rbx_msgs);
}
/**
* show available commands
* @autocomplete command interactive_runner::get_commands_list
* @alias man
*/
function help( $command = null){
$rbx_msgs = array();
if($this->classDoc) {
$rbx_msgs [] = "Global usage";
$rbx_msgs [] = join(LF, $this->classDoc['doc']);
}
$commands_list = $this->commands_list;
if($command = $this->lookup($command))
$commands_list = array_sort($commands_list, $command['command_hash']);
foreach($commands_list as $command_hash=>$command_infos) {
if($command_hash != $command['command_hash'] && ( $command_infos['usage']['hide']
|| $command_infos['command_ns'] == self::ns) ) continue;
//title
$msgs = $command_infos['usage']['doc'] ? $command_infos['usage']['doc'] : array();
$rbx_msgs[] = "Command : {$command_infos['command_key']}";
array_unshift($msgs, "Usage :". $this->help_cmd($command_infos));
$rbx_msgs[] = join(LF, $msgs);
}
if(!$rbx_msgs) {
rbx::error("No documentation available");
return;
}
$rbx_msgs[] = array('notrim' => true);
call_user_func_array(array('rbx', 'box'), $rbx_msgs);
}
/**
* replay last command
* @alias r
*/
function replay(){
//this is only a placeholder
}
/**
* @interactive_runner hide
*/
function register_alias($instance, $command_key, $alias_name, $args = array()){
$this->command_aliases(get_class($instance), $command_key, array($alias_name=>$args) );
}
private function generate_command_hash($command_ns, $command_key){
return "$command_ns::$command_key";
}
private function lookup($command_prompt){
$command_resolve = array();
foreach($this->commands_list as $command_hash=>$command_infos)
if(isset($command_infos['aliases'][$command_prompt]))
$command_resolve[] = $command_infos;
if(count($command_resolve) > 1)
throw rbx::error("Too many results for command '*::$command_prompt', please specify ns");
return first($command_resolve);
}
private function command_aliases($command_ns, $command_key){
$command_hash = $this->generate_command_hash($command_ns, $command_key);
if(!isset($this->commands_list[$command_hash]))
return false;
$aliases_list = &$this->commands_list[$command_hash]['aliases'];
$aliases = func_get_args(); $aliases = array_slice($aliases, 2);
foreach($aliases as $alias){
if(!is_array($alias)) $alias = array($alias=>false);
$aliases_list = array_merge($aliases_list, $alias);
}
return true;
}
private function command_register($command_ns, $command_key, $callback, $usage){
$command_hash = $this->generate_command_hash($command_ns, $command_key);
$this->commands_list[$command_hash] = array(
'command_hash'=> $command_hash,
'command_ns' => $command_ns,
'command_key' => $command_key,
'usage' => $usage,
'callback' => $callback,
'aliases' => array(),
);
$this->command_aliases($command_ns, $command_key, $command_key, $command_hash);
}
private $mandatory_arg_index; //autocomplete helper
private function command_parse($command_prompt, $command_args = array(), $command_dict = array()) {
if(!$command_prompt)
throw new Exception("No command");
$command_infos = $this->lookup($command_prompt);
if($command_infos['usage']['cli'] && $this->command_pipe == self::LOOP_RUN) {
if(cli::bool_prompt("Command only available through cli. Open help ?", false)) $this->help($command_infos['command_hash']);
throw new Exception("Invalid command");
}
if(!$command_infos)
throw rbx::error("Invalid command key '$command_prompt'");
$alias_args = $command_infos['aliases'][$command_prompt];
if(is_array($alias_args))
$command_args = array_merge($alias_args, $command_args);
$command_args_mask = $command_infos['usage']['params'];
$this->mandatory_arg_index = 0;
$this->current_call['args'] = array();
foreach($command_args_mask as $param_name=>$param_infos){
if(array_key_exists($this->mandatory_arg_index, $command_args)){
$param_in = $command_args[$this->mandatory_arg_index];
}
else{
if(array_key_exists($param_name, $command_dict)){
$param_in = $command_dict[$param_name];
}
else {
if(array_key_exists('default', $param_infos)){
$param_in = $param_infos['default'];
}
else {
$this->current_command_completion = join(' ', $this->last_command).' ';
$param_in = cli::text_prompt("\${$command_infos['command_ns']}[{$param_name}]", null);
$this->current_command_completion = null;
}
}
}
$this->current_call['args'][$param_name] = $param_in;
$this->mandatory_arg_index++;
}
cli::add_history($command_prompt.' '.join(' ', $this->current_call['args']));
return array($command_infos['callback'], $this->current_call['args']);
}
/**
* ends interactive session
* @alias q
* @alias exit
*/
function quit(){
$this->command_pipe = self::LOOP_QUIT;
rbx::ok("Quit");
}
/**
* @interactive_runner disable
* runner's internal looop, signal management
*/
public function run() {
$run = $start = array();
if(!empty(cli::$dict['ir://run']))
$run = cli::$dict['ir://run'];
else if(!empty(cli::$dict['ir://start']))
$start = cli::$dict['ir://start'];
if($run || $start) {
if($run === true)
$run = 'run';
if($start && !is_array($start)) $start = array($start);
if($run && !is_array($run)) $run = array($run);
foreach(array_merge($start, $run) as $cmd) {
list($command_callback, $command_args) = $this->command_parse($cmd, array(), cli::$dict);
$res = call_user_func_array($command_callback, $command_args);
if($res !== null) {
if($this->output == self::OUTPUT_RAW)
echo $res;
elseif($this->output == self::OUTPUT_JSON)
echo json_encode($res);
else cli::box('Response', $res);
}
}
if($run)
exit();
}
$this->command_loop();
}
private $last_command = array();
private static $REPLAY_COMMAND = array('r');
//embeded object loop, deal with commands
private function command_loop(){
///system("stty -icanon");
$this->command_pipe = self::LOOP_RUN;
while($this->command_pipe != self::LOOP_QUIT){
try {
$command_split = array();
$this->mandatory_arg_index = null;
cli::text_prompt('$'.$this->className, null, $command_split);
if($command_split == self::$REPLAY_COMMAND)
$command_split = $this->last_command;
else
$this->last_command = $command_split;
$command_prompt = array_shift($command_split);
list($command_callback, $command_args) = $this->command_parse($command_prompt, $command_split);
} catch(Exception $e){ continue; }
try {
$res = call_user_func_array($command_callback, $command_args);
if($res !== null) {
if($this->output == self::OUTPUT_RAW)
echo $res;
elseif($this->output == self::OUTPUT_JSON)
echo json_encode($res);
else cli::box('Response', $res);
}
} catch(Exception $e){
echo CRLF;
$msg = $e->getMessage() ? $e->getMessage() : $e;
$trace = $e->getTraceAsString();
$trace = strtr($trace, array(YKS_PATH=>'yks', getcwd().DIRECTORY_SEPARATOR => ''));
cli::box("!! Uncatched exception !!", $msg , "trace", $trace, array('trim'));
continue;
}
}
}
private function reflection_scan_wsdl($instance, $command_ns){
$methods = $instance->__getFunctions();
foreach($methods as $method_sign){
$out = null;
if(!preg_match("#(.*?)\s+(.*?)\((.*?)\)#", $method_sign, $out)) continue;
list(,$method_type, $method_name, $method_args_str) = $out;
$method_args = array();
if(preg_match_all('#([^\s]*)\s+\$([^,\s]*)#', $method_args_str, $out)) {
$method_args = array_combine($out[2], $out[1]);
}
$callback = array($instance, $method_name);
$usage = array('params'=>array());
foreach($method_args as $arg_name => $arg_type)
$usage['params'][$arg_name] = array('type' => $arg_type);
$this->command_register($command_ns, $method_name, $callback, $usage);
}
}
/**
* @api public
* @instance must be an object
* @instance can be a string describing a class name (for static classes)
* @interactive_runner hide
*/
function reflection_scan($instance, $command_ns = null) {
if(!$instance)
throw new Exception("Invalid instance");
$className = is_string($instance) ? $instance : get_class($instance);
if(!$command_ns)
$command_ns = $className;
$reflect = new ReflectionClass($className);
$this->scan($reflect, $instance, $className, $command_ns);
if($command_ns !== self::ns && ! isset($this->classDoc) )
$this->classDoc = doc_parser::parse($reflect->getDocComment());
}
private function scan($reflect, $instance, $className, $command_ns) {
$methods = $reflect->getMethods();
foreach($methods as $method) {
$method_name = $method->getName();
$is_command = false;
$callback = null;
$is_magic = starts_with($method_name, "__");
if($method->isPublic()
&& !$method->isStatic()
&& !$is_magic
&& !$method->isConstructor()
&& !is_string($instance)) {
$callback = array($instance, $method_name);
} elseif($method->isPublic()
&& $method->isStatic()
&& !$is_magic
&& $method_name != "init"){
$callback = array($className, $method_name);
} else {
continue;
}
$command_key = $method_name;
$params = $method->getParameters();
$doc = doc_parser::parse($method->getDocComment());
$tmp = isset($doc['args']['interactive_runner'])
? $doc['args']['interactive_runner']['computed'] : null;
if(!$tmp) $tmp = array();
if(in_array("disable", $tmp))
continue;
$usage = array('params'=>array(), 'doc' => $doc['doc'],
'hide' => in_array("hide", $tmp),
'cli' => in_array("cli", $tmp), //only available via cli
);
foreach($params as $param) {
$param_infos = array(
'position' => $param->getPosition(),
'completion_values' => null,
'completion_callback' => null,
);
if($param->isOptional()){
$param_infos['optional'] = true;
$param_infos['default'] = $param->getDefaultValue();
}
$usage['params'][$param->getName()] = $param_infos;
}
$this->command_register($command_ns, $command_key, $callback, $usage);
$command_hash = $this->generate_command_hash($command_ns, $command_key);
if( isset($doc['args']['autocomplete']) ){
foreach($doc['args']['autocomplete']['values'] as $values){
$arg_name = strip_start(array_shift($values), '$');
$raw = first($values);
if(!isset($this->commands_list[$command_hash]['usage']['params'][$arg_name]))
continue;
unset($callback_definition);
unset($statics_definition);
$callback = preg_match("#^([a-z0-9_]+)::([a-z0-9_]+)$#i", $raw, $callback_definition) && count($values) == 1;
$statics = preg_match("#^([a-z0-9_]+)::\\\$([a-z0-9_]+)$#i", $raw, $statics_definition) && count($values) == 1;
$usage = &$this->commands_list[$command_hash]['usage']['params'][$arg_name];
if($statics) {
$class = new ReflectionClass($statics_definition[1]);
$usage['completion_values'] = $class->getStaticPropertyValue($statics_definition[2]);
} elseif($callback)
$usage['completion_callback'] = array($callback_definition[1] == 'self' ? $className : $callback_definition[1], $callback_definition[2]);
else
$usage['completion_values'] = $values;
unset($usage);
}
}
if(isset($doc['args']['alias'])) foreach($doc['args']['alias']['values'] as $args) {
$alias_name = array_shift($args);
if(!( $alias_name && $command_key)) continue;
$this->command_aliases($command_ns, $command_key, array($alias_name=>$args) );
}
}
}
/**
* toggle fullsize mode
* @interactive_runner hide
* @alias fs
*/
function fullsize(){
if(cli::$OS == cli::OS_WINDOWS) {
$cmd = "mode CON";
exec($cmd, $out);
if( preg_match_all("#(lines|columns):\s+([0-9]+)$#mi", join("\n", $out), $out))
$size = array_combine(array_map('strtolower', $out[1]), $out[2]);
cli::$cols = $size['columns'] ? $size['columns'] - 1 : cli::$cols;
} else
cli::$cols = trim(`tput cols`);
$this->list_commands();
}
private static $current_runner = null;
/**
* @interactive_runner disable
*/
static public function start($className, $args = array()) {
if(!is_array($args))
$args = array($args);
$runner = new self();
//register runners own commands
if(is_object($className))
$runner->reflection_scan($className);
else if ($className == "SoapClient") {
$source = new SoapClient($args[0]);
$runner->reflection_scan_wsdl($source, "SoapClient");
} else if(is_string($className)) {
$runner->className = $className;
$classes = array($className);
if(array_get(cli::$dict, "ir://load"))
$classes = array_merge($classes, (array) cli::$dict["ir://load"]);
foreach($classes as $className) {
if(!is_string($className))
continue;
ob_start(); //prevent autoloader to print anything (e.g. shebang)
if(!class_exists($className)) //FORCE AUTOLOADER HERE !!
throw new Exception("Invalid class '$className'");
ob_end_clean();
$reflector = new ReflectionClass($className);
// class { private function __construct(){} function instanciate(){} }
$static = bool(array_get(cli::$dict, 'ir://static'));
$instanciate = $reflector->hasMethod('instanciate') && ( !$reflector->IsInstantiable() || cli::$dict['ir://instanciate']) && !$static;
if($instanciate)
$instance = call_user_func_array(array($className, 'instanciate'), $args);
else
$instance = $reflector->IsInstantiable() && ! $static
? ($args && !is_null( $reflector->getConstructor ())
? $reflector->newInstanceArgs($args)
: $reflector->newInstance() )
: $className; //static
$runner->scan($reflector, $instance, $className, $className);
}
}
if(!empty(cli::$dict['ir://fs']))
$runner->fullsize();
else
$runner->list_commands();
self::$current_runner = $runner;
$runner->run(); //private internal
}
//helpers for interactive runner completion
private static function get_commands_list(){ return array_keys(self::$current_runner->commands_list); }
}