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\Server; |
13
|
|
|
|
14
|
|
|
use WebHelper\Parser\Parser\Checker; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A web server instance. |
18
|
|
|
* |
19
|
|
|
* @author James <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Server implements ServerInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* a Checker instance. |
25
|
|
|
* |
26
|
|
|
* @var \WebHelper\Parser\Parser\Checker |
27
|
|
|
*/ |
28
|
|
|
private $checker; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The filesystem path where the web server is installed. |
32
|
|
|
* |
33
|
|
|
* It has to be an absolute path. |
34
|
|
|
* |
35
|
|
|
* Apache httpd server does not accept a relative prefix path at compilation. |
36
|
|
|
* Nginx does, but this is a very risky practice... |
37
|
|
|
* So relative prefix path in nginx configuration will not be considered |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $prefix = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The string to match as a starting multi-line directive. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $startMultiLine = ''; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The string to match as an ending multi-line directive. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $endMultiLine = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* The string to match a simple directive. |
59
|
|
|
* |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $simpleDirective = ''; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* binaries that can be used to control the webserver. |
66
|
|
|
* |
67
|
|
|
* @var array |
68
|
|
|
*/ |
69
|
|
|
private $binaries = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* the parameter string to use to detect version and config file. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
private $detectionParameter = ''; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* The ordered list of methods to apply before convertion. |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
private $beforeMethods = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The ordered list of methods to apply after convertion. |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
private $afterMethods = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Sets the Checker instance. |
94
|
|
|
* |
95
|
|
|
* @param Checker $checker a Checker instance |
96
|
|
|
*/ |
97
|
29 |
|
public function setChecker(Checker $checker) |
98
|
|
|
{ |
99
|
29 |
|
$this->checker = $checker; |
100
|
|
|
|
101
|
29 |
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Confirms if the server instance has valid parameters. |
106
|
|
|
* |
107
|
|
|
* @return bool true if all parameters are initialized, false otherwise |
108
|
|
|
*/ |
109
|
2 |
|
public function isValid() |
110
|
|
|
{ |
111
|
2 |
|
$valid = $this->prefix != ''; |
112
|
2 |
|
$valid = $valid && $this->startMultiLine != ''; |
113
|
2 |
|
$valid = $valid && $this->endMultiLine != ''; |
114
|
2 |
|
$valid = $valid && $this->simpleDirective != ''; |
115
|
|
|
|
116
|
2 |
|
return $valid; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Getter for the prefix. |
121
|
|
|
* |
122
|
|
|
* @return string the filesystem path where the web server is installed |
123
|
|
|
*/ |
124
|
2 |
|
public function getPrefix() |
125
|
|
|
{ |
126
|
2 |
|
return $this->prefix; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the prefix of a server instance. |
131
|
|
|
* |
132
|
|
|
* @throws ServerException if the prefix is invalid |
133
|
|
|
* |
134
|
|
|
* @param string $prefix the filesystem path where the web server is installed |
135
|
|
|
*/ |
136
|
11 |
|
public function setPrefix($prefix) |
137
|
|
|
{ |
138
|
11 |
|
if (!$this->checker->setString($prefix)->getString()) { |
139
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be a string. Got: %s'); |
140
|
|
|
} |
141
|
|
|
|
142
|
10 |
|
if (!$this->checker->isValidAbsolutePath()) { |
143
|
2 |
|
throw ServerException::forInvalidPrefix( |
144
|
2 |
|
$prefix, |
145
|
|
|
'The path is expected to be absolute and an existing directory. Got: %s' |
146
|
2 |
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
8 |
|
$this->prefix = $prefix; |
150
|
|
|
|
151
|
8 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Gets the regexp that will match the starting block directives. |
156
|
|
|
* |
157
|
|
|
* @return string the regexp that will match the starting block directives |
158
|
|
|
*/ |
159
|
8 |
|
public function getStartMultiLine() |
160
|
|
|
{ |
161
|
8 |
|
return $this->startMultiLine; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets the regexp that will match the starting block directives. |
166
|
|
|
* |
167
|
|
|
* @param string $startMultiLine the regexp that will match the starting block directives |
168
|
|
|
*/ |
169
|
13 |
|
public function setStartMultiLine($startMultiLine) |
170
|
|
|
{ |
171
|
13 |
|
if ($this->isValidDirective( |
172
|
13 |
|
$startMultiLine, |
173
|
13 |
|
'The starting block directive matcher is expected to be a string. Got: %s', |
174
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
175
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
176
|
13 |
|
)) { |
177
|
8 |
|
$this->startMultiLine = $startMultiLine; |
178
|
8 |
|
} |
179
|
|
|
|
180
|
8 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Gets the regexp that will match the ending block directives. |
185
|
|
|
* |
186
|
|
|
* @return string the regexp that will match the ending block directives |
187
|
|
|
*/ |
188
|
8 |
|
public function getEndMultiLine() |
189
|
|
|
{ |
190
|
8 |
|
return $this->endMultiLine; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Sets the regexp that will match the ending block directives. |
195
|
|
|
* |
196
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
197
|
|
|
*/ |
198
|
10 |
|
public function setEndMultiLine($endMultiLine) |
199
|
|
|
{ |
200
|
10 |
|
if (!$this->checker->setString($endMultiLine)->getString()) { |
201
|
1 |
|
throw ServerException::forInvalidMatcher( |
202
|
1 |
|
$endMultiLine, |
203
|
|
|
'The ending block directive matcher is expected to be a string. Got: %s' |
204
|
1 |
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
9 |
|
if (!$this->checker->isValidRegex()) { |
208
|
1 |
|
throw ServerException::forInvalidMatcher( |
209
|
1 |
|
$endMultiLine, |
210
|
|
|
'The ending block directive matcher is expected to be a regexp.' |
211
|
1 |
|
); |
212
|
|
|
} |
213
|
|
|
|
214
|
8 |
|
$this->endMultiLine = $endMultiLine; |
215
|
|
|
|
216
|
8 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets the regexp that will match the simple directives. |
221
|
|
|
* |
222
|
|
|
* @return string the regexp that will match the simple directives |
223
|
|
|
*/ |
224
|
8 |
|
public function getSimpleDirective() |
225
|
|
|
{ |
226
|
8 |
|
return $this->simpleDirective; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets the regexp that will match the simple directives. |
231
|
|
|
* |
232
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
233
|
|
|
*/ |
234
|
13 |
|
public function setSimpleDirective($simpleDirective) |
235
|
|
|
{ |
236
|
13 |
|
if ($this->isValidDirective( |
237
|
13 |
|
$simpleDirective, |
238
|
13 |
|
'The simple directive matcher is expected to be a string. Got: %s', |
239
|
|
|
'The simple directive matcher is expected to be a regexp '. |
240
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
241
|
13 |
|
)) { |
242
|
8 |
|
$this->simpleDirective = $simpleDirective; |
243
|
8 |
|
} |
244
|
|
|
|
245
|
8 |
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Confirms if a directive matcher is a valid regex. |
250
|
|
|
* |
251
|
|
|
* @param string $directive the directive matcher to check |
252
|
|
|
* @param string $message1 message exception if the matcher is not a string |
253
|
|
|
* @param string $message2 message exception if the matcher is not a valid regex |
254
|
|
|
* |
255
|
|
|
* @throws ServerException if the directive matcher is invalid |
256
|
|
|
* |
257
|
|
|
* @return bool true if the directive matcher is valid |
258
|
|
|
*/ |
259
|
19 |
|
private function isValidDirective($directive, $message1, $message2) |
260
|
|
|
{ |
261
|
19 |
|
if (!$this->checker->setString($directive)->getString()) { |
262
|
2 |
|
throw ServerException::forInvalidMatcher( |
263
|
2 |
|
$directive, |
264
|
|
|
$message1 |
265
|
2 |
|
); |
266
|
|
|
} |
267
|
|
|
|
268
|
17 |
|
if (!$this->checker->hasKeyAndValueSubPattern()) { |
269
|
8 |
|
throw ServerException::forInvalidMatcher( |
270
|
8 |
|
$directive, |
271
|
|
|
$message2 |
272
|
8 |
|
); |
273
|
|
|
} |
274
|
|
|
|
275
|
9 |
|
return true; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Gets the list of binaries that can be run to analyze. |
280
|
|
|
* |
281
|
|
|
* @return array the list of binaries that can be run |
282
|
|
|
*/ |
283
|
1 |
|
public function getBinaries() |
284
|
|
|
{ |
285
|
1 |
|
return $this->binaries; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Sets the list of binaries that can be run to analyze. |
290
|
|
|
* |
291
|
|
|
* @param array $binaries list of controlers |
292
|
|
|
*/ |
293
|
8 |
|
public function setBinaries(array $binaries) |
294
|
|
|
{ |
295
|
8 |
|
$this->binaries = $binaries; |
296
|
|
|
|
297
|
8 |
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Sets the parameter string to use to detect version and config file. |
302
|
|
|
* |
303
|
|
|
* @param string $parameter parameter string |
304
|
|
|
*/ |
305
|
7 |
|
public function setDetectionParameter($parameter = '') |
306
|
|
|
{ |
307
|
7 |
|
$this->detectionParameter = $parameter; |
308
|
|
|
|
309
|
7 |
|
return $this; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Gets the ordered list of methods to apply before the config file turns into an array. |
314
|
|
|
* |
315
|
|
|
* @return array the ordered list of methods to apply before convertion |
316
|
|
|
*/ |
317
|
11 |
|
public function getBeforeMethods() |
318
|
|
|
{ |
319
|
11 |
|
return $this->beforeMethods; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Sets the ordered list of methods to apply before the config file turns into an array. |
324
|
|
|
* |
325
|
|
|
* @param array $methods the ordered list of methods to apply before convertion |
326
|
|
|
*/ |
327
|
13 |
|
public function setBeforeMethods(array $methods) |
328
|
|
|
{ |
329
|
13 |
|
$this->beforeMethods = $methods; |
330
|
|
|
|
331
|
13 |
|
return $this; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Gets the ordered list of methods to apply after the config file has turned into an array. |
336
|
|
|
* |
337
|
|
|
* @return array the ordered list of methods to apply after convertion |
338
|
|
|
*/ |
339
|
11 |
|
public function getAfterMethods() |
340
|
|
|
{ |
341
|
11 |
|
return $this->afterMethods; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Sets the ordered list of methods to apply after the config file has turned into an array. |
346
|
|
|
* |
347
|
|
|
* @param array $methods the ordered list of methods to apply after convertion |
348
|
|
|
*/ |
349
|
13 |
|
public function setAfterMethods(array $methods) |
350
|
|
|
{ |
351
|
13 |
|
$this->afterMethods = $methods; |
352
|
|
|
|
353
|
13 |
|
return $this; |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
|