-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommand.php
More file actions
280 lines (272 loc) · 7.8 KB
/
Command.php
File metadata and controls
280 lines (272 loc) · 7.8 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
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
<?php
/**
* M PHP Framework
*
* @package M
* @subpackage Command.php
* @author Arnaud Sellenet <demental at github>
* @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
* @version 0.1
*/
/**
* Class for command-line generation
*/
class Command {
/**
* Options that can be set to true at startup
* @example : sh>./M host_name.org front --noheader --noninteractive
*/
protected static $options = array(
'noheader'=>false,
'noninteractive'=>false,
'silent'=>false,
'assetsurlrewriting'=>false,
);
/**
* Command interface core
*/
public static function start($initialcommand,$options = array()){
self::setOptions($options);
if(!self::getOption('noheader')) {
self::header('Application '.APP_NAME.' booted'."\n".'Welcome to the M command line tool'."\n\n".'Type \'help\' for full commands list');
}
if(!empty($initialcommand)) {
self::launch($initialcommand);
}
while(1 && !self::getOption('noninteractive')) {
$readline = popen('history -r "/tmp/.getline_history"
LINE=""
read -re -p "M console (Current App : '.APP_NAME.') > " LINE
history -s "$LINE"
history -w "/tmp/.getline_history"
echo $LINE','r');
$res = preg_replace('`;$`','',trim(substr(fgets($readline,1024),0,-1)));
fclose($readline);
self::launch($res);
}
if(self::getOption('noninteractive'))
self::launch('exit');
}
public static function launch($input) {
if(strpos($input,'--')===0) {
$input = explode('=',$input);
$input[0] = preg_replace('`^--`','',$input[0]);
if(empty($input[0])) {
self::error('no option specified');return;
}
if(isset($input[1])) {
self::setOption(trim($input[0]),trim($input[1]));
} else {
self::setOption(trim($input[0]),true);
}
self::line('Set "'.$input[0].'" option to '.self::getOption($input[0]));
return;
}
$args = explode(' ',$input);
$command = preg_replace('`\W`','',array_shift($args));
list($params,$options) = self::parseArgs($args);
try {
$exec = self::factory($command);
$exec->execute($params,$options);
} catch(Exception $e) {
self::error($e->getMessage());
self::error($e->getTraceAsString());
}
}
/**
* Determines from an args array which ones should be considered as arguments and which ones should be considered as options
* And returns an array of two arrays
* @param array raw args list
* @return array array(array(args),array(options))
*/
public static function parseArgs($arr)
{
$options = array();
$params = array();
foreach($arr as $item) {
// option
if(strpos($item,'--')===0) {
$item = explode('=',$item);
$item[0] = preg_replace('`^--`','',$item[0]);
if(empty($item[0])) {
// empty option => bypass
continue;
}
if(isset($item[1])) {
$options[trim($item[0])] = trim($item[1]);
} else {
$options[trim($item[0])] = true;
}
} else {
$params[] = $item;
}
}
return array($params,$options);
}
public static function setOptions($options) {
self::$options = array_merge(self::$options,$options);
}
public static function setOption($option,$val) {
self::$options[$option] = $val;
}
public static function getOption($name)
{
return self::$options[$name];
}
/**
* Factory to create command instances
*/
public static function factory($command,$path = null) {
$commandclass = 'Command_'.$command;
class_exists($commandclass) || self::_load_command($command, $path);
return new $commandclass;
}
/**
* Load a command file
* @access private
*/
private static function _load_command($command, $path = null) {
if(is_null($path)) { $path = __DIR__ . '/commands/'; }
$commandfile = $path . strtolower($command).'.php';
if(!FileUtils::File_exists_incpath($commandfile)) {
throw new Exception('Command "'.$command.'" not found');
}
require_once $commandfile;
}
/**
* display a text line (for information)
* @param string text to be displayed
*/
public static function line($message) {
if(self::getOption('silent')) return;
echo $message."\n";
}
/**
* renders each line of an iterable object
*/
public static function dump($object)
{
self::line(print_r($object, true));
}
/**
* Display an inline content
*/
public static function inline($message)
{
if(self::getOption('silent')) return;
echo $message;
}
/**
* ask for a yes/no to the CLI user
* @param string prompt message
* @param string (default 'n') default value if user just types 'enter'
* @param string (default 'y') value displayed and expected for positive answer
* @param string (default 'n') value displayed and expected for negative answer
*/
public static function confirm($message,$default='n',$yes='y',$no='n')
{
switch($default) {
case $yes:
$yes = strtoupper($yes);
break;
default:
$no = strtoupper($no);
break;
}
if(self::getOption('noninteractive')){
$res = $default;
self::line($message.' : '.$res);
} else {
$res = self::prompt($message.' ['.$yes.'/'.$no.']');
if(empty($res)) {
$res = $default;
}
}
return strtolower($res) == strtolower($yes);
}
/**
* Ask the CLI user to choose between several choices
* @param string prompt message
* @param string default value if user just types 'enter'
* @param array indexed array of the different possible choices
*/
public static function choose($message,$default='',$values)
{
$message = $message.' ['.implode(' / ',$values).'] (default : '.$default.')';
if(!self::getOption('noninteractive')){
$res = self::prompt($message);
}
if(empty($res)) {
self::line($message.' : '.$default);
$res = $default;
}
if(!in_array($res,$values)) {
self::line('Incorrect input, try again.....');
self::choose($message,$default,$values);
} else {
return $res;
}
}
/**
* Ask the CLI user to type something, with a default value if provided
* @param string prompt message
* @param string default value id user just types 'enter'
*/
public static function ask($message,$default='')
{
$message = $message.(empty($default)?'':' ['.$default.']');
if(!self::getOption('noninteractive')) {
$res = self::prompt($message);
}
if(empty($res)) return $default;
return $res;
}
public static function error($message)
{
if(self::getOption('silent')) return;
echo "\n".'***[ERROR]***'."\n".$message."\n";
}
public static function info($message)
{
if(self::getOption('silent')) return;
echo '[INFO] '.$message."\n";
}
public static function header($message) {
if(self::getOption('silent')) return;
echo "\n".str_repeat('*',80)."\n";
$content = explode("\n",$message);
foreach($content as $line) {
printf("* %-76s *\n",$line);
}
echo str_repeat('*',80)."\n";
}
public static function prompt($message)
{
echo "\n".$message.' > ';
$res = strtolower(trim(fgets(STDIN)));
return $res;
}
/**
* this method must implement the script fired when command is executed
* Can throw an Exception if command fails
*/
public function execute($params,$options)
{
# code...
}
/**
* display short help when 'help' global command is fired
*/
public function shortHelp()
{
$this->line('No help for this command');
}
/**
* display long help when 'help [command_name]' command is fired
* @param array additional params that may refer to subcommands
*/
public function longHelp($params)
{
$this->line('No help for this command');
}
}