/** * Parse an existing crontab * * @param Crontab $crontab * * @return CrontabFileHandler */ public function parseExistingCrontab(Crontab $crontab) { $result = exec($this->crontabCommand($crontab) . ' -l', $output, $retval); if (!empty($output)) { //\Dsc\System::addMessage(\Dsc\Debug::dump($output)); foreach ($output as $line) { if (trim($line) == '') { continue; } try { $job = \Dsc\Cron\Job::parse($line); $crontab->addJob($job); } catch (\Exception $e) { \Dsc\System::addMessage('Encountered error (' . $e->getMessage() . ') when parsing cron job: ' . $line, 'error'); } } } /* // parsing cron file $process = new Process($this->crontabCommand($crontab).' -l'); $process->run(); foreach ($this->parseString($process->getOutput()) as $job) { $crontab->addJob($job); } $this->error = $process->getErrorOutput(); */ return $this; }
/** * Returns an array of Cron Jobs based on the contents of a file. * * @param string $input * * @return Job[] */ protected function parseString($input) { $jobs = array(); $lines = array_filter(explode(PHP_EOL, $input), function ($line) { return '' != trim($line); }); foreach ($lines as $line) { // if line is not a comment, convert it to a cron if (0 !== \strpos($line, '#')) { $jobs[] = Job::parse($line); } } return $jobs; }