From 8c1a7afda544201128de13531edcc0b78fe46f26 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Wed, 22 Apr 2015 11:31:29 +0200 Subject: [PATCH] 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; } }