-
Notifications
You must be signed in to change notification settings - Fork 10
/
CommandTaskRunner.php
94 lines (77 loc) · 2.69 KB
/
CommandTaskRunner.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
<?php
declare(strict_types=1);
namespace SchedulerBundle\Runner;
use SchedulerBundle\Exception\InvalidArgumentException;
use SchedulerBundle\Worker\WorkerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\BufferedOutput;
use SchedulerBundle\Task\CommandTask;
use SchedulerBundle\Task\Output;
use SchedulerBundle\Task\TaskInterface;
use Throwable;
use function implode;
use function is_int;
use function sprintf;
/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class CommandTaskRunner implements RunnerInterface
{
public function __construct(private Application $application)
{
}
/**
* {@inheritdoc}
*/
public function run(TaskInterface $task, WorkerInterface $worker): Output
{
if (!$task instanceof CommandTask) {
return new Output($task, null, Output::ERROR);
}
$this->application->setCatchExceptions(false);
$this->application->setAutoExit(false);
$input = $this->buildInput($task);
$bufferedOutput = new BufferedOutput();
try {
$statusCode = $this->application->run($input, $bufferedOutput);
if (Command::FAILURE === $statusCode) {
return new Output($task, $bufferedOutput->fetch(), Output::ERROR);
}
} catch (Throwable) {
return new Output($task, $bufferedOutput->fetch(), Output::ERROR);
}
return new Output($task, $bufferedOutput->fetch());
}
/**
* {@inheritdoc}
*/
public function support(TaskInterface $task): bool
{
return $task instanceof CommandTask;
}
private function buildInput(CommandTask $commandTask): StringInput
{
$command = $this->application->find($commandTask->getCommand());
if (null === $name = $command->getName()) {
throw new InvalidArgumentException('The command name must be set');
}
return new StringInput(sprintf('%s %s %s', $name, implode(' ', $commandTask->getArguments()), implode(' ', $this->buildOptions($commandTask))));
}
/**
* @return array<int, mixed>
*/
private function buildOptions(CommandTask $commandTask): array
{
$options = [];
foreach ($commandTask->getOptions() as $key => $option) {
if (is_int($key)) {
$options[] = str_starts_with($option, '--') ? $option : sprintf('--%s', $option);
continue;
}
$options[] = sprintf('%s %s', str_starts_with($key, '--') ? $key : sprintf('--%s', $key), $option);
}
return $options;
}
}