File tree 3 files changed +71
-0
lines changed
3 files changed +71
-0
lines changed Original file line number Diff line number Diff line change 11
11
*/
12
12
namespace Hyperf \Di \Aop ;
13
13
14
+ use Hyperf \Support \Composer ;
14
15
use PhpParser \Node ;
15
16
use PhpParser \Node \Arg ;
16
17
use PhpParser \Node \Expr \Assign ;
21
22
use PhpParser \Node \Identifier ;
22
23
use PhpParser \Node \Name ;
23
24
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 ;
24
27
use PhpParser \Node \Scalar \MagicConst \Function_ as MagicConstFunction ;
25
28
use PhpParser \Node \Scalar \MagicConst \Method as MagicConstMethod ;
26
29
use PhpParser \Node \Scalar \MagicConst \Trait_ as MagicConstTrait ;
30
+ use PhpParser \Node \Scalar \String_ ;
27
31
use PhpParser \Node \Stmt \Class_ ;
28
32
use PhpParser \Node \Stmt \ClassMethod ;
29
33
use PhpParser \Node \Stmt \Expression ;
@@ -113,6 +117,18 @@ public function leaveNode(Node $node)
113
117
return new Variable ('__method__ ' );
114
118
}
115
119
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 ;
116
132
}
117
133
return null ;
118
134
}
Original file line number Diff line number Diff line change 27
27
use HyperfTest \Di \Stub \Ast \FooTrait ;
28
28
use HyperfTest \Di \Stub \FooEnumStruct ;
29
29
use HyperfTest \Di \Stub \Par2 ;
30
+ use HyperfTest \Di \Stub \PathStub ;
30
31
use PHPUnit \Framework \TestCase ;
31
32
32
33
/**
@@ -71,6 +72,35 @@ function __construct()
71
72
} ' , $ code );
72
73
}
73
74
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
+
74
104
public function testParentWith ()
75
105
{
76
106
$ ast = new Ast ();
Original file line number Diff line number Diff line change
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
+
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
+ }
You can’t perform that action at this time.
0 commit comments