@@ -17,7 +17,7 @@ class NamespaceMap
17
17
protected array $ namespaces = [];
18
18
19
19
/**
20
- * @var array<string, array<string> >
20
+ * @var array<string, NamespaceList >
21
21
*/
22
22
protected array $ aliases = [];
23
23
@@ -69,16 +69,20 @@ public function remove(
69
69
70
70
/**
71
71
* Add alias
72
+ *
73
+ * @return $this
72
74
*/
73
75
public function addAlias (
74
76
string $ interface ,
75
- string $ alias
76
- ): void {
77
+ string $ alias ,
78
+ int $ priority = 0
79
+ ): static {
77
80
if (!isset ($ this ->aliases [$ interface ])) {
78
- $ this ->aliases [$ interface ] = [] ;
81
+ $ this ->aliases [$ interface ] = new NamespaceList () ;
79
82
}
80
83
81
- $ this ->aliases [$ interface ][$ alias ] = $ alias ;
84
+ $ this ->aliases [$ interface ]->add ($ alias , $ priority );
85
+ return $ this ;
82
86
}
83
87
84
88
/**
@@ -88,51 +92,97 @@ public function hasAlias(
88
92
string $ interface ,
89
93
string $ alias
90
94
): bool {
91
- return isset ($ this ->aliases [$ interface ][$ alias ]);
95
+ return
96
+ isset ($ this ->aliases [$ interface ]) &&
97
+ $ this ->aliases [$ interface ]->has ($ alias );
92
98
}
93
99
94
100
/**
95
101
* Remove alias
102
+ *
103
+ * @return $this
96
104
*/
97
105
public function removeAlias (
98
106
string $ interface ,
99
107
string $ alias
100
- ): void {
101
- unset($ this ->aliases [$ interface ][$ alias ]);
108
+ ): static {
109
+ if (isset ($ this ->aliases [$ interface ])) {
110
+ $ this ->aliases [$ interface ]->remove ($ alias );
111
+ }
112
+
113
+ return $ this ;
102
114
}
103
115
104
116
/**
105
117
* Map namespace
106
118
*/
107
119
public function map (
108
- string $ namespace
120
+ string $ namespace ,
121
+ bool $ includeRoot = true
109
122
): NamespaceList {
110
123
$ output = new NamespaceList ();
111
- $ this ->applyMap ($ namespace , $ output );
112
-
113
- foreach ($ this ->aliases [$ namespace ] ?? [] as $ alias ) {
114
- $ this ->applyMap ($ alias , $ output );
115
- }
116
-
124
+ $ this ->applyMap ($ namespace , $ output , -1 , $ includeRoot );
117
125
return $ output ;
118
126
}
119
127
120
128
protected function applyMap (
121
129
string $ namespace ,
122
- NamespaceList $ namespaces
130
+ NamespaceList $ namespaces ,
131
+ int $ priority = 0 ,
132
+ bool $ includeRoot = true
123
133
): NamespaceList {
134
+ // Import root
135
+ if ($ includeRoot ) {
136
+ $ namespaces ->add ($ namespace , $ priority );
137
+ }
138
+
124
139
$ parts = explode ('\\' , $ namespace );
125
140
$ inner = [];
126
- $ namespaces ->add ($ namespace , -1 );
127
141
128
142
while (!empty ($ parts )) {
129
143
$ root = implode ('\\' , $ parts );
130
144
145
+ // Import root maps
131
146
if (isset ($ this ->namespaces [$ root ])) {
132
147
$ mapTo = empty ($ inner ) ? null : implode ('\\' , $ inner );
133
148
$ namespaces ->import ($ this ->namespaces [$ root ], $ mapTo , $ namespace );
134
149
}
135
150
151
+
152
+ // Aliases
153
+ $ wild = false ;
154
+ $ key = null ;
155
+
156
+ if (
157
+ isset ($ this ->aliases [$ root . '\\* ' ]) &&
158
+ // Wildcards only make sense for one level
159
+ count ($ inner ) <= 1
160
+ ) {
161
+ $ key = $ root . '\\* ' ;
162
+ $ wild = true ;
163
+ } elseif (isset ($ this ->aliases [$ root ])) {
164
+ $ key = $ root ;
165
+ }
166
+
167
+ if ($ key !== null ) {
168
+ foreach ($ this ->aliases [$ key ] ?? [] as $ priority => $ alias ) {
169
+ $ append = $ inner ;
170
+
171
+ if ($ wild ) {
172
+ array_pop ($ append );
173
+ }
174
+
175
+ if (!empty ($ append )) {
176
+ $ alias .= '\\' . implode ('\\' , $ append );
177
+ }
178
+
179
+ $ this ->applyMap ($ alias , $ namespaces , $ priority );
180
+ }
181
+
182
+ return $ namespaces ;
183
+ }
184
+
185
+ // Shift parts
136
186
array_unshift ($ inner , array_pop ($ parts ));
137
187
}
138
188
0 commit comments