@@ -643,14 +643,19 @@ private function loadClassHelper(string $className, string $classWithNs, string
643
643
$ isFileLoaded = in_array ($ f , $ allPaths );
644
644
645
645
if (!$ isFileLoaded && file_exists ($ f )) {
646
- $ ns = count (explode ('\\' , $ classWithNs )) == 1 ? '\\' : substr ($ classWithNs , 0 , strlen ($ classWithNs ) - strlen ($ className ) - 1 );
647
- $ this ->loadedClasses [] = [
648
- ClassInfo::NAME => $ className ,
649
- ClassInfo::NS => $ ns ,
650
- ClassInfo::PATH => $ f ,
651
- ClassInfo::CACHED => false
652
- ];
653
- $ loaded = true ;
646
+ $ nsFromPath = $ this ->createNSFromPath ($ f , $ className );
647
+
648
+ if (in_array ('\\' .$ classWithNs , $ nsFromPath )) {
649
+ require_once $ f ;
650
+ $ ns = count (explode ('\\' , $ classWithNs )) == 1 ? '\\' : substr ($ classWithNs , 0 , strlen ($ classWithNs ) - strlen ($ className ) - 1 );
651
+ $ this ->loadedClasses [] = [
652
+ ClassInfo::NAME => $ className ,
653
+ ClassInfo::NS => $ ns ,
654
+ ClassInfo::PATH => $ f ,
655
+ ClassInfo::CACHED => false
656
+ ];
657
+ $ loaded = true ;
658
+ }
654
659
}
655
660
656
661
return $ loaded ;
@@ -693,9 +698,27 @@ private function createNSFromPath(string $filePath, string $className) {
693
698
$ currentNs = '' ;
694
699
foreach ($ split as $ str ) {
695
700
if (self ::isValidNamespace ($ str )) {
696
- $ currentNs .= '\\' .$ str ;
701
+ if (strlen ($ currentNs ) == 0 ) {
702
+ $ currentNs = '\\' .$ str ;
703
+ } else {
704
+ $ currentNs = $ currentNs .'\\' .$ str ;
705
+ }
706
+ $ nsArr [] = $ currentNs .'\\' .$ className ;
707
+ }
708
+
709
+ }
710
+ $ currentNs = '' ;
711
+ for ($ x = count ($ split ) - 1 ; $ x > -1 ; $ x --) {
712
+ $ str = $ split [$ x ];
713
+ if (self ::isValidNamespace ($ str )) {
714
+ if (strlen ($ currentNs ) == 0 ) {
715
+ $ currentNs = '\\' .$ str ;
716
+ } else {
717
+ $ currentNs = '\\' .$ str .$ currentNs ;
718
+ }
719
+ $ nsArr [] = $ currentNs .'\\' .$ className ;
697
720
}
698
- $ nsArr [] = $ currentNs ;
721
+
699
722
}
700
723
return $ nsArr ;
701
724
}
0 commit comments