CORE UPDATE: Minor update concerning the autoloading class in the core, now it is also possible to give a loading order through the configuration
Minor update concerning the form factory classes in the core, now some javascript events are implemented as well, another update concerning functinoallity will follow soon.
Update on the example configuration file, implementing the autoloading order of interfaces, moduels and traits.
Update for multidatabase support, see the documentation on http://www.nibiru-framework.com
This commit is contained in:
@@ -12,25 +12,114 @@ require_once __DIR__ . '/../i/db.php';
|
||||
*/
|
||||
class Autoloader
|
||||
{
|
||||
const MY_FILE_NAME = "autoloader.php";
|
||||
const DB_MODEL_FOLDER = "dbmodel";
|
||||
const MODULE_FOLDER = "module";
|
||||
|
||||
const MY_FILE_NAME = "autoloader.php";
|
||||
const DB_MODEL_FOLDER = "dbmodel";
|
||||
const MODULE_FOLDER = "module";
|
||||
const INTERFACE_FOLDER = "interfaces";
|
||||
const TRAIT_FOLDER = "trait";
|
||||
const SETTINGS_SECTION = "AUTOLOADER";
|
||||
const SETTINGS_CLASS_POS = "class.pos";
|
||||
const SETTINGS_TRAIT_POS = "trait.pos";
|
||||
const SETTINGS_IFACE_POS = "iface.pos";
|
||||
|
||||
private static $_filesInFoler = array();
|
||||
private static $_instance;
|
||||
|
||||
private static $_debug = false;
|
||||
private static $_modules = array();
|
||||
|
||||
protected function __construct()
|
||||
{
|
||||
self::_setFilesInFoler();
|
||||
}
|
||||
|
||||
|
||||
public static function getInstance()
|
||||
{
|
||||
$className = get_called_class();
|
||||
if(self::$_instance==null) self::$_instance = new $className();
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
protected static function isDebug(): bool
|
||||
{
|
||||
return self::$_debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc activate debug into the debugbar, before calling the autoloader
|
||||
* Autoloader::getInstance()->debug( true );
|
||||
* @param bool $debug
|
||||
*/
|
||||
public static function debug( bool $debug ): void
|
||||
{
|
||||
self::$_debug = $debug;
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc will sort the array by a given sort order from the configuration file
|
||||
* just provided in the AUTOLOADER section.
|
||||
* @param array $modules
|
||||
* @return 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];
|
||||
}
|
||||
if($section == self::SETTINGS_TRAIT_POS)
|
||||
{
|
||||
$moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_TRAIT_POS];
|
||||
}
|
||||
if($section == self::SETTINGS_IFACE_POS)
|
||||
{
|
||||
$moduleSortOrder = Config::getInstance()->getConfig()[self::SETTINGS_SECTION][self::SETTINGS_IFACE_POS];
|
||||
}
|
||||
if(sizeof($moduleSortOrder)>0)
|
||||
{
|
||||
foreach ($modules as $module)
|
||||
{
|
||||
foreach ($moduleSortOrder as $sortOrder)
|
||||
{
|
||||
if($module['nfilename'] == $sortOrder)
|
||||
{
|
||||
$first[] = $module;
|
||||
}
|
||||
else
|
||||
{
|
||||
$skip = true;
|
||||
}
|
||||
}
|
||||
if($skip)
|
||||
{
|
||||
$normal[] = $module;
|
||||
$skip = false;
|
||||
}
|
||||
}
|
||||
if(array_key_exists(0, $first))
|
||||
{
|
||||
$sorted = array();
|
||||
foreach($moduleSortOrder as $item)
|
||||
{
|
||||
foreach ($first as $fmodule)
|
||||
{
|
||||
if($item==$fmodule['nfilename'])
|
||||
{
|
||||
$sorted[] = $fmodule;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$modules = array_merge($sorted, $normal);
|
||||
}
|
||||
return $modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @desc includes all database model files in preparation for the
|
||||
* database factory model
|
||||
@@ -39,10 +128,19 @@ class Autoloader
|
||||
{
|
||||
foreach (self::getFilesInFoler() as $file)
|
||||
{
|
||||
$required[] = $file;
|
||||
require_once $file;
|
||||
}
|
||||
if(self::isDebug())
|
||||
{
|
||||
View::getInstance()->assign(
|
||||
array(
|
||||
'ndbraw_output' => '<pre>' . print_r($required, true) . '</pre>'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
@@ -50,47 +148,106 @@ class Autoloader
|
||||
{
|
||||
return self::$_filesInFoler;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
protected static function getModules(): array
|
||||
{
|
||||
return self::$_modules;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $filesInFoler
|
||||
*/
|
||||
private static function _setFilesInFoler( )
|
||||
{
|
||||
$modelFolder = Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER];
|
||||
if(is_array($modelFolder))
|
||||
/**
|
||||
* @desc arrays for sorting the module order, so they will load
|
||||
* alphabetically
|
||||
*/
|
||||
$modules = array();
|
||||
|
||||
if( is_array( Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] ) )
|
||||
{
|
||||
foreach ($modelFolder as $folder)
|
||||
foreach ( Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::DB_MODEL_FOLDER] as $modelfolder )
|
||||
{
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . $folder ));
|
||||
foreach ($iterator as $item)
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator( __DIR__ . $modelfolder ));
|
||||
foreach ( $iterator as $item )
|
||||
{
|
||||
if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..")
|
||||
{
|
||||
self::$_filesInFoler[] = $item->getPathName();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . $modelFolder ));
|
||||
foreach ($iterator as $item)
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__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()!="..")
|
||||
{
|
||||
self::$_filesInFoler[] = $item->getPathName();
|
||||
}
|
||||
}
|
||||
}
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::MODULE_FOLDER] ));
|
||||
foreach ($iterator as $item)
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @desc run check on modules that should provide an interface as well as a trait
|
||||
*/
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::INTERFACE_FOLDER] ));
|
||||
foreach ( $iterator as $item )
|
||||
{
|
||||
if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..")
|
||||
{
|
||||
self::$_filesInFoler[] = $item->getPathName();
|
||||
$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)
|
||||
{
|
||||
self::$_filesInFoler[] = $interface['filepathname'];
|
||||
}
|
||||
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::TRAIT_FOLDER] ));
|
||||
foreach ( $iterator as $item )
|
||||
{
|
||||
if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..")
|
||||
{
|
||||
$traits[] = array(
|
||||
'nfilename' => str_replace('.php', '', $item->getFileName()),
|
||||
'filepathname' => $item->getPath() . '/' . $item->getFileName()
|
||||
);
|
||||
}
|
||||
}
|
||||
asort($traits);
|
||||
$Straits = self::sortOrderModules($traits, self::SETTINGS_TRAIT_POS);
|
||||
foreach($Straits as $trait)
|
||||
{
|
||||
self::$_filesInFoler[] = $trait['filepathname'];
|
||||
}
|
||||
|
||||
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator(__DIR__ . Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS][self::MODULE_FOLDER] ));
|
||||
foreach ( $iterator as $item )
|
||||
{
|
||||
if($item->getFileName()!= self::MY_FILE_NAME && $item->getFileName()!="." && $item->getFileName()!="..")
|
||||
{
|
||||
$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 $module)
|
||||
{
|
||||
self::$_filesInFoler[] = $module['filepathname'];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user