From 8c1a7afda544201128de13531edcc0b78fe46f26 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Wed, 22 Apr 2015 11:31:29 +0200 Subject: [PATCH 1/3] Fix for #28 and #23. This patch removes the mod and event registers so that modules can now be loaded dynamicly instead of using a global configuration file. --- Application/Config/config.eventregister.php | 5 -- Application/Config/config.main.php | 1 - Application/Config/config.modregister.php | 0 Core/Events/event.eventRegisterBuildEvent.php | 17 ------ Core/System/class.core.php | 59 +++++++++---------- Core/System/class.events.php | 26 ++++---- 6 files changed, 44 insertions(+), 64 deletions(-) delete mode 100644 Application/Config/config.eventregister.php delete mode 100644 Application/Config/config.modregister.php delete mode 100644 Core/Events/event.eventRegisterBuildEvent.php diff --git a/Application/Config/config.eventregister.php b/Application/Config/config.eventregister.php deleted file mode 100644 index 7d849f6..0000000 --- a/Application/Config/config.eventregister.php +++ /dev/null @@ -1,5 +0,0 @@ - - array ( - ), -) ; \ No newline at end of file diff --git a/Application/Config/config.main.php b/Application/Config/config.main.php index 118ca4b..76e258c 100644 --- a/Application/Config/config.main.php +++ b/Application/Config/config.main.php @@ -2,7 +2,6 @@ 'SITE_URL' => '', 'SITE_DOMAIN' => '', 'SERVER_NAME' => '', - 'registers_update_interval' => 3600, 'administrator_mail' => '', 'default_controller' => 'standard', 'default_function' => 'index', diff --git a/Application/Config/config.modregister.php b/Application/Config/config.modregister.php deleted file mode 100644 index e69de29..0000000 diff --git a/Core/Events/event.eventRegisterBuildEvent.php b/Core/Events/event.eventRegisterBuildEvent.php deleted file mode 100644 index 4c3d2d9..0000000 --- a/Core/Events/event.eventRegisterBuildEvent.php +++ /dev/null @@ -1,17 +0,0 @@ -register[$eventName])) { - $this->register[$eventName] = array(); - } - $this->register[$eventName][] = $modName; - } -} - -?> \ No newline at end of file diff --git a/Core/System/class.core.php b/Core/System/class.core.php index b533b5a..7774cd4 100644 --- a/Core/System/class.core.php +++ b/Core/System/class.core.php @@ -11,7 +11,7 @@ class Core { public $mods; private $loaded = false; - private $register; + public $register; ## START/STOP public function init() { @@ -23,12 +23,12 @@ class Core { $this->mods = new stdClass(); $this->loadStartupFiles(); - $this->mods->events->fireEvent('coreStartEvent'); - // Mod register exists, check if expired - if ( ( date('U') - $this->mods->config->main->registers_last_update) > $this->mods->config->main->registers_update_interval) { - $this->mods->logger->log("Registers have expired. Updating...", 'Core'); - $this->buildModRegister(); - $this->mods->events->buildEventRegister(); + $this->buildRegister(); + $this->mods->events->buildEventRegister(); + + $event = $this->mods->events->fireEvent('coreStartEvent'); + if ($event->isCancelled()) { + return true; } } @@ -99,13 +99,6 @@ class Core { } private function loadModule($name, $version = null) { - // Load the register if not loaded yet - if (!isset($this->mods->config->modregister->register)) { - $this->buildModRegister(); - } else { - $this->register = $this->mods->config->modregister->register; - } - // The basic module path $path = FUZEPATH . "Modules/"; @@ -186,14 +179,20 @@ class Core { 'moduleName' => $name); } - public function buildModRegister() { - $this->mods->logger->newLevel("Building Mod Register", 'Core'); - $dir = FUZEPATH . "Modules/"; - $mods = array_values(array_diff(scandir($dir), array('..', '.'))); - $register = array(); - for ($i=0; $i < count($mods); $i++) { - $mod_dir = $dir . $mods[$i] . "/"; - if (file_exists($mod_dir . "/moduleInfo.php")) { + public function buildRegister() { + $this->mods->logger->newLevel("Loading Module Headers", 'Core'); + + // Get all the module directories + $dir = FUZEPATH . "Modules/"; + $mod_dirs = array(); + $mod_dirs = array_values(array_diff(scandir($dir), array('..', '.'))); + + // Build the module register + $register = array(); + for ($i=0; $i < count($mod_dirs); $i++) { + $mod_dir = $dir . $mod_dirs[$i] . "/"; + // If a moduleInfo.php exists, load it + if (file_exists($mod_dir . "/moduleInfo.php")) { $cfg = (object) require($mod_dir . "/moduleInfo.php"); $name = ""; $name .= (!empty($cfg->author) ? strtolower($cfg->author)."/" : ""); @@ -203,11 +202,11 @@ class Core { $cfg->directory = $mod_dir; $register[$name] = (array) $cfg; $this->mods->logger->log("Found module: '".$name."'"); - } else { + } else { // Get the name - $name = $mods[$i]; + $name = $mod_dirs[$i]; - // Build a dynamic module config + // Build a default module config $cfg = new stdClass(); $cfg->module_class = ucfirst($name); $cfg->module_file = 'class.'.strtolower($name).".php"; @@ -217,12 +216,12 @@ class Core { $cfg->directory = $mod_dir; $register[$name] = (array)$cfg; $this->mods->logger->log("Found module: '".$name."'"); - } - } + } + } - $this->mods->logger->stopLevel(); - $this->mods->config->set('modregister', 'register', $register); - $this->mods->config->set('main', 'registers_last_update', date('U')); + $this->register = $register; + $this->mods->logger->stopLevel(); + } } diff --git a/Core/System/class.events.php b/Core/System/class.events.php index be8bb26..e3a98be 100644 --- a/Core/System/class.events.php +++ b/Core/System/class.events.php @@ -112,7 +112,7 @@ class Events extends Bus{ $this->logger->log("Checking for Listeners"); // Read the event register for listeners - $register = $this->config->eventregister->register; + $register = $this->register; if (isset($register[$eventName])) { for ($i=0; $i < count($register[$eventName]); $i++) { $this->core->loadMod($register[$eventName][$i]); @@ -154,18 +154,22 @@ class Events extends Bus{ // Event Preparation: public function buildEventRegister() { - $this->logger->newLevel("Building Event Register", 'Events'); - $dir = FUZEPATH . "/Modules/"; - $mods = $this->config->modregister->register; - foreach ($mods as $key => $value) { - try { - $this->core->loadMod($key); - } catch (Exception $e) {} + $event_register = array(); + foreach ($this->core->register as $key => $value) { + if (isset($value['events'])) { + if (!empty($value['events'])) { + for ($i=0; $i < count($value['events']); $i++) { + if (isset($event_register[$value['events'][$i]])) { + $event_register[$value['events'][$i]][] = $key; + } else { + $event_register[$value['events'][$i]] = array($key); + } + } + } + } } - $event = $this->fireEvent('eventRegisterBuildEvent', ''); - $this->config->set('eventregister', 'register', $event->register); - $this->logger->stopLevel(); + $this->register = $event_register; } } From e877d103c854e6777891d64af8a485621f77b11b Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Wed, 22 Apr 2015 12:11:28 +0200 Subject: [PATCH 2/3] Implemented renewed modloading, so that modules do not get loaded multiple times --- Core/System/class.core.php | 164 ++++++++++++++++--------------------- 1 file changed, 69 insertions(+), 95 deletions(-) diff --git a/Core/System/class.core.php b/Core/System/class.core.php index 7774cd4..db165c5 100644 --- a/Core/System/class.core.php +++ b/Core/System/class.core.php @@ -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() { From ba84fa5cef73c7b9ff75031eb89484d982111ba1 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Wed, 22 Apr 2015 12:16:14 +0200 Subject: [PATCH 3/3] Messages when a module is already loaded, and loaded modules get correctly registered --- Core/System/class.core.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Core/System/class.core.php b/Core/System/class.core.php index db165c5..2f51540 100644 --- a/Core/System/class.core.php +++ b/Core/System/class.core.php @@ -92,7 +92,9 @@ class Core { // 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)}; + $msg = "Module '".ucfirst((isset($cfg->name) ? $cfg->name : $cfg->module_name)) . "' is already loaded"; + $this->mods->logger->log($msg); + $c = &$this->mods->{strtolower($cfg->module_name)}; return $c; } else { // Load the module @@ -144,11 +146,12 @@ class Core { } $CLASS->onLoad(); + // Add to the loaded modules + $this->loaded_modules[] = $name; + // Return a reference return $this->mods->{strtolower($cfg->module_name)} = &$CLASS; } - - } } }