1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the alphaz Framework. |
5
|
|
|
* |
6
|
|
|
* @author Muhammad Umer Farooq (Malik) <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/alphazframework/framework |
9
|
|
|
* |
10
|
|
|
* @author Muhammad Umer Farooq <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @author-profile https://www.facebook.com/Muhammadumerfarooq01/ |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* @license MIT |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace alphaz\Console; |
21
|
|
|
|
22
|
|
|
use alphaz\Console\Commands as InternalCommands; |
23
|
|
|
use alphaz\Container\Container; |
24
|
|
|
use alphaz\Data\Arrays; |
25
|
|
|
|
26
|
|
|
class Console |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Instance of container. |
30
|
|
|
* |
31
|
|
|
* @since 1.0.0 |
32
|
|
|
* |
33
|
|
|
* @var \alphaz\Container\Container |
34
|
|
|
*/ |
35
|
|
|
private $container; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Commanads. |
39
|
|
|
* |
40
|
|
|
* @since 1.0.0 |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $commands = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Create a new console instance. |
48
|
|
|
* |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
$this->container = new Container(); |
54
|
|
|
$internalCommands = (new InternalCommands())->getCommands(); |
55
|
|
|
$externalCommands = []; |
56
|
|
|
if (class_exists("\Config\Commands")) { |
57
|
|
|
$externalCommands = (new \Config\Commands())->getCommands(); |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->commands = array_merge($internalCommands, $externalCommands); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Parse the flags from command. |
65
|
|
|
* |
66
|
|
|
* @param array $flags Raw flags. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function parseFlags($flags): array |
71
|
|
|
{ |
72
|
|
|
$params = []; |
73
|
|
|
$f = explode(',', $flags); |
|
|
|
|
74
|
|
|
if (Arrays::isReallyArray($f)) { |
75
|
|
|
foreach ($f as $flag => $fs) { |
76
|
|
|
$param = explode('=', $fs); |
77
|
|
|
if (isset($param[1])) { |
78
|
|
|
$params[$param[0]] = $param[1]; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} else { |
82
|
|
|
$param = explode('=', $flags); |
83
|
|
|
if (isset($param[1])) { |
84
|
|
|
$params[$param[0]] = $param[1]; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $params; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get all commands. |
93
|
|
|
* |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
|
|
public function getCommands(): array |
97
|
|
|
{ |
98
|
|
|
return $this->commands; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Run the alphaz console. |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
public function run($param): void |
107
|
|
|
{ |
108
|
|
|
// registering the commands to container. |
109
|
|
|
foreach ($this->commands as $command) { |
110
|
|
|
$this->container->register([$command[1], $command[0]], new $command[1]()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$sign = isset($param[1]) ? $param[1] : 'list'; |
114
|
|
|
$output = new Output(); |
115
|
|
|
$input = new Input(); |
116
|
|
|
if ($this->container->has($sign)) { |
117
|
|
|
$cmd = $this->container->get($sign); |
118
|
|
|
|
119
|
|
|
// default. |
120
|
|
|
if (!isset($param[2])) { |
121
|
|
|
if (count($cmd->getFlags()) > 0) { |
122
|
|
|
$output->error('You must provide the flags'); |
123
|
|
|
$output->error('For Help, php alphaz '.$cmd->getSign().' -h'); |
124
|
|
|
exit; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
$cmd->handle($output, $input); |
127
|
|
|
} |
128
|
|
|
// flag for quite |
129
|
|
|
if (isset($param[2]) && strtolower($param[2]) == '-q') { |
130
|
|
|
$cmd->handle($output->quiet(), $input); |
131
|
|
|
} |
132
|
|
|
if (isset($param[2]) && isset($param[3]) && strtolower($param[2]) == '-p') { |
133
|
|
|
$params = $this->parseFlags($param[3]); |
134
|
|
|
$command_flags = $cmd->getFlags(); |
135
|
|
|
// get keys from $params. |
136
|
|
|
$keys = array_keys($params); |
137
|
|
|
|
138
|
|
|
// check if the keys are in the command flags (check if extra flag passed). |
139
|
|
|
foreach ($keys as $key => $value) { |
140
|
|
|
if (!in_array($value, $command_flags)) { |
141
|
|
|
$output->error("Invalid flag: $value"); |
142
|
|
|
exit; |
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// check the keys should be in command flags. |
147
|
|
|
foreach ($command_flags as $command_flag) { |
148
|
|
|
if (!in_array($command_flag, $keys)) { |
149
|
|
|
$output->error("Missing flag: $command_flag"); |
150
|
|
|
exit; |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$cmd->handle($output, $input, $params); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// flag for help |
158
|
|
|
if (isset($param[2]) && strtolower($param[2]) == '-h') { |
159
|
|
|
$args = $cmd->getFlags(); |
160
|
|
|
$output->write('<yellow>Description:</yellow>', true); |
161
|
|
|
$output->write("<blue>\t".$cmd->getDescription().'</blue>', true); |
162
|
|
|
$output->write("\n<yellow>Usage:</yellow>", true); |
163
|
|
|
$output->write("<blue>\t".$cmd->getSign().'</blue>', true); |
164
|
|
|
if (count($args) > 0) { |
165
|
|
|
$output->write("\n<yellow>Arguments:</yellow>", true); |
166
|
|
|
$output->write("<blue>\t".implode(',', $args).'</blue>', true); |
167
|
|
|
} |
168
|
|
|
$output->write("\n<yellow>Options:</yellow>", true); |
169
|
|
|
$output->write('<green>-h, --help</green>'); |
170
|
|
|
$output->write("<blue>\tDisplay this help message</blue>", true); |
171
|
|
|
$output->write('<green>--q, --quiet</green>'); |
172
|
|
|
$output->write("<blue>\tDo not output any message</blue>", true); |
173
|
|
|
} |
174
|
|
|
} else { |
175
|
|
|
$output->error("Sorry, the given command ${sign} not found")->exit(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths