|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\ScheduleBundle\Tests\Fixture; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
8
|
|
|
use Zenstruck\ScheduleBundle\EventListener\ScheduleBuilderSubscriber; |
|
9
|
|
|
use Zenstruck\ScheduleBundle\Schedule; |
|
10
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\ExtensionHandler; |
|
11
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Extension\ExtensionHandlerRegistry; |
|
12
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleBuilder; |
|
13
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleRunContext; |
|
14
|
|
|
use Zenstruck\ScheduleBundle\Schedule\ScheduleRunner; |
|
15
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task; |
|
16
|
|
|
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Kevin Bond <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class MockScheduleBuilder implements ScheduleBuilder |
|
22
|
|
|
{ |
|
23
|
|
|
private $tasks = []; |
|
24
|
|
|
private $extensions = []; |
|
25
|
|
|
private $runners = []; |
|
26
|
|
|
private $subscribers = []; |
|
27
|
|
|
private $handlers = []; |
|
28
|
|
|
private $builders = []; |
|
29
|
|
|
|
|
30
|
|
|
public function addTask(Task $task): self |
|
31
|
|
|
{ |
|
32
|
|
|
$this->tasks[] = $task; |
|
33
|
|
|
|
|
34
|
|
|
return $this; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function addExtension(object $extension): self |
|
38
|
|
|
{ |
|
39
|
|
|
$this->extensions[] = $extension; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function addRunner(TaskRunner $runner): self |
|
45
|
|
|
{ |
|
46
|
|
|
$this->runners[] = $runner; |
|
47
|
|
|
|
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function addSubscriber(EventSubscriberInterface $subscriber): self |
|
52
|
|
|
{ |
|
53
|
|
|
$this->subscribers[] = $subscriber; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function addHandler(ExtensionHandler $handler): self |
|
59
|
|
|
{ |
|
60
|
|
|
$this->handlers[] = $handler; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function addBuilder(ScheduleBuilder $builder): self |
|
66
|
|
|
{ |
|
67
|
|
|
$this->builders[] = $builder; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
public function run(string ...$taskIds): ScheduleRunContext |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->getRunner()(...$taskIds); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function getRunner(?EventDispatcherInterface $dispatcher = null): ScheduleRunner |
|
78
|
|
|
{ |
|
79
|
|
|
$dispatcher = $dispatcher ?: new EventDispatcher(); |
|
80
|
|
|
$dispatcher->addSubscriber(new ScheduleBuilderSubscriber(\array_merge($this->builders, [$this]))); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($this->subscribers as $subscriber) { |
|
83
|
|
|
$dispatcher->addSubscriber($subscriber); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return new ScheduleRunner(\array_merge($this->runners, [new MockTaskRunner()]), new ExtensionHandlerRegistry($this->handlers), $dispatcher); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function buildSchedule(Schedule $schedule): void |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->tasks as $task) { |
|
92
|
|
|
$schedule->add($task); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
foreach ($this->extensions as $extension) { |
|
96
|
|
|
$schedule->addExtension($extension); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|