Skip to content

Commit 0d6d85a

Browse files
committed
feat(expr): add construct with injection/initiliazation
1 parent b8aa4ce commit 0d6d85a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Expression.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class Expression
2424
/** @var Expression */
2525
protected static $instance;
2626

27+
/** @var SegmentChecker */
28+
protected $checker;
29+
2730
protected static $expressions = [
2831
'@yearly' => '0 0 1 1 *',
2932
'@annually' => '0 0 1 1 *',
@@ -60,6 +63,15 @@ class Expression
6063
'dec' => 12,
6164
];
6265

66+
public function __construct(SegmentChecker $checker = null)
67+
{
68+
$this->checker = $checker ?: new SegmentChecker;
69+
70+
if (null === static::$instance) {
71+
static::$instance = $this;
72+
}
73+
}
74+
6375
public function instance()
6476
{
6577
if (null === static::$instance) {
@@ -109,13 +121,12 @@ public function isCronDue($expr, $time = null)
109121
{
110122
list($expr, $times) = $this->process($expr, $time);
111123

112-
$checker = new SegmentChecker;
113124
foreach ($expr as $pos => $segment) {
114125
if ($segment === '*' || $segment === '?') {
115126
continue;
116127
}
117128

118-
if (!$checker->checkDue($segment, $pos, $times)) {
129+
if (!$this->checker->checkDue($segment, $pos, $times)) {
119130
return false;
120131
}
121132
}

0 commit comments

Comments
 (0)