Skip to content

Commit 545c04f

Browse files
Try thread-safe env reading before getenv
1 parent e56b169 commit 545c04f

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/Env.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ final class Env
2525
public static function search($expression, $data)
2626
{
2727
static $runtime;
28+
2829
if (!$runtime) {
2930
$runtime = Env::createRuntime();
3031
}
32+
3133
return $runtime($expression, $data);
3234
}
3335

@@ -39,7 +41,7 @@ public static function search($expression, $data)
3941
*/
4042
public static function createRuntime()
4143
{
42-
switch ($compileDir = getenv(self::COMPILE_DIR)) {
44+
switch ($compileDir = self::getEnvVariable(self::COMPILE_DIR)) {
4345
case false: return new AstRuntime();
4446
case 'on': return new CompilerRuntime();
4547
default: return new CompilerRuntime($compileDir);
@@ -55,12 +57,35 @@ public static function createRuntime()
5557
public static function cleanCompileDir()
5658
{
5759
$total = 0;
58-
$compileDir = getenv(self::COMPILE_DIR) ?: sys_get_temp_dir();
60+
$compileDir = self::getEnvVariable(self::COMPILE_DIR) ?: sys_get_temp_dir();
61+
5962
foreach (glob("{$compileDir}/jmespath_*.php") as $file) {
6063
$total++;
6164
unlink($file);
6265
}
6366

6467
return $total;
6568
}
69+
70+
/**
71+
* Reads an environment variable from $_SERVER, $_ENV or via getenv().
72+
*
73+
* @param string $name
74+
*
75+
* @return string|null
76+
*/
77+
private static function getEnvVariable($name)
78+
{
79+
if (array_key_exists($name, $_SERVER)) {
80+
return $_SERVER[$name];
81+
}
82+
83+
if (array_key_exists($name, $_ENV)) {
84+
return $_ENV[$name];
85+
}
86+
87+
$value = getenv($name);
88+
89+
return $value === false ? null : $value;
90+
}
6691
}

0 commit comments

Comments
 (0)