| 1 |
<?php |
| 2 |
namespace JmesPath; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
final class Env |
| 14 |
{ |
| 15 |
const COMPILE_DIR = 'JP_PHP_COMPILE'; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
public static function search($expression, $data) |
| 26 |
{ |
| 27 |
static $runtime; |
| 28 |
|
| 29 |
if (!$runtime) { |
| 30 |
$runtime = Env::createRuntime(); |
| 31 |
} |
| 32 |
|
| 33 |
return $runtime($expression, $data); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public static function createRuntime() |
| 43 |
{ |
| 44 |
switch ($compileDir = self::getEnvVariable(self::COMPILE_DIR)) { |
| 45 |
case false: return new AstRuntime(); |
| 46 |
case 'on': return new CompilerRuntime(); |
| 47 |
default: return new CompilerRuntime($compileDir); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public static function cleanCompileDir() |
| 58 |
{ |
| 59 |
$total = 0; |
| 60 |
$compileDir = self::getEnvVariable(self::COMPILE_DIR); |
| 61 |
if ($compileDir === 'on' || !$compileDir) { |
| 62 |
$compileDir = sys_get_temp_dir(); |
| 63 |
} |
| 64 |
|
| 65 |
foreach (glob("{$compileDir}/jmespath_*.php") as $file) { |
| 66 |
$total++; |
| 67 |
unlink($file); |
| 68 |
} |
| 69 |
|
| 70 |
return $total; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private static function getEnvVariable($name) |
| 81 |
{ |
| 82 |
if (array_key_exists($name, $_SERVER)) { |
| 83 |
return $_SERVER[$name]; |
| 84 |
} |
| 85 |
|
| 86 |
if (array_key_exists($name, $_ENV)) { |
| 87 |
return $_ENV[$name]; |
| 88 |
} |
| 89 |
|
| 90 |
$value = getenv($name); |
| 91 |
|
| 92 |
return $value === false ? null : $value; |
| 93 |
} |
| 94 |
} |