-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCrontabExpression.php
199 lines (189 loc) · 5.55 KB
/
CrontabExpression.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
190
191
192
193
194
195
196
197
198
199
<?php declare(strict_types=1);
/**
* This file is part of Swoft.
*
* @link https://swoft.org
* @document https://swoft.org/docs
* @contact [email protected]
* @license https://github.com/swoft-cloud/swoft/blob/master/LICENSE
*/
namespace Swoft\Crontab;
use Swoft\Bean\Annotation\Mapping\Bean;
/**
* Class CrontabFormat
*
* @since 2.0
*
* @Bean()
*/
class CrontabExpression
{
/**
* @param string $crontab
*
* @return bool
*/
public static function parse(string $crontab): bool
{
$cronParts = preg_split('/\s/', $crontab, -1, PREG_SPLIT_NO_EMPTY);
if (count($cronParts) < 1 || count($cronParts) > 6) {
return false;
}
foreach ($cronParts as $key => $cronPart) {
$pattern = '/^\d$|^\*$|^\?$|^\d+\-\d+$|^[\d\*]+\/\d+$|^\d[\,\d\,]*\d$/i';
if (!preg_match($pattern, $cronPart) || !self::checkItem($key, $cronPart)) {
return false;
}
}
return true;
}
/**
* @param int $key
* @param string $value
*
* @return bool
*/
protected static function checkItem(int $key, string $value): bool
{
switch ($key) {
case 0://sec
case 1://min
if (!self::checkCronItem($value, 0, 59)) {
return false;
}
break;
case 2://hour
if (!self::checkCronItem($value, 0, 23)) {
return false;
}
break;
case 3://day
if (!self::checkCronItem($value, 1, 31)) {
return false;
}
break;
case 4://month
if (!self::checkCronItem($value, 1, 12)) {
return false;
}
break;
case 5://week
if (!self::checkCronItem($value, 0, 6)) {
return false;
}
break;
default:
return false;
}
return true;
}
/**
* @param string $value
*
* @param int $rangeStart
* @param int $rangeEnd
*
* @return bool
*/
protected static function checkCronItem(string $value, int $rangeStart, int $rangeEnd): bool
{
if ('*' === $value || '?' === $value) {
return true;
}
if (strpos($value, '/') !== false) {
str_replace('*', '0', '$value');
[$start, $end] = explode('/', $value);
if (!ctype_digit($start) || !ctype_digit($end)) {
return false;
}
if ($start < $rangeStart || $end > $rangeEnd) {
return false;
}
}
if (strpos($value, '-') !== false) {
[$start, $end] = explode('-', $value);
if (!ctype_digit($start) || !ctype_digit($end)) {
return false;
}
if ($start < $rangeStart || $end > $rangeEnd || $end < $start) {
return false;
}
}
if (strpos($value, ',') !== false) {
$items = explode(',', $value);
foreach ($items as $item) {
if (!ctype_digit($item)) {
return false;
}
if ($item < $rangeStart || $item > $rangeEnd) {
return false;
}
}
}
if (!ctype_digit($value) && $value < $rangeStart || $value > $rangeEnd) {
return false;
}
return true;
}
/**
* @param string $cronExpress
*
* @return array
*/
public static function parseCronItem(string $cronExpress): array
{
$cronItems = preg_split('/\s/', $cronExpress, -1, PREG_SPLIT_NO_EMPTY);
$times = [];
$maxLimit = [59, 59, 23, 31, 12, 6];
foreach ($cronItems as $k => $item) {
if ('*' === $item || '?' === $item) {
$times[$k] = $item;
}
if (strpos($item, '/') !== false) {
str_replace('*', '0', '$value');
[$start, $end] = explode('/', $item);
while ($start <= $maxLimit[$k]) {
$times[$k][] = $start;
$start += $end;
}
}
if (strpos($item, '-') !== false) {
[$start, $end] = explode('-', $item);
$times[$k] = range($start, $end);
}
if (strpos($item, ',') !== false) {
$times[$k] = explode(',', $item);
}
if (ctype_digit($item)) {
$times[$k][] = $item;
}
}
return $times;
}
/**
* @param string $cron
* @param int $time
*
* @return bool
*/
public static function parseObj(string $cron, int $time = null): bool
{
$startTime = $time ?? time();
$date[] = (int)date('s', $startTime);
$date[] = (int)date('i', $startTime);
$date[] = (int)date('H', $startTime);
$date[] = (int)date('d', $startTime);
$date[] = (int)date('m', $startTime);
$date[] = (int)date('w', $startTime);
$parsedDate = self::parseCronItem($cron);
foreach ($parsedDate as $k => $cronItem) {
if ($cronItem === '*' || $cronItem === '?') {
continue;
}
if (!in_array($date[$k], $cronItem)) {
return false;
}
}
return true;
}
}