@@ -29,45 +29,13 @@ class Expression
29
29
/** @var SegmentChecker */
30
30
protected $ checker ;
31
31
32
- protected static $ expressions = [
33
- '@yearly ' => '0 0 1 1 * ' ,
34
- '@annually ' => '0 0 1 1 * ' ,
35
- '@monthly ' => '0 0 1 * * ' ,
36
- '@weekly ' => '0 0 * * 0 ' ,
37
- '@daily ' => '0 0 * * * ' ,
38
- '@hourly ' => '0 * * * * ' ,
39
- '@always ' => '* * * * * ' ,
40
- '@5minutes ' => '*/5 * * * * ' ,
41
- '@10minutes ' => '*/10 * * * * ' ,
42
- '@15minutes ' => '*/15 * * * * ' ,
43
- '@30minutes ' => '0,30 * * * * ' ,
44
- ];
45
-
46
- protected static $ literals = [
47
- 'sun ' => 0 ,
48
- 'mon ' => 1 ,
49
- 'tue ' => 2 ,
50
- 'wed ' => 3 ,
51
- 'thu ' => 4 ,
52
- 'fri ' => 5 ,
53
- 'sat ' => 6 ,
54
- 'jan ' => 1 ,
55
- 'feb ' => 2 ,
56
- 'mar ' => 3 ,
57
- 'apr ' => 4 ,
58
- 'may ' => 5 ,
59
- 'jun ' => 6 ,
60
- 'jul ' => 7 ,
61
- 'aug ' => 8 ,
62
- 'sep ' => 9 ,
63
- 'oct ' => 10 ,
64
- 'nov ' => 11 ,
65
- 'dec ' => 12 ,
66
- ];
67
-
68
- public function __construct (SegmentChecker $ checker = null )
32
+ /** @var Normalizer */
33
+ protected $ normalizer ;
34
+
35
+ public function __construct (SegmentChecker $ checker = null , Normalizer $ normalizer = null )
69
36
{
70
- $ this ->checker = $ checker ?: new SegmentChecker ;
37
+ $ this ->checker = $ checker ?: new SegmentChecker ;
38
+ $ this ->normalizer = $ normalizer ?: new Normalizer ;
71
39
72
40
if (null === static ::$ instance ) {
73
41
static ::$ instance = $ this ;
@@ -123,7 +91,7 @@ public function isCronDue(string $expr, $time = null): bool
123
91
{
124
92
$ this ->checker ->setReference (new ReferenceTime ($ time ));
125
93
126
- foreach (\explode (' ' , $ this ->normalizeExpr ($ expr )) as $ pos => $ segment ) {
94
+ foreach (\explode (' ' , $ this ->normalizer -> normalizeExpr ($ expr )) as $ pos => $ segment ) {
127
95
if ($ segment === '* ' || $ segment === '? ' ) {
128
96
continue ;
129
97
}
@@ -149,7 +117,7 @@ public function filter(array $jobs, $time = null): array
149
117
$ dues = $ cache = [];
150
118
151
119
foreach ($ jobs as $ name => $ expr ) {
152
- $ expr = $ this ->normalizeExpr ($ expr );
120
+ $ expr = $ this ->normalizer -> normalizeExpr ($ expr );
153
121
154
122
if (!isset ($ cache [$ expr ])) {
155
123
$ cache [$ expr ] = $ this ->isCronDue ($ expr , $ time );
@@ -162,24 +130,4 @@ public function filter(array $jobs, $time = null): array
162
130
163
131
return $ dues ;
164
132
}
165
-
166
- protected function normalizeExpr (string $ expr ): string
167
- {
168
- $ expr = \trim ($ expr );
169
-
170
- if (isset (static ::$ expressions [$ expr ])) {
171
- return static ::$ expressions [$ expr ];
172
- }
173
-
174
- $ expr = \preg_replace ('~\s+~ ' , ' ' , $ expr );
175
- $ count = \substr_count ($ expr , ' ' );
176
-
177
- if ($ count < 4 || $ count > 5 ) {
178
- throw new \UnexpectedValueException (
179
- 'Cron $expr should have 5 or 6 segments delimited by space '
180
- );
181
- }
182
-
183
- return \str_ireplace (\array_keys (static ::$ literals ), \array_values (static ::$ literals ), $ expr );
184
- }
185
133
}
0 commit comments