Merge branch 'master' into 'master'

Fixes for #23 and #28

Fixes all associated bugs. Might set the project on fire, but I don't care

See merge request !10
This commit is contained in:
Abel Hoogeveen 2015-04-22 12:26:13 +02:00
commit f6a282d38f
6 changed files with 116 additions and 159 deletions

View File

@ -1,5 +0,0 @@
<?php return array (
'register' =>
array (
),
) ;

View File

@ -2,7 +2,6 @@
'SITE_URL' => '',
'SITE_DOMAIN' => '',
'SERVER_NAME' => '',
'registers_update_interval' => 3600,
'administrator_mail' => '',
'default_controller' => 'standard',
'default_function' => 'index',

View File

@ -1,17 +0,0 @@
<?php
class EventRegisterBuildEvent extends Event {
public $register = array();
public function init($call){}
public function addEvent($modName, $eventName) {
if (!isset($this->register[$eventName])) {
$this->register[$eventName] = array();
}
$this->register[$eventName][] = $modName;
}
}
?>

View File

@ -9,9 +9,16 @@ if (!defined('FUZESYSPATH')) {
// Framework
class Core {
public $mods;
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;
private $register;
## START/STOP
public function init() {
@ -23,12 +30,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;
}
}
@ -64,136 +71,105 @@ 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) {
// 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
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
$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
$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();
// Add to the loaded modules
$this->loaded_modules[] = $name;
// 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 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 +179,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 +193,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();
}
}

View File

@ -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;
}
}