Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.33% |
11 / 15 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Server | |
73.33% |
11 / 15 |
|
50.00% |
1 / 2 |
13.29 | |
0.00% |
0 / 1 |
getUrl | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
7 | |||
getProjectPath | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | /** @noinspection PhpUnhandledExceptionInspection */ |
3 | declare(strict_types=1); |
4 | // TODO unit test |
5 | |
6 | class Server { |
7 | static function getUrl(array $senv = null): string { |
8 | $senv = $senv ?? $_SERVER; |
9 | |
10 | $ssl = (!empty($senv['HTTPS']) && $senv['HTTPS'] == 'on'); |
11 | |
12 | $sp = strtolower($senv['SERVER_PROTOCOL']); |
13 | $protocol = $senv['HTTP_X_FORWARDED_PROTO'] ?? |
14 | substr($sp, 0, strpos($sp, '/')) . (($ssl) ? 's' : ''); |
15 | |
16 | $port = $senv['SERVER_PORT']; |
17 | $port = ((!$ssl && $port == '80') || ($ssl && $port == '443')) ? '' : ':' . $port; |
18 | |
19 | $host = $senv['HTTP_X_FORWARDED_HOST'] ?? $senv['HTTP_HOST'] ?? ($senv['SERVER_NAME'] . $port); |
20 | |
21 | $prefix = $senv['HTTP_X_FORWARDED_PREFIX'] ?? ''; |
22 | |
23 | $folder = str_replace('/index.php', '', $senv['SCRIPT_NAME']); |
24 | |
25 | return $protocol . '://' . $host . $prefix . $folder; |
26 | } |
27 | |
28 | static function getProjectPath(array $senv = null): string { |
29 | $senv = $senv ?? $_SERVER; |
30 | |
31 | // dirname is quite a strange function |
32 | $returnPath = str_ends_with($senv['PHP_SELF'], '/') ? $senv['PHP_SELF'] : dirname($senv['PHP_SELF']); |
33 | $returnPath = str_ends_with($returnPath, '/') ? substr($returnPath, 0, -1) : $returnPath; |
34 | return $returnPath === '.' ? '' : $returnPath; |
35 | } |
36 | } |