/** * Parses a crontab with commentaries. It assumes the comment for a cron job * comes before the job definition. * * @return Crontab */ protected function _parseCrontab() { $pattern = "/\n(?(DEFINE)\n\n # Subroutine matching the (optional) comment sign preceding a cron definition\n (?<_commentSign>(?:\\#[ \t]*))\n \n # Subroutine matching the time expression\n (?<_expression>\n \n # either match expression\n (?:\n (?:\n [\\d\\*\\/\\,\\-\\%]+ # minute part\n )\n [ \t]+ # space\n (?:\n [\\d\\*\\/\\,\\-\\%]+ # hour part\n )\n [ \t]+ # space\n (?:\n [\\d\\*\\/\\,\\-\\%]+ # day of month part\n )\n [ \t]+ # space\n (?:\n [\\d\\*\\/\\,\\-\\%]+|jan|feb| # month part\n mar|apr|may|jun|jul|aug|\n sep|oct|nov|dec\n )\n [ \t]+ # space\n (?:\n [\\d\\*\\/\\,\\-\\%]+|mon|tue| # day of week part\n wed|thu|fri|sat|sun\n )\n )\n \n # or match specials\n | (?:\n @hourly|@midnight|@daily|\n @weekly|@monthly|@yearly|\n @annually|@reboot\n )\n )\n \n # Subroutine matching the command part (everything except line ending)\n (?<_command>[^\r\n]*)\n\n # Subroutine matching full cron definition (time + command)\n (?<_cronDefinition>\n ^(?&_commentSign)? # comment sign (optional)\n (?&_expression) # time expression part\n \\s+ # space\n (?&_command) # command part\n )\n \n # Subroutine matching comment (which is above cron, by convention)\n (?<_comment>[^\r\n]*)\n)\n\n# Here's where the actual matching happens.\n# Subroutine calls are wrapped by named capture groups so we could\n# easily reference the captured subpatterns later.\n\n# A comment isn't allowed to look like a cron definition, or otherwise\n# commented crons could pass as comments for neighboring crons\n^(?(?!(?&_cronDefinition)) # conditional: not a cron definition\n (?:\n (?&_commentSign) # comment sign preceding comment\n (?P<comment>(?&_comment)) # comment\n \\s* # trailing whitespace, if any\n [\r\n]{1,} # line endings\n )? # comment is, however, optional\n)\n(?P<cronCommentSign>^(?&_commentSign)?) # comment sign for 'paused' crons (optional)\n(?P<expression>(?&_expression)) # time expression\n\\s+ # space\n(?P<command>(?&_command)) # command to be run (everything, but line ending)\n\\s*[\r\n]{1,} # trailing space and line ending\n\n /imx"; $this->_jobs = array(); if (preg_match_all($pattern, $this->_rawTable, $lines, PREG_SET_ORDER)) { foreach ($lines as $lineParts) { $job = new Job(); $job->setRaw($lineParts[0]); $job->setComment(empty($lineParts['comment']) ? null : $lineParts['comment']); $job->setIsPaused($lineParts['cronCommentSign'] != ''); $job->setExpression($lineParts['expression']); $job->setCommand($lineParts['command']); $this->_jobs[] = $job; } } return $this; }
$expression = ExpressionService::createExpression($formData); } else { $expression = $formData['expression']; } // This is an edit if ($hash) { $job = $crontab->findByHash($hash); if (!$job) { $app->render(500, array('error' => true, 'msg' => 'Cron job no longer exists')); $app->stop(); } } else { $job = new Crontab\Job(); } $job->setExpression($expression); $job->setCommand($formData['command']); $job->setComment($formData['name']); if ($hash) { $crontab->update($job); } else { $crontab->add($job); } $crontab->save(); $response = array('error' => false, 'msg' => 'The job has been saved.', 'hash' => $job->getHash()); if ((bool) $formData['returnHtml']) { $response['html'] = $app->config('view')->partial('partials/job.phtml', array('job' => $job)); } $app->render(200, $response); } else { $app->render(500, array('error' => true, 'msg' => $form->getFormattedMessages())); }