|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Task; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Application; |
|
6
|
|
|
use Symfony\Component\Console\Command\Command; |
|
7
|
|
|
use Symfony\Component\Console\Exception\CommandNotFoundException; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Input\StringInput; |
|
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Kevin Bond <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
final class CommandTask extends Task |
|
16
|
|
|
{ |
|
17
|
|
|
private $name; |
|
18
|
|
|
private $arguments; |
|
19
|
|
|
|
|
20
|
28 |
|
public function __construct(string $name, string ...$arguments) |
|
21
|
|
|
{ |
|
22
|
28 |
|
$parts = \explode(' ', $name, 2); |
|
23
|
28 |
|
$name = $parts[0]; |
|
24
|
|
|
|
|
25
|
28 |
|
if (2 === \count($parts)) { |
|
26
|
8 |
|
$arguments = \array_merge([$parts[1]], $arguments); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
28 |
|
$this->name = $name; |
|
30
|
|
|
|
|
31
|
28 |
|
if (!empty($arguments)) { |
|
32
|
11 |
|
$this->arguments(...$arguments); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
28 |
|
parent::__construct($this->name); |
|
36
|
28 |
|
} |
|
37
|
|
|
|
|
38
|
12 |
|
public function arguments(string $argument, string ...$arguments): self |
|
39
|
|
|
{ |
|
40
|
12 |
|
$this->arguments = \implode(' ', \array_merge([$argument], $arguments)); |
|
41
|
|
|
|
|
42
|
12 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
9 |
|
public function getArguments(): string |
|
46
|
|
|
{ |
|
47
|
9 |
|
return (string) $this->arguments; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function getContext(): array |
|
51
|
|
|
{ |
|
52
|
|
|
return [ |
|
53
|
2 |
|
'Command Name' => $this->name, |
|
54
|
2 |
|
'Command Arguments' => $this->getArguments() ?: '(none)', |
|
55
|
|
|
]; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
6 |
|
public function createCommandInput(Application $application): InputInterface |
|
59
|
|
|
{ |
|
60
|
6 |
|
return new StringInput(\implode(' ', \array_filter([ |
|
61
|
6 |
|
$this->createCommand($application)->getName(), |
|
62
|
5 |
|
$this->getArguments(), |
|
63
|
|
|
]))); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
public function createCommand(Application $application): Command |
|
67
|
|
|
{ |
|
68
|
10 |
|
$registeredCommands = $application->all(); |
|
69
|
|
|
|
|
70
|
10 |
|
if (isset($registeredCommands[$this->name])) { |
|
71
|
5 |
|
return $registeredCommands[$this->name]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
5 |
|
foreach ($registeredCommands as $command) { |
|
75
|
5 |
|
if ($this->name === \get_class($command)) { |
|
76
|
1 |
|
return $command; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
4 |
|
throw new CommandNotFoundException("Command \"{$this->name}\" not registered."); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|