1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Schedule\Task; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Kevin Bond <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
final class CompoundTask extends Task implements \IteratorAggregate |
12
|
|
|
{ |
13
|
|
|
/** @var Task[] */ |
14
|
|
|
private $tasks = []; |
15
|
|
|
|
16
|
3 |
|
public function __construct() |
17
|
|
|
{ |
18
|
3 |
|
parent::__construct('compound task'); |
19
|
3 |
|
} |
20
|
|
|
|
21
|
3 |
|
public function add(Task $task): self |
22
|
|
|
{ |
23
|
3 |
|
if ($task instanceof self) { |
24
|
1 |
|
throw new \LogicException('Cannot nest compound tasks.'); |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
$this->tasks[] = $task; |
28
|
|
|
|
29
|
2 |
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $name Command class or name (my:command) |
34
|
|
|
*/ |
35
|
1 |
|
public function addCommand(string $name, array $arguments = [], string $description = null): self |
36
|
|
|
{ |
37
|
1 |
|
return $this->addWithDescription(new CommandTask($name, ...$arguments), $description); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param callable $callback Return value is considered "output" |
42
|
|
|
*/ |
43
|
1 |
|
public function addCallback(callable $callback, string $description = null): self |
44
|
|
|
{ |
45
|
1 |
|
return $this->addWithDescription(new CallbackTask($callback), $description); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param string|Process $process |
50
|
|
|
*/ |
51
|
1 |
|
public function addProcess($process, string $description = null): self |
52
|
|
|
{ |
53
|
1 |
|
return $this->addWithDescription(new ProcessTask($process), $description); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function addNull(string $description): self |
57
|
|
|
{ |
58
|
1 |
|
return $this->add(new NullTask($description)); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return Task[] |
63
|
|
|
*/ |
64
|
2 |
|
public function getIterator(): \Generator |
65
|
|
|
{ |
66
|
2 |
|
foreach ($this->tasks as $task) { |
67
|
2 |
|
$task->cron($this->getExpression()); |
68
|
|
|
|
69
|
2 |
|
if ($this->getTimezone()) { |
70
|
1 |
|
$task->timezone($this->getTimezone()->getName()); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
foreach ($this->getExtensions() as $extension) { |
74
|
1 |
|
$task->addExtension($extension); |
75
|
|
|
} |
76
|
|
|
|
77
|
2 |
|
yield $task; |
|
|
|
|
78
|
|
|
} |
79
|
2 |
|
} |
80
|
|
|
|
81
|
1 |
|
private function addWithDescription(Task $task, string $description = null): self |
82
|
|
|
{ |
83
|
1 |
|
if ($description) { |
84
|
1 |
|
$task->description($description); |
85
|
|
|
} |
86
|
|
|
|
87
|
1 |
|
return $this->add($task); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|