|
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 InvalidArgumentException; |
|
15
|
|
|
use Webmozart\Assert\Assert; |
|
16
|
|
|
use Webmozart\PathUtil\Path; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A web server instance. |
|
20
|
|
|
* |
|
21
|
|
|
* @author James <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Server implements ServerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The filesystem path where the web server is installed. |
|
27
|
|
|
* |
|
28
|
|
|
* It has to be an absolute path. |
|
29
|
|
|
* |
|
30
|
|
|
* Apache httpd server does not accept a relative prefix path at compilation. |
|
31
|
|
|
* Nginx does, but this is a very risky practice... |
|
32
|
|
|
* So relative prefix path in nginx configuration will not be considered |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $prefix = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The string to match as a starting multi-line directive. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $startMultiLine = ''; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The string to match as an ending multi-line directive. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private $endMultiLine = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* The string to match a simple directive. |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
private $simpleDirective = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Confirms if the server instance has valid parameters. |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool true if all parameters are initialized, false otherwise |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function isValid() |
|
65
|
|
|
{ |
|
66
|
2 |
|
$valid = $this->prefix != ''; |
|
67
|
2 |
|
$valid = $valid && $this->startMultiLine != ''; |
|
68
|
2 |
|
$valid = $valid && $this->endMultiLine != ''; |
|
69
|
2 |
|
$valid = $valid && $this->simpleDirective != ''; |
|
70
|
|
|
|
|
71
|
2 |
|
return $valid; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Getter for the prefix. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string the filesystem path where the web server is installed |
|
78
|
|
|
*/ |
|
79
|
2 |
|
public function getPrefix() |
|
80
|
|
|
{ |
|
81
|
2 |
|
return $this->prefix; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Sets the prefix of a server isntance. |
|
86
|
|
|
* |
|
87
|
|
|
* @throws ServerException if the prefix is invalid |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $prefix the filesystem path where the web server is installed |
|
90
|
|
|
*/ |
|
91
|
11 |
|
public function setPrefix($prefix) |
|
92
|
|
|
{ |
|
93
|
|
|
try { |
|
94
|
11 |
|
Assert::string($prefix); |
|
95
|
11 |
|
} catch (InvalidArgumentException $e) { |
|
96
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be a string. Got: %s'); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
10 |
|
if (!Path::isAbsolute($prefix)) { |
|
100
|
1 |
|
throw ServerException::forInvalidPrefix($prefix, 'The path is expected to be absolute. Got: %s'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
9 |
|
if (!is_dir($prefix)) { |
|
104
|
1 |
|
throw ServerException::forInvalidPrefix( |
|
105
|
1 |
|
$prefix, |
|
106
|
|
|
'The path is expected to be an existing directory. Got: %s' |
|
107
|
1 |
|
); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
8 |
|
$this->prefix = $prefix; |
|
111
|
|
|
|
|
112
|
8 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Gets the regexp that will match the starting block directives. |
|
117
|
|
|
* |
|
118
|
|
|
* @return string the regexp that will match the starting block directives |
|
119
|
|
|
*/ |
|
120
|
1 |
|
public function getStartMultiLine() |
|
121
|
|
|
{ |
|
122
|
1 |
|
return $this->startMultiLine; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Sets the regexp that will match the starting block directives. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $simpleDirective the regexp that will match the starting block directives |
|
|
|
|
|
|
129
|
|
|
*/ |
|
130
|
6 |
View Code Duplication |
public function setStartMultiLine($startMultiLine) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
try { |
|
133
|
6 |
|
Assert::string($startMultiLine); |
|
134
|
6 |
|
} catch (InvalidArgumentException $e) { |
|
135
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
136
|
1 |
|
$startMultiLine, |
|
137
|
|
|
'The starting block directive matcher is expected to be a string. Got: %s' |
|
138
|
1 |
|
); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
5 |
|
if (!$this->isValidRegex($startMultiLine)) { |
|
142
|
4 |
|
throw ServerException::forInvalidMatcher( |
|
143
|
4 |
|
$startMultiLine, |
|
144
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
|
145
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
146
|
4 |
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
$this->startMultiLine = $startMultiLine; |
|
150
|
|
|
|
|
151
|
1 |
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Gets the regexp that will match the ending block directives. |
|
156
|
|
|
* |
|
157
|
|
|
* @return string the regexp that will match the ending block directives |
|
158
|
|
|
*/ |
|
159
|
1 |
|
public function getEndMultiLine() |
|
160
|
|
|
{ |
|
161
|
1 |
|
return $this->endMultiLine; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Sets the regexp that will match the ending block directives. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
|
168
|
|
|
*/ |
|
169
|
3 |
View Code Duplication |
public function setEndMultiLine($endMultiLine) |
|
|
|
|
|
|
170
|
|
|
{ |
|
171
|
|
|
try { |
|
172
|
3 |
|
Assert::string($endMultiLine); |
|
173
|
3 |
|
} catch (InvalidArgumentException $e) { |
|
174
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
175
|
1 |
|
$endMultiLine, |
|
176
|
|
|
'The endind block directive matcher is expected to be a string. Got: %s' |
|
177
|
1 |
|
); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
2 |
|
if (!$this->isValidRegex($endMultiLine, false)) { |
|
181
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
182
|
1 |
|
$endMultiLine, |
|
183
|
|
|
'The ending block directive matcher is expected to be a regexp . Got: %s' |
|
184
|
1 |
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
1 |
|
$this->endMultiLine = $endMultiLine; |
|
188
|
|
|
|
|
189
|
1 |
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Gets the regexp that will match the simple directives. |
|
194
|
|
|
* |
|
195
|
|
|
* @return string the regexp that will match the simple directives |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function getSimpleDirective() |
|
198
|
|
|
{ |
|
199
|
1 |
|
return $this->simpleDirective; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Sets the regexp that will match the simple directives. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
|
206
|
|
|
*/ |
|
207
|
6 |
View Code Duplication |
public function setSimpleDirective($simpleDirective) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
try { |
|
210
|
6 |
|
Assert::string($simpleDirective); |
|
211
|
6 |
|
} catch (InvalidArgumentException $e) { |
|
212
|
1 |
|
throw ServerException::forInvalidMatcher( |
|
213
|
1 |
|
$simpleDirective, |
|
214
|
|
|
'The simple directive matcher is expected to be a string. Got: %s' |
|
215
|
1 |
|
); |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
5 |
|
if (!$this->isValidRegex($simpleDirective)) { |
|
219
|
4 |
|
throw ServerException::forInvalidMatcher( |
|
220
|
4 |
|
$simpleDirective, |
|
221
|
|
|
'The simple directive matcher is expected to be a regexp '. |
|
222
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
|
223
|
4 |
|
); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
1 |
|
$this->simpleDirective = $simpleDirective; |
|
227
|
|
|
|
|
228
|
1 |
|
return $this; |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Confirms if a matcher is a valid reguler expression. |
|
233
|
|
|
* |
|
234
|
|
|
* A directive matcher MUST contain a key and a value named subpattern. |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $matcher the matcher to validate |
|
237
|
|
|
* @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
|
238
|
|
|
* |
|
239
|
|
|
* @return bool true if the matcher is valid, false otherwise |
|
240
|
|
|
*/ |
|
241
|
12 |
|
private function isValidRegex($matcher, $subpattern = true) |
|
242
|
|
|
{ |
|
243
|
12 |
|
if (false === @preg_match($matcher, 'tester')) { |
|
244
|
3 |
|
return false; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
9 |
|
if ($subpattern && (false === strpos($matcher, '(?<key>') || false === strpos($matcher, '(?<value>'))) { |
|
248
|
6 |
|
return false; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
3 |
|
return true; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.