| 1 |
<?php |
| 2 |
namespace JmesPath; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
class AstRuntime |
| 8 |
{ |
| 9 |
private $parser; |
| 10 |
private $interpreter; |
| 11 |
private $cache = []; |
| 12 |
|
| 13 |
public function __construct( |
| 14 |
?Parser $parser = null, |
| 15 |
?callable $fnDispatcher = null |
| 16 |
) { |
| 17 |
$fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance(); |
| 18 |
$this->interpreter = new TreeInterpreter($fnDispatcher); |
| 19 |
$this->parser = $parser ?: new Parser(); |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public function __invoke($expression, $data) |
| 34 |
{ |
| 35 |
if (!isset($this->cache[$expression])) { |
| 36 |
|
| 37 |
if (count($this->cache) >= 1024) { |
| 38 |
$this->cache = []; |
| 39 |
} |
| 40 |
$this->cache[$expression] = $this->parser->parse($expression); |
| 41 |
} |
| 42 |
|
| 43 |
return $this->interpreter->visit($this->cache[$expression], $data); |
| 44 |
} |
| 45 |
} |