Browse Source

improve code styling

Signed-off-by: Sina Kuhestani <sina@ephpic.org>
Sina Kuhestani 2 days ago
parent
commit
a2a05aa046
2 changed files with 92 additions and 83 deletions
  1. 1 1
      autoload.php
  2. 91 82
      src/Autoloader.php

+ 1 - 1
autoload.php

@@ -2,7 +2,7 @@
 
 declare(strict_types=1);
 
-/*******************************************************************************
+/** ****************************************************************************
  * Front Controller
  * *****************************************************************************
  * This file serves as the front controller for the ePHPic Autoloader.

+ 91 - 82
src/Autoloader.php

@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace ePHPic\Autload;
 
-/*******************************************************************************
+/** ****************************************************************************
  * Class Autoloader
  * *****************************************************************************
  * A simple autoloader for loading PHP classes based on a defined mapping and 
@@ -18,6 +18,8 @@ namespace ePHPic\Autload;
  ******************************************************************************/
 class Autoloader
 {
+    /* *********************************************************** Properties */
+
     /**
      * The base directory for the application.
      *
@@ -41,68 +43,34 @@ class Autoloader
      */
     private static $map = [];
 
+    /* ******************************************************** Class Methods */
+
     /**
-     * Resolves the full file path for a given class name.
+     * Autoloads a class file based on the 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.
+     * This method resolves the file path for the class and includes the file if it exists.
      *
-     * @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.
+     * @param string $class The fully qualified class name to autoload.
+     * @return void
      */
-    public static function resolve(string $class, ?string $path = null): string
+    public static function autoload($class): void
     {
-        /* Boolean var, indicates if the class name is present in class map */
-        $isClassInMap = key_exists(
-            key: $class,
-            array: static::$map
+        /* Get expected class file */
+        $filePath = static::resolve(
+            class: $class,
+            path: null
         );
 
-        /* When class name is explicitly present in the class map */
-        if (true === $isClassInMap)
-        {/* ⚠️ NOT COMMON CASE */
-            /* Use path from class map to create full path of the class file */
-            return static::buildPath($class, $path, true);
-        }
-
-        /* Get position of last backslash in the class name */
-        $lastBackslashPos = strrpos(
-            haystack: $class,
-            needle: '\\'
+        /* Boolean var, indicates if class file exists */
+        $fileExists = @file_exists(
+            filename: $filePath
         );
 
-        /* Boolean var, indicates if class name has backslash */
-        $hasBackSlash = false !== $lastBackslashPos;
-
-        /* When class name has backslashes */
-        if (true === $hasBackSlash)
-        { /* 🚀 COMMON CASE */
-            /* Remove last section of calss name and add to expected file path */
-            $subString = substr(
-                string: $class,
-                offset: $lastBackslashPos + 1,
-                length: strlen($class) - 1
-            );
-            $suffix = $path ? DIRECTORY_SEPARATOR . $path : '';
-
-            /* This is expected path for file location */
-            $path = $subString . $suffix;
-
-            /* This is shortened class name to resolve again */
-            $class = substr(
-                string: $class,
-                offset: 0,
-                length: $lastBackslashPos
-            );
-
-            /* Retry to resolve new shortened $class */
-            return static::resolve($class, $path);
+        /* When class file exists simply include it */
+        if (true === $fileExists)
+        {
+            require_once $filePath;
         }
-
-        /* Return completely expected file path */
-        return static::buildPath($class, $path, false);
     }
 
     /**
@@ -121,7 +89,11 @@ class Autoloader
      * file path. If false, it will construct the path based on the class name.
      * @return string The constructed file path for the class.
      */
-    private static function buildPath(string $class, string $path, bool $useMap = true): string
+    private static function buildPath(
+        string $class,
+        string $path,
+        bool $useMap = true
+    ): string
     {
         /* When $useMap is true, then it uses class map to expect file path */
         if (true === $useMap)
@@ -156,34 +128,6 @@ class Autoloader
         return $filePath;
     }
 
-    /**
-     * 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): void
-    {
-        /* Get expected class file */
-        $filePath = static::resolve(
-            class: $class,
-            path: null
-        );
-
-        /* Boolean var, indicates if class file exists */
-        $fileExists = @file_exists(
-            filename: $filePath
-        );
-
-        /* When class file exists simply include it */
-        if (true === $fileExists)
-        {
-            require_once $filePath;
-        }
-    }
-
     /**
      * Registers the autoloader with the SPL autoload stack.
      *
@@ -207,4 +151,69 @@ class Autoloader
             prepend: true
         );
     }
+
+    /**
+     * 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(string $class, ?string $path = null): string
+    {
+        /* Boolean var, indicates if the class name is present in class map */
+        $isClassInMap = key_exists(
+            key: $class,
+            array: static::$map
+        );
+
+        /* When class name is explicitly present in the class map */
+        if (true === $isClassInMap)
+        {/* ⚠️ NOT COMMON CASE */
+            /* Use path from class map to create full path of the class file */
+            return static::buildPath($class, $path, true);
+        }
+
+        /* Get position of last backslash in the class name */
+        $lastBackslashPos = strrpos(
+            haystack: $class,
+            needle: '\\'
+        );
+
+        /* Boolean var, indicates if class name has backslash */
+        $hasBackSlash = false !== $lastBackslashPos;
+
+        /* When class name has backslashes */
+        if (true === $hasBackSlash)
+        { /* 🚀 COMMON CASE */
+            /* Remove last section of calss name and add to expected file path */
+            $subString = substr(
+                string: $class,
+                offset: $lastBackslashPos + 1,
+                length: strlen($class) - 1
+            );
+            $suffix = $path ? DIRECTORY_SEPARATOR . $path : '';
+
+            /* This is expected path for file location */
+            $path = $subString . $suffix;
+
+            /* This is shortened class name to resolve again */
+            $class = substr(
+                string: $class,
+                offset: 0,
+                length: $lastBackslashPos
+            );
+
+            /* Retry to resolve new shortened $class */
+            return static::resolve($class, $path);
+        }
+
+        /* Return completely expected file path */
+        return static::buildPath($class, $path, false);
+    }
 }