Skip to content

Commit a3d4c6e

Browse files
committed
fix(autoload): Check NS with Path
1 parent 57082c0 commit a3d4c6e

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

webfiori/framework/autoload/AutoLoader.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,19 @@ private function loadClassHelper(string $className, string $classWithNs, string
643643
$isFileLoaded = in_array($f, $allPaths);
644644

645645
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+
}
654659
}
655660

656661
return $loaded;
@@ -693,9 +698,27 @@ private function createNSFromPath(string $filePath, string $className) {
693698
$currentNs = '';
694699
foreach ($split as $str) {
695700
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;
697720
}
698-
$nsArr[] = $currentNs;
721+
699722
}
700723
return $nsArr;
701724
}

0 commit comments

Comments
 (0)