|
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 Webino; |
|
14
|
|
|
use WebinoAppLib\Event\ConsoleEvent; |
|
15
|
|
|
use WebinoConfigLib\Feature\Route\ConsoleRoute; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ConsoleDefault |
|
19
|
|
|
*/ |
|
20
|
|
|
class ConsoleDefault extends AbstractConsoleCommand |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
protected function init() |
|
26
|
|
|
{ |
|
27
|
|
|
parent::init(); |
|
28
|
|
|
$this->listenConsole(ConsoleEvent::ROUTE_MATCH, 'showSystemBanner'); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
|
|
public function configure(ConsoleRoute $console) |
|
35
|
|
|
{ |
|
36
|
|
|
$console->setType('catchall'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param ConsoleEvent $event |
|
41
|
|
|
*/ |
|
42
|
|
|
public function showSystemBanner(ConsoleEvent $event) |
|
43
|
|
|
{ |
|
44
|
|
|
$cli = $event->getCli(); |
|
45
|
|
|
|
|
46
|
|
|
$cli |
|
47
|
|
|
->br() |
|
48
|
|
|
->white()->greenBg()->inline(' Webino™ ') |
|
49
|
|
|
->inline(' version ') |
|
50
|
|
|
->yellow()->out(Webino::VERSION) |
|
51
|
|
|
->br(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param ConsoleEvent $event |
|
56
|
|
|
*/ |
|
57
|
|
|
public function handle(ConsoleEvent $event) |
|
58
|
|
|
{ |
|
59
|
|
|
$cfg = $event->getApp()->getConfig('console'); |
|
60
|
|
|
if (empty($cfg->router->routes)) { |
|
61
|
|
|
return; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$routes = $cfg->router->routes; |
|
65
|
|
|
$maxLen = 0; |
|
66
|
|
|
$newRoutes = []; |
|
67
|
|
|
|
|
68
|
|
|
foreach ($routes as $route) { |
|
69
|
|
|
if ('catchall' === $route->type) { |
|
70
|
|
|
continue; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$routeLen = strlen($this->resolveRouteCommand($route->options->route)); |
|
74
|
|
|
$routeLen > $maxLen and $maxLen = $routeLen; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$newRoutes[$route->options->route] = $route; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$cli = $event->getCli(); |
|
80
|
|
|
|
|
81
|
|
|
ksort($newRoutes); |
|
82
|
|
|
foreach ($newRoutes as $route) { |
|
83
|
|
|
|
|
84
|
|
|
$command = $this->resolveRouteCommand($route->options->route); |
|
85
|
|
|
$title = isset($route->options->defaults->title) |
|
86
|
|
|
? $route->options->defaults->title |
|
87
|
|
|
: null; |
|
88
|
|
|
|
|
89
|
|
|
$cli->bold()->green()->inline($command); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
if (empty($title)) { |
|
92
|
|
|
$cli->br(); |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$space = str_repeat(' ', $maxLen + 10 - strlen($command)); |
|
97
|
|
|
$cli->out($space . ' - ' . $title); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$cli->br(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return console route command name |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $route |
|
107
|
|
|
* @return string |
|
108
|
|
|
*/ |
|
109
|
|
|
private function resolveRouteCommand($route) |
|
110
|
|
|
{ |
|
111
|
|
|
$matches = []; |
|
112
|
|
|
preg_match('~^[a-zA-Z:_-]+~', $route, $matches); |
|
113
|
|
|
return current($matches); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
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
dieintroduces 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 withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.