@@ -6,6 +6,7 @@ class FileManager
6
6
{
7
7
private const CLASSES = '.Classes ' ;
8
8
private const GIT = '.Git ' ;
9
+ private const SOURCE = '.Source ' ;
9
10
private const NAMESPACE = '.Namespace ' ;
10
11
11
12
private const REPO_ROOT = '.Root ' ;
@@ -44,10 +45,7 @@ public function getComposerPath() : string
44
45
*/
45
46
public function addNamespace (string $ namespace , string $ directory , bool $ localGit = false ) : FileManager
46
47
{
47
- $ this ->includedNamespaces [$ namespace ][FileManager::REPO_ROOT ] = $ directory ;
48
- $ this ->includedNamespaces [$ namespace ][] = $ directory ;
49
- $ this ->includedNamespaces [$ namespace ][FileManager::GIT ] = $ localGit ;
50
- $ this ->includedNamespaces [$ namespace ][FileManager::CLASSES ] = [];
48
+ NamespaceTree::addNameSpace ($ namespace , $ directory , $ localGit );
51
49
52
50
return $ this ;
53
51
}
@@ -91,7 +89,7 @@ public function getAllNamespaceDirectories(bool $rescan = true) : array
91
89
92
90
if ($ this ->composerJsonPath )
93
91
{
94
- $ directories = $ this ->getAllVendorDirectories ($ this -> composerJsonPath );
92
+ $ directories = $ this ->getAllVendorDirectories ();
95
93
}
96
94
$ directories = array_merge ($ directories , $ this ->includedNamespaces );
97
95
@@ -117,27 +115,21 @@ public function getAllNamespaces() : array
117
115
* Read the composer files to get all namespaces for include
118
116
* libraries.
119
117
*/
120
- public function getAllVendorDirectories () : array
118
+ private function getAllVendorDirectories () : array
121
119
{
122
- $ composerJsonPath = $ this ->composerJsonPath ;
123
-
124
- if (is_dir ($ composerJsonPath ))
125
- {
126
- $ composerJsonPath .= '/composer.lock ' ;
127
- }
120
+ $ composerJsonPath = $ this ->composerJsonPath . 'composer.lock ' ;
128
121
$ composerJsonPath = str_replace ('// ' , '/ ' , $ composerJsonPath );
129
122
$ json = json_decode (@file_get_contents ($ composerJsonPath ), true );
130
123
131
124
if (! $ json )
132
125
{
133
126
throw new \Exception ("{$ composerJsonPath } does not appear to be a valid composer.lock file " );
134
127
}
135
- $ dir = str_replace ('composer.lock ' , '' , $ composerJsonPath );
136
128
$ directories = [];
137
129
138
130
foreach ($ json ['packages ' ] as $ package )
139
131
{
140
- $ packagePath = $ dir . 'vendor/ ' . $ package ['name ' ];
132
+ $ packagePath = $ this -> composerJsonPath . 'vendor/ ' . $ package ['name ' ];
141
133
$ autoload = $ package ['autoload ' ] ?? [];
142
134
$ namespace = $ sourceDir = '' ;
143
135
$ autoLoadTypes = ['psr-4 ' , 'psr-0 ' , 'classmap ' ];
@@ -166,7 +158,11 @@ public function getAllVendorDirectories() : array
166
158
$ path = str_replace ('// ' , '/ ' , $ path );
167
159
$ directories [$ namespace ][FileManager::GIT ] = false ;
168
160
$ directories [$ namespace ][FileManager::REPO_ROOT ] = $ path ;
169
- $ directories [$ namespace ][] = $ path . $ sourceDir ;
161
+ if (is_array ($ sourceDir ))
162
+ {
163
+ $ sourceDir = reset ($ sourceDir );
164
+ }
165
+ $ directories [$ namespace ][FileManager::SOURCE ][$ path . $ sourceDir ] = true ;
170
166
$ directories [$ namespace ][FileManager::CLASSES ] = [];
171
167
}
172
168
}
@@ -191,16 +187,29 @@ public function getClassesInNamespace(string $namespace) : array
191
187
$ files = $ this ->getFilesInNamespace ($ namespace , $ extension );
192
188
$ namespaceInfo = $ this ->namespaces [$ namespace ];
193
189
190
+ // echo '<pre>';
194
191
foreach ($ files as $ file )
195
192
{
196
- $ class = substr ($ file , strlen ($ namespaceInfo [0 ]));
197
- $ class = str_replace ('/ ' , '\\' , $ class );
198
- if (0 === strpos ($ class , $ namespace ))
193
+ $ file = str_replace ('\\' , '/ ' , $ file );
194
+ foreach ($ namespaceInfo [FileManager::SOURCE ] as $ directory => $ value )
199
195
{
200
- $ class = substr ($ class , strlen ($ namespace ));
196
+ $ directory = str_replace ('\\' , '/ ' , $ directory );
197
+ $ start = strpos ($ file , $ directory );
198
+ if ($ start !== false )
199
+ {
200
+ $ class = substr ($ file , $ start + strlen ($ directory ));
201
+ $ class = str_replace (['/ ' , '.php ' ], ['\\' , '' ], $ class );
202
+ if (strpos ($ class , $ namespace ) !== 0 )
203
+ {
204
+ $ class = str_replace ('\\\\' , '\\' , $ namespace . '\\' . $ class );
205
+ }
206
+ $ class = substr ($ class , strlen ($ namespace ) + 1 );
207
+ // echo "add class {$namespace} => {$class}\n";
208
+ $ classes [$ file ] = $ class ;
209
+ }
201
210
}
202
- $ classes [$ file ] = substr ($ class , 0 , strlen ($ class ) - strlen ($ extension ));
203
211
}
212
+ // echo '<pre>';
204
213
205
214
return $ this ->namespaces [$ namespace ][FileManager::CLASSES ] = $ classes ;
206
215
}
@@ -219,32 +228,18 @@ public function getFilesInNamespace(string $namespace, string $extension = '') :
219
228
throw new \Exception ('In ' . __METHOD__ . " -> {$ namespace } was not found. " );
220
229
}
221
230
222
- foreach ($ this ->namespaces [$ namespace ] as $ key => $ directory )
231
+ $ directory = $ this ->namespaces [$ namespace ][FileManager::REPO_ROOT ];
232
+ if (is_dir ($ directory ))
223
233
{
224
- if ('integer ' != gettype ($ key ))
225
- {
226
- continue ;
227
- }
234
+ $ directory = str_replace ('\\' , '/ ' , $ directory );
235
+ $ rdi = new \RecursiveDirectoryIterator ($ directory );
236
+ $ iterator = new \RecursiveIteratorIterator ($ rdi , \RecursiveIteratorIterator::CHILD_FIRST );
228
237
229
- if (is_file ($ directory ))
230
- {
231
- if ($ this ->hasExtension ($ filename , $ extension ))
232
- {
233
- $ files [] = $ directory ;
234
- }
235
- }
236
- else
238
+ foreach ($ iterator as $ filename => $ fileInfo )
237
239
{
238
- $ directory = str_replace ('\\' , '/ ' , $ directory );
239
- $ rdi = new \RecursiveDirectoryIterator ($ directory );
240
- $ iterator = new \RecursiveIteratorIterator ($ rdi , \RecursiveIteratorIterator::CHILD_FIRST );
241
-
242
- foreach ($ iterator as $ filename => $ fileInfo )
240
+ if (! $ fileInfo ->isDir () && $ this ->hasExtension ($ filename , $ extension ))
243
241
{
244
- if (! $ fileInfo ->isDir () && $ this ->hasExtension ($ filename , $ extension ))
245
- {
246
- $ files [] = $ filename ;
247
- }
242
+ $ files [] = $ filename ;
248
243
}
249
244
}
250
245
}
@@ -299,6 +294,8 @@ public function load(string $file = '') : FileManager
299
294
{
300
295
$ file = $ this ->getSerializedName ($ file );
301
296
297
+ @unlink ($ file );
298
+
302
299
if (! file_exists ($ file ))
303
300
{
304
301
$ this ->rescan ();
@@ -333,7 +330,7 @@ public function rescan() : FileManager
333
330
*/
334
331
public function save (string $ file = '' ) : FileManager
335
332
{
336
- file_put_contents ($ this ->getSerializedName ($ file ), json_encode ($ this ->namespaces ));
333
+ file_put_contents ($ this ->getSerializedName ($ file ), json_encode ($ this ->namespaces , JSON_PRETTY_PRINT ));
337
334
338
335
return $ this ;
339
336
}
0 commit comments