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
|
|
|
*/ |
25
|
|
|
public static function getLogLevel(): string |
26
|
|
|
{ |
27
|
|
|
$logLevel = getenv('PHEROMONE_LOG_LEVEL'); |
28
|
|
|
|
29
|
|
|
if ($logLevel === false) { |
30
|
|
|
throw LogLevelException::emptyLogLevel(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!array_key_exists($logLevel, self::$levels)) { |
34
|
|
|
throw LogLevelException::invalidLogLevel($logLevel); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $logLevel; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
private static function getOrFail(string $variableName): string |
41
|
|
|
{ |
42
|
|
|
$value = \getenv($variableName); |
43
|
|
|
if ($value === false) { |
44
|
|
|
throw MissingEnvironmentVariableException::missingEnv($variableName); |
45
|
|
|
} |
46
|
|
|
return $value; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function getWhoAmI(): string |
50
|
|
|
{ |
51
|
|
|
return self::getOrFail('PHEROMONE_WHOAMI'); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* The project directory path on the host machine |
56
|
|
|
*/ |
57
|
|
|
public static function getHostProjectDirectory(): string |
58
|
|
|
{ |
59
|
|
|
return self::getOrFail('PHEROMONE_HOST_PROJECT_DIR'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The project directory path in the container |
64
|
|
|
*/ |
65
|
|
|
public static function getContainerProjectDirectory(): string |
66
|
|
|
{ |
67
|
|
|
return rtrim(self::getOrFail('PHEROMONE_CONTAINER_PROJECT_DIR'), '/'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public static function getOriginContainer(): ?string |
71
|
|
|
{ |
72
|
|
|
$from = getenv('PHEROMONE_FROM_CONTAINER_ID'); |
73
|
|
|
return $from === false ? null : $from; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public static function getOriginImage(): ?string |
77
|
|
|
{ |
78
|
|
|
$from = getenv('PHEROMONE_FROM_IMAGE_NAME'); |
79
|
|
|
return $from === false ? null : $from; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed[] |
84
|
|
|
*/ |
85
|
|
|
public static function getAenthillManifestContent(): array |
86
|
|
|
{ |
87
|
|
|
$containerProjectDir = self::getContainerProjectDirectory(); |
88
|
|
|
$aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json'); |
89
|
|
|
if ($aenthillJSONstr === false) { |
90
|
|
|
throw new \RuntimeException('Failed to load the aenthill manifest file ' . $containerProjectDir . '/aenthill.json'); |
91
|
|
|
} |
92
|
|
|
return \GuzzleHttp\json_decode($aenthillJSONstr, true); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|