From 918a2cb4ec0d71c645caeda30b5fccb8ffa0f490 Mon Sep 17 00:00:00 2001 From: Stephan Kasdorf Date: Sun, 10 Nov 2019 23:00:26 +0100 Subject: [PATCH] REFACTORING: updated the autoloader, currently a duplicate section has to be refactored. Added a Registry for the settings section of every module. --- .../controller/controllerController.php | 1 + application/module/users/settings/users.ini | 3 + .../settings/config/settings.development.ini | 7 +- .../view/templates/shared/debugbar.tpl | 2 + core/c/autoloader.php | 99 +++++++++++---- core/c/registry.php | 119 ++++++++++++++++++ core/framework.php | 1 + 7 files changed, 206 insertions(+), 26 deletions(-) create mode 100644 application/module/users/settings/users.ini create mode 100644 core/c/registry.php diff --git a/application/controller/controllerController.php b/application/controller/controllerController.php index 5fe43e9..fd20864 100644 --- a/application/controller/controllerController.php +++ b/application/controller/controllerController.php @@ -10,6 +10,7 @@ class controllerController extends Controller View::assign([ 'name' => 'rapid prototyping framework', 'title' => 'Nibiru Controller Example', + 'ndbraw_output' => print_r(Registry::getInstance()->loadModuleConfigByName('users'), true), 'css' => Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS]["smarty.css"], 'js' => Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS]["smarty.js"] ]); diff --git a/application/module/users/settings/users.ini b/application/module/users/settings/users.ini new file mode 100644 index 0000000..2a59187 --- /dev/null +++ b/application/module/users/settings/users.ini @@ -0,0 +1,3 @@ +[USERS] +name = "users" +path = "/application/module/users" \ No newline at end of file diff --git a/application/settings/config/settings.development.ini b/application/settings/config/settings.development.ini index 272fda0..185f5b7 100644 --- a/application/settings/config/settings.development.ini +++ b/application/settings/config/settings.development.ini @@ -7,9 +7,12 @@ debug_template = "/../../application/view/templates/shared/debug.tpl" debugbar = true [AUTOLOADER] -class.pos[] = "users" -trait.pos[] = "users" +iface.pos[] = "register" iface.pos[] = "users" +trait.pos[] = "register" +trait.pos[] = "users" +class.pos[] = "register" +class.pos[] = "users" [EMAIL] register.smtp = 1 diff --git a/application/view/templates/shared/debugbar.tpl b/application/view/templates/shared/debugbar.tpl index 33a4f86..2048ef3 100644 --- a/application/view/templates/shared/debugbar.tpl +++ b/application/view/templates/shared/debugbar.tpl @@ -121,7 +121,9 @@

{if isset($ndbraw_output)} +

                     {$ndbraw_output}
