|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheAentMachine; |
|
4
|
|
|
|
|
5
|
|
|
use TheAentMachine\Exception\LogLevelException; |
|
6
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Utility class to access the aent configuration settings (stored in environment variables) |
|
10
|
|
|
*/ |
|
11
|
|
|
class Pheromone |
|
12
|
|
|
{ |
|
13
|
|
|
private static $levels = [ |
|
14
|
|
|
'DEBUG' => true, |
|
15
|
|
|
'INFO' => true, |
|
16
|
|
|
'WARN' => true, |
|
17
|
|
|
'ERROR' => true, |
|
18
|
|
|
]; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Returns the log level for this aent. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
* @throws LogLevelException |
|
25
|
|
|
*/ |
|
26
|
|
|
public static function getLogLevel(): string |
|
27
|
|
|
{ |
|
28
|
|
|
$logLevel = getenv('PHEROMONE_LOG_LEVEL'); |
|
29
|
|
|
|
|
30
|
|
|
if ($logLevel === false) { |
|
31
|
|
|
throw LogLevelException::emptyLogLevel(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (!array_key_exists($logLevel, self::$levels)) { |
|
35
|
|
|
throw LogLevelException::invalidLogLevel($logLevel); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $logLevel; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Tries to returns the value of an environment variable or throws an exception. |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $variableName the environment variable key |
|
45
|
|
|
* @return string |
|
46
|
|
|
* @throws MissingEnvironmentVariableException |
|
47
|
|
|
*/ |
|
48
|
|
|
private static function mustGet(string $variableName): string |
|
49
|
|
|
{ |
|
50
|
|
|
$value = getenv($variableName); |
|
51
|
|
|
if ($value === false) { |
|
52
|
|
|
throw MissingEnvironmentVariableException::missingEnv($variableName); |
|
53
|
|
|
} |
|
54
|
|
|
return $value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Tries to returns the value of an environment variable or null. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $variableName the environment variable key |
|
61
|
|
|
* @return null|string |
|
62
|
|
|
*/ |
|
63
|
|
|
private static function get(string $variableName): ?string |
|
64
|
|
|
{ |
|
65
|
|
|
$value = getenv($variableName); |
|
66
|
|
|
return $value === false ? null : $value; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The project directory path on the host machine. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string |
|
73
|
|
|
* @throws MissingEnvironmentVariableException |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getHostProjectDirectory(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return self::mustGet('PHEROMONE_HOST_PROJECT_DIR'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* The project directory path in the container. |
|
82
|
|
|
* |
|
83
|
|
|
* @return string |
|
84
|
|
|
* @throws MissingEnvironmentVariableException |
|
85
|
|
|
*/ |
|
86
|
|
|
public static function getContainerProjectDirectory(): string |
|
87
|
|
|
{ |
|
88
|
|
|
return rtrim(self::mustGet('PHEROMONE_CONTAINER_PROJECT_DIR'), '/'); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* The current image of this aent. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
* @throws MissingEnvironmentVariableException |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function getImage(): string |
|
98
|
|
|
{ |
|
99
|
|
|
return self::mustGet('PHEROMONE_IMAGE_NAME'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* The key from the manifest if this aent has been installed. |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
* @throws MissingEnvironmentVariableException |
|
107
|
|
|
*/ |
|
108
|
|
|
public static function getKey(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return self::mustGet('PHEROMONE_KEY'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* The container ID which has started this aent or null. |
|
115
|
|
|
* |
|
116
|
|
|
* @return null|string |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function getOriginContainer(): ?string |
|
119
|
|
|
{ |
|
120
|
|
|
return self::get('PHEROMONE_FROM_CONTAINER_ID'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|