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
|
|
|
* The string to match an inclusion directive. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $inclusionDirective = ''; |
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* binaries that can be used to control the webserver. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
private $binaries = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* the parameter string to use to detect version and config file. |
80
|
|
|
* |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
private $detectionParameter = ''; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The ordered list of methods to apply before convertion. |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
private $beforeMethods = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* The ordered list of methods to apply after convertion. |
94
|
|
|
* |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
private $afterMethods = []; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* The simple directive syntax when dumped. |
101
|
|
|
* |
102
|
|
|
* @var string |
103
|
|
|
*/ |
104
|
|
|
private $dumperSimpleDirective = ''; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* The starting block directive syntax when dumped. |
108
|
|
|
* |
109
|
|
|
* @var string |
110
|
|
|
*/ |
111
|
|
|
private $dumperStartDirective = ''; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* The ending block directive syntax when dumped. |
115
|
|
|
* |
116
|
|
|
* @var string |
117
|
|
|
*/ |
118
|
|
|
private $dumperEndDirective = ''; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* The known directives of the server |
122
|
|
|
* |
123
|
|
|
* @var array |
124
|
|
|
*/ |
125
|
|
|
private $knownDirectives = []; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Sets the Checker instance. |
129
|
|
|
* |
130
|
|
|
* @param Checker $checker a Checker instance |
131
|
|
|
*/ |
132
|
30 |
|
public function setChecker(Checker $checker) |
133
|
|
|
{ |
134
|
30 |
|
$this->checker = $checker; |
135
|
|
|
|
136
|
30 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Confirms if the server instance has valid parameters. |
141
|
|
|
* |
142
|
|
|
* @return bool true if all parameters are initialized, false otherwise |
143
|
|
|
*/ |
144
|
2 |
|
public function isValid() |
145
|
|
|
{ |
146
|
2 |
|
$valid = $this->prefix != ''; |
147
|
2 |
|
$valid = $valid && $this->startMultiLine != ''; |
148
|
2 |
|
$valid = $valid && $this->endMultiLine != ''; |
149
|
2 |
|
$valid = $valid && $this->simpleDirective != ''; |
150
|
|
|
|
151
|
2 |
|
return $valid; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Getter for the prefix. |
156
|
|
|
* |
157
|
|
|
* @return string the filesystem path where the web server is installed |
158
|
|
|
*/ |
159
|
4 |
|
public function getPrefix() |
160
|
|
|
{ |
161
|
4 |
|
return $this->prefix; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets the prefix of a server instance. |
166
|
|
|
* |
167
|
|
|
* |
168
|
|
|
* @param string $prefix the filesystem path where the web server is installed |
169
|
|
|
* |
170
|
|
|
* @throws ServerException if the prefix is invalid |
171
|
|
|
*/ |
172
|
11 |
|
public function setPrefix($prefix) |
173
|
|
|
{ |
174
|
11 |
|
if (!$this->checker->setString($prefix)->getString()) { |
175
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be a string. Got: %s'); |
176
|
|
|
} |
177
|
|
|
|
178
|
10 |
|
if (!$this->checker->isValidAbsolutePath()) { |
179
|
2 |
|
throw ServerException::forInvalidPrefix( |
180
|
2 |
|
$prefix, |
181
|
|
|
'The path is expected to be absolute and an existing directory. Got: %s' |
182
|
2 |
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
8 |
|
$this->prefix = $prefix; |
186
|
|
|
|
187
|
8 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Gets the regexp that will match the starting block directives. |
192
|
|
|
* |
193
|
|
|
* @return string the regexp that will match the starting block directives |
194
|
|
|
*/ |
195
|
8 |
|
public function getStartMultiLine() |
196
|
|
|
{ |
197
|
8 |
|
return $this->startMultiLine; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets the regexp that will match the starting block directives. |
202
|
|
|
* |
203
|
|
|
* @param string $startMultiLine the regexp that will match the starting block directives |
204
|
|
|
*/ |
205
|
13 |
|
public function setStartMultiLine($startMultiLine) |
206
|
|
|
{ |
207
|
13 |
|
if ($this->isValidDirective( |
208
|
13 |
|
$startMultiLine, |
209
|
13 |
|
'The starting block directive matcher is expected to be a string. Got: %s', |
210
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
211
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
212
|
13 |
|
)) { |
213
|
8 |
|
$this->startMultiLine = $startMultiLine; |
214
|
8 |
|
} |
215
|
|
|
|
216
|
8 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets the regexp that will match the ending block directives. |
221
|
|
|
* |
222
|
|
|
* @return string the regexp that will match the ending block directives |
223
|
|
|
*/ |
224
|
8 |
|
public function getEndMultiLine() |
225
|
|
|
{ |
226
|
8 |
|
return $this->endMultiLine; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Sets the regexp that will match the ending block directives. |
231
|
|
|
* |
232
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
233
|
|
|
*/ |
234
|
10 |
|
public function setEndMultiLine($endMultiLine) |
235
|
|
|
{ |
236
|
10 |
|
$this->endMultiLine = $this->setRegexDirective( |
237
|
10 |
|
$endMultiLine, |
238
|
10 |
|
'The ending block directive matcher is expected to be a string. Got: %s', |
239
|
|
|
'The ending block directive matcher is expected to be a regexp.' |
240
|
10 |
|
); |
241
|
|
|
|
242
|
8 |
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Gets the regexp that will match the simple directives. |
247
|
|
|
* |
248
|
|
|
* @return string the regexp that will match the simple directives |
249
|
|
|
*/ |
250
|
8 |
|
public function getSimpleDirective() |
251
|
|
|
{ |
252
|
8 |
|
return $this->simpleDirective; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Sets the regexp that will match the simple directives. |
257
|
|
|
* |
258
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
259
|
|
|
*/ |
260
|
13 |
|
public function setSimpleDirective($simpleDirective) |
261
|
|
|
{ |
262
|
13 |
|
if ($this->isValidDirective( |
263
|
13 |
|
$simpleDirective, |
264
|
13 |
|
'The simple directive matcher is expected to be a string. Got: %s', |
265
|
|
|
'The simple directive matcher is expected to be a regexp '. |
266
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
267
|
13 |
|
)) { |
268
|
8 |
|
$this->simpleDirective = $simpleDirective; |
269
|
8 |
|
} |
270
|
|
|
|
271
|
8 |
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Sets the regular expression directive. |
276
|
|
|
* |
277
|
|
|
* @param string $directive the directive string |
278
|
|
|
* @param string $message1 message exception if the matcher is not a string |
279
|
|
|
* @param string $message2 message exception if the matcher is not a valid regex |
280
|
|
|
* |
281
|
|
|
* @throws ServerException if the directive matcher is invalid |
282
|
|
|
* |
283
|
|
|
* @return string the regular expression directive |
284
|
|
|
*/ |
285
|
10 |
View Code Duplication |
private function setRegexDirective($directive, $message1, $message2) |
|
|
|
|
286
|
|
|
{ |
287
|
10 |
|
if (!$this->checker->setString($directive)->getString()) { |
288
|
1 |
|
throw ServerException::forInvalidMatcher( |
289
|
1 |
|
$directive, |
290
|
|
|
$message1 |
291
|
1 |
|
); |
292
|
|
|
} |
293
|
|
|
|
294
|
9 |
|
if (!$this->checker->isValidRegex()) { |
295
|
1 |
|
throw ServerException::forInvalidMatcher( |
296
|
1 |
|
$directive, |
297
|
|
|
$message2 |
298
|
1 |
|
); |
299
|
|
|
} |
300
|
|
|
|
301
|
8 |
|
return $directive; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Confirms if a directive matcher is a valid regex. |
306
|
|
|
* |
307
|
|
|
* @param string $directive the directive matcher to check |
308
|
|
|
* @param string $message1 message exception if the matcher is not a string |
309
|
|
|
* @param string $message2 message exception if the matcher is not a valid regex |
310
|
|
|
* |
311
|
|
|
* @throws ServerException if the directive matcher is invalid |
312
|
|
|
* |
313
|
|
|
* @return bool true if the directive matcher is valid |
314
|
|
|
*/ |
315
|
19 |
View Code Duplication |
private function isValidDirective($directive, $message1, $message2) |
|
|
|
|
316
|
|
|
{ |
317
|
19 |
|
if (!$this->checker->setString($directive)->getString()) { |
318
|
2 |
|
throw ServerException::forInvalidMatcher( |
319
|
2 |
|
$directive, |
320
|
|
|
$message1 |
321
|
2 |
|
); |
322
|
|
|
} |
323
|
|
|
|
324
|
17 |
|
if (!$this->checker->hasKeyAndValueSubPattern()) { |
325
|
8 |
|
throw ServerException::forInvalidMatcher( |
326
|
8 |
|
$directive, |
327
|
|
|
$message2 |
328
|
8 |
|
); |
329
|
|
|
} |
330
|
|
|
|
331
|
9 |
|
return true; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* Gets the list of binaries that can be run to analyze. |
336
|
|
|
* |
337
|
|
|
* @return array the list of binaries that can be run |
338
|
|
|
*/ |
339
|
1 |
|
public function getBinaries() |
340
|
|
|
{ |
341
|
1 |
|
return $this->binaries; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Sets the list of binaries that can be run to analyze. |
346
|
|
|
* |
347
|
|
|
* @param array $binaries list of controlers |
348
|
|
|
*/ |
349
|
8 |
|
public function setBinaries(array $binaries) |
350
|
|
|
{ |
351
|
8 |
|
$this->binaries = $binaries; |
352
|
|
|
|
353
|
8 |
|
return $this; |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Gets the parameter string to use to detect version and config file. |
358
|
|
|
* |
359
|
|
|
* @return string parameter string |
360
|
|
|
*/ |
361
|
1 |
|
public function getDetectionParameter() |
362
|
|
|
{ |
363
|
1 |
|
return $this->detectionParameter; |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Sets the parameter string to use to detect version and config file. |
368
|
|
|
* |
369
|
|
|
* @param string $parameter parameter string |
370
|
|
|
*/ |
371
|
8 |
|
public function setDetectionParameter($parameter = '') |
372
|
|
|
{ |
373
|
8 |
|
$this->detectionParameter = $parameter; |
374
|
|
|
|
375
|
8 |
|
return $this; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Gets the ordered list of methods to apply before the config file turns into an array. |
380
|
|
|
* |
381
|
|
|
* @return array the ordered list of methods to apply before convertion |
382
|
|
|
*/ |
383
|
14 |
|
public function getBeforeMethods() |
384
|
|
|
{ |
385
|
14 |
|
return $this->beforeMethods; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Sets the ordered list of methods to apply before the config file turns into an array. |
390
|
|
|
* |
391
|
|
|
* @param array $methods the ordered list of methods to apply before convertion |
392
|
|
|
*/ |
393
|
13 |
|
public function setBeforeMethods(array $methods) |
394
|
|
|
{ |
395
|
13 |
|
$this->beforeMethods = $methods; |
396
|
|
|
|
397
|
13 |
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* Gets the ordered list of methods to apply after the config file has turned into an array. |
402
|
|
|
* |
403
|
|
|
* @return array the ordered list of methods to apply after convertion |
404
|
|
|
*/ |
405
|
14 |
|
public function getAfterMethods() |
406
|
|
|
{ |
407
|
14 |
|
return $this->afterMethods; |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
/** |
411
|
|
|
* Sets the ordered list of methods to apply after the config file has turned into an array. |
412
|
|
|
* |
413
|
|
|
* @param array $methods the ordered list of methods to apply after convertion |
414
|
|
|
*/ |
415
|
13 |
|
public function setAfterMethods(array $methods) |
416
|
|
|
{ |
417
|
13 |
|
$this->afterMethods = $methods; |
418
|
|
|
|
419
|
13 |
|
return $this; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Gets the simple directive syntax when dumped. |
424
|
|
|
* |
425
|
|
|
* @return string the simple directive syntax when dumped |
426
|
|
|
*/ |
427
|
1 |
|
public function getDumperSimpleDirective() |
428
|
|
|
{ |
429
|
1 |
|
return $this->dumperSimpleDirective; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Gets the starting block directive syntax when dumped. |
434
|
|
|
* |
435
|
|
|
* @return string the starting block directive syntax when dumped |
436
|
|
|
*/ |
437
|
1 |
|
public function getDumperStartDirective() |
438
|
|
|
{ |
439
|
1 |
|
return $this->dumperStartDirective; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Gets the ending block directive syntax when dumped. |
444
|
|
|
* |
445
|
|
|
* @return string the ending block directive syntax when dumped |
446
|
|
|
*/ |
447
|
1 |
|
public function getDumperEndDirective() |
448
|
|
|
{ |
449
|
1 |
|
return $this->dumperEndDirective; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Sets the simple directive syntax when dumped. |
454
|
|
|
* |
455
|
|
|
* @param string $simpleDirective the simple directive syntax when dumped |
456
|
|
|
*/ |
457
|
8 |
|
public function setDumperSimpleDirective($simpleDirective) |
458
|
|
|
{ |
459
|
8 |
|
$this->dumperSimpleDirective = $simpleDirective; |
460
|
|
|
|
461
|
8 |
|
return $this; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* Sets the starting block directive syntax when dumped. |
466
|
|
|
* |
467
|
|
|
* @param string $startMultiLine the starting block directive syntax when dumped |
468
|
|
|
*/ |
469
|
8 |
|
public function setDumperStartDirective($startMultiLine) |
470
|
|
|
{ |
471
|
8 |
|
$this->dumperStartDirective = $startMultiLine; |
472
|
|
|
|
473
|
8 |
|
return $this; |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Sets the ending block directive syntax when dumped. |
478
|
|
|
* |
479
|
|
|
* @param string $endMultiLine the ending block directive syntax when dumped |
480
|
|
|
*/ |
481
|
8 |
|
public function setDumperEndDirective($endMultiLine) |
482
|
|
|
{ |
483
|
8 |
|
$this->dumperEndDirective = $endMultiLine; |
484
|
|
|
|
485
|
8 |
|
return $this; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Gets the known directives of the server. |
490
|
|
|
* |
491
|
|
|
* @return array the known directives of the server |
492
|
|
|
*/ |
493
|
9 |
|
public function getKnownDirectives() |
494
|
|
|
{ |
495
|
9 |
|
return $this->knownDirectives; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* Sets the known directives of the server. |
500
|
|
|
* |
501
|
|
|
* @param array $knownDirectives the known directives of the server |
502
|
|
|
*/ |
503
|
7 |
|
public function setKnownDirectives(array $knownDirectives) |
504
|
|
|
{ |
505
|
7 |
|
$this->knownDirectives = $knownDirectives; |
506
|
|
|
|
507
|
7 |
|
return $this; |
508
|
|
|
} |
509
|
|
|
} |
510
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.