39 private static $order = array(self::YEAR, self::MONTH, self::DAY, self::WEEKDAY, self::HOUR, self::MINUTE);
61 '@yearly' =>
'0 0 1 1 *',
62 '@annually' =>
'0 0 1 1 *',
63 '@monthly' =>
'0 0 1 * *',
64 '@weekly' =>
'0 0 * * 0',
65 '@daily' =>
'0 0 * * *',
66 '@hourly' =>
'0 * * * *',
69 if (isset($mappings[$expression])) {
70 $expression = $mappings[$expression];
99 $this->cronParts = explode(
' ', $value);
100 if (count($this->cronParts) < 5) {
101 throw new InvalidArgumentException(
102 $value.
' is not a valid CRON expression'
106 foreach ($this->cronParts as $position => $part) {
107 $this->
setPart($position, $part);
125 if (!$this->fieldFactory->getField($position)->validate($value)) {
126 throw new InvalidArgumentException(
127 'Invalid CRON field value '.$value.
' as position '.$position
131 $this->cronParts[$position] = $value;
153 public function getNextRunDate($currentTime =
'now', $nth = 0, $allowCurrentDate =
false)
155 return $this->
getRunDate($currentTime, $nth,
false, $allowCurrentDate);
174 return $this->
getRunDate($currentTime, $nth,
true, $allowCurrentDate);
188 public function getMultipleRunDates($total, $currentTime =
'now', $invert =
false, $allowCurrentDate =
false)
191 for (
$i = 0;
$i < max(0, $total);
$i++) {
192 $matches[] = $this->
getRunDate($currentTime,
$i, $invert, $allowCurrentDate);
209 if (null === $part) {
210 return implode(
' ', $this->cronParts);
211 } elseif (array_key_exists($part, $this->cronParts)) {
212 return $this->cronParts[$part];
237 public function isDue($currentTime = null)
239 if (null === $currentTime ||
'now' === $currentTime) {
240 $currentDate = date(
'Y-m-d H:i');
241 $currentTime = strtotime($currentDate);
242 } elseif ($currentTime instanceof DateTime) {
243 $currentDate = $currentTime->format(
'Y-m-d H:i');
244 $currentTime = strtotime($currentDate);
246 $currentTime =
new DateTime($currentTime);
247 $currentTime->setTime($currentTime->format(
'H'), $currentTime->format(
'i'), 0);
248 $currentDate = $currentTime->format(
'Y-m-d H:i');
249 $currentTime = $currentTime->getTimeStamp();
252 return $this->
getNextRunDate($currentDate, 0,
true)->getTimestamp() == $currentTime;
268 protected function getRunDate($currentTime = null, $nth = 0, $invert =
false, $allowCurrentDate =
false)
270 $currentDate = $currentTime instanceof DateTime
272 :
new DateTime($currentTime ?:
'now');
275 $currentDate->setTimezone(
new DateTimeZone(date_default_timezone_get()));
277 $currentDate->setTime($currentDate->format(
'H'), $currentDate->format(
'i'), 0);
278 $nextRun = clone $currentDate;
282 for (
$i = 0;
$i < 1000;
$i++) {
283 foreach (self::$order as $position) {
285 if (null === $part) {
291 $field = $this->fieldFactory->getField($position);
293 if (strpos($part,
',') ===
false) {
294 $satisfied = $field->isSatisfiedBy($nextRun, $part);
296 foreach (array_map(
'trim', explode(
',', $part)) as $listPart) {
297 if ($field->isSatisfiedBy($nextRun, $listPart)) {
306 $field->increment($nextRun, $invert);
312 if ((!$allowCurrentDate && $nextRun == $currentDate) || --$nth > -1) {
313 $this->fieldFactory->getField(0)->increment($nextRun, $invert);
321 throw new RuntimeException(
'Impossible CRON expression');