3
3
4
4
namespace TheWebSolver \Codegarage \Generator ;
5
5
6
+ use Closure ;
6
7
use Nette \Utils \Strings ;
7
8
use Nette \PhpGenerator \Dumper ;
9
+ use Nette \PhpGenerator \Helpers ;
8
10
use Nette \PhpGenerator \Literal ;
9
11
use Nette \PhpGenerator \PhpFile ;
10
12
use Nette \PhpGenerator \Printer ;
11
13
use TheWebSolver \Codegarage \Generator \Traits \ArrayExport ;
14
+ use TheWebSolver \Codegarage \Generator \Traits \ImportResolver ;
12
15
13
16
class ArrayPhpFile {
14
- use ArrayExport;
17
+ use ArrayExport, ImportResolver;
18
+
15
19
16
20
/** @var mixed[] */
17
21
private array $ content ;
18
22
private string |int $ parentKey ;
19
23
24
+ private static bool $ subscribeImports = true ;
25
+
20
26
public function __construct (
21
27
public readonly PhpFile $ phpFile = new PhpFile (),
22
28
private Printer $ printer = new Printer (),
23
29
private Dumper $ dumper = new Dumper ()
24
30
) {
25
- $ phpFile ->setStrictTypes ();
31
+ $ phpFile ->setStrictTypes ()->addNamespace ( $ this ->setNamespace ()->getNamespace () );
32
+ }
33
+
34
+ public static function subscribeForImport ( bool $ addUseStatement = true ): Closure {
35
+ $ previousSubscription = self ::$ subscribeImports ;
36
+ self ::$ subscribeImports = $ addUseStatement ;
37
+
38
+ return static fn () => self ::$ subscribeImports = $ previousSubscription ;
39
+ }
40
+
41
+ public static function isSubscribedForImport (): bool {
42
+ return self ::$ subscribeImports ;
26
43
}
27
44
28
45
/**
@@ -34,6 +51,8 @@ public function set( array &$array, string $key, mixed $value ): void {
34
51
$ index = $ key ;
35
52
36
53
foreach ( $ keys as $ i => $ key ) {
54
+ $ this ->maybeAddUseOf ( $ key );
55
+
37
56
if ( count ( $ keys ) === 1 ) {
38
57
$ index = $ key ;
39
58
@@ -69,8 +88,8 @@ protected function export( mixed &$content, int $level = 0, int $column = 0 ): s
69
88
}
70
89
71
90
public function exportString ( string $ content ): string {
72
- return str_ends_with ( $ content, needle: ' ::class ' )
73
- ? (string ) ( new Literal ( $ content ) )
91
+ return ( ( $ alias = $ this -> resolveImports ( $ content ) ) !== $ content )
92
+ ? (string ) new Literal ( $ alias )
74
93
: $ this ->dumper ->dump ( $ content );
75
94
}
76
95
@@ -81,14 +100,14 @@ public function childOf( string|int $parentKey ): static {
81
100
return $ this ;
82
101
}
83
102
84
- public function addContent ( string |int $ key , mixed $ value, string | int $ index = null ): static {
103
+ public function addContent ( string |int $ key , mixed $ value ): static {
85
104
$ parentKey = ( $ this ->parentKey ?? null );
86
105
$ array = $ this ->content ?? array ();
87
106
88
107
unset( $ this ->parentKey );
89
108
90
109
if ( ! $ parentKey ) {
91
- $ this ->content [ $ index ?? $ key ] = $ value ;
110
+ $ this ->content [ $ key ] = $ value ;
92
111
93
112
return $ this ;
94
113
}
@@ -107,11 +126,31 @@ public function addCallable( string|int $key, string|array $value ): static {
107
126
default => $ value ,
108
127
};
109
128
129
+ $ this ->maybeAddUseOf ( $ fqcn );
130
+
110
131
return $ this ->addContent ( $ key , array ( $ fqcn , $ methodName ) );
111
132
}
112
133
113
134
/** @return mixed[] */
114
135
public function getContent (): array {
115
136
return $ this ->content ;
116
137
}
138
+
139
+ protected function getAliasOf ( string $ import ): string {
140
+ if ( ! in_array ( $ import , $ uses = $ this ->getNamespace ()->getUses (), strict: true ) ) {
141
+ return $ import ;
142
+ }
143
+
144
+ return ( $ alias = array_search ( $ import , $ uses , strict: true ) ) ? "{$ alias }::class " : $ import ;
145
+ }
146
+
147
+ protected function resolveImports ( string $ content ): string {
148
+ return self ::isSubscribedForImport () ? $ this ->getAliasOf ( $ content ) : $ content ;
149
+ }
150
+
151
+ private function maybeAddUseOf ( string $ key ): void {
152
+ self ::isSubscribedForImport ()
153
+ && Helpers::isNamespaceIdentifier ( $ key )
154
+ && $ this ->addUseStatementOf ( $ key );
155
+ }
117
156
}
0 commit comments