1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Parsing; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Implementation of the function reflection interface. |
7
|
|
|
* |
8
|
|
|
* @author Elliot Levin <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class FunctionReflection extends LocatedFunction implements IFunctionReflection |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var callable |
14
|
|
|
*/ |
15
|
|
|
protected $callable; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var \ReflectionFunctionAbstract |
19
|
|
|
*/ |
20
|
|
|
protected $innerReflection; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var IFunctionScope |
24
|
|
|
*/ |
25
|
|
|
protected $scope; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $globalHash; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
callable $callable, |
34
|
|
|
\ReflectionFunctionAbstract $innerReflection, |
35
|
|
|
IFunctionSignature $signature, |
36
|
|
|
IFunctionLocation $location, |
37
|
|
|
IFunctionScope $scope |
38
|
|
|
) { |
39
|
|
|
parent::__construct($signature, $location); |
40
|
|
|
$this->callable = $callable; |
41
|
|
|
$this->innerReflection = $innerReflection; |
42
|
|
|
$this->scope = $scope; |
43
|
|
|
|
44
|
|
|
//Hashes the signature and location along with the scoped class type due to the |
45
|
|
|
//resolution of scoped class constants (self::, static::, parent::). |
46
|
|
|
//These should be fully qualified in the expression tree hence requiring |
47
|
|
|
//a different hash. |
48
|
|
|
$this->globalHash = md5( |
49
|
|
|
implode( |
50
|
|
|
'!', |
51
|
|
|
[ |
52
|
|
|
$this->scope->getThisType(), |
53
|
|
|
$this->locationAndSignatureHash, |
54
|
|
|
] |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates a new function reflection instance from the supplied callable. |
61
|
|
|
* |
62
|
|
|
* @param callable $callable |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public static function fromCallable(callable $callable) |
67
|
|
|
{ |
68
|
|
|
$reflection = Reflection::fromCallable($callable); |
69
|
|
|
|
70
|
|
|
return new self( |
71
|
|
|
$callable, |
72
|
|
|
$reflection, |
73
|
|
|
FunctionSignature::fromReflection($reflection), |
74
|
|
|
FunctionLocation::fromReflection($reflection), |
75
|
|
|
FunctionScope::fromReflection($reflection, $callable)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function resolveMagic(IFunctionDeclaration $declaration) |
79
|
|
|
{ |
80
|
|
|
$magicConstants = $this->resolveMagicConstants($declaration); |
81
|
|
|
$magicScopes = $this->resolveMagicScopes($declaration); |
82
|
|
|
|
83
|
|
|
return new FunctionMagic($magicConstants, $magicScopes); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
private function resolveMagicConstants(IFunctionDeclaration $declaration) |
87
|
|
|
{ |
88
|
|
|
$reflection = $this->innerReflection; |
89
|
|
|
$__FILE__ = $this->location->getFilePath(); |
90
|
|
|
$__DIR__ = dirname($__FILE__); |
91
|
|
|
$__NAMESPACE__ = $declaration->getNamespace() ?: ''; |
92
|
|
|
$__TRAIT__ = ''; |
93
|
|
|
$namespacePrefix = $__NAMESPACE__ === '' ? '' : $__NAMESPACE__ . '\\'; |
94
|
|
|
|
95
|
|
|
if ($declaration->isWithinClass()) { |
96
|
|
|
$__CLASS__ = $namespacePrefix . $declaration->getClass(); |
97
|
|
|
$declarationType = $__CLASS__; |
98
|
|
|
} elseif ($declaration->isWithinTrait()) { |
99
|
|
|
$__TRAIT__ = $namespacePrefix . $declaration->getTrait(); |
100
|
|
|
$declarationType = $__TRAIT__; |
101
|
|
|
|
102
|
|
|
//If the function is method declared within a trait the __CLASS__ constant |
103
|
|
|
//is programmed to be the class in which the trait is used in: https://bugs.php.net/bug.php?id=55214&edit=1 |
104
|
|
|
//ReflectionMethod::getDeclaringClass() will resolve this. |
105
|
|
|
if ($reflection instanceof \ReflectionMethod) { |
106
|
|
|
$__CLASS__ = $reflection->getDeclaringClass()->getName(); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
//Else the function must be a closure declared in a trait, __CLASS__ will resolve to |
109
|
|
|
//the scoped class e.g. get_called_class(). |
110
|
|
|
else { |
111
|
|
|
$__CLASS__ = $this->scope->getScopeType() ?: ''; |
112
|
|
|
} |
113
|
|
|
} else { |
114
|
|
|
$__CLASS__ = ''; |
115
|
|
|
$__TRAIT__ = ''; |
116
|
|
|
$declarationType = null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$__FUNCTION__ = $reflection->getName(); |
120
|
|
|
$__FUNCTION__WithinClosure = $namespacePrefix . '{closure}'; |
121
|
|
|
if ($declarationType === null) { |
122
|
|
|
$__METHOD__ = $__FUNCTION__; |
123
|
|
|
$__METHOD__WithinClosure = $__FUNCTION__WithinClosure; |
124
|
|
|
} //__METHOD__ always uses declaration type |
125
|
|
|
else { |
126
|
|
|
$__METHOD__ = $declarationType . '::' . $__FUNCTION__; |
127
|
|
|
$__METHOD__WithinClosure = $declarationType . '::' . $__FUNCTION__WithinClosure; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
return new MagicConstants( |
131
|
|
|
$__DIR__, |
132
|
|
|
$__FILE__, |
133
|
|
|
$__NAMESPACE__, |
134
|
|
|
$__CLASS__, |
135
|
|
|
$__TRAIT__, |
136
|
|
|
$__FUNCTION__, |
137
|
|
|
$__FUNCTION__WithinClosure, |
138
|
|
|
$__METHOD__, |
139
|
|
|
$__METHOD__WithinClosure); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function resolveMagicScopes(IFunctionDeclaration $declaration) |
143
|
|
|
{ |
144
|
|
|
$selfType = $this->scope->getScopeType(); |
145
|
|
|
$declarationType = $declaration->getClass() ?: $declaration->getTrait(); |
146
|
|
|
$selfConstant = $declarationType !== null ? |
147
|
|
|
($declaration->getNamespace() !== null ? $declaration->getNamespace() . '\\' : '') . $declarationType : null; |
148
|
|
|
$staticType = $this->scope->hasThis() ? $this->scope->getThisType() : $selfType; |
149
|
|
|
$staticConstant = $staticType; |
150
|
|
|
$parentType = get_parent_class($selfType) ?: null; |
151
|
|
|
$parentConstant = $parentType; |
152
|
|
|
|
153
|
|
|
return new MagicScopes( |
154
|
|
|
$this->fullyQualify($selfType), //self:: |
155
|
|
|
$selfConstant, //self::class |
156
|
|
|
$this->fullyQualify($staticType), //static:: |
157
|
|
|
$staticConstant, //static::class |
158
|
|
|
$this->fullyQualify($parentType), //parent:: |
159
|
|
|
$parentConstant); //parent::class |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
protected function fullyQualify($type) |
163
|
|
|
{ |
164
|
|
|
return $type[0] !== '\\' ? '\\' . $type : $type; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function getCallable() |
168
|
|
|
{ |
169
|
|
|
return $this->callable; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getInnerReflection() |
173
|
|
|
{ |
174
|
|
|
return $this->innerReflection; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function getScope() |
178
|
|
|
{ |
179
|
|
|
return $this->scope; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function getGlobalHash() |
183
|
|
|
{ |
184
|
|
|
return $this->globalHash; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
public function asEvaluationContext(array $variableTable = []) |
188
|
|
|
{ |
189
|
|
|
return $this->scope->asEvaluationContext($variableTable, $this->location->getNamespace()); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|