1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoAppLib\Console; |
12
|
|
|
|
13
|
|
|
use WebinoAppLib\Event\ConsoleEvent; |
14
|
|
|
use WebinoConfigLib\Feature\Route\ConsoleRoute; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ConsoleHelp |
18
|
|
|
*/ |
19
|
|
|
class ConsoleHelp extends AbstractConsoleCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function configure(ConsoleRoute $route) |
25
|
|
|
{ |
26
|
|
|
$route |
27
|
|
|
->setPath('help [<command>]') |
28
|
|
|
->setTitle('Show command description'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param ConsoleEvent $event |
33
|
|
|
*/ |
34
|
|
|
public function handle(ConsoleEvent $event) |
35
|
|
|
{ |
36
|
|
|
$cfg = $event->getApp()->getConfig('console'); |
37
|
|
|
if (empty($cfg->router->routes)) { |
38
|
|
|
return; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$routes = $cfg->router->routes; |
42
|
|
|
$requiredCommand = $event->getParam('command', 'help'); |
43
|
|
|
|
44
|
|
|
// find route |
45
|
|
|
$route = null; |
46
|
|
|
foreach ($routes as $item) { |
47
|
|
|
if (!empty($item->options) && 0 === strpos($item->options->route, $requiredCommand)) { |
48
|
|
|
$route = $item; |
49
|
|
|
break; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if (empty($route)) { |
54
|
|
|
// command not found |
55
|
|
|
$event->getCli()->draw('404')->br(); |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$title = (string) $route->options->defaults->title ?? null; |
60
|
|
|
$description = (string) $route->options->defaults->description ?? null; |
61
|
|
|
$argumentsDescription = (array) $route->options->defaults->argumentsDescription ?? []; |
62
|
|
|
$optionsDescription = (array) $route->options->defaults->optionsDescription ?? []; |
63
|
|
|
|
64
|
|
|
$newArgumentsDescription = []; |
65
|
|
|
foreach ($argumentsDescription as $key => $value) { |
66
|
|
|
$newArgumentsDescription[" <green>$key</green>"] = '- ' . $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$newOptionsDescription = []; |
70
|
|
|
foreach ($optionsDescription as $key => $value) { |
71
|
|
|
$newOptionsDescription[" <green>--$key</green>"] = '- ' . $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$cli = $event->getCli(); |
75
|
|
|
|
76
|
|
|
$cli |
77
|
|
|
->invert()->bold(" $title ")->br() |
78
|
|
|
->yellow()->bold('Usage:') |
79
|
|
|
->white()->blackBg(" {$route->options->route} ")->br(); |
80
|
|
|
|
81
|
|
|
$description |
82
|
|
|
and $cli |
|
|
|
|
83
|
|
|
->yellow()->bold('Description:') |
84
|
|
|
->out(' ' . str_replace(PHP_EOL, PHP_EOL . ' ', $description))->br(); |
85
|
|
|
|
86
|
|
|
$newArgumentsDescription |
87
|
|
|
and $cli |
|
|
|
|
88
|
|
|
->yellow()->bold('Arguments:') |
89
|
|
|
->columns($newArgumentsDescription)->br(); |
90
|
|
|
|
91
|
|
|
$newOptionsDescription |
92
|
|
|
and $cli |
|
|
|
|
93
|
|
|
->yellow()->bold('Options:') |
94
|
|
|
->columns($newOptionsDescription)->br(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.