|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of WebHelper Parser. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) James <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace WebHelper\Parser; |
|
13
|
|
|
|
|
14
|
|
|
use WebHelper\Parser\Server\ServerInterface; |
|
15
|
|
|
use WebHelper\Parser\Exception\ParserException; |
|
16
|
|
|
use WebHelper\Parser\Exception\InvalidConfigException; |
|
17
|
|
|
use WebHelper\Parser\Parser\Before; |
|
18
|
|
|
use WebHelper\Parser\Parser\After; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Web server configuration generic parser. |
|
22
|
|
|
* |
|
23
|
|
|
* @author James <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class Parser implements ParserInterface |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var Server\ServerInterface a server instance */ |
|
28
|
|
|
private $server; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Compiler a Compiler instance */ |
|
31
|
|
|
private $compiler; |
|
32
|
|
|
|
|
33
|
|
|
/** @var string configuration file */ |
|
34
|
|
|
private $configFile = ''; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array active directives in an array */ |
|
37
|
|
|
protected $activeConfig = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Setter for the server instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @see Server\ServerInterface Server Documentation |
|
43
|
|
|
* |
|
44
|
|
|
* @param Server\ServerInterface $server the server instance |
|
45
|
|
|
*/ |
|
46
|
13 |
|
public function setServer(ServerInterface $server) |
|
47
|
|
|
{ |
|
48
|
13 |
|
$this->server = $server; |
|
49
|
|
|
|
|
50
|
13 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Setter for the compiler instance. |
|
55
|
|
|
* |
|
56
|
|
|
* @param Compiler $compiler the compiler instance |
|
57
|
|
|
*/ |
|
58
|
7 |
|
public function setCompiler(Compiler $compiler) |
|
59
|
|
|
{ |
|
60
|
7 |
|
$this->compiler = $compiler; |
|
61
|
|
|
|
|
62
|
7 |
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Setter for the config file to parse. |
|
67
|
|
|
* |
|
68
|
|
|
* @param string $configFile configuration file |
|
69
|
|
|
* |
|
70
|
|
|
* @throws Exception\ParserException if configuration file is not readable |
|
71
|
|
|
* @throws Exception\InvalidConfigException if active configuration is empty |
|
72
|
|
|
*/ |
|
73
|
12 |
|
public function setConfigFile($configFile = '') |
|
74
|
|
|
{ |
|
75
|
12 |
|
if (!is_readable($configFile)) { |
|
76
|
1 |
|
$this->activeConfig = []; |
|
77
|
|
|
|
|
78
|
1 |
|
throw ParserException::forFileUnreadable($configFile); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
11 |
|
$this->configFile = $configFile; |
|
82
|
11 |
|
if (!$this->parseConfigFile()) { |
|
83
|
2 |
|
throw InvalidConfigException::forEmptyConfig($configFile); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
9 |
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Getter for the server instance. |
|
91
|
|
|
* |
|
92
|
|
|
* @see Server\ServerInterface Server Documentation |
|
93
|
|
|
* |
|
94
|
|
|
* @param Server\ServerInterface the server instance |
|
95
|
|
|
*/ |
|
96
|
8 |
|
public function getServer() |
|
97
|
|
|
{ |
|
98
|
8 |
|
return $this->server; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Getter for the active config main context. |
|
103
|
|
|
* |
|
104
|
|
|
* @return Directive\DirectiveInterface the active config |
|
105
|
|
|
*/ |
|
106
|
7 |
|
public function getActiveConfig() |
|
107
|
|
|
{ |
|
108
|
7 |
|
return $this->compiler->doCompile($this->activeConfig); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Getter for the content of the configuration file. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string content of the configuration file |
|
115
|
|
|
*/ |
|
116
|
11 |
|
public function getOriginalConfig() |
|
117
|
|
|
{ |
|
118
|
11 |
|
return file_get_contents($this->configFile); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Does some extra parsing before the active config turns into an array. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $config a config file content |
|
125
|
|
|
* |
|
126
|
|
|
* @return string a config file content |
|
127
|
|
|
*/ |
|
128
|
11 |
|
protected function beforeExplode($config) |
|
129
|
|
|
{ |
|
130
|
11 |
|
foreach ($this->server->getBeforeMethods() as $beforeMethod) { |
|
131
|
11 |
|
$config = Before::$beforeMethod($config); |
|
132
|
11 |
|
} |
|
133
|
|
|
|
|
134
|
11 |
|
return $config; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Does some extra parsing after the active config has turned into an array. |
|
139
|
|
|
* |
|
140
|
|
|
* @param array $activeConfig an active config |
|
141
|
|
|
* |
|
142
|
|
|
* @return array an active config |
|
143
|
|
|
*/ |
|
144
|
11 |
|
protected function afterExplode(array $activeConfig) |
|
145
|
|
|
{ |
|
146
|
11 |
|
foreach ($this->server->getAfterMethods() as $afterMethod) { |
|
147
|
11 |
|
$activeConfig = After::$afterMethod($activeConfig); |
|
148
|
11 |
|
} |
|
149
|
|
|
|
|
150
|
11 |
|
return $activeConfig; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Comon parsing to both apache and nginx. |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool true if active lines were found |
|
157
|
|
|
*/ |
|
158
|
11 |
|
private function parseConfigFile() |
|
159
|
|
|
{ |
|
160
|
11 |
|
$activeConfig = $this->getOriginalConfig(); |
|
161
|
11 |
|
$activeConfig = $this->beforeExplode($activeConfig); |
|
162
|
|
|
|
|
163
|
|
|
//convert into an array |
|
164
|
11 |
|
$activeConfig = explode("\n", $activeConfig); |
|
165
|
|
|
|
|
166
|
11 |
|
$this->activeConfig = $this->afterExplode($activeConfig); |
|
167
|
|
|
|
|
168
|
11 |
|
return !empty($this->activeConfig); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|