Example #1
0
 /**
  * Parses a cron line into a Cron instance
  *
  * TODO: this deserves a serious regex
  *
  * @static
  * @param $cron string The cron line
  * @return Cron
  */
 public static function parse($cron)
 {
     $parts = \explode(' ', $cron);
     $command = \implode(' ', \array_slice($parts, 5));
     // extract comment
     if (\strpos($command, '#')) {
         list($command, $comment) = \explode('#', $command);
         $comment = \trim($comment);
     }
     // extract error file
     if (\strpos($command, '2>')) {
         list($command, $errorFile) = \explode('2>', $command);
         $errorFile = \trim($errorFile);
     }
     // extract log file
     if (\strpos($command, '>')) {
         list($command, $logFile) = \explode('>', $command);
         $logFile = \trim($logFile);
     }
     // compute last run time, and file size
     $lastRunTime = null;
     $logSize = null;
     $errorSize = null;
     if (isset($logFile) && \file_exists($logFile)) {
         $lastRunTime = \filemtime($logFile);
         $logSize = \filesize($logFile);
     }
     if (isset($errorFile) && \file_exists($errorFile)) {
         $lastRunTime = \max($lastRunTime ?: 0, \filemtime($errorFile));
         $errorSize = \filesize($errorFile);
     }
     // compute status
     $status = 'error';
     if ($logSize === null && $errorSize === null) {
         $status = 'unknown';
     } else {
         if ($errorSize === null || $errorSize == 0) {
             $status = 'success';
         }
     }
     // create cron instance
     $cron = new self();
     $cron->setMinute($parts[0]);
     $cron->setHour($parts[1]);
     $cron->setDayOfMonth($parts[2]);
     $cron->setMonth($parts[3]);
     $cron->setDayOfWeek($parts[4]);
     $cron->setCommand(\trim($command));
     $cron->setLastRunTime($lastRunTime);
     $cron->setLogSize($logSize);
     $cron->setErrorSize($errorSize);
     $cron->setStatus($status);
     if (isset($comment)) {
         $cron->setComment($comment);
     }
     if (isset($logFile)) {
         $cron->setLogFile($logFile);
     }
     if (isset($errorFile)) {
         $cron->setErrorFile($errorFile);
     }
     return $cron;
 }