CronExpression.php
1 year ago
CronExpression_AbstractField.php
4 years ago
CronExpression_DayOfMonthField.php
4 years ago
CronExpression_DayOfWeekField.php
4 years ago
CronExpression_FieldFactory.php
4 years ago
CronExpression_FieldInterface.php
4 years ago
CronExpression_HoursField.php
4 years ago
CronExpression_MinutesField.php
4 years ago
CronExpression_MonthField.php
4 years ago
CronExpression_YearField.php
4 years ago
index.php
3 years ago
CronExpression.php
152 lines
| 1 | <?php |
| 2 | if (!defined('ABSPATH')) exit; |
| 3 | class CronExpression |
| 4 | { |
| 5 | const MINUTE = 0; |
| 6 | const HOUR = 1; |
| 7 | const DAY = 2; |
| 8 | const MONTH = 3; |
| 9 | const WEEKDAY = 4; |
| 10 | const YEAR = 5; |
| 11 | private $cronParts; |
| 12 | private $fieldFactory; |
| 13 | private static $order = array(self::YEAR, self::MONTH, self::DAY, self::WEEKDAY, self::HOUR, self::MINUTE); |
| 14 | public static function factory($expression, ?CronExpression_FieldFactory $fieldFactory = null) |
| 15 | { |
| 16 | $mappings = array( |
| 17 | '@yearly' => '0 0 1 1 *', |
| 18 | '@annually' => '0 0 1 1 *', |
| 19 | '@monthly' => '0 0 1 * *', |
| 20 | '@weekly' => '0 0 * * 0', |
| 21 | '@daily' => '0 0 * * *', |
| 22 | '@hourly' => '0 * * * *' |
| 23 | ); |
| 24 | if (isset($mappings[$expression])) { |
| 25 | $expression = $mappings[$expression]; |
| 26 | } |
| 27 | return new self($expression, $fieldFactory ? $fieldFactory : new CronExpression_FieldFactory()); |
| 28 | } |
| 29 | public function __construct($expression, CronExpression_FieldFactory $fieldFactory) |
| 30 | { |
| 31 | $this->fieldFactory = $fieldFactory; |
| 32 | $this->setExpression($expression); |
| 33 | } |
| 34 | public function setExpression($value) |
| 35 | { |
| 36 | $this->cronParts = preg_split('/\s/', $value, -1, PREG_SPLIT_NO_EMPTY); |
| 37 | if (count($this->cronParts) < 5) { |
| 38 | throw new InvalidArgumentException( |
| 39 | $value . ' is not a valid CRON expression' |
| 40 | ); |
| 41 | } |
| 42 | foreach ($this->cronParts as $position => $part) { |
| 43 | $this->setPart($position, $part); |
| 44 | } |
| 45 | return $this; |
| 46 | } |
| 47 | public function setPart($position, $value) |
| 48 | { |
| 49 | if (!$this->fieldFactory->getField($position)->validate($value)) { |
| 50 | throw new InvalidArgumentException( |
| 51 | 'Invalid CRON field value ' . $value . ' as position ' . $position |
| 52 | ); |
| 53 | } |
| 54 | $this->cronParts[$position] = $value; |
| 55 | return $this; |
| 56 | } |
| 57 | public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) |
| 58 | { |
| 59 | return $this->getRunDate($currentTime, $nth, false, $allowCurrentDate); |
| 60 | } |
| 61 | public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) |
| 62 | { |
| 63 | return $this->getRunDate($currentTime, $nth, true, $allowCurrentDate); |
| 64 | } |
| 65 | public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false) |
| 66 | { |
| 67 | $matches = array(); |
| 68 | for ($i = 0; $i < max(0, $total); $i++) { |
| 69 | $matches[] = $this->getRunDate($currentTime, $i, $invert, $allowCurrentDate); |
| 70 | } |
| 71 | return $matches; |
| 72 | } |
| 73 | public function getExpression($part = null) |
| 74 | { |
| 75 | if (null === $part) { |
| 76 | return implode(' ', $this->cronParts); |
| 77 | } elseif (array_key_exists($part, $this->cronParts)) { |
| 78 | return $this->cronParts[$part]; |
| 79 | } |
| 80 | return null; |
| 81 | } |
| 82 | public function __toString() |
| 83 | { |
| 84 | return $this->getExpression(); |
| 85 | } |
| 86 | public function isDue($currentTime = 'now') |
| 87 | { |
| 88 | if ('now' === $currentTime) { |
| 89 | $currentDate = date('Y-m-d H:i'); |
| 90 | $currentTime = strtotime($currentDate); |
| 91 | } elseif ($currentTime instanceof DateTime) { |
| 92 | $currentDate = $currentTime->format('Y-m-d H:i'); |
| 93 | $currentTime = strtotime($currentDate); |
| 94 | } else { |
| 95 | $currentTime = new DateTime($currentTime); |
| 96 | $currentTime->setTime($currentTime->format('H'), $currentTime->format('i'), 0); |
| 97 | $currentDate = $currentTime->format('Y-m-d H:i'); |
| 98 | $currentTime = (int)($currentTime->getTimestamp()); |
| 99 | } |
| 100 | return $this->getNextRunDate($currentDate, 0, true)->getTimestamp() == $currentTime; |
| 101 | } |
| 102 | protected function getRunDate($currentTime = null, $nth = 0, $invert = false, $allowCurrentDate = false) |
| 103 | { |
| 104 | if ($currentTime instanceof DateTime) { |
| 105 | $currentDate = $currentTime; |
| 106 | } else { |
| 107 | $currentDate = new DateTime($currentTime ? $currentTime : 'now'); |
| 108 | $currentDate->setTimezone(new DateTimeZone(date_default_timezone_get())); |
| 109 | } |
| 110 | $currentDate->setTime($currentDate->format('H'), $currentDate->format('i'), 0); |
| 111 | $nextRun = clone $currentDate; |
| 112 | $nth = (int) $nth; |
| 113 | // Set a hard limit to bail on an impossible date |
| 114 | for ($i = 0; $i < 1000; $i++) { |
| 115 | foreach (self::$order as $position) { |
| 116 | $part = $this->getExpression($position); |
| 117 | if (null === $part) { |
| 118 | continue; |
| 119 | } |
| 120 | $satisfied = false; |
| 121 | // Get the field object used to validate this part |
| 122 | $field = $this->fieldFactory->getField($position); |
| 123 | // Check if this is singular or a list |
| 124 | if (strpos($part, ',') === false) { |
| 125 | $satisfied = $field->isSatisfiedBy($nextRun, $part); |
| 126 | } else { |
| 127 | foreach (array_map('trim', explode(',', $part)) as $listPart) { |
| 128 | if ($field->isSatisfiedBy($nextRun, $listPart)) { |
| 129 | $satisfied = true; |
| 130 | break; |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | // If the field is not satisfied, then start over |
| 135 | if (!$satisfied) { |
| 136 | $field->increment($nextRun, $invert); |
| 137 | continue 2; |
| 138 | } |
| 139 | } |
| 140 | // Skip this match if needed |
| 141 | if ((!$allowCurrentDate && $nextRun == $currentDate) || --$nth > -1) { |
| 142 | $this->fieldFactory->getField(0)->increment($nextRun, $invert); |
| 143 | continue; |
| 144 | } |
| 145 | return $nextRun; |
| 146 | } |
| 147 | // @codeCoverageIgnoreStart |
| 148 | throw new RuntimeException('Impossible CRON expression'); |
| 149 | // @codeCoverageIgnoreEnd |
| 150 | } |
| 151 | } |
| 152 |