-
Notifications
You must be signed in to change notification settings - Fork 16
/
SymfonyTaskCommand.php
83 lines (73 loc) 路 2.74 KB
/
SymfonyTaskCommand.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
<?php
namespace Castor\Console\Command;
use Castor\Attribute\AsSymfonyTask;
use Castor\Console\Input\Input;
use Symfony\Component\Console\Command\Command;
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 Symfony\Component\Process\Process;
/** @internal */
class SymfonyTaskCommand extends Command
{
private const OPTIONS_FILTERS = [
'--help',
'--quiet',
'--verbose',
'--version',
'--ansi',
'--no-ansi',
'--no-interaction',
];
/**
* @param mixed[] $definition
*/
public function __construct(
public readonly AsSymfonyTask $taskAttribute,
public readonly \ReflectionClass $class,
public readonly array $definition,
) {
parent::__construct($taskAttribute->name);
}
protected function configure(): void
{
$this->setAliases($this->definition['aliases'] ?? []);
$this->setDescription($this->definition['description']);
$this->setHelp($this->definition['help']);
$this->setHidden($this->definition['hidden']);
foreach ($this->definition['definition']['arguments'] as $argument) {
$this->addArgument(
$argument['name'],
($argument['is_required'] ? InputArgument::REQUIRED : InputArgument::OPTIONAL)
| ($argument['is_array'] ? InputArgument::IS_ARRAY : 0),
$argument['description'],
$argument['default'],
);
}
foreach ($this->definition['definition']['options'] as $option) {
if (\in_array($option['name'], self::OPTIONS_FILTERS, true)) {
continue;
}
$this->addOption(
$option['name'],
$option['shortcut'],
($option['accept_value'] ? 0 : InputOption::VALUE_NONE)
| ($option['is_value_required'] ? InputOption::VALUE_REQUIRED : InputOption::VALUE_OPTIONAL)
| ($option['is_multiple'] ? InputOption::VALUE_IS_ARRAY : 0),
$option['description'],
$option['accept_value'] ? $option['default'] : null,
);
}
}
/**
* @param Input $input
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$extra = array_filter($input->getRawParameters(), fn ($item) => $item !== $this->taskAttribute->name);
$p = new Process([...$this->taskAttribute->console, $this->taskAttribute->originalName, ...$extra]);
$p->run(fn ($type, $bytes) => print ($bytes));
return $p->getExitCode() ?? 0;
}
}