12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace JmesPath;
- final class Env
- {
- const COMPILE_DIR = 'JP_PHP_COMPILE';
-
- public static function search($expression, $data)
- {
- static $runtime;
- if (!$runtime) {
- $runtime = Env::createRuntime();
- }
- return $runtime($expression, $data);
- }
-
- public static function createRuntime()
- {
- switch ($compileDir = self::getEnvVariable(self::COMPILE_DIR)) {
- case false: return new AstRuntime();
- case 'on': return new CompilerRuntime();
- default: return new CompilerRuntime($compileDir);
- }
- }
-
- public static function cleanCompileDir()
- {
- $total = 0;
- $compileDir = self::getEnvVariable(self::COMPILE_DIR) ?: sys_get_temp_dir();
- foreach (glob("{$compileDir}/jmespath_*.php") as $file) {
- $total++;
- unlink($file);
- }
- return $total;
- }
-
- private static function getEnvVariable($name)
- {
- if (array_key_exists($name, $_SERVER)) {
- return $_SERVER[$name];
- }
- if (array_key_exists($name, $_ENV)) {
- return $_ENV[$name];
- }
- $value = getenv($name);
- return $value === false ? null : $value;
- }
- }
|