1 | <?php |
||
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( |
||
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) |
||
77 | |||
78 | public function resolveMagic(IFunctionDeclaration $declaration) |
||
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) |
||
166 | |||
167 | public function getCallable() |
||
168 | { |
||
169 | return $this->callable; |
||
170 | } |
||
171 | |||
172 | public function getInnerReflection() |
||
176 | |||
177 | public function getScope() |
||
181 | |||
182 | public function getGlobalHash() |
||
186 | |||
187 | public function asEvaluationContext(array $variableTable = []) |
||
188 | { |
||
189 | return $this->scope->asEvaluationContext($variableTable, $this->location->getNamespace()); |
||
191 | } |
||
192 |