1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\MockServer; |
4
|
|
|
|
5
|
|
|
use Composer\Autoload\ClassLoader; |
6
|
|
|
use EdmondsCommerce\MockServer\Exception\MockServerException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class MockServerConfig |
10
|
|
|
* |
11
|
|
|
* @package EdmondsCommerce\MockServer |
12
|
|
|
* @SuppressWarnings(PHPMD.Superglobals) |
13
|
|
|
*/ |
14
|
|
|
class MockServerConfig |
15
|
|
|
{ |
16
|
|
|
public const KEY_IP = 'MockServer_Ip'; |
17
|
|
|
public const IP_LOCALHOST = '0.0.0.0'; |
18
|
|
|
public const IP_CALCULATE_NETWORK_IP = 'calculate'; |
19
|
|
|
public const IP_CALCULATE_CMD = 'ip route get 8.8.8.8 | ' |
20
|
|
|
. 'awk \'{ for (nn=1;nn<=NF;nn++) if ($nn~"src") print $(nn+1) }\' '; |
21
|
|
|
public const DEFAULT_IP = self::IP_LOCALHOST; |
22
|
|
|
|
23
|
|
|
public const KEY_PORT = 'MockServer_Port'; |
24
|
|
|
public const DEFAULT_PORT = 8080; |
25
|
|
|
|
26
|
|
|
public const KEY_ROUTER_PATH = 'MockServer_RouterPath'; |
27
|
|
|
public const DEFAULT_ROUTER_PATH = '/tests/MockServer/router.php'; |
28
|
|
|
|
29
|
|
|
public const KEY_HTDOCS_PATH = 'MockServer_HtdocsPath'; |
30
|
|
|
public const DEFAULT_HTDOCS_PATH = '/tests/MockServer/htdocs'; |
31
|
|
|
|
32
|
|
|
public const KEY_LOGS_PATH = 'MockServer_LogsPath'; |
33
|
|
|
public const DEFAULT_LOGS_PATH = __DIR__ . '/../var/logs/'; |
34
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
* @throws MockServerException |
39
|
|
|
*/ |
40
|
3 |
|
public static function getIp(): string |
41
|
|
|
{ |
42
|
3 |
|
$mockServerIp = $_SERVER[self::KEY_IP] ?? self::DEFAULT_IP; |
43
|
3 |
|
if (self::IP_CALCULATE_NETWORK_IP === $mockServerIp) { |
44
|
|
|
$mockServerIp = self::calculateMockServerIp(); |
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
return $mockServerIp; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function calculateMockServerIp(): string |
51
|
|
|
{ |
52
|
|
|
exec( |
53
|
|
|
self::IP_CALCULATE_CMD, |
54
|
|
|
$output, |
55
|
|
|
$exitCode |
56
|
|
|
); |
57
|
|
|
if (0 !== $exitCode) { |
58
|
|
|
throw new MockServerException( |
59
|
|
|
'Failed getting mock server IP, got exit code ' . $exitCode . ' and output: ' |
60
|
|
|
. "\n" . implode("\n", $output) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return array_pop($output); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
public static function getPort(): int |
68
|
|
|
{ |
69
|
3 |
|
return (int)($_SERVER[self::KEY_PORT] ?? self::DEFAULT_PORT); |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
public static function getRouterPath(): string |
73
|
|
|
{ |
74
|
3 |
|
return $_SERVER[self::KEY_ROUTER_PATH] ?? self::getPathToProjectRoot() . '/' . self::DEFAULT_ROUTER_PATH; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
* @throws MockServerException |
80
|
|
|
*/ |
81
|
11 |
|
public static function getPathToProjectRoot(): string |
82
|
|
|
{ |
83
|
|
|
try { |
84
|
11 |
|
$reflection = new \ReflectionClass(ClassLoader::class); |
85
|
|
|
|
86
|
11 |
|
return \dirname($reflection->getFileName(), 3); |
87
|
|
|
} catch (\Exception $e) { |
88
|
|
|
throw new MockServerException('Exception in ' . __METHOD__, $e->getCode(), $e); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
public static function getHtdocsPath(): string |
93
|
|
|
{ |
94
|
11 |
|
return $_SERVER[self::KEY_HTDOCS_PATH] ?? self::getPathToProjectRoot() . '/' . self::DEFAULT_HTDOCS_PATH; |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
public static function getLogsPath(): string |
98
|
|
|
{ |
99
|
3 |
|
return $_SERVER[self::KEY_LOGS_PATH] ?? self::DEFAULT_LOGS_PATH; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|