-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.php
executable file
·188 lines (142 loc) · 4.92 KB
/
cli.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
<?php
/**
* Hydrogen Command Line Interface
*/
ini_set('memory_limit', '170M');
date_default_timezone_set('Asia/Shanghai');
if (!function_exists('DOPRINT')) {
function DOPRINT($str, $withDate = true) {
$date = $withDate ? '['.date('Y-m-d H:i:s O').']' : '';
$tpl = $date.' %s '.PHP_EOL;
if (is_array($str)) {
if (!$str) {
return;
}
foreach ($str as $line) {
if (is_array($line)) {
DOPRINT($line, $withDate);
} else {
print sprintf($tpl, $line);
}
}
} else {
print sprintf($tpl, $str);
}
}
}
$argc --;
array_shift($argv);
$base = __DIR__;
$vendor_path = $base.'/vendor';
$lib_path = $base.'/lib';
$application_path = $base.'/application';
$global_config_path = $base.'/config';
defined('APPLICATION_PATH') || define('APPLICATION_PATH', $application_path);
defined('VENDOR_PATH') || define('VENDOR_PATH', $vendor_path);
defined('LIB_PATH') || define('LIB_PATH', $lib_path);
defined('GLOBAL_CONFIG_PATH')
|| define('GLOBAL_CONFIG_PATH', $global_config_path);
defined('NAMESPACE_SEPARATOR') || define('NAMESPACE_SEPARATOR', '\\');
require LIB_PATH.'/Hydrogen/Load/Autoloader.php';
$autoloader = Hydrogen\Load\Autoloader::getInstance();
$autoloader->attachNamespace('application', $base.DIRECTORY_SEPARATOR.'application');
$autoloader->attachCallback(
array(
Hydrogen\Load\Autoloader::CALLBACK_NS2PATH
)
);
Hydrogen\Load\Loader::import(LIB_PATH.'/Hydrogen/Include/Functions.php');
static $_command_separatar = '::', $_baseNamespace = 'application\\console',
$_cslPostFix = 'Csl', $_actionPostFix = 'Act';
global $consoleHelper;
/**
* @return Hydrogen\Console\ConsoleHelper|null
*/
function instantiateConsoleHelper() {
global $consoleHelper;
if (null != $consoleHelper && $consoleHelper instanceof Hydrogen\Console\ConsoleHelper) {
return $consoleHelper;
}
global $_baseNamespace;
$consoleHelper = new Hydrogen\Console\ConsoleHelper($_baseNamespace, APPLICATION_PATH.'/console');
return $consoleHelper;
}
if (!isset($argv[0]) || !$argv[0]) {
$console = $consoleHelper;
$callableModules = $console->getCallableModules();
DOPRINT('Invalid command ! Callable module list: ##');
DOPRINT($callableModules);exit(1);
}
$pathSymbol = trim($argv[0], $_command_separatar." \n\r\0\x0B\t");
if (false === strpos($pathSymbol, $_command_separatar)) {
DOPRINT(' Specified Path is invalid! ');
DOPRINT(' example: Test::Abc::foo -p1=123 -p2=Jerry ');
exit(1);
}
$pathArr = explode($_command_separatar, $pathSymbol);
$module = $class = $action = '';
if ($pathArr) {
$pathArr = array_slice($pathArr, 0, 3);
$i = 0;
while ($tmp = array_pop($pathArr)) {
if (0 == $i) {
$action = $tmp;
} elseif (1 == $i) {
$class = ucfirst($tmp);
} else {
$module = $tmp;
}
$i ++;
}
}
print 'COMMAND: '.$module.'::'.$class.'::'.$action.PHP_EOL;
// route! start:
$className = $_baseNamespace.NAMESPACE_SEPARATOR
.trim($module.NAMESPACE_SEPARATOR.$class.$_cslPostFix, NAMESPACE_SEPARATOR);
/*echo 'app_path: '.APPLICATION_PATH.PHP_EOL;
echo 'className: '.$className.PHP_EOL;*/
if (!class_exists($className, true)) {
DOPRINT('Could not find the command script file');
DOPRINT(' Existing command class: '. $className.'.php');
exit(1);
}
$EXE = new $className;
## method
$expectedMethodName = $action.$_actionPostFix;
if (!method_exists($EXE, $expectedMethodName)) {
DOPRINT('Act: '.$action.' doesn\'t exist! in Class: '.$className);
DOPRINT('# Methods list: ');
$consoleHelper = instantiateConsoleHelper();
$methods = $consoleHelper->getCallableMethods($EXE, \ReflectionMethod::IS_PUBLIC);
$callableMethods = array();
foreach ($methods as $method) {
$tmpMethodName = $method->name;
if (strrchr($tmpMethodName, $_actionPostFix) === $_actionPostFix) {
$callableMethods[] = $tmpMethodName;
}
}
DOPRINT($callableMethods);
exit(1);
}
## arguments
## -args Terry,1223,false
$requireMethodArgs = array();
$consoleHelper = instantiateConsoleHelper();
$callableParams = $consoleHelper->extractCallableParams($argv);
// config
$CONFIG = Hydrogen\Config\Config::getInstance();
$result = $CONFIG->mergeConfigFile(APPLICATION_PATH.DIRECTORY_SEPARATOR
.'config'.DIRECTORY_SEPARATOR.ENV.DIRECTORY_SEPARATOR.'log.ini');
call_user_func_array(array($EXE, $expectedMethodName), $callableParams);
exit;
/*$reflectionClass = new ReflectionClass($className);
$methodList = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC);
DOPRINT('callable method list:');
$callableMethods = array();
foreach ($methodList as $method) {
$callableMethods[] =
$method->getName();
}*/
exit;
$reflectionMethod = $reflectionClass->getMethod($action);
var_dump($reflectionMethod);