@@ -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