Implemented renewed modloading, so that modules do not get loaded multiple times
This commit is contained in:
parent
8c1a7afda5
commit
e877d103c8
@ -9,9 +9,16 @@ if (!defined('FUZESYSPATH')) {
|
||||
// Framework
|
||||
class Core {
|
||||
|
||||
public $mods;
|
||||
private $loaded = false;
|
||||
public $mods;
|
||||
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() {
|
||||
@ -64,119 +71,86 @@ 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;
|
||||
}
|
||||
// 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
|
||||
$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
|
||||
$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);
|
||||
// Load the module class
|
||||
$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;
|
||||
$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;
|
||||
}
|
||||
} 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() {
|
||||
|
Loading…
Reference in New Issue
Block a user