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