From 30b0043da89aa34eeb91a5c27257f0039fbb4396 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 10 Jul 2024 00:18:44 +0300 Subject: [PATCH 01/13] refactor(autoloader): Use Class Constants Instead of Array --- webfiori/framework/AutoLoader.php | 60 ++++++++--------------- webfiori/framework/autoload/ClassInfo.php | 33 +++++++++++++ 2 files changed, 53 insertions(+), 40 deletions(-) create mode 100644 webfiori/framework/autoload/ClassInfo.php diff --git a/webfiori/framework/AutoLoader.php b/webfiori/framework/AutoLoader.php index 2db81b4b7..9b14a6aa8 100644 --- a/webfiori/framework/AutoLoader.php +++ b/webfiori/framework/AutoLoader.php @@ -11,6 +11,7 @@ namespace webfiori\framework; use Exception; +use webfiori\framework\autoload\ClassInfo; use webfiori\framework\exceptions\ClassLoaderException; /** * An autoloader class to load classes as needed during runtime. @@ -58,26 +59,6 @@ class AutoLoader { * @since 1.1.6 */ private $cacheArr; - /** - * An array that contains the names of indices that are used by loaded class - * info array. - * - * The array have the following indices: - * - * - * @var array - */ - private static $CLASS_INDICES = [ - 'class-name', - 'namespace', - 'path', - 'loaded-from-cache' - ]; /** * An array that contains the names of all loaded class. * @@ -172,10 +153,10 @@ private function __construct(string $root = '', array $searchFolders = [], bool $this->onFail = self::ON_FAIL_ACTIONS[0]; } $this->loadedClasses[] = [ - self::$CLASS_INDICES[0] => 'AutoLoader', - self::$CLASS_INDICES[1] => 'webfiori\\framework', - self::$CLASS_INDICES[2] => __DIR__, - self::$CLASS_INDICES[3] => false + ClassInfo::NAME => 'AutoLoader', + ClassInfo::NS => 'webfiori\\framework', + ClassInfo::PATH => __DIR__, + ClassInfo::CACHED => false ]; } /** @@ -320,11 +301,11 @@ public static function getClassPath(string $className, string $namespace = null, foreach ($loadedClasses as $classArr) { if ($namespace !== null) { - if ($classArr[self::$CLASS_INDICES[1]] == $namespace && $classArr[self::$CLASS_INDICES[0]] == $className) { - $retVal[] = $classArr[self::$CLASS_INDICES[2]]; + if ($classArr[ClassInfo::NS] == $namespace && $classArr[ClassInfo::NAME] == $className) { + $retVal[] = $classArr[ClassInfo::PATH]; } - } else if ($classArr[self::$CLASS_INDICES[0]] == $className) { - $retVal[] = $classArr[self::$CLASS_INDICES[2]]; + } else if ($classArr[ClassInfo::NAME] == $className) { + $retVal[] = $classArr[ClassInfo::PATH]; } } @@ -391,11 +372,11 @@ public static function getLoadedClasses(): array { public static function isLoaded(string $class, string $ns = null): bool { foreach (self::getLoadedClasses() as $classArr) { if ($ns !== null) { - if ($class == $classArr[self::$CLASS_INDICES[0]] - && $ns == $classArr[self::$CLASS_INDICES[1]]) { + if ($class == $classArr[ClassInfo::NAME] + && $ns == $classArr[ClassInfo::NS]) { return true; } - } else if ($class == $classArr[self::$CLASS_INDICES[0]]) { + } else if ($class == $classArr[ClassInfo::NAME]) { return true; } } @@ -650,7 +631,6 @@ private function loadClassHelper(string $className, string $classWithNs, string $isFileLoaded = in_array($f, $allPaths); if (!$isFileLoaded && file_exists($f)) { - require_once $f; $ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1); $this->loadedClasses[] = [ self::$CLASS_INDICES[0] => $className, @@ -672,10 +652,10 @@ private function loadFromCache($classNS, $className): bool { require_once $location; $ns = count(explode('\\', $classNS)) == 1 ? '\\' : substr($classNS, 0, strlen($classNS) - strlen($className) - 1); $this->loadedClasses[] = [ - self::$CLASS_INDICES[0] => $className, - self::$CLASS_INDICES[1] => $ns, - self::$CLASS_INDICES[2] => $location, - self::$CLASS_INDICES[3] => true + ClassInfo::NAME => $className, + ClassInfo::NS => $ns, + ClassInfo::PATH => $location, + ClassInfo::CACHED => true ]; $loaded = true; } @@ -740,13 +720,13 @@ private function updateCacheHelper() { if (is_resource($h)) { foreach ($this->loadedClasses as $classArr) { - $path = substr($classArr[self::$CLASS_INDICES[2]], strlen($root)).'=>'; + $path = substr($classArr[ClassInfo::PATH], strlen($root)).'=>'; - if ($classArr[self::$CLASS_INDICES[1]] == '\\') { + if ($classArr[ClassInfo::NS] == '\\') { //A class without a namespace - fwrite($h, $path.$classArr[self::$CLASS_INDICES[0]]."\n"); + fwrite($h, $path.$classArr[ClassInfo::NAME]."\n"); } else { - fwrite($h, $path.$classArr[self::$CLASS_INDICES[1]].'\\'.$classArr[self::$CLASS_INDICES[0]]."\n"); + fwrite($h, $path.$classArr[ClassInfo::NS].'\\'.$classArr[ClassInfo::NAME]."\n"); } } fclose($h); diff --git a/webfiori/framework/autoload/ClassInfo.php b/webfiori/framework/autoload/ClassInfo.php new file mode 100644 index 000000000..394d36711 --- /dev/null +++ b/webfiori/framework/autoload/ClassInfo.php @@ -0,0 +1,33 @@ + Date: Wed, 10 Jul 2024 00:19:18 +0300 Subject: [PATCH 02/13] feat(autoloader): Added a Method to Check Validity of Namespace --- webfiori/framework/AutoLoader.php | 44 +++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/webfiori/framework/AutoLoader.php b/webfiori/framework/AutoLoader.php index 9b14a6aa8..918a3c9cc 100644 --- a/webfiori/framework/AutoLoader.php +++ b/webfiori/framework/AutoLoader.php @@ -643,6 +643,50 @@ private function loadClassHelper(string $className, string $classWithNs, string return $loaded; } + /** + * Checks if provided string represents a valid namespace or not. + * + * @param string $ns A string to be validated. + * + * @return bool If the provided string represents a valid namespace, the + * method will return true. False if it does not represent a valid namespace. + */ + public static function isValidNamespace(string $ns) { + if ($ns == '\\') { + return true; + } + $split = explode('\\', $ns); + + foreach ($split as $subNs) { + $len = strlen($subNs); + + for ($x = 0 ; $x < $len ; $x++) { + $char = $subNs[$x]; + + if ($x == 0 && $char >= '0' && $char <= '9') { + return false; + } + + if (!(($char <= 'Z' && $char >= 'A') || ($char <= 'z' && $char >= 'a') || ($char >= '0' && $char <= '9') || $char == '_')) { + return false; + } + } + } + + return true; + } + private function createNSFromPath(string $filePath, string $className) { + $split = explode(DIRECTORY_SEPARATOR, $filePath); + $nsArr = []; + $currentNs = ''; + foreach ($split as $str) { + if (self::isValidNamespace($str)) { + $currentNs .= '\\'.$str; + } + $nsArr[] = $currentNs; + } + return $nsArr; + } private function loadFromCache($classNS, $className): bool { $loaded = false; From 46981d3a4e813e25b83a87906feecac4c1e342c7 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 10 Jul 2024 00:30:43 +0300 Subject: [PATCH 03/13] refactor(autoload): Moved All Related Components to Separate Folder --- bootstrap.php | 6 +++--- tests/webfiori/framework/test/TestAutoLoader.php | 2 +- webfiori/framework/App.php | 5 +++-- webfiori/framework/{ => autoload}/AutoLoader.php | 6 +++--- .../{exceptions => autoload}/ClassLoaderException.php | 2 +- 5 files changed, 11 insertions(+), 10 deletions(-) rename webfiori/framework/{ => autoload}/AutoLoader.php (99%) rename webfiori/framework/{exceptions => autoload}/ClassLoaderException.php (91%) diff --git a/bootstrap.php b/bootstrap.php index 241f4ffe5..c90956d79 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -4,7 +4,7 @@ use webfiori\framework\App; -use webfiori\framework\AutoLoader; +use webfiori\framework\autoload\AutoLoader; $DS = DIRECTORY_SEPARATOR; @@ -31,7 +31,7 @@ foreach ($WebFioriFrameworkDirs as $dir) { //linux - $file = $DS.$dir.'framework'.$DS.'AutoLoader.php'; + $file = $DS.$dir.'framework'.$DS.'autoload'.$DS.'AutoLoader.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.'AutoLoader.php'; + $file = $dir.$DS.'framework'.$DS.'autoload'.$DS.'AutoLoader.php'; fprintf(STDOUT,"Checking if file '$file' is exist...\n"); if (file_exists($file)) { diff --git a/tests/webfiori/framework/test/TestAutoLoader.php b/tests/webfiori/framework/test/TestAutoLoader.php index 4f2c9ba86..3a135d5b0 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\AutoLoader; +use webfiori\framework\autoload\AutoLoader; /** * Description of TestAutoLoader * diff --git a/webfiori/framework/App.php b/webfiori/framework/App.php index 90f394f7d..17b78d05f 100644 --- a/webfiori/framework/App.php +++ b/webfiori/framework/App.php @@ -17,6 +17,7 @@ use webfiori\error\Handler; use webfiori\file\exceptions\FileException; use webfiori\file\File; +use webfiori\framework\autoload\AutoLoader; use webfiori\framework\config\ConfigurationDriver; use webfiori\framework\config\Controller; use webfiori\framework\exceptions\InitializationException; @@ -526,8 +527,8 @@ private function initAutoLoader() { /** * Initialize autoloader. */ - if (!class_exists('webfiori\framework\AutoLoader',false)) { - require_once WF_CORE_PATH.DIRECTORY_SEPARATOR.'AutoLoader.php'; + if (!class_exists('webfiori\framework\autoload\AutoLoader',false)) { + require_once WF_CORE_PATH.DIRECTORY_SEPARATOR.'autoload'.DIRECTORY_SEPARATOR.'AutoLoader.php'; } self::$AU = AutoLoader::get(); diff --git a/webfiori/framework/AutoLoader.php b/webfiori/framework/autoload/AutoLoader.php similarity index 99% rename from webfiori/framework/AutoLoader.php rename to webfiori/framework/autoload/AutoLoader.php index 918a3c9cc..1eb40b45e 100644 --- a/webfiori/framework/AutoLoader.php +++ b/webfiori/framework/autoload/AutoLoader.php @@ -8,7 +8,7 @@ * https://github.com/WebFiori/.github/blob/main/LICENSE * */ -namespace webfiori\framework; +namespace webfiori\framework\autoload; use Exception; use webfiori\framework\autoload\ClassInfo; @@ -115,8 +115,8 @@ private function __construct(string $root = '', array $searchFolders = [], bool $this->searchFolders = []; $this->cacheArr = []; $this->loadedClasses = []; - require_once 'exceptions'.DIRECTORY_SEPARATOR.'ClassLoaderException.php'; - + require_once 'ClassLoaderException.php'; + require_once 'ClassInfo.php'; if (defined('ROOT_PATH')) { $this->rootDir = ROOT_PATH; } else if (strlen($root) != 0 && is_dir($root)) { diff --git a/webfiori/framework/exceptions/ClassLoaderException.php b/webfiori/framework/autoload/ClassLoaderException.php similarity index 91% rename from webfiori/framework/exceptions/ClassLoaderException.php rename to webfiori/framework/autoload/ClassLoaderException.php index 1676afc22..412f188c5 100644 --- a/webfiori/framework/exceptions/ClassLoaderException.php +++ b/webfiori/framework/autoload/ClassLoaderException.php @@ -8,7 +8,7 @@ * https://github.com/WebFiori/.github/blob/main/LICENSE * */ -namespace webfiori\framework\exceptions; +namespace webfiori\framework\autoload; use Exception; /** From aada84e4c246270997e69ffc47013f5b2d6cbfa6 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 10 Jul 2024 00:35:03 +0300 Subject: [PATCH 04/13] refactor: Use of Correct Indices --- webfiori/framework/autoload/AutoLoader.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/webfiori/framework/autoload/AutoLoader.php b/webfiori/framework/autoload/AutoLoader.php index 1eb40b45e..fbff66b99 100644 --- a/webfiori/framework/autoload/AutoLoader.php +++ b/webfiori/framework/autoload/AutoLoader.php @@ -633,10 +633,10 @@ private function loadClassHelper(string $className, string $classWithNs, string if (!$isFileLoaded && file_exists($f)) { $ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1); $this->loadedClasses[] = [ - self::$CLASS_INDICES[0] => $className, - self::$CLASS_INDICES[1] => $ns, - self::$CLASS_INDICES[2] => $f, - self::$CLASS_INDICES[3] => false + ClassInfo::NAME => $className, + ClassInfo::NS => $ns, + ClassInfo::PATH => $f, + ClassInfo::CACHED => false ]; $loaded = true; } From 57082c06920c869403c08b454c2dc8b3d442b7fd Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 10 Jul 2024 00:53:23 +0300 Subject: [PATCH 05/13] refactor: Added Additional Autoload Classes --- tests/webfiori/framework/test/TestAutoLoader.php | 2 +- webfiori/framework/autoload/AutoLoader.php | 14 +++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/webfiori/framework/test/TestAutoLoader.php b/tests/webfiori/framework/test/TestAutoLoader.php index 3a135d5b0..474e9f285 100644 --- a/tests/webfiori/framework/test/TestAutoLoader.php +++ b/tests/webfiori/framework/test/TestAutoLoader.php @@ -19,7 +19,7 @@ public function test00() { * @test */ public function test01() { - $cArr = explode('\\', 'webfiori\\entity\\AutoLoader'); + $cArr = explode('\\', 'webfiori\\framework\\autoload\\AutoLoader'); $className = $cArr[count($cArr) - 1]; $classNs = implode('\\', array_slice($cArr, 0, count($cArr) - 1)); diff --git a/webfiori/framework/autoload/AutoLoader.php b/webfiori/framework/autoload/AutoLoader.php index fbff66b99..7ac16f84c 100644 --- a/webfiori/framework/autoload/AutoLoader.php +++ b/webfiori/framework/autoload/AutoLoader.php @@ -154,7 +154,19 @@ private function __construct(string $root = '', array $searchFolders = [], bool } $this->loadedClasses[] = [ ClassInfo::NAME => 'AutoLoader', - ClassInfo::NS => 'webfiori\\framework', + ClassInfo::NS => substr(self::class, 0, strlen(self::class) - strlen('AutoLoader') - 1), + ClassInfo::PATH => __DIR__, + ClassInfo::CACHED => false + ]; + $this->loadedClasses[] = [ + ClassInfo::NAME => 'ClassInfo', + ClassInfo::NS => substr(ClassInfo::class, 0, strlen(ClassInfo::class) - strlen('ClassInfo') - 1), + ClassInfo::PATH => __DIR__, + ClassInfo::CACHED => false + ]; + $this->loadedClasses[] = [ + ClassInfo::NAME => 'ClassLoaderException', + ClassInfo::NS => substr(ClassLoaderException::class, 0, strlen(ClassLoaderException::class) - strlen('ClassLoaderException') - 1), ClassInfo::PATH => __DIR__, ClassInfo::CACHED => false ]; From a3d4c6e2e52eae4f6c421f533b7316b8562b1bf8 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 10 Jul 2024 01:19:31 +0300 Subject: [PATCH 06/13] fix(autoload): Check NS with Path --- webfiori/framework/autoload/AutoLoader.php | 43 +++++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/webfiori/framework/autoload/AutoLoader.php b/webfiori/framework/autoload/AutoLoader.php index 7ac16f84c..24adac1cc 100644 --- a/webfiori/framework/autoload/AutoLoader.php +++ b/webfiori/framework/autoload/AutoLoader.php @@ -643,14 +643,19 @@ private function loadClassHelper(string $className, string $classWithNs, string $isFileLoaded = in_array($f, $allPaths); if (!$isFileLoaded && file_exists($f)) { - $ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1); - $this->loadedClasses[] = [ - ClassInfo::NAME => $className, - ClassInfo::NS => $ns, - ClassInfo::PATH => $f, - ClassInfo::CACHED => false - ]; - $loaded = true; + $nsFromPath = $this->createNSFromPath($f, $className); + + if (in_array('\\'.$classWithNs, $nsFromPath)) { + require_once $f; + $ns = count(explode('\\', $classWithNs)) == 1 ? '\\' : substr($classWithNs, 0, strlen($classWithNs) - strlen($className) - 1); + $this->loadedClasses[] = [ + ClassInfo::NAME => $className, + ClassInfo::NS => $ns, + ClassInfo::PATH => $f, + ClassInfo::CACHED => false + ]; + $loaded = true; + } } return $loaded; @@ -693,9 +698,27 @@ private function createNSFromPath(string $filePath, string $className) { $currentNs = ''; foreach ($split as $str) { if (self::isValidNamespace($str)) { - $currentNs .= '\\'.$str; + if (strlen($currentNs) == 0) { + $currentNs = '\\'.$str; + } else { + $currentNs = $currentNs.'\\'.$str; + } + $nsArr[] = $currentNs.'\\'.$className; + } + + } + $currentNs = ''; + for ($x = count($split) - 1 ; $x > -1 ; $x--) { + $str = $split[$x]; + if (self::isValidNamespace($str)) { + if (strlen($currentNs) == 0) { + $currentNs = '\\'.$str; + } else { + $currentNs = '\\'.$str.$currentNs; + } + $nsArr[] = $currentNs.'\\'.$className; } - $nsArr[] = $currentNs; + } return $nsArr; } From 069364a4566dc15f917ae0469fb8548ae5411771 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:06:10 +0300 Subject: [PATCH 07/13] fix: Added Missing Namespace --- webfiori/framework/session/Session.php | 1 - webfiori/framework/session/SessionStatus.php | 2 +- webfiori/framework/session/SessionsManager.php | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/webfiori/framework/session/Session.php b/webfiori/framework/session/Session.php index 2b2b742c1..a3defc788 100644 --- a/webfiori/framework/session/Session.php +++ b/webfiori/framework/session/Session.php @@ -10,7 +10,6 @@ */ namespace webfiori\framework\session; -use SessionStatus; use webfiori\framework\App; use webfiori\framework\exceptions\SessionException; use webfiori\http\HttpCookie; diff --git a/webfiori/framework/session/SessionStatus.php b/webfiori/framework/session/SessionStatus.php index 4f8c55935..9fca10c0c 100644 --- a/webfiori/framework/session/SessionStatus.php +++ b/webfiori/framework/session/SessionStatus.php @@ -8,7 +8,7 @@ * https://github.com/WebFiori/.github/blob/main/LICENSE * */ - +namespace webfiori\framework\session; /** * A class which is used to hold constants which represents different statuses * of a session. diff --git a/webfiori/framework/session/SessionsManager.php b/webfiori/framework/session/SessionsManager.php index c76569c3e..38ea9d62d 100644 --- a/webfiori/framework/session/SessionsManager.php +++ b/webfiori/framework/session/SessionsManager.php @@ -10,7 +10,6 @@ */ namespace webfiori\framework\session; -use SessionStatus; use webfiori\framework\exceptions\SessionException; use webfiori\http\Request; /** From 1cc64751075c6bead4da48ec028e4b40082f904f Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:07:28 +0300 Subject: [PATCH 08/13] Update SessionsManagerTest.php --- tests/webfiori/framework/test/session/SessionsManagerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webfiori/framework/test/session/SessionsManagerTest.php b/tests/webfiori/framework/test/session/SessionsManagerTest.php index 2caf00f5b..cc0b05fe3 100644 --- a/tests/webfiori/framework/test/session/SessionsManagerTest.php +++ b/tests/webfiori/framework/test/session/SessionsManagerTest.php @@ -2,13 +2,13 @@ namespace webfiori\framework\test\session; use PHPUnit\Framework\TestCase; -use SessionStatus; use webfiori\database\ConnectionInfo; use webfiori\database\DatabaseException; use webfiori\framework\App; use webfiori\framework\exceptions\SessionException; use webfiori\framework\session\DatabaseSessionStorage; use webfiori\framework\session\SessionsManager; +use webfiori\framework\session\SessionStatus; /** * Description of SessionsManagerTest From 267baba319729275503fd2ccd3ec92a7f6cce1ff Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:22:57 +0300 Subject: [PATCH 09/13] test: Fix Imports --- tests/webfiori/framework/test/router/RouterUriTest.php | 4 +++- tests/webfiori/framework/test/session/SessionTest.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/webfiori/framework/test/router/RouterUriTest.php b/tests/webfiori/framework/test/router/RouterUriTest.php index 9aba8465e..dd5443d23 100644 --- a/tests/webfiori/framework/test/router/RouterUriTest.php +++ b/tests/webfiori/framework/test/router/RouterUriTest.php @@ -2,6 +2,8 @@ namespace webfiori\framework\test\router; use PHPUnit\Framework\TestCase; +use TestMiddleware; +use webfiori\framework\middleware\MiddlewareManager; use webfiori\framework\router\Router; use webfiori\framework\router\RouterUri; /** @@ -15,7 +17,7 @@ class RouterUriTest extends TestCase { */ public function testAddToMiddleware00() { $uri = new RouterUri('https://www3.programmingacademia.com:80/test', ''); - \webfiori\framework\middleware\MiddlewareManager::register(new \TestMiddleware()); + MiddlewareManager::register(new TestMiddleware()); $uri->addMiddleware('global'); $this->assertEquals(1, $uri->getMiddleware()->size()); $uri->addMiddleware('Super Cool Middleware'); diff --git a/tests/webfiori/framework/test/session/SessionTest.php b/tests/webfiori/framework/test/session/SessionTest.php index e11210ca9..825e72233 100644 --- a/tests/webfiori/framework/test/session/SessionTest.php +++ b/tests/webfiori/framework/test/session/SessionTest.php @@ -2,10 +2,10 @@ namespace webfiori\framework\test\session; use PHPUnit\Framework\TestCase; -use SessionStatus; use webfiori\file\File; use webfiori\framework\exceptions\SessionException; use webfiori\framework\session\Session; +use webfiori\framework\session\SessionStatus; /** * Description of SessionTest * From eb4d5b93f6ea4dc12e5809a6fde63c9f2d4fa928 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:23:23 +0300 Subject: [PATCH 10/13] fix(autoload): Add File Name an NS --- webfiori/framework/autoload/AutoLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webfiori/framework/autoload/AutoLoader.php b/webfiori/framework/autoload/AutoLoader.php index 24adac1cc..2254bc87f 100644 --- a/webfiori/framework/autoload/AutoLoader.php +++ b/webfiori/framework/autoload/AutoLoader.php @@ -694,7 +694,7 @@ public static function isValidNamespace(string $ns) { } private function createNSFromPath(string $filePath, string $className) { $split = explode(DIRECTORY_SEPARATOR, $filePath); - $nsArr = []; + $nsArr = ['\\'.$className]; $currentNs = ''; foreach ($split as $str) { if (self::isValidNamespace($str)) { From 58d58e2fdfcdc29b797b1e5e81638831290255f1 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:32:04 +0300 Subject: [PATCH 11/13] test(i18n): Moved Test Language Files to Correct NS --- {tests/webfiori/framework/test => app}/langs/LangFR.php | 0 {tests/webfiori/framework/test => app}/langs/LangGB.php | 0 {tests/webfiori/framework/test => app}/langs/LangJP.php | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename {tests/webfiori/framework/test => app}/langs/LangFR.php (100%) rename {tests/webfiori/framework/test => app}/langs/LangGB.php (100%) rename {tests/webfiori/framework/test => app}/langs/LangJP.php (100%) diff --git a/tests/webfiori/framework/test/langs/LangFR.php b/app/langs/LangFR.php similarity index 100% rename from tests/webfiori/framework/test/langs/LangFR.php rename to app/langs/LangFR.php diff --git a/tests/webfiori/framework/test/langs/LangGB.php b/app/langs/LangGB.php similarity index 100% rename from tests/webfiori/framework/test/langs/LangGB.php rename to app/langs/LangGB.php diff --git a/tests/webfiori/framework/test/langs/LangJP.php b/app/langs/LangJP.php similarity index 100% rename from tests/webfiori/framework/test/langs/LangJP.php rename to app/langs/LangJP.php From 87085d285d059c6ba627aa8f4f10878934734fe6 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:44:10 +0300 Subject: [PATCH 12/13] 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 c90956d79..731ab6cf2 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 474e9f285..f10e18432 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 17b78d05f..a419efb98 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 2254bc87f..ab51106a9 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(); } From a817e8feec235cec29e6c20e00732a25d1c80534 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Thu, 11 Jul 2024 00:50:32 +0300 Subject: [PATCH 13/13] chore: Updated Framework Version --- webfiori/framework/App.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webfiori/framework/App.php b/webfiori/framework/App.php index a419efb98..d405928a8 100644 --- a/webfiori/framework/App.php +++ b/webfiori/framework/App.php @@ -544,7 +544,7 @@ private function initFrameworkVersionInfo() { * * @since 2.1 */ - define('WF_VERSION', '3.0.0-Beta.9'); + define('WF_VERSION', '3.0.0-Beta.10'); /** * A constant that tells the type of framework version. * @@ -560,7 +560,7 @@ private function initFrameworkVersionInfo() { * * @since 2.1 */ - define('WF_RELEASE_DATE', '2024-07-09'); + define('WF_RELEASE_DATE', '2024-07-11'); } /**