1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Task; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Messenger\Envelope; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kevin Bond <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @implements \IteratorAggregate<Task> |
13
|
|
|
*/ |
14
|
|
|
final class CompoundTask extends Task implements \IteratorAggregate |
15
|
6 |
|
{ |
16
|
|
|
/** @var Task[] */ |
17
|
6 |
|
private $tasks = []; |
18
|
6 |
|
|
19
|
|
|
public function __construct() |
20
|
6 |
|
{ |
21
|
|
|
parent::__construct('compound task'); |
22
|
6 |
|
} |
23
|
1 |
|
|
24
|
|
|
public function add(Task $task): self |
25
|
|
|
{ |
26
|
5 |
|
if ($task instanceof self) { |
27
|
|
|
throw new \LogicException('Cannot nest compound tasks.'); |
28
|
5 |
|
} |
29
|
|
|
|
30
|
|
|
$this->tasks[] = $task; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
2 |
|
* @see CommandTask::__construct() |
37
|
|
|
* |
38
|
2 |
|
* @param string|null $description optional description |
39
|
|
|
*/ |
40
|
|
|
public function addCommand(string $name, array $arguments = [], ?string $description = null): self |
41
|
|
|
{ |
42
|
|
|
return $this->addWithDescription(new CommandTask($name, ...$arguments), $description); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
2 |
|
* @see CallbackTask::__construct() |
47
|
|
|
* |
48
|
2 |
|
* @param string|null $description optional description |
49
|
|
|
*/ |
50
|
|
|
public function addCallback(callable $callback, ?string $description = null): self |
51
|
|
|
{ |
52
|
|
|
return $this->addWithDescription(new CallbackTask($callback), $description); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
2 |
|
* @see ProcessTask::__construct() |
57
|
|
|
* |
58
|
2 |
|
* @param string|Process $process |
59
|
|
|
* @param string|null $description optional description |
60
|
|
|
*/ |
61
|
|
|
public function addProcess($process, ?string $description = null): self |
62
|
|
|
{ |
63
|
|
|
return $this->addWithDescription(new ProcessTask($process), $description); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
/** |
67
|
|
|
* @see PingTask::__construct() |
68
|
1 |
|
* |
69
|
|
|
* @param string|null $description optional description |
70
|
|
|
*/ |
71
|
|
|
public function addPing(string $url, string $method = 'GET', array $options = [], ?string $description = null): self |
72
|
|
|
{ |
73
|
|
|
return $this->addWithDescription(new PingTask($url, $method, $options), $description); |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
/** |
77
|
|
|
* @see MessageTask::__construct() |
78
|
1 |
|
* |
79
|
|
|
* @param object|Envelope $message |
80
|
|
|
* @param string|null $description optional description |
81
|
|
|
*/ |
82
|
|
|
public function addMessage(object $message, array $stamps = [], ?string $description = null): self |
83
|
|
|
{ |
84
|
5 |
|
return $this->addWithDescription(new MessageTask($message, $stamps), $description); |
85
|
|
|
} |
86
|
5 |
|
|
87
|
5 |
|
/** |
88
|
|
|
* @return \Traversable<Task> |
89
|
5 |
|
*/ |
90
|
1 |
|
public function getIterator(): \Traversable |
91
|
|
|
{ |
92
|
|
|
foreach ($this->tasks as $task) { |
93
|
5 |
|
$task->cron($this->getExpression()); |
94
|
4 |
|
|
95
|
|
|
if ($this->getTimezone()) { |
96
|
|
|
$task->timezone($this->getTimezone()); |
97
|
5 |
|
} |
98
|
|
|
|
99
|
5 |
|
foreach ($this->getExtensions() as $extension) { |
100
|
|
|
$task->addExtension($extension); |
101
|
2 |
|
} |
102
|
|
|
|
103
|
2 |
|
yield $task; |
104
|
2 |
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
2 |
|
private function addWithDescription(Task $task, ?string $description = null): self |
108
|
|
|
{ |
109
|
|
|
if ($description) { |
110
|
|
|
$task->description($description); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->add($task); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|