Skip to content

Commit

Permalink
fix(autoload): Check NS with Path
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 9, 2024
1 parent 57082c0 commit a3d4c6e
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions webfiori/framework/autoload/AutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,19 @@ private function loadClassHelper(string $className, string $classWithNs, string
$isFileLoaded = in_array($f, $allPaths);

if (!$isFileLoaded && file_exists($f)) {
$ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1);
$this->loadedClasses[] = [
ClassInfo::NAME => $className,
ClassInfo::NS => $ns,
ClassInfo::PATH => $f,
ClassInfo::CACHED => false
];
$loaded = true;
$nsFromPath = $this->createNSFromPath($f, $className);

if (in_array('\\'.$classWithNs, $nsFromPath)) {
require_once $f;
$ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1);
$this->loadedClasses[] = [
ClassInfo::NAME => $className,
ClassInfo::NS => $ns,
ClassInfo::PATH => $f,
ClassInfo::CACHED => false
];
$loaded = true;
}
}

return $loaded;
Expand Down Expand Up @@ -693,9 +698,27 @@ private function createNSFromPath(string $filePath, string $className) {
$currentNs = '';
foreach ($split as $str) {
if (self::isValidNamespace($str)) {
$currentNs .= '\\'.$str;
if (strlen($currentNs) == 0) {
$currentNs = '\\'.$str;
} else {
$currentNs = $currentNs.'\\'.$str;
}
$nsArr[] = $currentNs.'\\'.$className;
}

}
$currentNs = '';
for ($x = count($split) - 1 ; $x > -1 ; $x--) {
$str = $split[$x];
if (self::isValidNamespace($str)) {
if (strlen($currentNs) == 0) {
$currentNs = '\\'.$str;
} else {
$currentNs = '\\'.$str.$currentNs;
}
$nsArr[] = $currentNs.'\\'.$className;
}
$nsArr[] = $currentNs;

}
return $nsArr;
}
Expand Down

0 comments on commit a3d4c6e

Please sign in to comment.