Skip to content

Commit

Permalink
feat(expr): add construct with injection/initiliazation
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Aug 16, 2018
1 parent b8aa4ce commit 0d6d85a
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class Expression
/** @var Expression */
protected static $instance;

/** @var SegmentChecker */
protected $checker;

protected static $expressions = [
'@yearly' => '0 0 1 1 *',
'@annually' => '0 0 1 1 *',
Expand Down Expand Up @@ -60,6 +63,15 @@ class Expression
'dec' => 12,
];

public function __construct(SegmentChecker $checker = null)
{
$this->checker = $checker ?: new SegmentChecker;

if (null === static::$instance) {
static::$instance = $this;
}
}

public function instance()
{
if (null === static::$instance) {
Expand Down Expand Up @@ -109,13 +121,12 @@ public function isCronDue($expr, $time = null)
{
list($expr, $times) = $this->process($expr, $time);

$checker = new SegmentChecker;
foreach ($expr as $pos => $segment) {
if ($segment === '*' || $segment === '?') {
continue;
}

if (!$checker->checkDue($segment, $pos, $times)) {
if (!$this->checker->checkDue($segment, $pos, $times)) {
return false;
}
}
Expand Down

0 comments on commit 0d6d85a

Please sign in to comment.