Implemented renewed modloading, so that modules do not get loaded multiple times

This commit is contained in:
Abel Hoogeveen 2015-04-22 12:11:28 +02:00
parent 8c1a7afda5
commit e877d103c8

View File

@ -10,9 +10,16 @@ if (!defined('FUZESYSPATH')) {
class Core {
public $mods;
private $loaded = false;
public $register;
/**
* An array which modules are loaded, and should not be loaded again
* @access private
* @var Array of module names
*/
private $loaded_modules = array();
private $loaded = false;
## START/STOP
public function init() {
// Load basics
@ -64,73 +71,31 @@ class Core {
$this->mods->events->fireEvent('coreShutdownEvent');
}
## MODLOADING
public function loadMod($name, $version = null) {
// Get class information
$data = $this->loadModule($name, $version);
// If it is an abstract class, create and StdClass
if (empty($data)) {
return $this->mods->{strtolower($name)} = new StdClass();
}
// Otherwise load the class
$class_name = $data['className'];
// Create the class object if not created yet
if (!isset($this->mods->{strtolower($data['moduleLinkName'])})) {
$CLASS = new $class_name($this);
if (method_exists($CLASS, 'setModulePath')) {
$CLASS->setModulePath($data['modulePath']);
}
if (method_exists($CLASS, 'setModuleLinkName')) {
$CLASS->setModuleLinkName($data['moduleLinkName']);
}
if (method_exists($CLASS, 'setModuleName')) {
$CLASS->setModuleName($data['moduleName']);
}
$CLASS->onLoad();
return $this->mods->{strtolower($data['moduleLinkName'])} = &$CLASS;
} else {
$c = &$this->mods->{strtolower($data['moduleLinkName'])};
return $c;
}
}
private function loadModule($name, $version = null) {
// The basic module path
public function loadMod($name) {
// Where the modules are
$path = FUZEPATH . "Modules/";
// Chech if the requested module is set
// Check if the requested module is registered
if (isset($this->register[$name])) {
// Check if the config file is loaded
if (!empty($this->register[$name])) {
// Load the config file
// Load the moduleInfo
$cfg = (object) $this->register[$name];
// Check if the module is enabled, otherwise abort
// Check if the module is enabled
if (isset($cfg->enabled)) {
if (!$cfg->enabled) {
// Module is disabled
throw new Exception("Module '".$name."' is not enabled!", 1);
// DO SOMETHING
return false;
}
}
// Check if a specific version is requested
if (isset($version)) {
if (isset($cfg->versions)) {
if (isset($cfg->versions[$version])) {
$ncfg = (object) $cfg->versions[$version];
foreach ($ncfg as $key => $value) {
$cfg->$key = $value;
}
}
}
}
// Or load the main version
// Check if the module is already loaded. If so, only return a reference, if not, load the module
if (in_array($name, $this->loaded_modules)) {
// return the link
$c = &$this->mods->{strtolower($cfg->name)};
return $c;
} else {
// Load the module
$file = $cfg->directory . $cfg->module_file;
// Load the dependencies before the module loads
@ -154,29 +119,38 @@ class Core {
throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1);
return false;
}
} else {
// Throw Exception if the module has an invalid config file
throw new Exception("Requested mod '".$name."' could not be loaded. Invalid config", 1);
return false;
}
} else {
// Throw Exception if the module is not defined
throw new Exception("Requested mod '".$name."' was not found", 1);
return false;
}
// If it is an abstract module, return an StdClass for the memory address
// If it is an abstract module, load an StdClass for the module address
if (isset($cfg->abstract)) {
if ($cfg->abstract) {
$c = new stdClass();
return array();
$CLASS = new stdClass();
return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
}
}
return array('className' => $class_name,
'modulePath' => $cfg->directory,
'moduleLinkName' => $cfg->module_name,
'moduleName' => $name);
// Load the module class
$class_name = $cfg->module_class;
$CLASS = new $class_name($this);
// Apply default methods
if (method_exists($CLASS, 'setModulePath')) {
$CLASS->setModulePath($cfg->directory);
}
if (method_exists($CLASS, 'setModuleLinkName')) {
$CLASS->setModuleLinkName($cfg->name);
}
if (method_exists($CLASS, 'setModuleName')) {
$CLASS->setModuleName($name);
}
$CLASS->onLoad();
// Return a reference
return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
}
}
}
}
public function buildRegister() {