+                
{else} {$message} {/if} diff --git a/core/c/autoloader.php b/core/c/autoloader.php index f5edf84..39eee4a 100644 --- a/core/c/autoloader.php +++ b/core/c/autoloader.php @@ -13,12 +13,11 @@ require_once __DIR__ . '/../i/db.php'; class Autoloader { const MY_FILE_NAME = "autoloader.php"; + const PHP_FILE_EXTENSION = ".php"; const DB_MODEL_FOLDER = "dbmodel"; - const MODULES = [ - 'module', - 'interfaces', - 'traits' - ]; + const MODULE_FOLDER = "module"; + const INTERFACE_FOLDER = "interfaces"; + const TRAIT_FOLDER = "traits"; const SETTINGS_SECTION = "AUTOLOADER"; const SETTINGS_CLASS_POS = "class.pos"; const SETTINGS_TRAIT_POS = "trait.pos"; @@ -32,7 +31,7 @@ class Autoloader protected function __construct() { - self::_setFilesInFoler(); + self::_setFilesInFolder(); } public static function getInstance() @@ -67,11 +66,10 @@ class Autoloader * @param mixed $section * @return array */ - private static function sortOrderModules( array $modules, $section ): array + private static function sortOrderModules( array $modules, $section ): ?array { (bool) $skip = false; (array) $normal = array(); - if($section == self::SETTINGS_CLASS_POS) { $moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_CLASS_POS]; @@ -163,14 +161,14 @@ class Autoloader /** * @param string $folderPath + * @param string $moduleName * @return \RecursiveIteratorIterator */ - private static function folderContent( string $folderPath ): \RecursiveIteratorIterator + private static function folderContent( string $folderPath, string $moduleName = '' ): \RecursiveIteratorIterator { - $folderSettings = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_CLASS_POS]; - foreach($folderSettings as $moduleFolderName) + if(strstr($folderPath, self::REGEX_PATH_NAME) && $moduleName!="") { - $folderPath = str_replace(self::REGEX_PATH_NAME, $moduleFolderName, $folderPath); + $folderPath = str_replace(self::REGEX_PATH_NAME, $moduleName, $folderPath); } return new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator( $folderPath )); } @@ -178,13 +176,14 @@ class Autoloader /** * @param array $filesInFoler */ - private static function _setFilesInFoler( ) + private static function _setFilesInFolder( ) { /** * @desc arrays for sorting the module order, so they will load * alphabetically */ $modules = array(); + self::$_filesInFoler = array(); if( is_array( Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] ) ) { @@ -193,7 +192,7 @@ class Autoloader $iterator = self::folderContent( __DIR__ . $modelfolder ); foreach ( $iterator as $item ) { - if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!=".." && strstr($item->getFileName(), self::PHP_FILE_EXTENSION)) { self::$_filesInFoler[] = $item->getPathName(); } @@ -205,33 +204,85 @@ class Autoloader $iterator = self::folderContent(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] ); foreach ( $iterator as $item ) { - if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!=".." && strstr($item->getFileName(), self::PHP_FILE_EXTENSION)) { self::$_filesInFoler[] = $item->getPathName(); } } } /** - * @desc run check on modules that should provide an interface as well as a trait + * TODO: refactor this section */ - foreach(self::MODULES as $module) + $moduleInterfaceNames = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_IFACE_POS]; + foreach($moduleInterfaceNames as $interfaceName) { - $iterator = self::folderContent(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][$module] ); + $iterator = self::folderContent(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::INTERFACE_FOLDER], $interfaceName); + foreach ($iterator as $item) + { + if($item->getFileName() != self::MY_FILE_NAME && $item->getFileName() != "." && $item->getFileName() != ".." && strstr($item->getFileName(), self::PHP_FILE_EXTENSION)) + { + $interfaces[] = array( + 'nfilename' => str_replace('.php', '', $item->getFileName()), + 'filepathname' => $item->getPath() . '/' . $item->getFileName() + ); + } + } + asort($interfaces); + $Sinterfaces = self::sortOrderModules($interfaces, self::SETTINGS_IFACE_POS); + foreach ($Sinterfaces as $interface) + { + if(!in_array($interface['filepathname'], self::$_filesInFoler)) + { + self::$_filesInFoler[] = $interface['filepathname']; + } + } + } + $modulesTraitsNames = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_TRAIT_POS]; + foreach($modulesTraitsNames as $traitsName) + { + $iterator = self::folderContent(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::TRAIT_FOLDER], $traitsName ); foreach ( $iterator as $item ) { - if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..") + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!=".." && strstr($item->getFileName(), self::PHP_FILE_EXTENSION)) { - $moduleFolder[] = array( + $traits[] = array( 'nfilename' => str_replace('.php', '', $item->getFileName()), 'filepathname' => $item->getPath() . '/' . $item->getFileName() ); } } - asort($moduleFolder); - $itms = self::sortOrderModules($moduleFolder, self::SETTINGS_IFACE_POS); - foreach ($itms as $itm) + asort($traits); + $Straits = self::sortOrderModules($traits, self::SETTINGS_TRAIT_POS); + foreach($Straits as $trait) { - self::$_filesInFoler[] = $itm['filepathname']; + if(!in_array($trait['filepathname'], self::$_filesInFoler)) + { + self::$_filesInFoler[] = $trait['filepathname']; + } + } + } + $modulesClassNames = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_CLASS_POS]; + foreach($modulesClassNames as $className) + { + $iterator = self::folderContent(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::MODULE_FOLDER], $className ); + foreach ( $iterator as $item ) + { + if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!=".." && strstr($item->getFileName(), self::PHP_FILE_EXTENSION)) + { + $modules[] = array( + 'nfilename' => str_replace('.php', '', $item->getFileName()), + 'filepathname' => $item->getPath() . '/' . $item->getFileName() + ); + } + } + asort($modules); + $Smodules = self::sortOrderModules($modules, self::SETTINGS_CLASS_POS); + foreach($Smodules as $smodule) + { + if(!in_array($smodule['filepathname'], self::$_filesInFoler)) + { + self::$_filesInFoler[] = $smodule['filepathname']; + } } } } diff --git a/core/c/registry.php b/core/c/registry.php new file mode 100644 index 0000000..2af7581 --- /dev/null +++ b/core/c/registry.php @@ -0,0 +1,119 @@ +loadModuleRegistry(); + return self::$_instance; + } + + /** + * @return string + */ + private function getModulesPath(): string + { + return $this->_modules_path; + } + + /** + * set the path to the modules + */ + private function _setModulesPath( ): void + { + $this->_modules_path = __DIR__ . str_replace(Autoloader::REGEX_PATH_NAME, '', Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::CONFIG_MODULE_KEY]); + } + + /** + * @return array + */ + private function getModulesConfig(): array + { + return $this->_modules_config; + } + + /** + * set the modules configuration by module name + */ + private function _setModulesConfig( ): void + { + $modules_setting_path = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->getModulesPath())); + foreach($modules_setting_path as $settings) + { + if(strstr($settings->getPathName(), self::CONFIG_SETTINGS_KEY) && $settings->getFileName()!='.' && $settings->getFileName()!='..') + { + $module = new \stdClass(); + $module_settings = parse_ini_file($settings->getPathName(), true); + foreach ( $module_settings[strtoupper($this->getModuleName())] as $key=>$value) + { + $module->$key = $value; + } + $this->_modules_config[$this->getModuleName()] = $module; + } + } + } + + /** + * @return string + */ + private function getModuleName(): string + { + return $this->_module_name; + } + + /** + * @param string $module_name + */ + private function _setModuleName(string $module_name): void + { + $this->_module_name = $module_name; + $this->_setModulesPath(); + $this->_setModulesConfig(); + } + + /** + * @return void + */ + private function loadModuleRegistry(): void + { + foreach(Config::getInstance()->getConfig()[Autoloader::SETTINGS_SECTION][Autoloader::SETTINGS_CLASS_POS] as $module) + { + $this->_setModuleName($module); + } + } + + /** + * @param string $module_name + * @return object + */ + public function loadModuleConfigByName(string $module_name): object + { + return $this->getModulesConfig()[$module_name]; + } +} \ No newline at end of file diff --git a/core/framework.php b/core/framework.php index 587eccd..8e56fed 100644 --- a/core/framework.php +++ b/core/framework.php @@ -18,6 +18,7 @@ require_once __DIR__ . '/t/messages.php'; require_once __DIR__ . '/t/pageination.php'; require_once __DIR__ . '/c/settings.php'; require_once __DIR__ . '/c/config.php'; +require_once __DIR__ . '/c/registry.php'; require_once __DIR__ . '/c/router.php'; require_once __DIR__ . '/i/engine.php'; require_once __DIR__ . '/c/engine.php';