1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Queries\Builders\Interpretations; |
4
|
|
|
|
5
|
|
|
use Pinq\Parsing\IFunctionInterpreter; |
6
|
|
|
use Pinq\PinqException; |
7
|
|
|
use Pinq\Queries; |
8
|
|
|
use Pinq\Queries\Builders\Functions\CallableFunction; |
9
|
|
|
use Pinq\Queries\Builders\Functions\ClosureExpressionFunction; |
10
|
|
|
use Pinq\Queries\Builders\Functions\IFunction; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Base class for query expression parsing. |
14
|
|
|
* |
15
|
|
|
* @author Elliot Levin <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class BaseParser extends BaseInterpretation implements IQueryParser |
18
|
|
|
{ |
19
|
|
|
public function __construct(IFunctionInterpreter $functionInterpreter) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($functionInterpreter); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
final protected function buildFunction(IFunction $function, callable $factory) |
25
|
|
|
{ |
26
|
|
|
if ($function instanceof CallableFunction) { |
27
|
|
|
$reflection = $this->functionInterpreter->getReflection($function->getCallable()); |
28
|
|
|
$scopeType = $reflection->getScope()->getScopeType(); |
29
|
|
|
$namespace = $reflection->getInnerReflection()->getNamespaceName() ?: null; |
30
|
|
|
$parameterExpressions = $reflection->getSignature()->getParameterExpressions(); |
31
|
|
|
$scopedVariableNames = $reflection->getSignature()->isStatic() ? [] : ['this']; |
32
|
|
|
$scopedVariableNames = array_merge( |
33
|
|
|
$scopedVariableNames, |
34
|
|
|
$reflection->getSignature()->getScopedVariableNames() ?: [] |
35
|
|
|
); |
36
|
|
|
$bodyExpressions = $reflection->getInnerReflection()->isUserDefined() |
37
|
|
|
? $this->functionInterpreter->getStructure($reflection)->getBodyExpressions() : null; |
38
|
|
|
} elseif ($function instanceof ClosureExpressionFunction) { |
39
|
|
|
if ($function->hasEvaluationContext()) { |
40
|
|
|
$scopeType = $function->getEvaluationContext()->getScopeType(); |
41
|
|
|
$namespace = $function->getEvaluationContext()->getNamespace(); |
42
|
|
|
} else { |
43
|
|
|
$scopeType = null; |
44
|
|
|
$namespace = null; |
45
|
|
|
} |
46
|
|
|
$expression = $function->getExpression(); |
47
|
|
|
$parameterExpressions = $expression->getParameters(); |
48
|
|
|
$bodyExpressions = $expression->getBodyExpressions(); |
49
|
|
|
$scopedVariableNames = array_merge(['this'], $expression->getUsedVariableNames()); |
50
|
|
|
} else { |
51
|
|
|
throw new PinqException( |
52
|
|
|
'Cannot build function: unsupported function type, %s', |
53
|
|
|
get_class($function)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $factory( |
57
|
|
|
$this->getFunctionCallableParameter($function), |
58
|
|
|
$scopeType, |
59
|
|
|
$namespace, |
60
|
|
|
$this->getFunctionScopeParameterMap($function, $scopedVariableNames), |
61
|
|
|
$parameterExpressions, |
62
|
|
|
$bodyExpressions |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
final protected function getFunctionScopeParameterMap(IFunction $function, array $scopedVariableNames) |
67
|
|
|
{ |
68
|
|
|
$parameterScopedVariableMap = []; |
69
|
|
|
|
70
|
|
|
foreach (array_unique($scopedVariableNames) as $scopedVariable) { |
71
|
|
|
$parameterScopedVariableMap[$this->getFunctionScopedVariableParameter( |
72
|
|
|
$function, |
73
|
|
|
$scopedVariable |
74
|
|
|
)] = $scopedVariable; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $parameterScopedVariableMap; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|