1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheAentMachine; |
5
|
|
|
|
6
|
|
|
use TheAentMachine\Exception\LogLevelException; |
7
|
|
|
use TheAentMachine\Exception\MissingEnvironmentVariableException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Utility class to access the Aent configuration settings (stored in environment variables) |
11
|
|
|
*/ |
12
|
|
|
class Pheromone |
13
|
|
|
{ |
14
|
|
|
private static $levels = [ |
15
|
|
|
'DEBUG' => true, |
16
|
|
|
'INFO' => true, |
17
|
|
|
'WARN' => true, |
18
|
|
|
'ERROR' => true, |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Returns the log level for this Aent. |
23
|
|
|
* |
24
|
|
|
* @return string |
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
|
|
|
private static function getOrFail(string $variableName): string |
42
|
|
|
{ |
43
|
|
|
$value = \getenv($variableName); |
44
|
|
|
if ($value === false) { |
45
|
|
|
throw MissingEnvironmentVariableException::missingEnv($variableName); |
46
|
|
|
} |
47
|
|
|
return $value; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public static function getWhoAmI(): string |
51
|
|
|
{ |
52
|
|
|
return self::getOrFail('PHEROMONE_WHOAMI'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The project directory path on the host machine |
57
|
|
|
*/ |
58
|
|
|
public static function getHostProjectDirectory(): string |
59
|
|
|
{ |
60
|
|
|
return self::getOrFail('PHEROMONE_HOST_PROJECT_DIR'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The project directory path in the container |
65
|
|
|
*/ |
66
|
|
|
public static function getContainerProjectDirectory(): string |
67
|
|
|
{ |
68
|
|
|
return rtrim(self::getOrFail('PHEROMONE_CONTAINER_PROJECT_DIR'), '/'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public static function getOriginContainer(): ?string |
72
|
|
|
{ |
73
|
|
|
$from = getenv('PHEROMONE_FROM_CONTAINER_ID'); |
74
|
|
|
return $from === false ? null : $from; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public static function getOriginImage(): ?string |
78
|
|
|
{ |
79
|
|
|
$from = getenv('PHEROMONE_FROM_IMAGE_NAME'); |
80
|
|
|
return $from === false ? null : $from; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public static function getAenthillManifestContent(): array |
84
|
|
|
{ |
85
|
|
|
$containerProjectDir = self::getContainerProjectDirectory(); |
86
|
|
|
$aenthillJSONstr = file_get_contents($containerProjectDir . '/aenthill.json'); |
87
|
|
|
if ($aenthillJSONstr === false) { |
88
|
|
|
throw new \RuntimeException('Failed to load the aenthill manifest file ' . $containerProjectDir . '/aenthill.json'); |
89
|
|
|
} |
90
|
|
|
return \GuzzleHttp\json_decode($aenthillJSONstr, true); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|