1 | <?php |
||
16 | class Parser |
||
17 | { |
||
18 | |||
19 | const MAGIC_COMMENT_PREFIX = '# HAPHPROXY_COMMENT'; |
||
20 | |||
21 | /** |
||
22 | * @var string |
||
23 | */ |
||
24 | private $filePath; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * Parser constructor. |
||
29 | * |
||
30 | * @param string $filePath |
||
31 | * |
||
32 | * @throws FileNotFoundException if the specified file could not be found or is not readable |
||
33 | */ |
||
34 | public function __construct($filePath) |
||
35 | { |
||
36 | if (!file_exists($filePath) || !is_readable($filePath)) { |
||
37 | throw new FileNotFoundException($filePath . ' not found or is not readable'); |
||
38 | } |
||
39 | |||
40 | $this->filePath = $filePath; |
||
41 | } |
||
42 | |||
43 | |||
44 | /** |
||
45 | * @return Configuration |
||
46 | */ |
||
47 | public function parse() |
||
48 | { |
||
49 | $configuration = new Configuration(); |
||
50 | $currentSection = null; |
||
51 | |||
52 | // Parse the preface |
||
53 | $configuration->setPreface($this->parsePreface()); |
||
54 | |||
55 | foreach ($this->getNormalizedConfigurationLines() as $line) { |
||
56 | // Check for section changes |
||
57 | $newSection = Factory::makeFactory($line); |
||
58 | |||
59 | if ($newSection !== null) { |
||
60 | $currentSection = $newSection; |
||
61 | $configuration->addSection($currentSection); |
||
62 | |||
63 | continue; |
||
64 | } |
||
65 | |||
66 | // Parse the current section line by line |
||
67 | if ($currentSection !== null) { |
||
68 | $this->parseSectionLine($currentSection, $line); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $configuration; |
||
73 | } |
||
74 | |||
75 | |||
76 | /** |
||
77 | * Reads the configuration and yields one line at a time |
||
78 | * |
||
79 | * @return \Generator |
||
80 | */ |
||
81 | private function getConfigurationLines() |
||
82 | { |
||
83 | $handle = fopen($this->filePath, "r"); |
||
84 | |||
85 | while (($line = fgets($handle)) !== false) { |
||
86 | yield $line; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * @return \Generator |
||
93 | */ |
||
94 | private function getNormalizedConfigurationLines() |
||
95 | { |
||
96 | foreach ($this->getConfigurationLines() as $line) { |
||
97 | $line = $this->normalizeLine($line); |
||
98 | |||
99 | if (!empty($line)) { |
||
100 | yield $line; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * @param string $line |
||
108 | * |
||
109 | * @return string |
||
110 | */ |
||
111 | private function normalizeLine($line) |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @param string $line |
||
119 | * |
||
120 | * @return bool if the line is a comment |
||
121 | */ |
||
122 | private function isComment($line) |
||
126 | |||
127 | |||
128 | /** |
||
129 | * @param string $line |
||
130 | * |
||
131 | * @return bool whether the line is a magic comment |
||
132 | */ |
||
133 | private function isMagicComment($line) |
||
137 | |||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | private function parsePreface() |
||
156 | |||
157 | |||
158 | /** |
||
159 | * @param AbstractSection $section |
||
160 | * @param string $line |
||
161 | */ |
||
162 | private function parseSectionLine($section, $line) |
||
171 | |||
172 | |||
173 | /** |
||
174 | * @param string $line |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | private function parseMagicComment($line) |
||
182 | |||
183 | |||
184 | /** |
||
185 | * @param string $line |
||
186 | * |
||
187 | * @return Parameter |
||
188 | */ |
||
189 | private function parseParameter($line) |
||
203 | |||
204 | } |
||
205 |