/
CLImate.php
470 lines (418 loc) · 13.2 KB
/
CLImate.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
<?php
namespace League\CLImate;
use League\CLImate\Argument\Manager as ArgumentManager;
use League\CLImate\Decorator\Style;
use League\CLImate\Settings\Manager as SettingsManager;
use League\CLImate\TerminalObject\Router\Router;
use League\CLImate\Util\Helper;
use League\CLImate\Util\Output;
use League\CLImate\Util\UtilFactory;
/**
* @method CLImate black(string $str = null)
* @method CLImate red(string $str = null)
* @method CLImate green(string $str = null)
* @method CLImate yellow(string $str = null)
* @method CLImate blue(string $str = null)
* @method CLImate magenta(string $str = null)
* @method CLImate cyan(string $str = null)
* @method CLImate lightGray(string $str = null)
* @method CLImate darkGray(string $str = null)
* @method CLImate lightRed(string $str = null)
* @method CLImate lightGreen(string $str = null)
* @method CLImate lightYellow(string $str = null)
* @method CLImate lightBlue(string $str = null)
* @method CLImate lightMagenta(string $str = null)
* @method CLImate lightCyan(string $str = null)
* @method CLImate white(string $str = null)
*
* @method CLImate backgroundBlack(string $str = null)
* @method CLImate backgroundRed(string $str = null)
* @method CLImate backgroundGreen(string $str = null)
* @method CLImate backgroundYellow(string $str = null)
* @method CLImate backgroundBlue(string $str = null)
* @method CLImate backgroundMagenta(string $str = null)
* @method CLImate backgroundCyan(string $str = null)
* @method CLImate backgroundLightGray(string $str = null)
* @method CLImate backgroundDarkGray(string $str = null)
* @method CLImate backgroundLightRed(string $str = null)
* @method CLImate backgroundLightGreen(string $str = null)
* @method CLImate backgroundLightYellow(string $str = null)
* @method CLImate backgroundLightBlue(string $str = null)
* @method CLImate backgroundLightMagenta(string $str = null)
* @method CLImate backgroundLightCyan(string $str = null)
* @method CLImate backgroundWhite(string $str = null)
*
* @method CLImate bold(string $str = null)
* @method CLImate dim(string $str = null)
* @method CLImate underline(string $str = null)
* @method CLImate blink(string $str = null)
* @method CLImate invert(string $str = null)
* @method CLImate hidden(string $str = null)
*
* @method CLImate info(string $str = null)
* @method CLImate comment(string $str = null)
* @method CLImate whisper(string $str = null)
* @method CLImate shout(string $str = null)
* @method CLImate error(string $str = null)
*
* @method mixed out(string $str)
* @method mixed inline(string $str)
* @method mixed table(array $data)
* @method mixed json(mixed $var)
* @method mixed br($count = 1)
* @method mixed tab($count = 1)
* @method mixed draw(string $art)
* @method mixed border(string $char = null, integer $length = null)
* @method mixed dump(mixed $var)
* @method mixed flank(string $output, string $char = null, integer $length = null)
* @method mixed progress(integer $total = null)
* @method mixed padding(integer $length = 0, string $char = '.')
* @method mixed input(string $prompt, Util\Reader\ReaderInterface $reader = null)
* @method mixed confirm(string $prompt, Util\Reader\ReaderInterface $reader = null)
* @method mixed password(string $prompt, Util\Reader\ReaderInterface $reader = null)
* @method mixed checkboxes(string $prompt, array $options, Util\Reader\ReaderInterface $reader = null)
* @method mixed radio(string $prompt, array $options, Util\Reader\ReaderInterface $reader = null)
* @method mixed animation(string $art, TerminalObject\Helper\Sleeper $sleeper = null)
* @method mixed columns(array $data, $column_count = null)
* @method mixed clear()
*
* @method CLImate addArt(string $dir)
*/
class CLImate
{
/**
* An instance of the Style class
*
* @var \League\CLImate\Decorator\Style $style
*/
public $style;
/**
* An instance of the Terminal Object Router class
*
* @var \League\CLImate\TerminalObject\Router\Router $router
*/
protected $router;
/**
* An instance of the Settings Manager class
*
* @var \League\CLImate\Settings\Manager $settings
*/
protected $settings;
/**
* An instance of the Argument Manager class
*
* @var \League\CLImate\Argument\Manager $arguments
*/
public $arguments;
/**
* An instance of the Output class
*
* @var \League\CLImate\Util\Output $output
*/
public $output;
/**
* An instance of the Util Factory
*
* @var \League\CLImate\Util\UtilFactory $util
*/
protected $util;
public function __construct()
{
$this->setStyle(new Style());
$this->setRouter(new Router());
$this->setSettingsManager(new SettingsManager());
$this->setOutput(new Output());
$this->setUtil(new UtilFactory());
$this->setArgumentManager(new ArgumentManager());
}
/**
* Set the style property
*
* @param \League\CLImate\Decorator\Style $style
*/
public function setStyle(Style $style)
{
$this->style = $style;
}
/**
* Set the router property
*
* @param \League\CLImate\TerminalObject\Router\Router $router
*/
public function setRouter(Router $router)
{
$this->router = $router;
}
/**
* Set the settings property
*
* @param \League\CLImate\Settings\Manager $manager
*/
public function setSettingsManager(SettingsManager $manager)
{
$this->settings = $manager;
}
/**
* Set the arguments property
*
* @param \League\CLImate\Argument\Manager $manager
*/
public function setArgumentManager(ArgumentManager $manager)
{
$this->arguments = $manager;
}
/**
* Set the output property
*
* @param \League\CLImate\Util\Output $output
*/
public function setOutput(Output $output)
{
$this->output = $output;
}
/**
* Set the util property
*
* @param \League\CLImate\Util\UtilFactory $util
*/
public function setUtil(UtilFactory $util)
{
$this->util = $util;
}
/**
* Extend CLImate with custom methods
*
* @param string|object|array $class
* @param string $key Optional custom key instead of class name
*
* @return \League\CLImate\CLImate
*/
public function extend($class, $key = null)
{
$this->router->addExtension($key, $class);
return $this;
}
/**
* Force ansi support on
*
* @return \League\CLImate\CLImate
*/
public function forceAnsiOn()
{
$this->util->system->forceAnsi();
return $this;
}
/**
* Force ansi support off
*
* @return \League\CLImate\CLImate
*/
public function forceAnsiOff()
{
$this->util->system->forceAnsi(false);
return $this;
}
/**
* Write line to writer once
*
* @param string|array $writer
*
* @return \League\CLImate\CLImate
*/
public function to($writer)
{
$this->output->once($writer);
return $this;
}
/**
* Output the program's usage statement
*
* @param array $argv
*/
public function usage(array $argv = null)
{
return $this->arguments->usage($this, $argv);
}
/**
* Set the program's description
*
* @param string $description
*
* @return \League\CLImate\CLImate
*/
public function description($description)
{
$this->arguments->description($description);
return $this;
}
/**
* they pass in a total, set the total
*
* @param iterable $data Array or any other iterable object
* @param callable $callback
*/
public function each($data, callable $callback = null)
{
if ($data instanceof \Countable || is_array($data)) {
$total = count($data);
if (!$total) {
return;
}
$progress = $this->progress($total);
} else {
$progress = $this->spinner();
}
foreach ($data as $key => $item) {
if ($callback) {
$callback($item, $key);
}
$progress->advance();
}
}
/**
* Check if we have valid output
*
* @param mixed $output
*
* @return boolean
*/
protected function hasOutput($output)
{
if (!empty($output)) {
return true;
}
// Check for type first to avoid errors with objects/arrays/etc
return ((is_string($output) || is_numeric($output)) && strlen($output) > 0);
}
/**
* Search for the method within the string
* and route it if we find one.
*
* @param string $method
* @param string $name
*
* @return string The new string without the executed method.
*/
protected function parseStyleMethod($method, $name)
{
// If the name starts with this method string...
if (substr($name, 0, strlen($method)) == $method) {
// ...remove the method name from the beginning of the string...
$name = substr($name, strlen($method));
// ...and trim off any of those underscores hanging around
$name = ltrim($name, '_');
$this->style->set($method);
}
return $name;
}
/**
* Search for any style methods within the name and apply them
*
* @param string $name
* @param array $method_search
*
* @return string Anything left over after applying styles
*/
protected function applyStyleMethods($name, $method_search = null)
{
// Get all of the possible style attributes
$method_search = $method_search ?: array_keys($this->style->all());
$new_name = $this->searchForStyleMethods($name, $method_search);
// While we still have a name left and we keep finding methods,
// loop through the possibilities
if (strlen($new_name) > 0 && $new_name != $name) {
return $this->applyStyleMethods($new_name, $method_search);
}
return $new_name;
}
/**
* Search for style methods in the current name
*
* @param string $name
* @param array $search
* @return string
*/
protected function searchForStyleMethods($name, $search)
{
// Loop through the possible methods
foreach ($search as $method) {
// See if we found a valid method
$name = $this->parseStyleMethod($method, $name);
}
return $name;
}
/**
* Build up the terminal object and return it
*
* @param string $name
* @param array $arguments
*
* @return object|null
*/
protected function buildTerminalObject($name, $arguments)
{
// Retrieve the parser for the current set of styles
$parser = $this->style->parser($this->util->system);
// Reset the styles
$this->style->reset();
// Execute the terminal object
$this->router->settings($this->settings);
$this->router->parser($parser);
$this->router->output($this->output);
$this->router->util($this->util);
return $this->router->execute($name, $arguments);
}
/**
* Route anything leftover after styles were applied
*
* @param string $name
* @param array $arguments
*
* @return object|null
*/
protected function routeRemainingMethod($name, array $arguments)
{
// If we still have something left, let's figure out what it is
if ($this->router->exists($name)) {
$obj = $this->buildTerminalObject($name, $arguments);
// If something was returned, return it
if (is_object($obj)) {
return $obj;
}
} elseif ($this->settings->exists($name)) {
$this->settings->add($name, reset($arguments));
// Handle passthroughs to the arguments manager.
} else {
// If we can't find it at this point, let's fail gracefully
$this->out(reset($arguments));
}
}
/**
* Magic method for anything called that doesn't exist
*
* @param string $requested_method
* @param array $arguments
*
* @return \League\CLImate\CLImate|\League\CLImate\TerminalObject\Dynamic\DynamicTerminalObject
*
* List of many of the possible method being called here
* documented at the top of this class.
*/
public function __call($requested_method, $arguments)
{
// Apply any style methods that we can find first
$name = $this->applyStyleMethods(Helper::snakeCase($requested_method));
// The first argument is the string|array|object we want to echo out
$output = reset($arguments);
if (strlen($name)) {
// If we have something left, let's try and route it to the appropriate place
if ($result = $this->routeRemainingMethod($name, $arguments)) {
return $result;
}
} elseif ($this->hasOutput($output)) {
// If we have fulfilled all of the requested methods and we have output, output it
$this->out($output);
}
return $this;
}
}