1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pinq\Parsing\Resolvers; |
4
|
|
|
|
5
|
|
|
use Pinq\Expressions as O; |
6
|
|
|
use Pinq\Expressions\ParameterExpression; |
7
|
|
|
use Pinq\Parsing\IFunctionMagic; |
8
|
|
|
use Pinq\Parsing\IMagicConstants; |
9
|
|
|
use Pinq\Parsing\IMagicScopes; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Resolves magic constants (__DIR__...) and scopes (self::...) |
13
|
|
|
* to their actual values. |
14
|
|
|
* |
15
|
|
|
* @author Elliot Levin <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class FunctionMagicResolver extends O\ExpressionWalker |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var IMagicConstants |
21
|
|
|
*/ |
22
|
|
|
private $magicConstants; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var IMagicScopes |
26
|
|
|
*/ |
27
|
|
|
private $magicScopes; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $closureNestingLevel = 0; |
33
|
|
|
|
34
|
|
|
public function __construct(IFunctionMagic $functionMagic) |
35
|
|
|
{ |
36
|
|
|
$this->magicConstants = $functionMagic->getConstants(); |
37
|
|
|
$this->magicScopes = $functionMagic->getScopes(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Resolves any magic constants / scopes with the supplied resolved values. |
42
|
|
|
* |
43
|
|
|
* @param IFunctionMagic $functionMagic |
44
|
|
|
* @param O\Expression[] $expressions |
45
|
|
|
* |
46
|
|
|
* @return O\Expression[] |
47
|
|
|
*/ |
48
|
|
|
public static function resolve(IFunctionMagic $functionMagic, array $expressions) |
49
|
|
|
{ |
50
|
|
|
$self = new self($functionMagic); |
51
|
|
|
|
52
|
|
|
return $self->walkAll($expressions); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function walkParameter(ParameterExpression $expression) |
56
|
|
|
{ |
57
|
|
|
$parameter = parent::walkParameter($expression); |
58
|
|
|
|
59
|
|
|
return $parameter->update( |
60
|
|
|
$parameter->getName(), |
61
|
|
|
$this->resolveMagicScopeClass($expression->getTypeHint()) ?: $expression->getTypeHint(), |
62
|
|
|
$parameter->getDefaultValue(), |
63
|
|
|
$parameter->isPassedByReference(), |
64
|
|
|
$parameter->isVariadic() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function walkClosure(O\ClosureExpression $expression) |
69
|
|
|
{ |
70
|
|
|
$this->closureNestingLevel++; |
71
|
|
|
$walkedClosure = parent::walkClosure($expression); |
72
|
|
|
$this->closureNestingLevel--; |
73
|
|
|
|
74
|
|
|
return $walkedClosure; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function walkStaticField(O\StaticFieldExpression $expression) |
78
|
|
|
{ |
79
|
|
|
return parent::walkStaticField($this->resolveMagicScopeExpression($expression)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function walkStaticMethodCall(O\StaticMethodCallExpression $expression) |
83
|
|
|
{ |
84
|
|
|
return parent::walkStaticMethodCall($this->resolveMagicScopeExpression($expression)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function walkClassConstant(O\ClassConstantExpression $expression) |
88
|
|
|
{ |
89
|
|
|
$classExpression = $expression->getClass(); |
90
|
|
|
|
91
|
|
|
if ($classExpression instanceof O\ValueExpression && strtolower($expression->getName()) === 'class') { |
92
|
|
|
$classConstantValue = $this->resolveMagicScopeClassConstant($classExpression->getValue()); |
93
|
|
|
|
94
|
|
|
if ($classConstantValue === null) { |
95
|
|
|
return $expression; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return O\Expression::value($classConstantValue); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return parent::walkClassConstant($this->resolveMagicScopeExpression($expression)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function resolveMagicScopeClassConstant($class) |
105
|
|
|
{ |
106
|
|
|
switch ($this->normalScopeClass($class)) { |
107
|
|
|
case 'self': |
108
|
|
|
return $this->magicScopes->getSelfClassConstant(); |
109
|
|
|
|
110
|
|
|
case 'static': |
111
|
|
|
return $this->magicScopes->getStaticClassConstant(); |
112
|
|
|
|
113
|
|
|
case 'parent': |
114
|
|
|
return $this->magicScopes->getParentClassConstant(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private function normalScopeClass($class) |
119
|
|
|
{ |
120
|
|
|
return strtolower(ltrim($class, '\\')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function resolveMagicScopeExpression(O\StaticClassExpression $expression) |
124
|
|
|
{ |
125
|
|
|
$classExpression = $expression->getClass(); |
126
|
|
|
|
127
|
|
|
if (!($classExpression instanceof O\ValueExpression)) { |
128
|
|
|
return $expression; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$classScope = $this->resolveMagicScopeClass($classExpression->getValue()); |
132
|
|
|
|
133
|
|
|
if ($classScope === null) { |
134
|
|
|
return $expression; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $expression->updateClass(O\Expression::value($classScope)); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
private function resolveMagicScopeClass($class) |
141
|
|
|
{ |
142
|
|
|
switch ($this->normalScopeClass($class)) { |
143
|
|
|
case 'self': |
144
|
|
|
return $this->magicScopes->getSelfClass(); |
145
|
|
|
|
146
|
|
|
case 'static': |
147
|
|
|
return $this->magicScopes->getStaticClass(); |
148
|
|
|
|
149
|
|
|
case 'parent': |
150
|
|
|
return $this->magicScopes->getParentClass(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function walkConstant(O\ConstantExpression $expression) |
155
|
|
|
{ |
156
|
|
|
$resolvedMagicConstant = $this->resolveMagicConstantValue($expression->getName()); |
157
|
|
|
|
158
|
|
|
if ($resolvedMagicConstant !== null) { |
159
|
|
|
return O\Expression::value($resolvedMagicConstant); |
160
|
|
|
} else { |
161
|
|
|
return $expression; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
private function resolveMagicConstantValue($name) |
166
|
|
|
{ |
167
|
|
|
switch (strtoupper($name)) { |
168
|
|
|
case '__DIR__': |
169
|
|
|
return $this->magicConstants->getDirectory(); |
170
|
|
|
|
171
|
|
|
case '__FILE__': |
172
|
|
|
return $this->magicConstants->getFile(); |
173
|
|
|
|
174
|
|
|
case '__NAMESPACE__': |
175
|
|
|
return $this->magicConstants->getNamespace(); |
176
|
|
|
|
177
|
|
|
case '__CLASS__': |
178
|
|
|
return $this->magicConstants->getClass(); |
179
|
|
|
|
180
|
|
|
case '__TRAIT__': |
181
|
|
|
return $this->magicConstants->getTrait(); |
182
|
|
|
|
183
|
|
|
case '__METHOD__': |
184
|
|
|
return $this->magicConstants->getMethod($this->closureNestingLevel > 0); |
185
|
|
|
|
186
|
|
|
case '__FUNCTION__': |
187
|
|
|
return $this->magicConstants->getFunction($this->closureNestingLevel > 0); |
188
|
|
|
|
189
|
|
|
default: |
190
|
|
|
return null; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|