Skip to content

Commit

Permalink
feat: Added Support for Loading Non-PSR-4 Compliant Classes
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Oct 21, 2024
1 parent 2791223 commit a9772b4
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion webfiori/framework/autoload/ClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,60 @@ public static function get(array $options = [

return self::$loader;
}

/**
* Load a class using its specified path.
*
* This method can be used in case the class that the user tries to load does
* not comply with PSR-4 standard for placing classes in correct folder
* with correct namespace.
*
* @param string $className The name of the class that will be loaded.
*
* @param string $classWithNs The full name of the class including its namespace.
*
* @param string $path The path to PHP file that has class implementation.
*
* @return bool If file is exist and class is loaded, true is returned. False
* otherwise.
*/
public static function map(string $className, string $classWithNs, string $path) {
self::get()->addClassMap($className, $classWithNs, $path);
}
/**
* Load a class using its specified path.
*
* This method can be used in case the class that the user tries to load does
* not comply with PSR-4 standard for placing classes in correct folder
* with correct namespace.
*
* @param string $className The name of the class that will be loaded.
*
* @param string $classWithNs The full name of the class including its namespace.
*
* @param string $path The path to PHP file that has class implementation.
*
* @return bool If file is exist and class is loaded, true is returned. False
* otherwise.
*/
public function addClassMap(string $className, string $classWithNs, string $path) : bool {
$ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1);

if ($this->loadFromCache($ns, $className)) {
return true;
}
if (!file_exists($path)) {
return false;
}
require_once $path;

$this->loadedClasses[] = [
ClassInfo::NAME => $className,
ClassInfo::NS => $ns,
ClassInfo::PATH => $path,
ClassInfo::CACHED => false
];
return true;
}
/**
* Returns an array that contains all cached classes information.
*
Expand Down

0 comments on commit a9772b4

Please sign in to comment.