Skip to content

Commit

Permalink
refactor(autoload): Renamed AutoLoader to ClassLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jul 10, 2024
1 parent 58d58e2 commit 87085d2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 34 deletions.
18 changes: 9 additions & 9 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


use webfiori\framework\App;
use webfiori\framework\autoload\AutoLoader;
use webfiori\framework\autoload\ClassLoader;

$DS = DIRECTORY_SEPARATOR;

Expand All @@ -23,15 +23,15 @@
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') {
fprintf(STDOUT,"Run Environment: Linux.\n");

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)) {
Expand All @@ -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)) {
Expand All @@ -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',
Expand All @@ -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");
Expand Down
10 changes: 5 additions & 5 deletions tests/webfiori/framework/test/TestAutoLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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);
}
}
16 changes: 8 additions & 8 deletions webfiori/framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,7 +64,7 @@ class App {
/**
* An instance of autoloader class.
*
* @var AutoLoader
* @var ClassLoader
*
* @since 1.0
*/
Expand Down Expand Up @@ -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;
}
/**
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -28,7 +26,7 @@
*
* @version 1.1.7
*/
class AutoLoader {
class ClassLoader {
/**
* The name of the file that represents autoloader's cache.
* @var string
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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') {
Expand All @@ -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
];
Expand All @@ -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:
Expand All @@ -196,7 +194,7 @@ private function __construct(string $root = '', array $searchFolders = [], bool
* </li>
* </ul>
*
* @return AutoLoader
* @return ClassLoader
*
* @throws Exception
*/
Expand All @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 87085d2

Please sign in to comment.