-
Notifications
You must be signed in to change notification settings - Fork 16
/
TaskCommand.php
237 lines (195 loc) 路 8.49 KB
/
TaskCommand.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
229
230
231
232
233
234
235
236
237
<?php
namespace Castor\Console\Command;
use Castor\Attribute\AsArgument;
use Castor\Attribute\AsCommandArgument;
use Castor\Attribute\AsOption;
use Castor\Attribute\AsRawTokens;
use Castor\Attribute\AsTask;
use Castor\Console\Application;
use Castor\Console\Input\Input;
use Castor\Event\AfterExecuteTaskEvent;
use Castor\Event\BeforeExecuteTaskEvent;
use Castor\EventDispatcher;
use Castor\Exception\FunctionConfigurationException;
use Castor\ExpressionLanguage;
use Castor\SluggerHelper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\SignalableCommandInterface;
use Symfony\Component\Console\Exception\LogicException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/** @internal */
class TaskCommand extends Command implements SignalableCommandInterface
{
/**
* @var array<string, string>
*/
private array $argumentsMap = [];
public function __construct(
public readonly AsTask $taskAttribute,
public readonly \ReflectionFunction $function,
private readonly EventDispatcher $eventDispatcher,
private readonly ExpressionLanguage $expressionLanguage,
) {
$this->setDescription($taskAttribute->description);
$this->setAliases($taskAttribute->aliases);
$taskName = $taskAttribute->name;
if ($taskAttribute->namespace) {
$taskName = $taskAttribute->namespace . ':' . $taskName;
}
$this->setProcessTitle(Application::NAME . ':' . $taskName);
parent::__construct($taskName);
}
/**
* @return array<int>
*/
public function getSubscribedSignals(): array
{
return array_keys($this->taskAttribute->onSignals);
}
public function handleSignal(int $signal): int|false
{
if (!\array_key_exists($signal, $this->taskAttribute->onSignals)) {
return false;
}
return $this->taskAttribute->onSignals[$signal]($signal);
}
public function isEnabled(): bool
{
if (\is_bool($this->taskAttribute->enabled)) {
return $this->taskAttribute->enabled;
}
return $this->expressionLanguage->evaluate($this->taskAttribute->enabled);
}
protected function configure(): void
{
if ($this->taskAttribute->ignoreValidationErrors) {
$this->ignoreValidationErrors();
}
foreach ($this->function->getParameters() as $parameter) {
if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
continue;
}
$taskArgumentAttribute = $parameter->getAttributes(AsCommandArgument::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null;
if ($taskArgumentAttribute) {
$taskArgumentAttribute = $taskArgumentAttribute->newInstance();
} elseif ($parameter->isOptional()) {
$taskArgumentAttribute = new AsOption();
} else {
$taskArgumentAttribute = new AsArgument();
}
$name = $this->setParameterName($parameter, $taskArgumentAttribute->name);
try {
if ($taskArgumentAttribute instanceof AsArgument) {
if ($parameter->isOptional()) {
$mode = InputArgument::OPTIONAL;
} else {
$mode = InputArgument::REQUIRED;
}
if (($type = $parameter->getType()) instanceof \ReflectionNamedType && 'array' === $type->getName()) {
$mode |= InputArgument::IS_ARRAY;
}
$this->addArgument(
$name,
$mode,
$taskArgumentAttribute->description,
$parameter->isOptional() ? $parameter->getDefaultValue() : null,
$taskArgumentAttribute->suggestedValues,
);
} elseif ($taskArgumentAttribute instanceof AsOption) {
if ('verbose' === $name) {
throw new FunctionConfigurationException('You cannot re-define a "verbose" option. But you can use "output()->isVerbose()" in your code instead.', $this->function);
}
$mode = $taskArgumentAttribute->mode;
$defaultValue = $parameter->isOptional() ? $parameter->getDefaultValue() : null;
if (!$mode) {
if (($type = $parameter->getType()) instanceof \ReflectionNamedType && 'bool' === $type->getName()) {
$mode |= InputOption::VALUE_NONE;
// Fix Symfony limitation: "Cannot set a default value when using InputOption::VALUE_NONE mode.
$defaultValue = null;
} else {
$mode = InputOption::VALUE_OPTIONAL;
}
}
$this->addOption(
$name,
$taskArgumentAttribute->shortcut,
$mode,
$taskArgumentAttribute->description,
$defaultValue,
$taskArgumentAttribute->suggestedValues,
);
}
} catch (LogicException $e) {
throw new FunctionConfigurationException(sprintf('The argument "%s" cannot be configured: "%s".', $parameter->getName(), $e->getMessage()), $this->function, $e);
}
}
}
/**
* @param Input $input
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$args = [];
foreach ($this->function->getParameters() as $parameter) {
if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) {
$parameters = [];
$keep = false;
foreach ($input->getRawTokens() as $value) {
if ($value === $input->getFirstArgument()) {
$keep = true;
continue;
}
if ($keep) {
$parameters[] = $value;
}
}
$args[] = $parameters;
continue;
}
$name = $this->getParameterName($parameter);
if ($input->hasArgument($name)) {
$args[] = $input->getArgument($name);
continue;
}
$args[] = $input->getOption($name);
}
try {
$function = $this->function->getClosure();
if (!\is_callable($function)) {
throw new \LogicException('The function is not a callable.');
}
$this->eventDispatcher->dispatch(new BeforeExecuteTaskEvent($this));
$result = $function(...$args);
$this->eventDispatcher->dispatch(new AfterExecuteTaskEvent($this, $result));
} catch (\Error $e) {
$castorFunctions = array_filter(get_defined_functions()['user'], fn (string $functionName) => str_starts_with($functionName, 'castor\\'));
$castorFunctionsWithoutNamespace = array_map(fn (string $functionName) => substr($functionName, \strlen('castor\\')), $castorFunctions);
foreach ($castorFunctionsWithoutNamespace as $function) {
if ("Call to undefined function {$function}()" === $e->getMessage()) {
throw new \LogicException(sprintf('Call to undefined function %s(). Did you forget to import it? Try to add "use function Castor\%s;" in top of "%s" file.', $function, $function, $this->function->getFileName()));
}
}
throw $e;
}
if (null === $result) {
return Command::SUCCESS;
}
if (\is_int($result)) {
return $result;
}
return Command::SUCCESS;
}
private function setParameterName(\ReflectionParameter $parameter, ?string $name): string
{
$name = SluggerHelper::slug($name ?? $parameter->getName());
$this->argumentsMap[$parameter->getName()] = $name;
return $name;
}
private function getParameterName(\ReflectionParameter $parameter): string
{
return $this->argumentsMap[$parameter->getName()];
}
}