Skip to content

Commit cee9bf7

Browse files
committed
feat(expr): add filter(), ::getDues() and normalizeExpr()
1 parent d73a078 commit cee9bf7

File tree

1 file changed

+66
-9
lines changed

1 file changed

+66
-9
lines changed

src/Expression.php

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
*/
2222
class Expression
2323
{
24+
/** @var Expression */
25+
protected static $instance;
26+
2427
protected static $expressions = [
2528
'@yearly' => '0 0 1 1 *',
2629
'@annually' => '0 0 1 1 *',
@@ -67,13 +70,28 @@ class Expression
6770
*/
6871
public static function isDue($expr, $time = null)
6972
{
70-
static $instance;
73+
if (null === static::$instance) {
74+
static::$instance = new static;
75+
}
7176

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;
7492
}
7593

76-
return $instance->isCronDue($expr, $time);
94+
return static::$instance->filter($jobs, $time);
7795
}
7896

7997
/**
@@ -82,7 +100,7 @@ public static function isDue($expr, $time = null)
82100
* Parse cron expression to decide if it can be run on given time (or default now).
83101
*
84102
* @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.
86104
*
87105
* @return bool
88106
*/
@@ -104,6 +122,38 @@ public function isCronDue($expr, $time = null)
104122
return true;
105123
}
106124

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+
107157
/**
108158
* Process and prepare input.
109159
*
@@ -114,10 +164,7 @@ public function isCronDue($expr, $time = null)
114164
*/
115165
protected function process($expr, $time)
116166
{
117-
if (isset(static::$expressions[$expr])) {
118-
$expr = static::$expressions[$expr];
119-
}
120-
167+
$expr = $this->normalizeExpr($expr);
121168
$expr = \str_ireplace(\array_keys(static::$literals), \array_values(static::$literals), $expr);
122169
$expr = \explode(' ', $expr);
123170

@@ -145,4 +192,14 @@ protected function normalizeTime($time)
145192

146193
return $time;
147194
}
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+
148205
}

0 commit comments

Comments
 (0)