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

@ -9,9 +9,16 @@ if (!defined('FUZESYSPATH')) {
// Framework // Framework
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() {
@ -64,119 +71,86 @@ 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
} $file = $cfg->directory . $cfg->module_file;
// Load the dependencies before the module loads
$deps = (isset($cfg->dependencies) ? $cfg->dependencies : array());
for ($i=0; $i < count($deps); $i++) {
$this->loadMod($deps[$i]);
}
// Check if the file exists
if (file_exists($file)) {
// And load it
require_once($file);
$class_name = $cfg->module_class;
$msg = "Loading Module '".ucfirst((isset($cfg->name) ? $cfg->name : $cfg->module_name)) . "'";
$msg .= (isset($cfg->version) ? " version:".$cfg->version : "");
$msg .= (isset($cfg->author) ? " made by ".$cfg->author : "");
$msg .= (isset($cfg->website) ? " from ".$cfg->website: "");
$this->mods->logger->log($msg);
} else {
// Throw Exception if the file does not exist
throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1);
return false;
}
// If it is an abstract module, load an StdClass for the module address
if (isset($cfg->abstract)) {
if ($cfg->abstract) {
$CLASS = new stdClass();
return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
} }
} }
}
// Or load the main version // Load the module class
$file = $cfg->directory . $cfg->module_file;
// Load the dependencies before the module loads
$deps = (isset($cfg->dependencies) ? $cfg->dependencies : array());
for ($i=0; $i < count($deps); $i++) {
$this->loadMod($deps[$i]);
}
// Check if the file exists
if (file_exists($file)) {
// And load it
require_once($file);
$class_name = $cfg->module_class; $class_name = $cfg->module_class;
$msg = "Loading Module '".ucfirst((isset($cfg->name) ? $cfg->name : $cfg->module_name)) . "'"; $CLASS = new $class_name($this);
$msg .= (isset($cfg->version) ? " version:".$cfg->version : "");
$msg .= (isset($cfg->author) ? " made by ".$cfg->author : ""); // Apply default methods
$msg .= (isset($cfg->website) ? " from ".$cfg->website: ""); if (method_exists($CLASS, 'setModulePath')) {
$this->mods->logger->log($msg); $CLASS->setModulePath($cfg->directory);
} else { }
// Throw Exception if the file does not exist if (method_exists($CLASS, 'setModuleLinkName')) {
throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1); $CLASS->setModuleLinkName($cfg->name);
return false; }
if (method_exists($CLASS, 'setModuleName')) {
$CLASS->setModuleName($name);
}
$CLASS->onLoad();
// Return a reference
return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
} }
} 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 (isset($cfg->abstract)) {
if ($cfg->abstract) {
$c = new stdClass();
return array();
} }
} }
return array('className' => $class_name,
'modulePath' => $cfg->directory,
'moduleLinkName' => $cfg->module_name,
'moduleName' => $name);
} }
public function buildRegister() { public function buildRegister() {