1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.8.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Console; |
16
|
|
|
|
17
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
21
|
|
|
use Symfony\Component\Console\Input\InputOption; |
22
|
|
|
use Symfony\Component\Console\Command\Command; |
23
|
|
|
use Quantum\Environment\Environment; |
24
|
|
|
use Quantum\Loader\Setup; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class QtCommand |
28
|
|
|
* @package Quantum\Console |
29
|
|
|
*/ |
30
|
|
|
abstract class QtCommand extends Command implements CommandInterface |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Console command name |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $name; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Console command description. |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $description; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Console command help text |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
protected $help; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Console command input arguments |
53
|
|
|
* @var array |
54
|
|
|
* @example ['name', 'type', 'description'] |
55
|
|
|
*/ |
56
|
|
|
protected $args = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Console command options |
60
|
|
|
* @var array |
61
|
|
|
* @example ['name', 'shortcut', 'type', 'description', 'default'] |
62
|
|
|
*/ |
63
|
|
|
protected $options = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var InputInterface |
67
|
|
|
*/ |
68
|
|
|
protected $input; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var OutputInterface; |
72
|
|
|
*/ |
73
|
|
|
protected $output; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* QtCommand constructor. |
77
|
|
|
*/ |
78
|
|
|
public function __construct() |
79
|
|
|
{ |
80
|
|
|
parent::__construct($this->name); |
81
|
|
|
|
82
|
|
|
$this->setDescription($this->description); |
83
|
|
|
|
84
|
|
|
$this->setHelp($this->help); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Configures the current command. |
89
|
|
|
*/ |
90
|
|
|
protected function configure() |
91
|
|
|
{ |
92
|
|
|
$this->setArguments(); |
93
|
|
|
$this->setOptions(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Executes the current command. |
98
|
|
|
* @param InputInterface $input |
99
|
|
|
* @param OutputInterface $output |
100
|
|
|
* @throws \Quantum\Exceptions\DiException |
101
|
|
|
* @throws \Quantum\Exceptions\EnvException |
102
|
|
|
* @throws \ReflectionException |
103
|
|
|
*/ |
104
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
105
|
|
|
{ |
106
|
|
|
if ($this->getName() != 'core:env') { |
107
|
|
|
Environment::getInstance()->load(new Setup('config', 'env')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->input = $input; |
111
|
|
|
$this->output = $output; |
112
|
|
|
$this->exec(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns the argument value for a given argument name. |
117
|
|
|
* @param string|null $key |
118
|
|
|
* @return mixed|string |
119
|
|
|
*/ |
120
|
|
|
protected function getArgument(string $key = null) |
121
|
|
|
{ |
122
|
|
|
return $this->input->getArgument($key) ?? ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the option value for a given option name. |
127
|
|
|
* @param string|null $key |
128
|
|
|
* @return mixed|string |
129
|
|
|
*/ |
130
|
|
|
protected function getOption(string $key = null) |
131
|
|
|
{ |
132
|
|
|
return $this->input->getOption($key) ?? ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Outputs the string to console |
137
|
|
|
* @param string $message |
138
|
|
|
*/ |
139
|
|
|
public function output(string $message) |
140
|
|
|
{ |
141
|
|
|
$this->output->writeln($message); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Outputs the string to console as info |
146
|
|
|
* @param string $message |
147
|
|
|
*/ |
148
|
|
|
protected function info(string $message) |
149
|
|
|
{ |
150
|
|
|
$this->output->writeln("<info>$message</info>"); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Outputs the string to console as comment |
155
|
|
|
* @param string $message |
156
|
|
|
*/ |
157
|
|
|
protected function comment(string $message) |
158
|
|
|
{ |
159
|
|
|
$this->output->writeln("<comment>$message</comment>"); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Outputs the string to console as question |
164
|
|
|
* @param string $message |
165
|
|
|
*/ |
166
|
|
|
protected function question(string $message) |
167
|
|
|
{ |
168
|
|
|
$this->output->writeln("<question>$message</question>"); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Asks confirmation |
173
|
|
|
* @param string $message |
174
|
|
|
* @return bool |
175
|
|
|
*/ |
176
|
|
|
public function confirm(string $message): bool |
177
|
|
|
{ |
178
|
|
|
$helper = $this->getHelper('question'); |
179
|
|
|
$question = new ConfirmationQuestion($message . ' ', false); |
180
|
|
|
|
181
|
|
|
if ($helper->ask($this->input, $this->output, $question)) { |
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Outputs the string to console as error |
190
|
|
|
* @param string $message |
191
|
|
|
*/ |
192
|
|
|
protected function error(string $message) |
193
|
|
|
{ |
194
|
|
|
$this->output->writeln("<error>$message</error>"); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets command arguments |
199
|
|
|
*/ |
200
|
|
|
private function setArguments() |
201
|
|
|
{ |
202
|
|
|
foreach ($this->args as $arg) { |
203
|
|
|
switch ($arg[1]) { |
204
|
|
|
case 'required': |
205
|
|
|
$this->addArgument($arg[0], InputArgument::REQUIRED, $arg[2]); |
206
|
|
|
break; |
207
|
|
|
case 'optional': |
208
|
|
|
$this->addArgument($arg[0], InputArgument::OPTIONAL, $arg[2]); |
209
|
|
|
break; |
210
|
|
|
case 'array': |
211
|
|
|
$this->addArgument($arg[0], InputArgument::IS_ARRAY, $arg[2]); |
212
|
|
|
break; |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Sets command options |
219
|
|
|
*/ |
220
|
|
|
private function setOptions() |
221
|
|
|
{ |
222
|
|
|
foreach ($this->options as $option) { |
223
|
|
|
switch ($option[2]) { |
224
|
|
|
case 'none': |
225
|
|
|
$this->addOption($option[0], $option[1], InputOption::VALUE_NONE, $option[3]); |
226
|
|
|
break; |
227
|
|
|
case 'required': |
228
|
|
|
$this->addOption($option[0], $option[1], InputOption::VALUE_REQUIRED, $option[3], $option[4] ?? ''); |
229
|
|
|
break; |
230
|
|
|
case 'optional': |
231
|
|
|
$this->addOption($option[0], $option[1], InputOption::VALUE_OPTIONAL, $option[3], $option[4] ?? ''); |
232
|
|
|
break; |
233
|
|
|
case 'array': |
234
|
|
|
$this->addOption($option[0], $option[1], InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, $option[3], $option[4] ?? ''); |
235
|
|
|
break; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|