5
5
/**
6
6
* Helpers for working with the Drupal environment.
7
7
*
8
- * @method static mixed get(string $name)
9
8
* @method static string getEnvironment()
10
9
* @method static bool isAcquia()
11
10
* @method static bool isPantheon()
@@ -41,17 +40,17 @@ class Environment
41
40
* @return string
42
41
* The class name.
43
42
*/
44
- public static function getEnvironmentClass ()
43
+ public static function getEnvironmentClass (): string
45
44
{
46
45
static $ class ;
47
46
if (!isset ($ class )) {
48
- if ($ class = getenv ('DRUPAL_ENVIRONMENT_CLASS ' )) {
47
+ if ($ class = static :: get ('DRUPAL_ENVIRONMENT_CLASS ' )) {
49
48
// Do nothing. The class was assigned in the if.
50
49
} else {
51
50
// Intentionally re-assigning the class variable here so that a match
52
51
// breaks the foreach loop, or we fall back to the default class.
53
52
foreach (static ::CLASSES as $ class ) {
54
- if (getenv ($ class ::ENVIRONMENT_NAME )) {
53
+ if (static :: get ($ class ::ENVIRONMENT_NAME )) {
55
54
break ;
56
55
}
57
56
}
@@ -75,6 +74,24 @@ public static function __callStatic(string $name, array $arguments)
75
74
return $ class ::$ name (...$ arguments );
76
75
}
77
76
77
+ /**
78
+ * Get an environment variable.
79
+ *
80
+ * @param string $name
81
+ * The name of the environment variable to retrieve.
82
+ *
83
+ * @return mixed
84
+ * The environment variable, if it's set.
85
+ */
86
+ public static function get (string $ name )
87
+ {
88
+ static $ cache = [];
89
+ if (!array_key_exists ($ name , $ cache )) {
90
+ $ cache [$ name ] = getenv ($ name );
91
+ }
92
+ return $ cache [$ name ];
93
+ }
94
+
78
95
/**
79
96
* Determine if this is a local environment.
80
97
*
0 commit comments