1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Parsing; |
4
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Implementation of the function scope interface. |
9
|
|
|
* |
10
|
|
|
* @author Elliot Levin <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class FunctionScope implements IFunctionScope |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var object|null |
16
|
|
|
*/ |
17
|
|
|
protected $thisObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
protected $thisType; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string|null |
26
|
|
|
*/ |
27
|
|
|
protected $scopeType; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<string, mixed> |
31
|
|
|
*/ |
32
|
|
|
protected $variableValueMap; |
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
$thisObject, |
36
|
|
|
$scopeType, |
37
|
|
|
array $variableValueMap |
38
|
|
|
) { |
39
|
|
|
$this->thisObject = $thisObject; |
40
|
|
|
$this->thisType = $thisObject !== null ? get_class($thisObject) : null; |
41
|
|
|
$this->scopeType = $scopeType; |
42
|
|
|
$this->variableValueMap = $variableValueMap; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Creates a function scope instance from the supplied reflection and callable. |
47
|
|
|
* |
48
|
|
|
* @param \ReflectionFunctionAbstract $reflection |
49
|
|
|
* @param callable $callable |
50
|
|
|
* |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
|
|
public static function fromReflection(\ReflectionFunctionAbstract $reflection, callable $callable) |
54
|
|
|
{ |
55
|
|
|
if (is_array($callable)) { |
56
|
|
|
/** @var $reflection \ReflectionMethod */ |
57
|
|
|
$thisObject = is_object($callable[0]) ? $callable[0] : null; |
58
|
|
|
$scopeType = $reflection->getDeclaringClass()->getName(); |
|
|
|
|
59
|
|
|
} elseif (is_object($callable) && !($callable instanceof \Closure)) { |
60
|
|
|
/** @var $reflection \ReflectionMethod */ |
61
|
|
|
$thisObject = $callable; |
62
|
|
|
$scopeType = $reflection->getDeclaringClass()->getName(); |
|
|
|
|
63
|
|
|
} elseif ($reflection->isClosure()) { |
64
|
|
|
$thisObject = $reflection->getClosureThis(); |
65
|
|
|
$scopeClass = $reflection->getClosureScopeClass(); |
66
|
|
|
$scopeType = $scopeClass === null ? null : $scopeClass->getName(); |
67
|
|
|
} else { |
68
|
|
|
$thisObject = null; |
69
|
|
|
$scopeType = null; |
70
|
|
|
} |
71
|
|
|
$variableTable = $reflection->getStaticVariables(); |
72
|
|
|
|
73
|
|
|
return new self($thisObject, $scopeType, $variableTable); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function hasThis() |
77
|
|
|
{ |
78
|
|
|
return $this->thisObject !== null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getThis() |
82
|
|
|
{ |
83
|
|
|
return $this->thisObject; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getThisType() |
87
|
|
|
{ |
88
|
|
|
return $this->thisType; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getScopeType() |
92
|
|
|
{ |
93
|
|
|
return $this->scopeType; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getVariableTable() |
97
|
|
|
{ |
98
|
|
|
return $this->variableValueMap; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function asEvaluationContext(array $variableTable = [], $namespace = null) |
102
|
|
|
{ |
103
|
|
|
return new O\EvaluationContext( |
104
|
|
|
$namespace, |
105
|
|
|
$this->scopeType, |
106
|
|
|
$this->thisObject, |
107
|
|
|
//Used variables take precedence over arguments: http://3v4l.org/V4LSE |
108
|
|
|
$this->variableValueMap + $variableTable |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|