1 |
efrain |
1 |
<?php
|
|
|
2 |
namespace JmesPath;
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Compiles JMESPath expressions to PHP source code and executes it.
|
|
|
6 |
*
|
|
|
7 |
* JMESPath file names are stored in the cache directory using the following
|
|
|
8 |
* logic to determine the filename:
|
|
|
9 |
*
|
|
|
10 |
* 1. Start with the string "jmespath_"
|
|
|
11 |
* 2. Append the MD5 checksum of the expression.
|
|
|
12 |
* 3. Append ".php"
|
|
|
13 |
*/
|
|
|
14 |
class CompilerRuntime
|
|
|
15 |
{
|
|
|
16 |
private $parser;
|
|
|
17 |
private $compiler;
|
|
|
18 |
private $cacheDir;
|
|
|
19 |
private $interpreter;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* @param string|null $dir Directory used to store compiled PHP files.
|
|
|
23 |
* @param Parser|null $parser JMESPath parser to utilize
|
|
|
24 |
* @throws \RuntimeException if the cache directory cannot be created
|
|
|
25 |
*/
|
|
|
26 |
public function __construct($dir = null, Parser $parser = null)
|
|
|
27 |
{
|
|
|
28 |
$this->parser = $parser ?: new Parser();
|
|
|
29 |
$this->compiler = new TreeCompiler();
|
|
|
30 |
$dir = $dir ?: sys_get_temp_dir();
|
|
|
31 |
|
|
|
32 |
if (!is_dir($dir) && !mkdir($dir, 0755, true)) {
|
|
|
33 |
throw new \RuntimeException("Unable to create cache directory: $dir");
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
$this->cacheDir = realpath($dir);
|
|
|
37 |
$this->interpreter = new TreeInterpreter();
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Returns data from the provided input that matches a given JMESPath
|
|
|
42 |
* expression.
|
|
|
43 |
*
|
|
|
44 |
* @param string $expression JMESPath expression to evaluate
|
|
|
45 |
* @param mixed $data Data to search. This data should be data that
|
|
|
46 |
* is similar to data returned from json_decode
|
|
|
47 |
* using associative arrays rather than objects.
|
|
|
48 |
*
|
|
|
49 |
* @return mixed Returns the matching data or null
|
|
|
50 |
* @throws \RuntimeException
|
|
|
51 |
*/
|
|
|
52 |
public function __invoke($expression, $data)
|
|
|
53 |
{
|
|
|
54 |
$functionName = 'jmespath_' . md5($expression);
|
|
|
55 |
|
|
|
56 |
if (!function_exists($functionName)) {
|
|
|
57 |
$filename = "{$this->cacheDir}/{$functionName}.php";
|
|
|
58 |
if (!file_exists($filename)) {
|
|
|
59 |
$this->compile($filename, $expression, $functionName);
|
|
|
60 |
}
|
|
|
61 |
require $filename;
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
return $functionName($this->interpreter, $data);
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
private function compile($filename, $expression, $functionName)
|
|
|
68 |
{
|
|
|
69 |
$code = $this->compiler->visit(
|
|
|
70 |
$this->parser->parse($expression),
|
|
|
71 |
$functionName,
|
|
|
72 |
$expression
|
|
|
73 |
);
|
|
|
74 |
|
|
|
75 |
if (!file_put_contents($filename, $code)) {
|
|
|
76 |
throw new \RuntimeException(sprintf(
|
|
|
77 |
'Unable to write the compiled PHP code to: %s (%s)',
|
|
|
78 |
$filename,
|
|
|
79 |
var_export(error_get_last(), true)
|
|
|
80 |
));
|
|
|
81 |
}
|
|
|
82 |
}
|
|
|
83 |
}
|