1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jalle19\HaPHProxy; |
4
|
|
|
|
5
|
|
|
use Jalle19\HaPHProxy\Exception\FileNotFoundException; |
6
|
|
|
use Jalle19\HaPHProxy\Parameter\Parameter; |
7
|
|
|
use Jalle19\HaPHProxy\Section\Factory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Parser |
11
|
|
|
* @package Jalle19\HaPHProxy |
12
|
|
|
* @author Sam Stenvall <[email protected]> |
13
|
|
|
* @license GNU General Public License 2.0+ |
14
|
|
|
*/ |
15
|
|
|
class Parser |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $filePath; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Parser constructor. |
26
|
|
|
* |
27
|
|
|
* @param string $filePath |
28
|
|
|
* |
29
|
|
|
* @throws FileNotFoundException if the specified file could not be found or is not readable |
30
|
|
|
*/ |
31
|
|
|
public function __construct($filePath) |
32
|
|
|
{ |
33
|
|
|
if (!file_exists($filePath) || !is_readable($filePath)) { |
34
|
|
|
throw new FileNotFoundException($filePath . ' not found or is not readable'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$this->filePath = $filePath; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return Configuration |
43
|
|
|
*/ |
44
|
|
|
public function parse() |
45
|
|
|
{ |
46
|
|
|
$configuration = new Configuration(); |
47
|
|
|
$currentSection = null; |
48
|
|
|
|
49
|
|
|
foreach ($this->getNormalizedConfigurationLines() as $line) { |
50
|
|
|
// Check for section changes |
51
|
|
|
$newSection = Factory::makeFactory($line); |
52
|
|
|
|
53
|
|
|
if ($newSection !== null) { |
54
|
|
|
$currentSection = $newSection; |
55
|
|
|
$configuration->addSection($currentSection); |
56
|
|
|
|
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Parse parameters into the current section |
61
|
|
|
if ($currentSection !== null) { |
62
|
|
|
$currentSection->addParameter(self::parseParameter($line)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $configuration; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Reads the configuration and yields one line at a time |
72
|
|
|
* |
73
|
|
|
* @return \Generator |
74
|
|
|
*/ |
75
|
|
|
private function getConfigurationLines() |
76
|
|
|
{ |
77
|
|
|
$handle = fopen($this->filePath, "r"); |
78
|
|
|
|
79
|
|
|
while (($line = fgets($handle)) !== false) { |
80
|
|
|
yield $line; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return array the normalized configuration lines |
87
|
|
|
*/ |
88
|
|
|
private function getNormalizedConfigurationLines() |
89
|
|
|
{ |
90
|
|
|
$lines = []; |
91
|
|
|
|
92
|
|
|
foreach ($this->getConfigurationLines() as $line) { |
93
|
|
|
$line = self::normalizeLine($line); |
94
|
|
|
|
95
|
|
|
if (self::shouldOmitLine($line)) { |
96
|
|
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$lines[] = $line; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $lines; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $line |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private static function normalizeLine($line) |
112
|
|
|
{ |
113
|
|
|
return preg_replace('/\s+/', ' ', trim($line)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $line |
119
|
|
|
* |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
private static function shouldOmitLine($line) |
123
|
|
|
{ |
124
|
|
|
if (substr($line, 0, 1) === '#') { |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (empty($line)) { |
129
|
|
|
return true; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $line |
138
|
|
|
* |
139
|
|
|
* @return Parameter |
140
|
|
|
*/ |
141
|
|
|
private static function parseParameter($line) |
142
|
|
|
{ |
143
|
|
|
$words = explode(' ', $line, 2); |
144
|
|
|
|
145
|
|
|
if (count($words) > 1) { |
146
|
|
|
list($name, $value) = $words; |
147
|
|
|
|
148
|
|
|
return new Parameter($name, $value); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
list($name) = $words; |
152
|
|
|
|
153
|
|
|
return new Parameter($name); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|