|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace T4web\Cron\Executor; |
|
4
|
|
|
|
|
5
|
|
|
use Zend\EventManager\EventManager; |
|
6
|
|
|
use Cron\Job\JobInterface; |
|
7
|
|
|
use Cron\Report\ReportInterface; |
|
8
|
|
|
use Cron\Report\CronReport; |
|
9
|
|
|
use Cron\Executor\ExecutorInterface; |
|
10
|
|
|
use T4web\Cron\JobEndedEvent; |
|
11
|
|
|
use T4web\Cron\Job\ShellJob; |
|
12
|
|
|
|
|
13
|
|
|
class Executor implements ExecutorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var JobInterface[] |
|
17
|
|
|
*/ |
|
18
|
|
|
private $jobs; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var EventManager |
|
22
|
|
|
*/ |
|
23
|
|
|
private $eventManager; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $processedJobs = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param JobInterface[] $jobs |
|
32
|
|
|
* |
|
33
|
|
|
* @return ReportInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
public function execute(array $jobs) |
|
36
|
|
|
{ |
|
37
|
|
|
$report = new CronReport(); |
|
38
|
|
|
|
|
39
|
|
|
$this->jobs = $jobs; |
|
40
|
|
|
$this->startProcesses($report); |
|
41
|
|
|
|
|
42
|
|
|
return $report; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param CronReport $report |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function startProcesses(CronReport $report) |
|
49
|
|
|
{ |
|
50
|
|
|
foreach ($this->jobs as $job) { |
|
51
|
|
|
$report->addJobReport($job->getReport()); |
|
|
|
|
|
|
52
|
|
|
// :) brilliantly |
|
53
|
|
|
$job->run($job->getReport()); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
private function getEventManager() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->eventManager) { |
|
60
|
|
|
$this->eventManager = new EventManager(); |
|
61
|
|
|
$this->eventManager->setIdentifiers(get_class($this)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return $this->eventManager; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function isRunning() |
|
71
|
|
|
{ |
|
72
|
|
|
$isRunning = false; |
|
73
|
|
|
|
|
74
|
|
|
foreach ($this->jobs as $id => $job) { |
|
75
|
|
|
if ($job->isRunning()) { |
|
76
|
|
|
$isRunning = true; |
|
77
|
|
|
} else { |
|
78
|
|
|
|
|
79
|
|
|
if (isset($this->processedJobs[$id])) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$event = new JobEndedEvent(); |
|
84
|
|
|
$event->setTarget($this); |
|
85
|
|
|
$event->setJob($job); |
|
|
|
|
|
|
86
|
|
|
$event->setReport($job->getReport()); |
|
|
|
|
|
|
87
|
|
|
$this->getEventManager()->trigger($event); |
|
88
|
|
|
|
|
89
|
|
|
$this->processedJobs[$id] = true; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $isRunning; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return ShellJob[] |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getRunningJobs() |
|
100
|
|
|
{ |
|
101
|
|
|
$jobs = []; |
|
102
|
|
|
foreach ($this->jobs as $job) { |
|
103
|
|
|
if ($job->isRunning()) { |
|
104
|
|
|
$jobs[] = $job; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $jobs; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: