-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleCommand.php
228 lines (201 loc) · 6.69 KB
/
ConsoleCommand.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
<?php
declare(strict_types=1);
namespace Codefy\Framework\Console;
use Codefy\Framework\Application;
use Qubus\Exception\Data\TypeException;
use ReflectionException;
use ReflectionMethod;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use function count;
use function method_exists;
abstract class ConsoleCommand extends SymfonyCommand
{
protected string $name = '';
protected string $description = '';
protected string $help = '';
/* Refers to name, type, description argument. */
protected array $args = [];
/* Refers to name, shortcut, type, description and default. */
protected array $options = [];
protected InputInterface $input;
protected OutputInterface $output;
public function __construct(protected Application $codefy)
{
$this->setName(name: $this->name)
->setDescription(description: $this->description)
->setHelp(help: $this->help);
parent::__construct();
}
/**
* Configure commands.
*
* @return void
*/
protected function configure(): void
{
try {
$this->setArguments();
$this->setOptions();
} catch (TypeException) {
}
}
/**
* @throws ReflectionException|TypeException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->input = $input;
$this->output = $output;
$method = method_exists(object_or_class: $this, method: 'handle') ? 'handle' : '__invoke';
$classParameters = [];
$parameters = new ReflectionMethod(objectOrMethod: $this, method: $method);
foreach ($parameters->getParameters() as $arg) {
$classParameters[] = $this->codefy->make(name: $arg->getType()->getName());
}
if ($parameters->getNumberOfParameters() > 0) {
return (int) $this->codefy->execute(callableOrMethodStr: [$this, $method], args: $classParameters);
}
return (int) $this->codefy->execute(callableOrMethodStr: [$this, $method]);
}
/**
* Returns the argument value for the given argument name.
*
* @param string|null $key
* @return mixed
*/
protected function getArgument(?string $key = null): mixed
{
return $this->input->getArgument(name: $key) ?? '';
}
/**
* Returns the option value for the given option name.
*
* @param string|null $key
* @return bool|string|string[]
*/
protected function getOptions(?string $key = null): mixed
{
return $this->input->getOption(name: $key) ?? '';
}
/**
* Outputs the string to the console without any tag.
*
* @param string $string
* @return mixed
*/
protected function terminalRaw(string $string): mixed
{
return $this->output->writeln(messages: $string);
}
/**
* Output to the terminal wrap in info tags.
*
* @param string $string
* @return string
*/
protected function terminalInfo(string $string): mixed
{
return $this->output->writeln(messages: '<info>' . $string . '</info>');
}
/**
* Output to the terminal wrap in comment tags.
*
* @param string $string
* @return string
*/
protected function terminalComment(string $string): mixed
{
return $this->output->writeln(messages: '<comment>' . $string . '</comment>');
}
/**
* Output to the terminal wrap in question tags.
*
* @param string $string
* @return string
*/
protected function terminalQuestion(string $string): mixed
{
return $this->output->writeln(messages: '<question>' . $string . '</question>');
}
/**
* Output to the terminal wrap in error tags.
*
* @param string $string
* @return string
*/
protected function terminalError(string $string): mixed
{
return $this->output->writeln(messages: '<error>' . $string . '</error>');
}
/**
* $arg[0] = argument name, $arg[1] = argument type and $arg[2] = argument description.
*
* @return ConsoleCommand|bool
* @throws TypeException
*/
private function setArguments(): ConsoleCommand|bool
{
if (count($this->args) <= 0) {
return false;
}
foreach ($this->args as $arg) {
match ($arg[1]) { /* match based on the argument type */
'required' => $this->addArgument(name: $arg[0], mode: InputArgument::REQUIRED, description: $arg[2]),
'optional' => $this->addArgument(name: $arg[0], mode: InputArgument::OPTIONAL, description: $arg[2]),
'array' => $this->addArgument(name: $arg[0], mode: InputArgument::IS_ARRAY, description: $arg[2]),
default => throw new TypeException(message: 'Invalid input argument passed.')
};
}
return true;
}
/**
* @return bool|ConsoleCommand
* @throws TypeException
*/
private function setOptions(): bool|ConsoleCommand
{
if (count($this->options) <= 0) {
return false;
}
foreach ($this->options as $option) {
match ($option[2]) {
'none' => $this->addOption(
name: $option[0],
shortcut: $option[1],
mode: InputOption::VALUE_NONE,
description: $option[3]
),
'required' => $this->addOption(
name: $option[0],
shortcut: $option[1],
mode: InputOption::VALUE_REQUIRED,
description: $option[3]
),
'optional' => $this->addOption(
name: $option[0],
shortcut: $option[1],
mode: InputOption::VALUE_OPTIONAL,
description: $option[3]
),
'array' => $this->addOption(
name: $option[0],
shortcut: $option[1],
mode: InputOption::VALUE_IS_ARRAY,
description: $option[3]
),
'negatable' => $this->addOption(
name: $option[0],
shortcut: $option[1],
mode: InputOption::VALUE_NEGATABLE,
description: $option[3]
),
default => throw new TypeException(message: 'Invalid input argument passed.')
};
}
return true;
}
}