Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.CrontabParser.php
Go to the documentation of this file.
1 <?php
2 namespace Dcp;
3 
5 {
6  const REGEXP_SECTION_BEGIN = '/^#\s+BEGIN:FREEDOM_CRONTAB:(?P<contextRoot>[^:]+):(?P<file>.*)$/';
7  const REGEXP_SECTION_END = '/^#\s+END:FREEDOM_CRONTAB:(?P<contextRoot>[^:]+):(?P<file>.*)$/';
8  /**
9  * Parse a crontab into Text and Section elements
10  *
11  * @param string $crontab
12  * @return CrontabDocument
13  * @throws CrontabParserException
14  */
15  public function parse($crontab)
16  {
17  $lines = explode("\n", $crontab);
18  /**
19  * @var $currentSection CrontabSectionElement
20  */
21  $currentSection = null;
22  $crontabDocument = new CrontabDocument();
23  $i = 0;
24  while ($i < count($lines)) {
25  $line = $lines[$i];
26  if ($currentSection !== null) {
27  if (preg_match(self::REGEXP_SECTION_END, $line, $m)) {
28  if (!$currentSection->match($m['contextRoot'], $m['file'])) {
29  throw new CrontabParserException(sprintf("Section end mismatch at line #%d: expecting '%s:%s', found '%s:%s'", ($i + 1) , $currentSection->contextRoot, $currentSection->file, $m['contextRoot'], $m['file']));
30  }
31  $crontabDocument->appendChild($currentSection);
32  $currentSection = null;
33  } else {
34  $newTextElement = new CrontabTextElement($line);
35  $currentSection->appendChild($newTextElement);
36  }
37  } else {
38  if (preg_match(self::REGEXP_SECTION_BEGIN, $line, $m)) {
39  if ($currentSection !== null) {
40  throw new CrontabParserException(sprintf("Beginning of section found while already in a section at line #%d", ($i+1)));
41  }
42  $currentSection = new CrontabSectionElement($m['contextRoot'], $m['file']);
43  } else {
44  $newTextElement = new CrontabTextElement($line);
45  $crontabDocument->appendChild($newTextElement);
46  }
47  }
48  $i++;
49  }
50  if ($currentSection !== null) {
51  throw new CrontabParserException(sprintf("Section end not found at line #%d", ($i+1)));
52  }
53 
54  return $crontabDocument;
55  }
56 }
← centre documentaire © anakeen