This repository has been archived by the owner on Nov 27, 2021. It is now read-only.
forked from UnionOfRAD/lithium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Command.php
423 lines (387 loc) · 10.8 KB
/
Command.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
<?php
/**
* Lithium: the most rad php framework
*
* @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
* @license http://opensource.org/licenses/bsd-license.php The BSD License
*/
namespace lithium\console;
use Exception;
use lithium\console\command\Help;
/**
* All Commands to be run from the Lithium console must extend this class.
*
* The `run` method is automatically called if it exists. Otherwise, if a method does not exist
* the `Help` command will be run.
*
* {{{
* $ li3 example
* $ li3 example --format=json
* }}}
*
*/
class Command extends \lithium\core\Object {
/**
* If -h or --help param exists a help screen will be returned.
* Similar to running `li3 help COMMAND`.
*
* @var boolean
*/
public $help = false;
/**
* A Request object.
*
* @see lithium\console\Request
* @var object
*/
public $request;
/**
* A Response object.
*
* @see lithium\console\Response
* @var object
*/
public $response;
/**
* Only shows only text output without styles.
*
* @var boolean
*/
public $plain = false;
/**
* Only shows error output.
*
* @var boolean
*/
public $silent = false;
/**
* Dynamic dependencies.
*
* @var array
*/
protected $_classes = array(
'response' => 'lithium\console\Response'
);
/**
* Auto configuration.
*
* @var array
*/
protected $_autoConfig = array('classes' => 'merge');
/**
* Constructor.
*
* @param array $config
* @return void
*/
public function __construct(array $config = array()) {
$defaults = array('request' => null, 'response' => array(), 'classes' => $this->_classes);
parent::__construct($config + $defaults);
}
/**
* Command Initializer.
*
* Populates the `$response` property with a new instance of the `Response` class passing it
* configuration and assigns the values from named parameters of the request (if applicable) to
* properties of the command.
*
* @return void
*/
protected function _init() {
parent::_init();
$this->request = $this->_config['request'];
if (!is_object($this->request) || !$this->request->params) {
return;
}
$this->response = $this->_config['response'];
if (!is_object($this->response)) {
$this->response = $this->_instance('response', $this->response);
}
$default = array('command' => null, 'action' => null, 'args' => null);
$params = array_diff_key((array) $this->request->params, $default);
foreach ($params as $key => $param) {
$this->{$key} = $param;
}
}
/**
* Called by the `Dispatcher` class to invoke an action.
*
* @see lithium\console\Dispatcher
* @see lithium\console\Response
* @param string $action The name of the method to run.
* @param array $args The arguments from the request.
* @param array $options
* @return object The response object associated with this command.
* @todo Implement filters.
*/
public function __invoke($action, $args = array(), $options = array()) {
try {
$this->response->status = 1;
$result = $this->invokeMethod($action, $args);
if (is_int($result)) {
$this->response->status = $result;
} elseif ($result || $result === null) {
$this->response->status = 0;
}
} catch (Exception $e) {
$this->error($e->getMessage());
}
return $this->response;
}
/**
* Invokes the `Help` command.
*
* The invoked Help command will take over request and response objects of
* the originally invoked command. Thus the response of the Help command
* becomes the response of the original one.
*
* @return boolean
*/
protected function _help() {
$help = new Help(array(
'request' => $this->request,
'response' => $this->response,
'classes' => $this->_classes
));
return $help->run(get_class($this));
}
/**
* Writes a string to the output stream.
*
* @param string $output The string to write.
* @param integer|string|array $options
* integer as the number of new lines.
* string as the style
* array as :
* - nl : number of new lines to add at the end
* - style : the style name to wrap around the
* @return integer
*/
public function out($output = null, $options = array('nl' => 1)) {
if ($this->silent) {
return;
}
return $this->_response('output', $output, $options);
}
/**
* Writes a string to error stream.
*
* @param string $error The string to write.
* @param integer|string|array $options
* integer as the number of new lines.
* string as the style
* array as :
* - nl : number of new lines to add at the end
* - style : the style name to wrap around the
* @return integer
*/
public function error($error = null, $options = array('nl' => 1)) {
return $this->_response('error', $error, $options);
}
/**
* Handles input. Will continue to loop until `$options['quit']` or
* result is part of `$options['choices']`.
*
* @param string $prompt
* @param array $options
* @return string Returns the result of the input data. If the input is equal to the `quit`
* option boolean `false` is returned
*/
public function in($prompt = null, array $options = array()) {
$defaults = array('choices' => null, 'default' => null, 'quit' => 'q');
$options += $defaults;
$choices = null;
if (is_array($options['choices'])) {
$choices = '(' . implode('/', $options['choices']) . ')';
}
$default = $options['default'] ? "[{$options['default']}] " : '';
do {
$this->out("{$prompt} {$choices} \n {$default}> ", false);
$result = trim($this->request->input());
} while (
!empty($options['choices']) && !in_array($result, $options['choices'], true)
&& (empty($options['quit']) || $result !== $options['quit'])
&& ($options['default'] == null || $result !== '')
);
if ($result == $options['quit']) {
return false;
}
if ($options['default'] !== null && $result == '') {
return $options['default'];
}
return $result;
}
/**
* Writes a header to the output stream. In addition to the actual text,
* horizontal lines before and afterwards are written. The lines will have
* the same length as the text. This behavior can be modified by providing
* the length of lines as a second paramerter.
*
* Given the text `'Lithium'` this generates following output:
*
* {{{
* -------
* Lihtium
* -------
* }}}
*
* @param string $text The heading text.
* @param integer $line The length of the line. Defaults to the length of text.
* @return void
*/
public function header($text, $line = null) {
if (!$line) {
$line = strlen($text);
}
$this->hr($line);
$this->out($text, 1, 'heading');
$this->hr($line);
}
/**
* Writes rows of columns.
*
* This method expects asceding integer values as the keys, which map to the appropriate
* columns. Currently, there is no special "header" option, but you can define them for your
* own.
*
* Example Usage:
*
* {{{
* $output = array(
* array('Name', 'Age'),
* array('----', '---'),
* );
* foreach($users as $user) {
* $output[] = array($user->name, $user->age);
* }
* $this->columns($output);
* }}}
*
* Would render something similar to:
*
* {{{
* Name Age
* ---- ---
* Jane Doe 22
* Foo Bar 18
* }}}
*
* This method also calculates the needed space between the columns. All option params given
* also get passed down to the `out()` method, which allow custom formatting. Passing something
* like `$this->columns($output, array('style' => 'red)` would print the table in red.
*
* @see lithium\console\Response::styles()
* @param array $rows The rows to print, with each column as an array element.
* @param array $options Optional params:
* - separator : Different column separator, defaults to `\t`
* - style : the style name to wrap around the columns output
* @return void
*/
public function columns($rows, $options = array()) {
$defaults = array('separator' => "\t", "error" => false);
$options += $defaults;
$lengths = array_reduce($rows, function($columns, $row) {
foreach ((array) $row as $key => $val) {
if (!isset($columns[$key]) || strlen($val) > $columns[$key]) {
$columns[$key] = strlen($val);
}
}
return $columns;
});
$rows = array_reduce($rows, function($rows, $row) use ($lengths, $options) {
$text = '';
foreach ((array) $row as $key => $val) {
$text = $text . str_pad($val, $lengths[$key]) . $options['separator'];
}
$rows[] = $text;
return $rows;
});
if ($options['error']) {
$this->error($rows, $options);
return;
}
$this->out($rows, $options);
}
/**
* Add newlines ("\n") to the output stream.
*
* @param integer $number The number of new lines to print.
* @return integer
*/
public function nl($number = 1) {
return str_repeat("\n", $number);
}
/**
* Adds a horizontal line to output stream.
*
* @param integer $length The length of the line, defaults to 80.
* @param integer $newlines How many new lines to print afterwards, defaults to 1.
* @return integer
*/
public function hr($length = 80, $newlines = 1) {
return $this->out(str_repeat('-', $length), $newlines);
}
/**
* Clears the entire screen.
*
* @return void
*/
public function clear() {
passthru(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN' ? 'cls' : 'clear');
}
/**
* Stop execution, by exiting the script.
*
* @param integer $status Numeric value that will be used on `exit()`.
* @param boolean $message An optional message that will be written to the stream.
* @return void
*/
public function stop($status = 0, $message = null) {
if ($message) {
($status == 0) ? $this->out($message) : $this->error($message);
}
exit($status);
}
/**
* Handles the response that is sent to the stream.
*
* @param string $type the stream either output or error
* @param string $string the message to render
* @param integer|string|array $options
* integer as the number of new lines.
* string as the style
* array as :
* - nl : number of new lines to add at the end
* - style : the style name to wrap around the
* @return void
*/
protected function _response($type, $string, $options) {
$defaults = array('nl' => 1, 'style' => null);
if (!is_array($options)) {
if (!$options || is_int($options)) {
$options = array('nl' => $options);
} else if (is_string($options)) {
$options = array('style' => $options);
} else {
$options = array();
}
}
$options += $defaults;
if (is_array($string)) {
$method = ($type == 'error' ? $type : 'out');
foreach ($string as $out) {
$this->{$method}($out, $options);
}
return;
}
extract($options);
if ($style !== null && !$this->plain) {
$string = "{:{$style}}{$string}{:end}";
}
if ($nl) {
$string = $string . $this->nl($nl);
}
return $this->response->{$type}($string);
}
}
?>