21
21
*/
22
22
class Expression
23
23
{
24
+ /** @var Expression */
25
+ protected static $ instance ;
26
+
24
27
protected static $ expressions = [
25
28
'@yearly ' => '0 0 1 1 * ' ,
26
29
'@annually ' => '0 0 1 1 * ' ,
@@ -67,13 +70,28 @@ class Expression
67
70
*/
68
71
public static function isDue ($ expr , $ time = null )
69
72
{
70
- static $ instance ;
73
+ if (null === static ::$ instance ) {
74
+ static ::$ instance = new static ;
75
+ }
71
76
72
- if (!$ instance ) {
73
- $ instance = new static ;
77
+ return static ::$ instance ->isCronDue ($ expr , $ time );
78
+ }
79
+
80
+ /**
81
+ * Filter only the jobs that are due.
82
+ *
83
+ * @param array $jobs Jobs with cron exprs. [job1 => cron-expr1, job2 => cron-expr2, ...]
84
+ * @param mixed $time The timestamp to validate the cron expr against. Defaults to now.
85
+ *
86
+ * @return array Due job names: [job1name, ...];
87
+ */
88
+ public static function getDues (array $ jobs , $ time = null )
89
+ {
90
+ if (null === static ::$ instance ) {
91
+ static ::$ instance = new static ;
74
92
}
75
93
76
- return $ instance ->isCronDue ( $ expr , $ time );
94
+ return static :: $ instance ->filter ( $ jobs , $ time );
77
95
}
78
96
79
97
/**
@@ -82,7 +100,7 @@ public static function isDue($expr, $time = null)
82
100
* Parse cron expression to decide if it can be run on given time (or default now).
83
101
*
84
102
* @param string $expr The cron expression.
85
- * @param int $time The timestamp to validate the cron expr against. Defaults to now.
103
+ * @param mixed $time The timestamp to validate the cron expr against. Defaults to now.
86
104
*
87
105
* @return bool
88
106
*/
@@ -104,6 +122,38 @@ public function isCronDue($expr, $time = null)
104
122
return true ;
105
123
}
106
124
125
+ /**
126
+ * Filter only the jobs that are due.
127
+ *
128
+ * @param array $jobs Jobs with cron exprs. [job1 => cron-expr1, job2 => cron-expr2, ...]
129
+ * @param mixed $time The timestamp to validate the cron expr against. Defaults to now.
130
+ *
131
+ * @return array Due job names: [job1name, ...];
132
+ */
133
+ public function filter (array $ jobs , $ time = null )
134
+ {
135
+ $ dues = $ cache = [];
136
+ $ time = $ this ->normalizeTime ($ time );
137
+
138
+ foreach ($ jobs as $ name => $ expr ) {
139
+ $ expr = $ this ->normalizeExpr ($ expr );
140
+
141
+ if (isset ($ cache [$ expr ])) {
142
+ $ dues [] = $ name ;
143
+
144
+ continue ;
145
+ }
146
+
147
+ if ($ this ->isCronDue ($ expr , $ time )) {
148
+ $ dues [] = $ name ;
149
+
150
+ $ cache [$ expr ] = true ;
151
+ }
152
+ }
153
+
154
+ return $ dues ;
155
+ }
156
+
107
157
/**
108
158
* Process and prepare input.
109
159
*
@@ -114,10 +164,7 @@ public function isCronDue($expr, $time = null)
114
164
*/
115
165
protected function process ($ expr , $ time )
116
166
{
117
- if (isset (static ::$ expressions [$ expr ])) {
118
- $ expr = static ::$ expressions [$ expr ];
119
- }
120
-
167
+ $ expr = $ this ->normalizeExpr ($ expr );
121
168
$ expr = \str_ireplace (\array_keys (static ::$ literals ), \array_values (static ::$ literals ), $ expr );
122
169
$ expr = \explode (' ' , $ expr );
123
170
@@ -145,4 +192,14 @@ protected function normalizeTime($time)
145
192
146
193
return $ time ;
147
194
}
195
+
196
+ protected function normalizeExpr ($ expr )
197
+ {
198
+ if (isset (static ::$ expressions [$ expr ])) {
199
+ $ expr = static ::$ expressions [$ expr ];
200
+ }
201
+
202
+ return $ expr ;
203
+ }
204
+
148
205
}
0 commit comments