|
13 | 13 | namespace Hyperf\Di\Aop;
|
14 | 14 |
|
15 | 15 | use PhpParser\Node;
|
| 16 | +use PhpParser\Node\Expr\Assign; |
16 | 17 | use PhpParser\Node\Expr\ClassConstFetch;
|
17 | 18 | use PhpParser\Node\Expr\Closure;
|
18 | 19 | use PhpParser\Node\Expr\FuncCall;
|
19 | 20 | use PhpParser\Node\Expr\StaticCall;
|
20 | 21 | use PhpParser\Node\Expr\StaticPropertyFetch;
|
| 22 | +use PhpParser\Node\Expr\Variable; |
21 | 23 | use PhpParser\Node\Identifier;
|
22 | 24 | use PhpParser\Node\Name;
|
23 | 25 | use PhpParser\Node\Scalar\MagicConst\Function_ as MagicConstFunction;
|
@@ -137,6 +139,14 @@ public function leaveNode(Node $node)
|
137 | 139 | return $node;
|
138 | 140 | }
|
139 | 141 | break;
|
| 142 | + case $node instanceof Node\Scalar\MagicConst\Function_: |
| 143 | + // Rewrite __FUNCTION__ to $__function__ variable. |
| 144 | + return new Variable('__function__'); |
| 145 | + break; |
| 146 | + case $node instanceof Node\Scalar\MagicConst\Method: |
| 147 | + // Rewrite __METHOD__ to $__method__ variable. |
| 148 | + return new Variable('__method__'); |
| 149 | + break; |
140 | 150 | }
|
141 | 151 | }
|
142 | 152 |
|
@@ -238,15 +248,25 @@ private function rewriteMethod(ClassMethod $node): ClassMethod
|
238 | 248 | }
|
239 | 249 | return $params;
|
240 | 250 | }),
|
| 251 | + 'uses' => [ |
| 252 | + new Variable('__function__'), |
| 253 | + new Variable('__method__'), |
| 254 | + ], |
241 | 255 | 'stmts' => $node->stmts,
|
242 | 256 | ]),
|
243 | 257 | ]);
|
| 258 | + $magicConstFunction = new Expression(new Assign(new Variable('__function__'), new Node\Scalar\MagicConst\Function_())); |
| 259 | + $magicConstMethod = new Expression(new Assign(new Variable('__method__'), new Node\Scalar\MagicConst\Method())); |
244 | 260 | if ($shouldReturn) {
|
245 | 261 | $node->stmts = [
|
| 262 | + $magicConstFunction, |
| 263 | + $magicConstMethod, |
246 | 264 | new Return_($staticCall),
|
247 | 265 | ];
|
248 | 266 | } else {
|
249 | 267 | $node->stmts = [
|
| 268 | + $magicConstFunction, |
| 269 | + $magicConstMethod, |
250 | 270 | new Expression($staticCall),
|
251 | 271 | ];
|
252 | 272 | }
|
|
0 commit comments