|
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 |
View Code Duplication |
public function setStartMultiLine($startMultiLine) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
13 |
|
if (!$this->checker->setString($startMultiLine)->getString()) { |
|
172
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
173
|
1 |
|
$startMultiLine, |
|
174
|
|
|
'The starting block directive matcher is expected to be a string. Got: %s' |
|
175
|
1 |
|
); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
12 |
|
if (!$this->checker->hasKeyAndValueSubPattern()) { |
|
179
|
4 |
|
throw ServerException::forInvalidMatcher( |
|
180
|
4 |
|
$startMultiLine, |
|
181
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
|
182
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
183
|
4 |
|
); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
8 |
|
$this->startMultiLine = $startMultiLine; |
|
187
|
|
|
|
|
188
|
8 |
|
return $this; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Gets the regexp that will match the ending block directives. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string the regexp that will match the ending block directives |
|
195
|
|
|
*/ |
|
196
|
8 |
|
public function getEndMultiLine() |
|
197
|
|
|
{ |
|
198
|
8 |
|
return $this->endMultiLine; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Sets the regexp that will match the ending block directives. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
|
205
|
|
|
*/ |
|
206
|
10 |
View Code Duplication |
public function setEndMultiLine($endMultiLine) |
|
|
|
|
|
|
207
|
|
|
{ |
|
208
|
10 |
|
if (!$this->checker->setString($endMultiLine)->getString()) { |
|
209
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
210
|
1 |
|
$endMultiLine, |
|
211
|
|
|
'The ending block directive matcher is expected to be a string. Got: %s' |
|
212
|
1 |
|
); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
9 |
|
if (!$this->checker->isValidRegex()) { |
|
216
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
217
|
1 |
|
$endMultiLine, |
|
218
|
|
|
'The ending block directive matcher is expected to be a regexp.' |
|
219
|
1 |
|
); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
8 |
|
$this->endMultiLine = $endMultiLine; |
|
223
|
|
|
|
|
224
|
8 |
|
return $this; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Gets the regexp that will match the simple directives. |
|
229
|
|
|
* |
|
230
|
|
|
* @return string the regexp that will match the simple directives |
|
231
|
|
|
*/ |
|
232
|
8 |
|
public function getSimpleDirective() |
|
233
|
|
|
{ |
|
234
|
8 |
|
return $this->simpleDirective; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Sets the regexp that will match the simple directives. |
|
239
|
|
|
* |
|
240
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
|
241
|
|
|
*/ |
|
242
|
13 |
View Code Duplication |
public function setSimpleDirective($simpleDirective) |
|
|
|
|
|
|
243
|
|
|
{ |
|
244
|
13 |
|
if (!$this->checker->setString($simpleDirective)->getString()) { |
|
245
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
246
|
1 |
|
$simpleDirective, |
|
247
|
|
|
'The simple directive matcher is expected to be a string. Got: %s' |
|
248
|
1 |
|
); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
12 |
|
if (!$this->checker->hasKeyAndValueSubPattern()) { |
|
252
|
4 |
|
throw ServerException::forInvalidMatcher( |
|
253
|
4 |
|
$simpleDirective, |
|
254
|
|
|
'The simple directive matcher is expected to be a regexp '. |
|
255
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
256
|
4 |
|
); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
8 |
|
$this->simpleDirective = $simpleDirective; |
|
260
|
|
|
|
|
261
|
8 |
|
return $this; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Gets the list of binaries that can be run to analyze. |
|
266
|
|
|
* |
|
267
|
|
|
* @return array the list of binaries that can be run |
|
268
|
|
|
*/ |
|
269
|
1 |
|
public function getBinaries() |
|
270
|
|
|
{ |
|
271
|
1 |
|
return $this->binaries; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Sets the list of binaries that can be run to analyze. |
|
276
|
|
|
* |
|
277
|
|
|
* @param array $binaries list of controlers |
|
278
|
|
|
*/ |
|
279
|
8 |
|
public function setBinaries(array $binaries) |
|
280
|
|
|
{ |
|
281
|
8 |
|
$this->binaries = $binaries; |
|
282
|
|
|
|
|
283
|
8 |
|
return $this; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Sets the parameter string to use to detect version and config file. |
|
288
|
|
|
* |
|
289
|
|
|
* @param string $parameter parameter string |
|
290
|
|
|
*/ |
|
291
|
7 |
|
public function setDetectionParameter($parameter = '') |
|
292
|
|
|
{ |
|
293
|
7 |
|
$this->detectionParameter = $parameter; |
|
294
|
|
|
|
|
295
|
7 |
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* Gets the ordered list of methods to apply before the config file turns into an array. |
|
300
|
|
|
* |
|
301
|
|
|
* @return array the ordered list of methods to apply before convertion |
|
302
|
|
|
*/ |
|
303
|
11 |
|
public function getBeforeMethods() |
|
304
|
|
|
{ |
|
305
|
11 |
|
return $this->beforeMethods; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Sets the ordered list of methods to apply before the config file turns into an array. |
|
310
|
|
|
* |
|
311
|
|
|
* @param array $methods the ordered list of methods to apply before convertion |
|
312
|
|
|
*/ |
|
313
|
13 |
|
public function setBeforeMethods(array $methods) |
|
314
|
|
|
{ |
|
315
|
13 |
|
$this->beforeMethods = $methods; |
|
316
|
|
|
|
|
317
|
13 |
|
return $this; |
|
318
|
|
|
} |
|
319
|
|
|
|
|
320
|
|
|
/** |
|
321
|
|
|
* Gets the ordered list of methods to apply after the config file has turned into an array. |
|
322
|
|
|
* |
|
323
|
|
|
* @return array the ordered list of methods to apply after convertion |
|
324
|
|
|
*/ |
|
325
|
11 |
|
public function getAfterMethods() |
|
326
|
|
|
{ |
|
327
|
11 |
|
return $this->afterMethods; |
|
328
|
|
|
} |
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* Sets the ordered list of methods to apply after the config file has turned into an array. |
|
332
|
|
|
* |
|
333
|
|
|
* @param array $methods the ordered list of methods to apply after convertion |
|
334
|
|
|
*/ |
|
335
|
13 |
|
public function setAfterMethods(array $methods) |
|
336
|
|
|
{ |
|
337
|
13 |
|
$this->afterMethods = $methods; |
|
338
|
|
|
|
|
339
|
13 |
|
return $this; |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..