Skip to content

Commit 4fa4689

Browse files
Chance-fyihuangdijialimingxinleo
authored
Fixed bug that __FILE__ and __DIR__ cannot be rewritten successfully in proxy classes. (#5794)
Co-authored-by: Deeka Wong <[email protected]> Co-authored-by: 李铭昕 <[email protected]>
1 parent 32664ce commit 4fa4689

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

src/Aop/ProxyCallVisitor.php

+16
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace Hyperf\Di\Aop;
1313

14+
use Hyperf\Support\Composer;
1415
use PhpParser\Node;
1516
use PhpParser\Node\Arg;
1617
use PhpParser\Node\Expr\Assign;
@@ -21,9 +22,12 @@
2122
use PhpParser\Node\Identifier;
2223
use PhpParser\Node\Name;
2324
use PhpParser\Node\Scalar\MagicConst\Class_ as MagicConstClass;
25+
use PhpParser\Node\Scalar\MagicConst\Dir as MagicConstDir;
26+
use PhpParser\Node\Scalar\MagicConst\File as MagicConstFile;
2427
use PhpParser\Node\Scalar\MagicConst\Function_ as MagicConstFunction;
2528
use PhpParser\Node\Scalar\MagicConst\Method as MagicConstMethod;
2629
use PhpParser\Node\Scalar\MagicConst\Trait_ as MagicConstTrait;
30+
use PhpParser\Node\Scalar\String_;
2731
use PhpParser\Node\Stmt\Class_;
2832
use PhpParser\Node\Stmt\ClassMethod;
2933
use PhpParser\Node\Stmt\Expression;
@@ -113,6 +117,18 @@ public function leaveNode(Node $node)
113117
return new Variable('__method__');
114118
}
115119
break;
120+
case $node instanceof MagicConstDir:
121+
// Rewrite __DIR__ as the real directory path
122+
if ($file = Composer::getLoader()->findFile($this->visitorMetadata->className)) {
123+
return new String_(dirname(realpath($file)));
124+
}
125+
break;
126+
case $node instanceof MagicConstFile:
127+
// Rewrite __FILE__ to the real file path
128+
if ($file = Composer::getLoader()->findFile($this->visitorMetadata->className)) {
129+
return new String_(realpath($file));
130+
}
131+
break;
116132
}
117133
return null;
118134
}

tests/AstTest.php

+30
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use HyperfTest\Di\Stub\Ast\FooTrait;
2828
use HyperfTest\Di\Stub\FooEnumStruct;
2929
use HyperfTest\Di\Stub\Par2;
30+
use HyperfTest\Di\Stub\PathStub;
3031
use PHPUnit\Framework\TestCase;
3132

3233
/**
@@ -71,6 +72,35 @@ function __construct()
7172
}', $code);
7273
}
7374

75+
public function testMagicConstDirAndFile()
76+
{
77+
$ast = new Ast();
78+
$code = $ast->proxy(PathStub::class);
79+
$path = (new PathStub())->file();
80+
$dir = (new PathStub())->dir();
81+
82+
$this->assertSame($this->license . '
83+
namespace HyperfTest\Di\Stub;
84+
85+
class PathStub
86+
{
87+
use \Hyperf\Di\Aop\ProxyTrait;
88+
use \Hyperf\Di\Aop\PropertyHandlerTrait;
89+
function __construct()
90+
{
91+
$this->__handlePropertyHandler(__CLASS__);
92+
}
93+
public function file() : string
94+
{
95+
return \'' . $path . '\';
96+
}
97+
public function dir() : string
98+
{
99+
return \'' . $dir . '\';
100+
}
101+
}', $code);
102+
}
103+
74104
public function testParentWith()
75105
{
76106
$ast = new Ast();

tests/Stub/PathStub.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Di\Stub;
13+
14+
class PathStub
15+
{
16+
public function file(): string
17+
{
18+
return __FILE__;
19+
}
20+
21+
public function dir(): string
22+
{
23+
return __DIR__;
24+
}
25+
}

0 commit comments

Comments
 (0)