-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkehikko.php
More file actions
executable file
·78 lines (69 loc) · 2.46 KB
/
kehikko.php
File metadata and controls
executable file
·78 lines (69 loc) · 2.46 KB
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
<?php
require_once __DIR__ . '/../../autoload.php';
/* start profiler automatically if it is found and enabled */
if (function_exists('profiler_start') && cfg(['profiler', 'console', 'enabled']) === true) {
profiler_start();
}
/* create command line option parser */
$optparser = new \Console_CommandLine();
$optparser->description = 'Kehikko console';
$optparser->version = '2.0';
$optparser->subcommand_required = true;
/* find console command definitions */
$commands = [];
$files = tool_system_find_files(['console.yml']);
foreach ($files as $file) {
$yaml = tool_yaml_load([$file, dirname($file) . '/console-local.yml']);
if (!isset($yaml['commands']) || !is_array($yaml['commands'])) {
continue;
}
$prefix = strtolower(basename(dirname($file))) . ':';
if ($prefix == 'models:') {
$prefix = '';
}
if (array_key_exists('prefix', $yaml)) {
$prefix = is_string($yaml['prefix']) && !empty($yaml['prefix']) ? strtolower($yaml['prefix']) . ':' : '';
}
foreach ($yaml['commands'] as $command => $data) {
$command = $prefix . $command;
$description = tr(isset($data['description']) ? $data['description'] : 'no description');
$cmd = $optparser->addCommand($command, array('description' => $description));
if (isset($data['options'])) {
foreach ($data['options'] as $key => $value) {
$cmd->addOption($key, $value);
}
}
if (isset($data['arguments'])) {
foreach ($data['arguments'] as $key => $value) {
$cmd->addArgument($key, $value);
}
}
$commands[$command] = $data;
}
}
/* run command line parser */
$opt = null;
try
{
$opt = $optparser->parse();
} catch (Exception $e) {
$optparser->displayError($e->getMessage());
exit(1);
}
$command = $opt->command_name;
$args = $opt->command->args;
$options = $opt->command->options;
/* execute command */
$r = tool_call($commands[$command], [$command, $args, $options]);
if (is_int($r) && $r >= 0 && $r <= 255) {
/* return value is int between 0 and 255 */
exit(intval($r));
} else if ($r === true) {
/* return value is true so return ok */
exit(0);
} else if ($r === false) {
/* default error value */
exit(1);
}
log_debug('return value for call behind {0} is unsupported (not bool or int between 0-255), returning default error value of 1', [$command]);
exit(1);