1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Expressions; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implementation of the expression evaluator using compiled closures. |
7
|
|
|
* |
8
|
|
|
* @author Elliot Levin <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class CompiledEvaluator extends Evaluator implements \Serializable |
11
|
|
|
{ |
12
|
|
|
const CONTEXT_PARAMETER_NAME = '__CONTEXT__'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $code; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var \Closure |
21
|
|
|
*/ |
22
|
|
|
protected $originalCompiledEvaluator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Closure |
26
|
|
|
*/ |
27
|
|
|
protected $boundCompiledEvaluator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var mixed[] |
31
|
|
|
*/ |
32
|
|
|
protected $extraVariables = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a new compiled evaluator from the supplied expressions. |
36
|
|
|
* |
37
|
|
|
* @param Expression[] $expressions |
38
|
|
|
* @param IEvaluationContext $context |
39
|
|
|
* |
40
|
|
|
* @return CompiledEvaluator |
41
|
|
|
*/ |
42
|
|
|
public static function fromExpressions(array $expressions, IEvaluationContext $context = null) |
43
|
|
|
{ |
44
|
|
|
$evaluator = new self($context); |
45
|
|
|
$namespace = $evaluator->context->getNamespace(); |
46
|
|
|
$contextParameterName = self::CONTEXT_PARAMETER_NAME; |
47
|
|
|
$variableTable = $evaluator->context->getVariableTable(); |
48
|
|
|
|
49
|
|
|
//Replace any non value types with references in the variable table. |
50
|
|
|
$expressions = (new DynamicExpressionWalker([ |
51
|
|
|
ValueExpression::getType() => |
52
|
|
|
function (ValueExpression $expression) use ($evaluator, $variableTable, &$name) { |
53
|
|
|
$value = $expression->getValue(); |
54
|
|
|
if (!ValueExpression::isValueType($value)) { |
55
|
|
|
$name = $name !== null ? $name : 0; |
56
|
|
|
do { |
57
|
|
|
$name++; |
58
|
|
|
} while (isset($variableTable['o' . $name])); |
59
|
|
|
|
60
|
|
|
$name = 'o' . $name; |
61
|
|
|
$evaluator->extraVariables[$name] = $value; |
62
|
|
|
|
63
|
|
|
return Expression::variable(Expression::value($name)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $expression; |
67
|
|
|
} |
68
|
|
|
]))->walkAll($expressions); |
69
|
|
|
|
70
|
|
|
$bodyCode = ''; |
71
|
|
|
foreach ($evaluator->extraVariables + $variableTable as $variable => $value) { |
72
|
|
|
$variableName = Expression::value($variable); |
73
|
|
|
$variableCode = Expression::variable($variableName)->compile(); |
74
|
|
|
$bodyCode .= "$variableCode =& $$contextParameterName" . '[' . $variableName->compile() . '];'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$bodyCode .= "unset($$contextParameterName);"; |
78
|
|
|
$bodyCode .= implode(';', Expression::compileAll($expressions)) . ';'; |
79
|
|
|
$evaluator->code = <<<PHP |
80
|
|
|
namespace {$namespace} { |
81
|
|
|
return function ($$contextParameterName) { |
82
|
|
|
$bodyCode |
83
|
|
|
}; |
84
|
|
|
} |
85
|
|
|
PHP; |
86
|
|
|
|
87
|
|
|
$evaluator->initializeEvaluator(); |
88
|
|
|
|
89
|
|
|
return $evaluator; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function serialize() |
93
|
|
|
{ |
94
|
|
|
return serialize([$this->code, $this->context, $this->requiredVariables, $this->extraVariables]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function unserialize($serialized) |
98
|
|
|
{ |
99
|
|
|
list($this->code, $this->context, $this->requiredVariables, $this->extraVariables) = unserialize($serialized); |
100
|
|
|
$this->initializeEvaluator(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function initializeEvaluator() |
104
|
|
|
{ |
105
|
|
|
$this->originalCompiledEvaluator = self::evalCode($this->code); |
106
|
|
|
$this->boundCompiledEvaluator = $this->originalCompiledEvaluator->bindTo( |
107
|
|
|
$this->context->getThis(), |
108
|
|
|
$this->context->getScopeType() |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private static function evalCode(string $code): \Closure |
113
|
|
|
{ |
114
|
|
|
return eval($code); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public function getCode() |
121
|
|
|
{ |
122
|
|
|
return $this->code; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function doEvaluation(array $variableTable) |
126
|
|
|
{ |
127
|
|
|
$evaluator = $this->boundCompiledEvaluator; |
128
|
|
|
|
129
|
|
|
return $evaluator($variableTable + $this->extraVariables); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function doEvaluationWithNewThis(array $variableTable, $newThis) |
133
|
|
|
{ |
134
|
|
|
$evaluator = $this->originalCompiledEvaluator->bindTo($newThis, $this->context->getScopeType()); |
135
|
|
|
|
136
|
|
|
return $evaluator($variableTable + $this->extraVariables); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|