-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathCronExpression.php
189 lines (157 loc) · 4.49 KB
/
CronExpression.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Tasks.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Tasks;
use CodeIgniter\I18n\Time;
use CodeIgniter\Tasks\Exceptions\TasksException;
use Exception;
class CronExpression
{
/**
* The timezone this should be considered under.
*/
protected string $timezone;
/**
* The current date/time. Used for testing.
*/
protected ?Time $testTime = null;
/**
* The current Cron expression string to process
*/
private ?string $currentExpression = null;
/**
* Allows us to set global timezone for all tasks
* on construct
*
* @param string $timezone The global timezone for all tasks
*
* @return void
*/
public function __construct(?string $timezone = null)
{
$this->timezone = $timezone ?? app_timezone();
}
/**
* Checks whether cron expression should be run. Allows
* for custom timezone to be used for specific task
*
* @param string $expression The Cron Expression to be evaluated
*/
public function shouldRun(string $expression): bool
{
$this->setTime();
$this->currentExpression = $expression;
// Break the expression into separate parts
[
$min,
$hour,
$monthDay,
$month,
$weekDay,
] = explode(' ', $expression);
return $this->checkMinute($min)
&& $this->checkHour($hour)
&& $this->checkMonthDay($monthDay)
&& $this->checkMonth($month)
&& $this->checkWeekDay($weekDay);
}
/**
* Returns a Time instance representing the next
* date/time this expression would be ran.
*/
public function nextRun(string $expression): Time
{
$this->setTime();
return (new RunResolver())->nextRun($expression, clone $this->testTime);
}
/**
* Returns a Time instance representing the last
* date/time this expression would have ran.
*/
public function lastRun(string $expression): Time
{
return new Time();
}
/**
* Sets a date/time that will be used in place
* of the current time to help with testing.
*
* @return $this
*
* @throws Exception
*/
public function testTime(string $dateTime)
{
$this->testTime = Time::parse($dateTime, $this->timezone);
return $this;
}
private function checkMinute(string $time): bool
{
return $this->checkTime($time, 'i');
}
private function checkHour(string $time): bool
{
return $this->checkTime($time, 'G');
}
private function checkMonthDay(string $time): bool
{
return $this->checkTime($time, 'j');
}
private function checkMonth(string $time): bool
{
return $this->checkTime($time, 'n');
}
private function checkWeekDay(string $time): bool
{
return $this->checkTime($time, 'w');
}
private function checkTime(string $time, string $format): bool
{
if ($time === '*') {
return true;
}
$currentTime = $this->testTime->format($format);
assert(ctype_digit($currentTime));
// Handle repeating times (i.e. /5 or */5 for every 5 minutes)
if (str_contains($time, '/')) {
$period = substr($time, strpos($time, '/') + 1) ?: '';
if ($period === '' || ! ctype_digit($period)) {
throw TasksException::forInvalidCronExpression($this->currentExpression);
}
return ($currentTime % $period) === 0;
}
// Handle ranges (1-5)
if (str_contains($time, '-')) {
$items = [];
[$start, $end] = explode('-', $time);
for ($i = $start; $i <= $end; $i++) {
$items[] = $i;
}
}
// Handle multiple days
else {
$items = explode(',', $time);
}
return in_array($currentTime, $items, false);
}
/**
* Sets the current time if it hasn't already been set.
*
* @throws Exception
*/
private function setTime(): void
{
// Set our current time
if ($this->testTime instanceof Time) {
return;
}
$this->testTime = Time::now($this->timezone);
}
}