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 { class Core {
public $mods; public $mods;
private $loaded = false;
public $register; 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 ## START/STOP
public function init() { public function init() {
// Load basics // Load basics
@ -64,73 +71,31 @@ class Core {
$this->mods->events->fireEvent('coreShutdownEvent'); $this->mods->events->fireEvent('coreShutdownEvent');
} }
## MODLOADING public function loadMod($name) {
public function loadMod($name, $version = null) { // Where the modules are
// 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
$path = FUZEPATH . "Modules/"; $path = FUZEPATH . "Modules/";
// Chech if the requested module is set // Check if the requested module is registered
if (isset($this->register[$name])) { if (isset($this->register[$name])) {
// Check if the config file is loaded
if (!empty($this->register[$name])) { if (!empty($this->register[$name])) {
// Load the config file // Load the moduleInfo
$cfg = (object) $this->register[$name]; $cfg = (object) $this->register[$name];
// Check if the module is enabled, otherwise abort // Check if the module is enabled
if (isset($cfg->enabled)) { if (isset($cfg->enabled)) {
if (!$cfg->enabled) { if (!$cfg->enabled) {
// Module is disabled // DO SOMETHING
throw new Exception("Module '".$name."' is not enabled!", 1);
return false; return false;
} }
} }
// Check if a specific version is requested // Check if the module is already loaded. If so, only return a reference, if not, load the module
if (isset($version)) { if (in_array($name, $this->loaded_modules)) {
if (isset($cfg->versions)) { // return the link
if (isset($cfg->versions[$version])) { $c = &$this->mods->{strtolower($cfg->name)};
$ncfg = (object) $cfg->versions[$version]; return $c;
foreach ($ncfg as $key => $value) { } else {
$cfg->$key = $value; // Load the module
}
}
}
}
// Or load the main version
$file = $cfg->directory . $cfg->module_file; $file = $cfg->directory . $cfg->module_file;
// Load the dependencies before the module loads // 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); throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1);
return false; 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 (isset($cfg->abstract)) {
if ($cfg->abstract) { if ($cfg->abstract) {
$c = new stdClass(); $CLASS = new stdClass();
return array(); return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
} }
} }
return array('className' => $class_name, // Load the module class
'modulePath' => $cfg->directory, $class_name = $cfg->module_class;
'moduleLinkName' => $cfg->module_name, $CLASS = new $class_name($this);
'moduleName' => $name);
// 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() { public function buildRegister() {