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:
commit
f6a282d38f
|
@ -1,5 +0,0 @@
|
||||||
<?php return array (
|
|
||||||
'register' =>
|
|
||||||
array (
|
|
||||||
),
|
|
||||||
) ;
|
|
|
@ -2,7 +2,6 @@
|
||||||
'SITE_URL' => '',
|
'SITE_URL' => '',
|
||||||
'SITE_DOMAIN' => '',
|
'SITE_DOMAIN' => '',
|
||||||
'SERVER_NAME' => '',
|
'SERVER_NAME' => '',
|
||||||
'registers_update_interval' => 3600,
|
|
||||||
'administrator_mail' => '',
|
'administrator_mail' => '',
|
||||||
'default_controller' => 'standard',
|
'default_controller' => 'standard',
|
||||||
'default_function' => 'index',
|
'default_function' => 'index',
|
||||||
|
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
|
@ -10,8 +10,15 @@ if (!defined('FUZESYSPATH')) {
|
||||||
class Core {
|
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 $loaded = false;
|
||||||
private $register;
|
|
||||||
|
|
||||||
## START/STOP
|
## START/STOP
|
||||||
public function init() {
|
public function init() {
|
||||||
|
@ -23,12 +30,12 @@ class Core {
|
||||||
$this->mods = new stdClass();
|
$this->mods = new stdClass();
|
||||||
$this->loadStartupFiles();
|
$this->loadStartupFiles();
|
||||||
|
|
||||||
$this->mods->events->fireEvent('coreStartEvent');
|
$this->buildRegister();
|
||||||
// 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->mods->events->buildEventRegister();
|
||||||
|
|
||||||
|
$event = $this->mods->events->fireEvent('coreStartEvent');
|
||||||
|
if ($event->isCancelled()) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -64,80 +71,33 @@ 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) {
|
|
||||||
// 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/";
|
$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])) {
|
$msg = "Module '".ucfirst((isset($cfg->name) ? $cfg->name : $cfg->module_name)) . "' is already loaded";
|
||||||
$ncfg = (object) $cfg->versions[$version];
|
$this->mods->logger->log($msg);
|
||||||
foreach ($ncfg as $key => $value) {
|
$c = &$this->mods->{strtolower($cfg->module_name)};
|
||||||
$cfg->$key = $value;
|
return $c;
|
||||||
}
|
} else {
|
||||||
}
|
// Load the module
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Or load the main version
|
|
||||||
$file = $cfg->directory . $cfg->module_file;
|
$file = $cfg->directory . $cfg->module_file;
|
||||||
|
|
||||||
// Load the dependencies before the module loads
|
// Load the dependencies before the module loads
|
||||||
|
@ -161,38 +121,54 @@ class Core {
|
||||||
throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1);
|
throw new Exception("Requested mod '".$name."' could not be loaded. Class file not found", 1);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} 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 it is an abstract module, load an StdClass for the module address
|
||||||
if (isset($cfg->abstract)) {
|
if (isset($cfg->abstract)) {
|
||||||
if ($cfg->abstract) {
|
if ($cfg->abstract) {
|
||||||
$c = new stdClass();
|
$CLASS = new stdClass();
|
||||||
return array();
|
return $this->mods->{strtolower($cfg->module_name)} = &$CLASS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return array('className' => $class_name,
|
// Load the module class
|
||||||
'modulePath' => $cfg->directory,
|
$class_name = $cfg->module_class;
|
||||||
'moduleLinkName' => $cfg->module_name,
|
$CLASS = new $class_name($this);
|
||||||
'moduleName' => $name);
|
|
||||||
|
// 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildModRegister() {
|
public function buildRegister() {
|
||||||
$this->mods->logger->newLevel("Building Mod Register", 'Core');
|
$this->mods->logger->newLevel("Loading Module Headers", 'Core');
|
||||||
|
|
||||||
|
// Get all the module directories
|
||||||
$dir = FUZEPATH . "Modules/";
|
$dir = FUZEPATH . "Modules/";
|
||||||
$mods = array_values(array_diff(scandir($dir), array('..', '.')));
|
$mod_dirs = array();
|
||||||
|
$mod_dirs = array_values(array_diff(scandir($dir), array('..', '.')));
|
||||||
|
|
||||||
|
// Build the module register
|
||||||
$register = array();
|
$register = array();
|
||||||
for ($i=0; $i < count($mods); $i++) {
|
for ($i=0; $i < count($mod_dirs); $i++) {
|
||||||
$mod_dir = $dir . $mods[$i] . "/";
|
$mod_dir = $dir . $mod_dirs[$i] . "/";
|
||||||
|
// If a moduleInfo.php exists, load it
|
||||||
if (file_exists($mod_dir . "/moduleInfo.php")) {
|
if (file_exists($mod_dir . "/moduleInfo.php")) {
|
||||||
$cfg = (object) require($mod_dir . "/moduleInfo.php");
|
$cfg = (object) require($mod_dir . "/moduleInfo.php");
|
||||||
$name = "";
|
$name = "";
|
||||||
|
@ -205,9 +181,9 @@ class Core {
|
||||||
$this->mods->logger->log("Found module: '".$name."'");
|
$this->mods->logger->log("Found module: '".$name."'");
|
||||||
} else {
|
} else {
|
||||||
// Get the name
|
// Get the name
|
||||||
$name = $mods[$i];
|
$name = $mod_dirs[$i];
|
||||||
|
|
||||||
// Build a dynamic module config
|
// Build a default module config
|
||||||
$cfg = new stdClass();
|
$cfg = new stdClass();
|
||||||
$cfg->module_class = ucfirst($name);
|
$cfg->module_class = ucfirst($name);
|
||||||
$cfg->module_file = 'class.'.strtolower($name).".php";
|
$cfg->module_file = 'class.'.strtolower($name).".php";
|
||||||
|
@ -220,9 +196,9 @@ class Core {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->register = $register;
|
||||||
$this->mods->logger->stopLevel();
|
$this->mods->logger->stopLevel();
|
||||||
$this->mods->config->set('modregister', 'register', $register);
|
|
||||||
$this->mods->config->set('main', 'registers_last_update', date('U'));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,7 +112,7 @@ class Events extends Bus{
|
||||||
$this->logger->log("Checking for Listeners");
|
$this->logger->log("Checking for Listeners");
|
||||||
|
|
||||||
// Read the event register for listeners
|
// Read the event register for listeners
|
||||||
$register = $this->config->eventregister->register;
|
$register = $this->register;
|
||||||
if (isset($register[$eventName])) {
|
if (isset($register[$eventName])) {
|
||||||
for ($i=0; $i < count($register[$eventName]); $i++) {
|
for ($i=0; $i < count($register[$eventName]); $i++) {
|
||||||
$this->core->loadMod($register[$eventName][$i]);
|
$this->core->loadMod($register[$eventName][$i]);
|
||||||
|
@ -154,18 +154,22 @@ class Events extends Bus{
|
||||||
|
|
||||||
// Event Preparation:
|
// Event Preparation:
|
||||||
public function buildEventRegister() {
|
public function buildEventRegister() {
|
||||||
$this->logger->newLevel("Building Event Register", 'Events');
|
$event_register = array();
|
||||||
$dir = FUZEPATH . "/Modules/";
|
foreach ($this->core->register as $key => $value) {
|
||||||
$mods = $this->config->modregister->register;
|
if (isset($value['events'])) {
|
||||||
foreach ($mods as $key => $value) {
|
if (!empty($value['events'])) {
|
||||||
try {
|
for ($i=0; $i < count($value['events']); $i++) {
|
||||||
$this->core->loadMod($key);
|
if (isset($event_register[$value['events'][$i]])) {
|
||||||
} catch (Exception $e) {}
|
$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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user