@@ -6,6 +6,7 @@ class FileManager
66 {
77 private const CLASSES = '.Classes ' ;
88 private const GIT = '.Git ' ;
9+ private const SOURCE = '.Source ' ;
910 private const NAMESPACE = '.Namespace ' ;
1011
1112 private const REPO_ROOT = '.Root ' ;
@@ -44,10 +45,7 @@ public function getComposerPath() : string
4445 */
4546 public function addNamespace (string $ namespace , string $ directory , bool $ localGit = false ) : FileManager
4647 {
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 );
5149
5250 return $ this ;
5351 }
@@ -91,7 +89,7 @@ public function getAllNamespaceDirectories(bool $rescan = true) : array
9189
9290 if ($ this ->composerJsonPath )
9391 {
94- $ directories = $ this ->getAllVendorDirectories ($ this -> composerJsonPath );
92+ $ directories = $ this ->getAllVendorDirectories ();
9593 }
9694 $ directories = array_merge ($ directories , $ this ->includedNamespaces );
9795
@@ -117,27 +115,21 @@ public function getAllNamespaces() : array
117115 * Read the composer files to get all namespaces for include
118116 * libraries.
119117 */
120- public function getAllVendorDirectories () : array
118+ private function getAllVendorDirectories () : array
121119 {
122- $ composerJsonPath = $ this ->composerJsonPath ;
123-
124- if (is_dir ($ composerJsonPath ))
125- {
126- $ composerJsonPath .= '/composer.lock ' ;
127- }
120+ $ composerJsonPath = $ this ->composerJsonPath . 'composer.lock ' ;
128121 $ composerJsonPath = str_replace ('// ' , '/ ' , $ composerJsonPath );
129122 $ json = json_decode (@file_get_contents ($ composerJsonPath ), true );
130123
131124 if (! $ json )
132125 {
133126 throw new \Exception ("{$ composerJsonPath } does not appear to be a valid composer.lock file " );
134127 }
135- $ dir = str_replace ('composer.lock ' , '' , $ composerJsonPath );
136128 $ directories = [];
137129
138130 foreach ($ json ['packages ' ] as $ package )
139131 {
140- $ packagePath = $ dir . 'vendor/ ' . $ package ['name ' ];
132+ $ packagePath = $ this -> composerJsonPath . 'vendor/ ' . $ package ['name ' ];
141133 $ autoload = $ package ['autoload ' ] ?? [];
142134 $ namespace = $ sourceDir = '' ;
143135 $ autoLoadTypes = ['psr-4 ' , 'psr-0 ' , 'classmap ' ];
@@ -166,7 +158,11 @@ public function getAllVendorDirectories() : array
166158 $ path = str_replace ('// ' , '/ ' , $ path );
167159 $ directories [$ namespace ][FileManager::GIT ] = false ;
168160 $ 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 ;
170166 $ directories [$ namespace ][FileManager::CLASSES ] = [];
171167 }
172168 }
@@ -191,16 +187,29 @@ public function getClassesInNamespace(string $namespace) : array
191187 $ files = $ this ->getFilesInNamespace ($ namespace , $ extension );
192188 $ namespaceInfo = $ this ->namespaces [$ namespace ];
193189
190+ // echo '<pre>';
194191 foreach ($ files as $ file )
195192 {
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 )
199195 {
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+ }
201210 }
202- $ classes [$ file ] = substr ($ class , 0 , strlen ($ class ) - strlen ($ extension ));
203211 }
212+ // echo '<pre>';
204213
205214 return $ this ->namespaces [$ namespace ][FileManager::CLASSES ] = $ classes ;
206215 }
@@ -219,32 +228,18 @@ public function getFilesInNamespace(string $namespace, string $extension = '') :
219228 throw new \Exception ('In ' . __METHOD__ . " -> {$ namespace } was not found. " );
220229 }
221230
222- foreach ($ this ->namespaces [$ namespace ] as $ key => $ directory )
231+ $ directory = $ this ->namespaces [$ namespace ][FileManager::REPO_ROOT ];
232+ if (is_dir ($ directory ))
223233 {
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 );
228237
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 )
237239 {
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 ))
243241 {
244- if (! $ fileInfo ->isDir () && $ this ->hasExtension ($ filename , $ extension ))
245- {
246- $ files [] = $ filename ;
247- }
242+ $ files [] = $ filename ;
248243 }
249244 }
250245 }
@@ -299,6 +294,8 @@ public function load(string $file = '') : FileManager
299294 {
300295 $ file = $ this ->getSerializedName ($ file );
301296
297+ @unlink ($ file );
298+
302299 if (! file_exists ($ file ))
303300 {
304301 $ this ->rescan ();
@@ -333,7 +330,7 @@ public function rescan() : FileManager
333330 */
334331 public function save (string $ file = '' ) : FileManager
335332 {
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 ));
337334
338335 return $ this ;
339336 }
0 commit comments