|
1
|
|
|
<?php namespace Indatus\Dispatcher\Drivers\DateTime; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This class is responsible for parsing the expression syntax |
|
5
|
|
|
* Dispatcher uses for scheduling. |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2014 Indatus |
|
8
|
|
|
* @package Indatus\Dispatcher\Drivers\PHP |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use App; |
|
12
|
|
|
use Carbon\Carbon; |
|
13
|
|
|
use Cron\CronExpression; |
|
14
|
|
|
|
|
15
|
|
|
class ScheduleInterpreter |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Carbon */ |
|
18
|
|
|
protected $now; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Scheduler */ |
|
21
|
|
|
protected $scheduler; |
|
22
|
|
|
|
|
23
|
|
|
protected $month = null; |
|
24
|
|
|
protected $week = null; |
|
25
|
|
|
protected $dayOfMonth = null; |
|
26
|
|
|
protected $dayOfWeek = null; |
|
27
|
|
|
protected $hour = null; |
|
28
|
|
|
protected $minute = null; |
|
29
|
|
|
|
|
30
|
9 |
|
public function __construct(Scheduler $scheduler, Carbon $now) |
|
31
|
|
|
{ |
|
32
|
9 |
|
$this->now = $now; |
|
33
|
9 |
|
$this->scheduler = $scheduler; |
|
34
|
9 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Determine if the current schedule is due to be run |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
2 |
|
public function isDue() |
|
42
|
|
|
{ |
|
43
|
2 |
|
$cron = App::make('Cron\CronExpression', [$this->scheduler->getCronSchedule()]); |
|
44
|
|
|
|
|
45
|
|
|
// if a week is defined, so some special weekly stuff |
|
46
|
2 |
|
if ($this->scheduler->getScheduleWeek() !== Scheduler::NONE) { |
|
47
|
1 |
|
return $this->thisWeek() && $cron->isDue(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
//otherwise us only standard cron scheduling |
|
51
|
1 |
|
return $cron->isDue(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Determine if the provided expression refers to this week |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
8 |
|
public function thisWeek() |
|
60
|
|
|
{ |
|
61
|
8 |
|
$scheduleWeek = $this->scheduler->getScheduleWeek(); |
|
62
|
8 |
|
$currentWeek = $this->now->format('W'); |
|
63
|
|
|
|
|
64
|
|
|
//if a month is defined, week refers to the week of the month |
|
65
|
8 |
|
$scheduleMonth = $this->scheduler->getScheduleMonth(); |
|
66
|
8 |
|
if (!is_null($scheduleMonth) && $scheduleMonth !== Scheduler::NONE) { |
|
67
|
1 |
|
return $this->isCurrent($scheduleWeek, $this->now->weekOfMonth); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
//if it's an odd or even week |
|
71
|
7 |
|
if ($scheduleWeek == 'odd' && $currentWeek & 1) { |
|
72
|
1 |
|
return true; |
|
73
|
7 |
|
} elseif ($scheduleWeek == 'even' && !($currentWeek & 1)) { |
|
74
|
2 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
6 |
|
return $this->isCurrent($scheduleWeek, $this->now->weekOfYear); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* This method checks syntax for all scheduling components. |
|
82
|
|
|
* |
|
83
|
|
|
* - If there's a direct match |
|
84
|
|
|
* - If it's an ANY match |
|
85
|
|
|
* - If the expression exists in the series |
|
86
|
|
|
* |
|
87
|
|
|
* @param string $expression The expression to compare |
|
88
|
|
|
* @param string $current The current value to compare against |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
7 |
|
protected function isCurrent($expression, $current) |
|
93
|
|
|
{ |
|
94
|
|
|
// if it's any |
|
95
|
7 |
|
if ($expression == Scheduler::ANY) { |
|
96
|
1 |
|
return true; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// if this is in a series |
|
100
|
6 |
|
if (is_array($expression)) { |
|
101
|
1 |
|
return in_array($current, $expression); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// if there's a direct match |
|
105
|
5 |
|
if ((string) $expression === (string) $current) { |
|
106
|
2 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
3 |
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|