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 $startMultiLine the regexp that will match the starting block directives |
129
|
|
|
*/ |
130
|
6 |
|
public function setStartMultiLine($startMultiLine) |
131
|
|
|
{ |
132
|
6 |
|
$this->startMultiLine = $this->checkString( |
133
|
6 |
|
$startMultiLine, |
134
|
6 |
|
'The starting block directive matcher is expected to be a string. Got: %s', |
135
|
|
|
'The starting block directive matcher is expected to be a regexp '. |
136
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
137
|
6 |
|
); |
138
|
|
|
|
139
|
1 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Gets the regexp that will match the ending block directives. |
144
|
|
|
* |
145
|
|
|
* @return string the regexp that will match the ending block directives |
146
|
|
|
*/ |
147
|
1 |
|
public function getEndMultiLine() |
148
|
|
|
{ |
149
|
1 |
|
return $this->endMultiLine; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the regexp that will match the ending block directives. |
154
|
|
|
* |
155
|
|
|
* @param string $endMultiLine the regexp that will match the ending block directives |
156
|
|
|
*/ |
157
|
3 |
|
public function setEndMultiLine($endMultiLine) |
158
|
|
|
{ |
159
|
3 |
|
$this->endMultiLine = $this->checkString( |
160
|
3 |
|
$endMultiLine, |
161
|
3 |
|
'The endind block directive matcher is expected to be a string. Got: %s', |
162
|
3 |
|
'The ending block directive matcher is expected to be a regexp . Got: %s', |
163
|
|
|
false |
164
|
3 |
|
); |
165
|
|
|
|
166
|
1 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Gets the regexp that will match the simple directives. |
171
|
|
|
* |
172
|
|
|
* @return string the regexp that will match the simple directives |
173
|
|
|
*/ |
174
|
1 |
|
public function getSimpleDirective() |
175
|
|
|
{ |
176
|
1 |
|
return $this->simpleDirective; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Sets the regexp that will match the simple directives. |
181
|
|
|
* |
182
|
|
|
* @param string $simpleDirective the regexp that will match the simple directives |
183
|
|
|
*/ |
184
|
6 |
|
public function setSimpleDirective($simpleDirective) |
185
|
|
|
{ |
186
|
6 |
|
$this->simpleDirective = $this->checkString( |
187
|
6 |
|
$simpleDirective, |
188
|
6 |
|
'The simple directive matcher is expected to be a string. Got: %s', |
189
|
|
|
'The simple directive matcher is expected to be a regexp '. |
190
|
|
|
'containing named subpatterns "key" and "value". Got: %s' |
191
|
6 |
|
); |
192
|
|
|
|
193
|
1 |
|
return $this; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Checks if the string matches some criterias. |
198
|
|
|
* |
199
|
|
|
* @param string $string the string to check |
200
|
|
|
* @param string $message1 message if not a string |
201
|
|
|
* @param string $message2 message if not a regexp |
202
|
|
|
* @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
203
|
|
|
* |
204
|
|
|
* @throws ServerException if the string is invalid |
205
|
|
|
* |
206
|
|
|
* @return string the string |
207
|
|
|
*/ |
208
|
15 |
|
private function checkString($string, $message1, $message2, $subpattern = true) |
209
|
|
|
{ |
210
|
|
|
try { |
211
|
15 |
|
Assert::string($string); |
212
|
15 |
|
} catch (InvalidArgumentException $e) { |
213
|
3 |
|
throw ServerException::forInvalidMatcher($string, $message1); |
214
|
|
|
} |
215
|
|
|
|
216
|
12 |
|
if (!$this->isValidRegex($string, $subpattern)) { |
217
|
9 |
|
throw ServerException::forInvalidMatcher($string, $message2); |
218
|
|
|
} |
219
|
|
|
|
220
|
3 |
|
return $string; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Confirms if a matcher is a valid reguler expression. |
225
|
|
|
* |
226
|
|
|
* A directive matcher MUST contain a key and a value named subpattern. |
227
|
|
|
* |
228
|
|
|
* @param string $matcher the matcher to validate |
229
|
|
|
* @param bool $subpattern confirms the presence of subpatterns "key" and "value" |
230
|
|
|
* |
231
|
|
|
* @return bool true if the matcher is valid, false otherwise |
232
|
|
|
*/ |
233
|
12 |
|
private function isValidRegex($matcher, $subpattern = true) |
234
|
|
|
{ |
235
|
12 |
|
if (false === @preg_match($matcher, 'tester')) { |
236
|
3 |
|
return false; |
237
|
|
|
} |
238
|
|
|
|
239
|
9 |
|
if ($subpattern && (false === strpos($matcher, '(?<key>') || false === strpos($matcher, '(?<value>'))) { |
240
|
6 |
|
return false; |
241
|
|
|
} |
242
|
|
|
|
243
|
3 |
|
return true; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|