standardize EOLs to output diffs properly on Windows
[phpt.git] / src / PHPT / Case / Parser.php
blobbed687b2b3c77a2600eec0f2b2a5ed1c4703bd41
1 <?php
3 class PHPT_Case_Parser
5 private $_reporter = null;
6 private $_validator = null;
8 public function __construct()
10 $this->_validator = new PHPT_Case_Validator_Runnable();
13 public function setReporter($reporter)
15 $this->_reporter = $reporter;
18 public function parse($file)
20 try {
21 return $this->_doParse($file);
22 } catch (Exception $e) {
23 if (is_null($this->_reporter)) {
24 throw $e;
26 $this->_reporter->onParserError($e);
30 private function _doParse($file)
32 $raw_sections = array();
33 $lines = file($file);
34 $section_name = '';
35 $section_data = '';
37 foreach ($lines as $line) {
38 if (preg_match('/^--([^-]+)--$/', trim($line), $matches)) {
39 if (!empty($section_name)) {
40 $raw_sections[$section_name] = $this->_createSection($section_name, $section_data);
43 $section_name = $matches[1];
44 $section_data = '';
45 continue;
48 if (is_null($section_name)) {
49 // nothing to do here yet
50 continue;
53 $section_data .= $line;
56 // set the last section
57 $raw_sections[$section_name] = $this->_createSection($section_name, $section_data);
59 $sections = new PHPT_SectionList($raw_sections);
61 $case = new PHPT_Case($sections, $file);
62 $case->validate('Runnable');
63 return $case;
66 private function _createSection($name, $data)
68 $object_name = 'PHPT_Section_' . $name;
69 if (!class_exists($object_name, true)) {
70 throw new PHPT_Case_Parser_UnknownSectionName($name);
72 return new $object_name(rtrim($data));
76 class PHPT_Case_Parser_UnknownSectionName extends Exception { }