|
@@ -2,12 +2,36 @@
|
|
|
|
|
|
namespace ePHPic\Autload;
|
|
|
|
|
|
+/**
|
|
|
+ * Class Autoloader
|
|
|
+ *
|
|
|
+ * A simple autoloader for loading PHP classes based on a defined mapping and base directory.
|
|
|
+ *
|
|
|
+ * @package ePHPic\Autload
|
|
|
+ */
|
|
|
class Autoloader
|
|
|
{
|
|
|
- private static $baseDir = "some/where/in/my/app";
|
|
|
- private static $map = [
|
|
|
- ];
|
|
|
+ /**
|
|
|
+ * @var string The base directory for the autoloader.
|
|
|
+ */
|
|
|
+ private static $baseDir = "/var/www/html";
|
|
|
|
|
|
+ /**
|
|
|
+ * @var array A mapping of class names to file paths.
|
|
|
+ */
|
|
|
+ private static $map = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Resolves the full file path for a given class name.
|
|
|
+ *
|
|
|
+ * This method checks if the class is in the map. If it is, it constructs the file path
|
|
|
+ * based on the mapping. If not, it attempts to resolve the class name by removing the
|
|
|
+ * namespace and constructing the path based on the class name.
|
|
|
+ *
|
|
|
+ * @param string $class The fully qualified class name.
|
|
|
+ * @param string|null $path An optional additional path to append.
|
|
|
+ * @return string The resolved file path for the class.
|
|
|
+ */
|
|
|
public static function resolve($class, $path = null)
|
|
|
{
|
|
|
if(isset(static::$map[$class]))
|
|
@@ -23,6 +47,15 @@ class Autoloader
|
|
|
|
|
|
return static::$baseDir . "/" . str_replace("\\","/",$class) . ($path ? "/$path" : "") . ".php";
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Autoloads a class file based on the class name.
|
|
|
+ *
|
|
|
+ * This method resolves the file path for the class and includes the file if it exists.
|
|
|
+ *
|
|
|
+ * @param string $class The fully qualified class name to autoload.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
public static function autoload($class)
|
|
|
{
|
|
|
$filePath = static::resolve($class);
|
|
@@ -31,7 +64,18 @@ class Autoloader
|
|
|
include_once $filePath;
|
|
|
}
|
|
|
}
|
|
|
- public static function register()
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Registers the autoloader with the SPL autoload stack.
|
|
|
+ *
|
|
|
+ * This method allows the autoloader to be registered so that it can be used
|
|
|
+ * to automatically load classes when they are referenced.
|
|
|
+ *
|
|
|
+ * @param string $baseDir The base directory for the autoloader.
|
|
|
+ * @param array $map An associative array mapping class names to file paths.
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public static function register($baseDir, $map)
|
|
|
{
|
|
|
spl_autoload_register(
|
|
|
[static::class, "autoload"],
|