From 87085d285d059c6ba627aa8f4f10878934734fe6 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:44:10 +0300 Subject: [PATCH] refactor(autoload): Renamed `AutoLoader` to `ClassLoader` --- bootstrap.php | 18 +++++++-------- .../framework/test/TestAutoLoader.php | 10 ++++----- webfiori/framework/App.php | 16 +++++++------- .../{AutoLoader.php => ClassLoader.php} | 22 +++++++++---------- 4 files changed, 32 insertions(+), 34 deletions(-) rename webfiori/framework/autoload/{AutoLoader.php => ClassLoader.php} (98%) diff --git a/bootstrap.php b/bootstrap.php index c90956d7..731ab6cf 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -4,7 +4,7 @@ use webfiori\framework\App; -use webfiori\framework\autoload\AutoLoader; +use webfiori\framework\autoload\ClassLoader; $DS = DIRECTORY_SEPARATOR; @@ -23,7 +23,7 @@ fprintf(STDOUT, "Bootstrap Path: '".__DIR__."'\n"); fprintf(STDOUT,"Tests Directory: '".TESTS_DIRECTORY."'.\n"); fprintf(STDOUT,'Include Path: \''.get_include_path().'\''."\n"); -fprintf(STDOUT,"Tryning to load the class 'AutoLoader'...\n"); +fprintf(STDOUT,"Tryning to load the class 'ClassLoader'...\n"); $isAutoloaderLoaded = false; if (explode($DS, __DIR__)[0] == 'home') { @@ -31,7 +31,7 @@ foreach ($WebFioriFrameworkDirs as $dir) { //linux - $file = $DS.$dir.'framework'.$DS.'autoload'.$DS.'AutoLoader.php'; + $file = $DS.$dir.'framework'.$DS.'autoload'.$DS.'ClassLoader.php'; fprintf(STDOUT,"Checking if file '$file' is exist...\n"); if (file_exists($file)) { @@ -45,7 +45,7 @@ foreach ($WebFioriFrameworkDirs as $dir) { //other - $file = $dir.$DS.'framework'.$DS.'autoload'.$DS.'AutoLoader.php'; + $file = $dir.$DS.'framework'.$DS.'autoload'.$DS.'ClassLoader.php'; fprintf(STDOUT,"Checking if file '$file' is exist...\n"); if (file_exists($file)) { @@ -57,13 +57,13 @@ } if ($isAutoloaderLoaded === false) { - fprintf(STDERR, "Error: Unable to find the class 'AutoLoader'.\n"); + fprintf(STDERR, "Error: Unable to find the class 'ClassLoader'.\n"); exit(-1); } else { - fprintf(STDOUT,"Class 'AutoLoader' successfully loaded.\n"); + fprintf(STDOUT,"Class 'ClassLoader' successfully loaded.\n"); } fprintf(STDOUT,"Initializing autoload directories...\n"); -AutoLoader::get([ +ClassLoader::get([ 'search-folders' => [ 'tests', 'webfiori', @@ -79,8 +79,8 @@ fprintf(STDOUT,"Initializing application...\n"); App::start(); fprintf(STDOUT,'Done.'."\n"); -fprintf(STDOUT,'Root Directory: \''.AutoLoader::get()->root().'\'.'."\n"); -define('TESTS_PATH', AutoLoader::get()->root().$DS.TESTS_DIRECTORY); +fprintf(STDOUT,'Root Directory: \''.ClassLoader::get()->root().'\'.'."\n"); +define('TESTS_PATH', ClassLoader::get()->root().$DS.TESTS_DIRECTORY); fprintf(STDOUT,'Tests Path: '.TESTS_PATH."\n"); fprintf(STDOUT,'App Path: '.APP_PATH."\n"); fprintf(STDOUT,"---------------------------------\n"); diff --git a/tests/webfiori/framework/test/TestAutoLoader.php b/tests/webfiori/framework/test/TestAutoLoader.php index 474e9f28..f10e1843 100644 --- a/tests/webfiori/framework/test/TestAutoLoader.php +++ b/tests/webfiori/framework/test/TestAutoLoader.php @@ -2,7 +2,7 @@ namespace webfiori\framework\test; use PHPUnit\Framework\TestCase; -use webfiori\framework\autoload\AutoLoader; +use webfiori\framework\autoload\ClassLoader; /** * Description of TestAutoLoader * @@ -13,24 +13,24 @@ class TestAutoLoader extends TestCase { * @test */ public function test00() { - $this->assertEquals(ROOT_PATH, AutoLoader::root()); + $this->assertEquals(ROOT_PATH, ClassLoader::root()); } /** * @test */ public function test01() { - $cArr = explode('\\', 'webfiori\\framework\\autoload\\AutoLoader'); + $cArr = explode('\\', 'webfiori\\framework\\autoload\\ClassLoader'); $className = $cArr[count($cArr) - 1]; $classNs = implode('\\', array_slice($cArr, 0, count($cArr) - 1)); - $isLoaded = AutoLoader::isLoaded($className, $classNs); + $isLoaded = ClassLoader::isLoaded($className, $classNs); $this->assertTrue($isLoaded); } /** * @test */ public function test02() { - $isLoaded = AutoLoader::isLoaded('AutoLoader'); + $isLoaded = ClassLoader::isLoaded('ClassLoader'); $this->assertTrue($isLoaded); } } diff --git a/webfiori/framework/App.php b/webfiori/framework/App.php index 17b78d05..a419efb9 100644 --- a/webfiori/framework/App.php +++ b/webfiori/framework/App.php @@ -17,7 +17,7 @@ use webfiori\error\Handler; use webfiori\file\exceptions\FileException; use webfiori\file\File; -use webfiori\framework\autoload\AutoLoader; +use webfiori\framework\autoload\ClassLoader; use webfiori\framework\config\ConfigurationDriver; use webfiori\framework\config\Controller; use webfiori\framework\exceptions\InitializationException; @@ -64,7 +64,7 @@ class App { /** * An instance of autoloader class. * - * @var AutoLoader + * @var ClassLoader * * @since 1.0 */ @@ -248,13 +248,13 @@ public static function autoRegister(string $folder, callable $regCallback, strin } } /** - * Returns a reference to an instance of 'AutoLoader'. + * Returns a reference to an instance of 'ClassLoader'. * - * @return AutoLoader A reference to an instance of 'AutoLoader'. + * @return ClassLoader A reference to an instance of 'ClassLoader'. * * @since 1.2.1 */ - public static function getAutoloader(): AutoLoader { + public static function getClassLoader(): ClassLoader { return self::$AU; } /** @@ -527,10 +527,10 @@ private function initAutoLoader() { /** * Initialize autoloader. */ - if (!class_exists('webfiori\framework\autoload\AutoLoader',false)) { - require_once WF_CORE_PATH.DIRECTORY_SEPARATOR.'autoload'.DIRECTORY_SEPARATOR.'AutoLoader.php'; + if (!class_exists('webfiori\framework\autoload\ClassLoader',false)) { + require_once WF_CORE_PATH.DIRECTORY_SEPARATOR.'autoload'.DIRECTORY_SEPARATOR.'ClassLoader.php'; } - self::$AU = AutoLoader::get(); + self::$AU = ClassLoader::get(); if (!class_exists(APP_DIR.'\ini\InitAutoLoad')) { Ini::createAppDirs(); diff --git a/webfiori/framework/autoload/AutoLoader.php b/webfiori/framework/autoload/ClassLoader.php similarity index 98% rename from webfiori/framework/autoload/AutoLoader.php rename to webfiori/framework/autoload/ClassLoader.php index 2254bc87..ab51106a 100644 --- a/webfiori/framework/autoload/AutoLoader.php +++ b/webfiori/framework/autoload/ClassLoader.php @@ -11,8 +11,6 @@ namespace webfiori\framework\autoload; use Exception; -use webfiori\framework\autoload\ClassInfo; -use webfiori\framework\exceptions\ClassLoaderException; /** * An autoloader class to load classes as needed during runtime. * @@ -28,7 +26,7 @@ * * @version 1.1.7 */ -class AutoLoader { +class ClassLoader { /** * The name of the file that represents autoloader's cache. * @var string @@ -68,9 +66,9 @@ class AutoLoader { */ private $loadedClasses; /** - * A single instance of the class 'AutoLoader'. + * A single instance of the class 'ClassLoader'. * - * @var AutoLoader + * @var ClassLoader * * @since 1.0 */ @@ -138,7 +136,7 @@ private function __construct(string $root = '', array $searchFolders = [], bool } spl_autoload_register(function($className) { - AutoLoader::get()->loadClass($className); + ClassLoader::get()->loadClass($className); }); if (gettype($onFail) == 'string') { @@ -153,8 +151,8 @@ private function __construct(string $root = '', array $searchFolders = [], bool $this->onFail = self::ON_FAIL_ACTIONS[0]; } $this->loadedClasses[] = [ - ClassInfo::NAME => 'AutoLoader', - ClassInfo::NS => substr(self::class, 0, strlen(self::class) - strlen('AutoLoader') - 1), + ClassInfo::NAME => 'ClassLoader', + ClassInfo::NS => substr(self::class, 0, strlen(self::class) - strlen('ClassLoader') - 1), ClassInfo::PATH => __DIR__, ClassInfo::CACHED => false ]; @@ -172,7 +170,7 @@ private function __construct(string $root = '', array $searchFolders = [], bool ]; } /** - * Returns a single instance of the class 'AutoLoader'. + * Returns a single instance of the class 'ClassLoader'. * * @param $options array An associative array of options that is used to initialize * the autoloader. The available options are: @@ -196,7 +194,7 @@ private function __construct(string $root = '', array $searchFolders = [], bool * * * - * @return AutoLoader + * @return ClassLoader * * @throws Exception */ @@ -205,7 +203,7 @@ public static function get(array $options = [ 'search-folders' => [], 'root' => '', 'on-load-failure' => self::ON_FAIL_ACTIONS[1] - ]): AutoLoader { + ]): ClassLoader { $DS = DIRECTORY_SEPARATOR; if (self::$loader === null) { @@ -243,7 +241,7 @@ public static function get(array $options = [ $root = $DS.$root; } $onFail = $options['on-load-failure'] ?? self::ON_FAIL_ACTIONS[0]; - self::$loader = new AutoLoader($root, $frameworkSearchFolders, $defineRoot,$onFail); + self::$loader = new ClassLoader($root, $frameworkSearchFolders, $defineRoot,$onFail); self::checkComposer(); }