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/Application/Controller/controller.standard.php b/Application/Controller/controller.standard.php index 644309c..90ebfc5 100644 --- a/Application/Controller/controller.standard.php +++ b/Application/Controller/controller.standard.php @@ -1,6 +1,9 @@ route = $route; + $this->controllerName = $controllerName; + $this->function = $function; + $this->parameters = $parameters; + $this->directory = $directory; + } +} + +?> \ No newline at end of file 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/Events/event.layoutLoadEvent.php b/Core/Events/event.layoutLoadEvent.php new file mode 100644 index 0000000..04f2514 --- /dev/null +++ b/Core/Events/event.layoutLoadEvent.php @@ -0,0 +1,15 @@ +layout = $layout; + } +} + +?> \ No newline at end of file diff --git a/Core/Events/event.modelLoadEvent.php b/Core/Events/event.modelLoadEvent.php new file mode 100644 index 0000000..ef5c08a --- /dev/null +++ b/Core/Events/event.modelLoadEvent.php @@ -0,0 +1,15 @@ +model = $model; + } +} + +?> \ No newline at end of file diff --git a/Core/Events/event.routerRouteEvent.php b/Core/Events/event.routerRouteEvent.php index ba4b34a..4441290 100644 --- a/Core/Events/event.routerRouteEvent.php +++ b/Core/Events/event.routerRouteEvent.php @@ -1,5 +1,7 @@ connect($this->getSystemDbSettings()); - $this->config->dbActive = true; - } - - public function connect($params = array()) { - if (isset($params['type'])) { - $type = $params['type']; - } else { - throw (new Exception("No database type given")); - } - - if (isset($params['datb'])) { - $database = $params['datb']; - } else { - throw (new Exception("No database given. Can not connect without database.")); - } - - if (isset($params['host'])) { - $host = $params['host']; - } else { - throw (new Exception("No database host given. Can not connect without hostname.")); - } - - $username = $params['user']; - $password = $params['pass']; - $this->prefix = $params['prefix']; - - if (isset($params['options'])) { - $options = $params['options']; - } else { - $options = null; - } - - - $DSN_FINAL = ""; - - switch ($type) { - case 'MYSQL': - $DSN = "mysql:host="; - $DSN2 = ";dbname="; - - // Check if charset is required - if (isset($extraOptions)) { - if (isset($extraOptions->charset)) { - $DSN3 = ";charset=" . $extraOptions->charset; - } else { - $DSN3 = ";"; - } - } else { - $DSN3 = ";"; - } - $DSN_FINAL = $DSN . $host . $DSN2 . $database . $DSN3; - break; - case 'sqlite': - $DSN = 'sqlite:' . $host . ($database != '' ? ";dbname=" .$database : ""); - $DSN_FINAL = $DSN; - break; - default: - throw (new Exception("Unknown database type given: '" . $type . "'")); - break; - } - - // Try and connect - try{ - $this->mods->logger->logInfo("Connecting to '" . $DSN_FINAL. "'", "Database", __FILE__, __LINE__); - $this->DBH = new \PDO($DSN_FINAL, $username, $password, $options); - $this->DBH->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - - $this->mods->logger->logInfo("Connected!", "Database", __FILE__, __LINE__); - }catch(\PDOException $e){ - throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"')); - } - } - - public function __call($name, $params) { - if ($this->is_active()) { - return call_user_func_array(array($this->DBH, $name), $params); - } else { - $this->connect($this->getSystemDbSettings()); - return call_user_func_array(array($this->DBH, $name), $params); - } - } - - public function __get($name) { - if ($this->is_active()) { - return $this->DBH->$name; - } else { - $this->connect($this->getSystemDbSettings()); - return $this->DBH->$name; - } - } - - public function __set($name, $value) { - if ($this->is_active()) { - $this->DBH->$name = $value; - } else { - $this->connect($this->getSystemDbSettings()); - $this->DBH->$name = $value; - } - } - - public function is_active() { - if($this->DBH === null){ - return false; - } else { - return true; - } - } - - public function getPrefix() { - return $this->prefix; - } - - /** - * Retrieve an array of the system DB settings. This is the configuration in the config file of FuzeWorks - * @access public - * @return DBSettings array - */ - public function getSystemDbSettings() { - $dbsettings = array( - 'type' => $this->mods->config->database->type, - 'host' => $this->mods->config->database->host, - 'user' => $this->mods->config->database->username, - 'pass' => $this->mods->config->database->password, - 'datb' => $this->mods->config->database->database, - 'prefix' => $this->mods->config->database->prefix, - ); - return $dbsettings; - } -} - - -?> \ No newline at end of file diff --git a/Core/Mods/sections/class.sections.php b/Core/Mods/sections/class.sections.php deleted file mode 100644 index 9ef404a..0000000 --- a/Core/Mods/sections/class.sections.php +++ /dev/null @@ -1,154 +0,0 @@ -cfg = $this->config->loadConfigFile('sections', $this->getModulePath()); - - // Register Events - $this->events->addListener(array($this, 'eventRegisterBuild'), 'eventRegisterBuildEvent', EventPriority::NORMAL); - $this->events->addListener(array($this, 'routerEvent'), 'routerRouteEvent', EventPriority::NORMAL); - } - - /** - * Registers this module in the eventRegister for routerRouteEvent - * @access public - * @param eventRegisterBuildEvent Event - * @return eventRegisterBuildEvent Event - */ - public function eventRegisterBuild($event) { - $event->addEvent('sections', 'routerRouteEvent'); - return $event; - } - - /** - * Add a section to the config file - * @access public - * @param String section_name, name of the section - * @param Boolean module_section wether this is a module_section - * @param String module_name to use when this is a module_section - * @param String Controller_path, where to find the controllers for this section - * @param String Model_path, where to find the models for this section - * @param String View_path, where to find the views for this section - */ - public function addSection($name, $module_section = false, $module_name = null, $controller_path = null, $model_path = null, $view_path = null) { - $data = array( - 'name' => $name, - 'module_section' => $module_section, - 'module_name' => $module_name, - 'controller_path' => FUZEPATH . $controller_path, - 'model_path' => FUZEPATH . $model_path, - 'view_path' => FUZEPATH . $view_path, - ); - $this->config->set('sections', $name, $data, $this->getModulePath()); - } - - /** - * Removes a section from the config file - * @access public - * @param String section_name, name of the section to remove - */ - public function removeSection($name) { - $this->config->set('sections', $name, null, $this->getModulePath()); - } - - /** - * Get's called on routerRouteEvent. Redirects when a section is found - * @access public - * @param routerRouteEvent Event - * @return routerRouteEvent Event - */ - public function routerEvent($event) { - $name = $event->controller; - $controller = null; - $function = null; - $parameters = array(); - $section = $this->{$name}; - - if ($section !== null) { - // Section found - $this->logger->log("Section found with name: '".$name."'", 'Sections'); - $this->currentSection = $name; - - // Logic here, first for module sections - if ($section['module_section']) { - $mod = $this->core->loadMod($section['module_name']); - $event->directory = $mod->getModulePath() . "/Controller/"; - } else { - // Now for regular sections - $event->directory = $section['controller_path']; - } - - // Move the path so it matches the new regime - if (count($event->parameters) > 0) { - $function = $event->parameters[0]; - $parameters = array_slice($event->parameters, 1); - } else { - $function = $this->config->main->default_function; - } - - // And finally set the controller, if no parameters are set, load the default function - $controller = (!empty($event->function) ? $event->function : $this->config->main->default_controller ); - } else { - $this->logger->log("No section was found with name: '".$name."'", 'Sections'); - } - - if($controller !== null)$event->controller = $controller; - if($function !== null)$event->function = $function; - if(count($parameters) !== 0)$event->parameters = $parameters; - - return $event; - } - - /** - * Retrieves section information from the config file - * @access public - * @param String section_name, name of the section - * @return Array section_information or null - */ - public function __get($name){ - // Something given? - if(empty($name)) { - // Currently in a section? - if($this->currentSection !== null){ - // Return that one then - return $this->cfg[$this->currentSection]; - } - return null; - } - - // Return element $name of the config file - if (isset($this->cfg->$name)) { - $section = $this->cfg->$name; - } else { - $section = null; - } - - return $section; - } - -} - -?> \ No newline at end of file diff --git a/Core/Mods/sections/config.sections.php b/Core/Mods/sections/config.sections.php deleted file mode 100644 index c3388f9..0000000 --- a/Core/Mods/sections/config.sections.php +++ /dev/null @@ -1 +0,0 @@ -fields = $table_fields; } - - public function toPHP() { - $values = array(); - foreach ($this->fields as $key => $value) { - $values[] = '"'.$value.'"'; - } - $values = implode(', ', $values); - $text = 'VALUES: array('.$values.') - TABLE: '.$this->table; - echo $text; - } } ?> \ No newline at end of file diff --git a/Core/System/class.abstract.bus.php b/Core/System/class.abstract.bus.php index 58e6552..2be7358 100644 --- a/Core/System/class.abstract.bus.php +++ b/Core/System/class.abstract.bus.php @@ -1,5 +1,7 @@ core->loadMod($module_name); + $this->parentClass = $mod->giveController($controller_type); + } + + /** + * Retrieves a value from the controller class + * @access public + * @param Any key + * @return Any value from the controller class + */ + public function __get($name) { + return $this->parentClass->$name; + } + + /** + * Sets a value in the controller class + * @access public + * @param Any key + * @param Any value + */ + public function __set($name, $value) { + $this->parentClass->$name = $value; + } + + /** + * Calls a function in the controller class + * @access public + * @param String function_name + * @param Array values + * @return Function return + */ + public function __call($name, $params) { + return call_user_func_array(array($this->parentClass, $name), $params); + } +} \ No newline at end of file diff --git a/Core/System/class.abstract.event.php b/Core/System/class.abstract.event.php index f203e02..1cd8210 100644 --- a/Core/System/class.abstract.event.php +++ b/Core/System/class.abstract.event.php @@ -1,5 +1,7 @@ \ No newline at end of file diff --git a/Core/System/class.abstract.eventPriority.php b/Core/System/class.abstract.eventPriority.php index cf70665..018adf2 100644 --- a/Core/System/class.abstract.eventPriority.php +++ b/Core/System/class.abstract.eventPriority.php @@ -1,5 +1,7 @@ core->loadMod($module_name); - $this->parentClass = new $class_name($this->core); + protected function setType($module_name, $model_type) { + $mod = $this->core->loadMod($module_name); + $this->parentClass = $mod->giveModel($model_type); } /** diff --git a/Core/System/class.abstract.module.php b/Core/System/class.abstract.module.php index 0003a7c..fa1550c 100644 --- a/Core/System/class.abstract.module.php +++ b/Core/System/class.abstract.module.php @@ -1,5 +1,7 @@ core->loadMod('database'); + $this->core->loadMod('techfuze/database'); if ($this->dbActive) { // Fetch me a query of 5 $prefix = $this->mods->database->getPrefix(); @@ -85,7 +88,7 @@ class Config extends Bus{ * @param String directory, default is Application/Config */ public function set($name, $key, $value, $directory = null) { - $dir = (isset($directory) ? $directory : FUZEPATH . "Application//config/"); + $dir = (isset($directory) ? $directory : "Application/config/"); $file = $dir . 'config.' . strtolower($name).".php"; if (file_exists($file)) { $DECODED = require($file); diff --git a/Core/System/class.core.php b/Core/System/class.core.php index b2c171d..4aaa1f4 100644 --- a/Core/System/class.core.php +++ b/Core/System/class.core.php @@ -1,20 +1,32 @@ 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; } } @@ -37,18 +49,19 @@ class Core { return; // Load core abstracts - require_once(FUZESYSPATH . "/class.abstract.bus.php"); - require_once(FUZESYSPATH . "/class.abstract.event.php"); - require_once(FUZESYSPATH . "/class.abstract.module.php"); - require_once(FUZESYSPATH . "/class.abstract.model.php"); - require_once(FUZESYSPATH . "/class.abstract.eventPriority.php"); + require_once("Core/System/class.abstract.bus.php"); + require_once("Core/System/class.abstract.event.php"); + require_once("Core/System/class.abstract.module.php"); + require_once("Core/System/class.abstract.model.php"); + require_once("Core/System/class.abstract.controller.php"); + require_once("Core/System/class.abstract.eventPriority.php"); // Load the core classes - require_once(FUZESYSPATH . "/class.config.php"); - require_once(FUZESYSPATH . "/class.logger.php"); - require_once(FUZESYSPATH . "/class.models.php"); - require_once(FUZESYSPATH . "/class.layout.php"); - require_once(FUZESYSPATH . "/class.events.php"); + require_once("Core/System/class.config.php"); + require_once("Core/System/class.logger.php"); + require_once("Core/System/class.models.php"); + require_once("Core/System/class.layout.php"); + require_once("Core/System/class.events.php"); // Load them $this->mods->events = new Events ($this); @@ -64,133 +77,101 @@ class Core { $this->mods->events->fireEvent('coreShutdownEvent'); } - ## MODLOADING - public function loadMod($name, $version = null) { - // Get class information - $data = $this->loadModule($name, $version); + public function loadMod($name) { + // Where the modules are + $path = "Modules/"; - // 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; - } - } - - 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 . "/Core/Mods/"; - - // 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 - if (isset($cfg->enabled)) { - if (!$cfg->enabled) { - // Module is disabled - throw new Exception("Module '".$name."' is not enabled!", 1); - return false; - } - } + // 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; - // 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; - } + // 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(strtolower($cfg->module_name)); + } + if (method_exists($CLASS, 'setModuleName')) { + $CLASS->setModuleName($name); + } + + if (!method_exists($CLASS, 'onLoad')) { + throw new Exception("Module '".$name."' does not have an onLoad() method! Invalid module", 1); + } + $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 . "Core/Mods/"; - $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 = "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)."/" : ""); @@ -198,12 +179,24 @@ class Core { // Append directory $cfg->directory = $mod_dir; - $register[$name] = (array) $cfg; - } else { + if (isset($cfg->enabled)) { + if ($cfg->enabled) { + $register[$name] = (array) $cfg; + $this->mods->logger->log("[ON] '".$name."'"); + } else { + $this->mods->logger->log("[OFF] '".$name."'"); + } + } else { + $register[$name] = (array) $cfg; + $this->mods->logger->log("[ON] '".$name."'"); + } + + + } 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"; @@ -212,12 +205,13 @@ class Core { $cfg->versions = array(); $cfg->directory = $mod_dir; $register[$name] = (array)$cfg; - } - } + $this->mods->logger->log("[ON] '".$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 8551276..b12d8ec 100644 --- a/Core/System/class.events.php +++ b/Core/System/class.events.php @@ -3,10 +3,21 @@ * @author FuzeNetwork * @package files */ - + +namespace FuzeWorks; +use \Exception; + /** - * @name Events -*/ + * Event Class + * + * Controls FuzeWorks Events. Events are classes that get loaded during special moments in the program. + * These Event objects get send to so-called 'listeners', which can modify the event object, and eventually return them to invoker. + * Typically an event process goes like this: + * - Event get's called + * - Event object is created + * - Event object is send to all listeners in order of EventPriority + * - Event is returned + */ class Events extends Bus{ private $listeners; @@ -71,23 +82,23 @@ class Events extends Bus{ ## EVENTS public function fireEvent($input) { if (is_string($input)) { - // STRING + // If the input is a string $eventClass = $input; $eventName = $input; if(!class_exists($eventClass)){ // Check if the file even exists - $file = FUZEPATH . "/Core/Events/event.".$eventName.".php"; + $file = "Core/Events/event.".$eventName.".php"; if(file_exists($file)){ // Load the file require_once($file); }else{ - // No event arguments? Looks like an notify-event + // No event arguments? Looks like a notify-event if(func_num_args() == 1){ // Load notify-event-class - $eventClass = 'NotifierEvent'; + $eventClass = '\FuzeWorks\NotifierEvent'; }else{ // No notify-event: we tried all we could - throw new \Exception("Event ".$eventName." could not be found!"); + throw new Exception("Event ".$eventName." could not be found!"); } } } @@ -112,7 +123,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,22 +165,24 @@ class Events extends Bus{ // Event Preparation: public function buildEventRegister() { - $this->logger->newLevel("Building Event Register", 'Events'); - $dir = FUZEPATH . "/Core/Mods/"; - $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; } } -class NotifierEvent extends Event {} - ?> \ No newline at end of file diff --git a/Core/System/class.layout.php b/Core/System/class.layout.php index d2dc8c3..94f28e0 100644 --- a/Core/System/class.layout.php +++ b/Core/System/class.layout.php @@ -1,5 +1,16 @@ setCompileDir(FUZEPATH . "/Core/Cache/Compile"); - $Smarty->setCacheDir(FUZEPATH . "/Core/Cache/"); + $Smarty->setCompileDir("Core/Cache/Compile"); + $Smarty->setCacheDir("Core/Cache/"); $Smarty->assign('siteURL', $this->config->main->SITE_URL); $Smarty->assign('serverName', $this->config->main->SERVER_NAME); $Smarty->assign('siteDomain', $this->config->main->SITE_DOMAIN); @@ -72,11 +83,15 @@ class Layout extends Bus { return $this->title; } - public function view($view = "default", $dir = null) { + public function view($view = "default") { // Chech if Smarty is loaded if (!$this->loaded) $this->load(); + $event = $this->events->fireEvent('layoutLoadEvent', $view); + $directory = ($event->directory === null ? "Application/Views" : $event->directory); + $view = ($event->layout === null ? $view : $event->layout); + // Set the file name and location $vw = explode('/', $view); if (count($vw) == 1) { @@ -98,65 +113,54 @@ class Layout extends Bus { } // Set the directory - $dir = (!isset($dir) ? FUZEPATH . "/Application/" . '/Views' : $dir); - $this->Smarty['main']->setTemplateDir($dir); + $this->Smarty['main']->setTemplateDir($directory); // Set the title $this->Smarty['main']->assign('title', $this->title); // Get the viewdir - // @TODO: Fix this for custom directories - $one = FUZEPATH; - $two = $dir . "/"; - $count_one = strlen($one); - $count_two = strlen($two); - $length_three = $count_two - $count_one; - $three = $this->config->main->SITE_URL . "/" . substr($two, -$length_three); - $this->layout->assign('viewDir', $three); + $viewDir = $this->config->main->SITE_URL . "/" . substr($directory . "/", -strlen($directory . "/")); + $this->layout->assign('viewDir', $viewDir); try{ // Load the page $this->Smarty['main']->display($vw); - $this->logger->logInfo("VIEW LOAD: '".$vw."'", "FuzeWorks->Layout", __FILE__, __LINE__); + $this->logger->logInfo("VIEW LOAD: '".$vw."'", "Layout", __FILE__, __LINE__); }catch (\SmartyException $e){ // Throw error on failure - $this->logger->logError('Could not load view '.$dir.'/'.$vw.' :: ' . $e->getMessage(), 'FuzeWorks->Layout', __FILE__, __LINE__); - throw new Exception\Layout('Could not load view '.$dir.'/'.$vw); + $this->logger->logError('Could not load view '.$directory.'/'.$vw.' :: ' . $e->getMessage(), 'Layout', __FILE__, __LINE__); + throw new Exception\Layout('Could not load view '.$directory.'/'.$vw); } } - public function get($view = "default", $dir = "") { + public function get($view = "default", $directory = "") { // Chech if Smarty is loaded if (!$this->loaded) $this->load(); // Set the directory - $dir = ($dir == "" ? FUZEPATH . "/Application/" . '/Views' : $dir); - $this->Smarty['main']->setTemplateDir($dir); + $directory = ($directory == "" ? "Application/" . '/Views' : $directory); + $this->Smarty['main']->setTemplateDir($directory); // Set the title $this->Smarty['main']->assign('title', $this->title); // Get the viewdir - $one = FUZEPATH; - $two = $dir . "/"; - $count_one = strlen($one); - $count_two = strlen($two); - $length_three = $count_two - $count_one; - $three = $this->config->main->SITE_URL . "/" . substr($two, -$length_three); - $this->layout->assign('viewdir', $three); + $viewDir = $this->config->main->SITE_URL . "/" . substr($directory . "/", -strlen($directory . "/")); + $this->layout->assign('viewDir', $viewDir); + try{ // Load the page return $this->Smarty['main']->fetch('view.'.$view.'.tpl'); - $this->logger->logInfo("VIEW LOAD: 'view.".$view.'.tpl'."'", "FuzeWorks->Layout", __FILE__, __LINE__); + $this->logger->logInfo("VIEW LOAD: 'view.".$view.'.tpl'."'", "Layout", __FILE__, __LINE__); }catch (\SmartyException $e){ // Throw error on failure - $this->logger->logError('Could not load view '.$dir.'/view.'.$view.'.tpl :: ' . $e->getMessage(), 'FuzeWorks->Layout', __FILE__, __LINE__); - throw new Exception\Layout('Could not load view '.$dir.'/view.'.$view.'.tpl'); + $this->logger->logError('Could not load view '.$directory.'/view.'.$view.'.tpl :: ' . $e->getMessage(), 'Layout', __FILE__, __LINE__); + throw new Exception\Layout('Could not load view '.$directory.'/view.'.$view.'.tpl'); } } } \ No newline at end of file diff --git a/Core/System/class.logger.php b/Core/System/class.logger.php index 4839074..7f5f126 100644 --- a/Core/System/class.logger.php +++ b/Core/System/class.logger.php @@ -1,5 +1,14 @@ errorHandler($errno, $errstr, $errfile, $errline); + $this->logInfo($this->backtrace()); } if ($this->mods->config->error->debug == true || $this->print_to_screen) { @@ -88,6 +98,13 @@ class Logger extends Bus{ } public function logToScreen() { + // Send a screenLogEvent, allows for new screen log designs + $event = $this->mods->events->fireEvent('screenLogEvent'); + if ($event->isCancelled()) { + return false; + } + + // Otherwise just load it echo '

FuzeWorks debug log

'; $layer = 0; for($i = 0; $i < count($this->Logs); $i++){ @@ -113,6 +130,24 @@ class Logger extends Bus{ } } + public function backtrace() { + $e = new Exception(); + $trace = explode("\n", $e->getTraceAsString()); + // reverse array to make steps line up chronologically + $trace = array_reverse($trace); + array_shift($trace); // remove {main} + array_pop($trace); // remove call to this method + $length = count($trace); + $result = array(); + + for ($i = 0; $i < $length; $i++) + { + $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering + } + + return "BACKTRACE:
\t" . implode("
", $result)."
"; + } + /* =========================================LOGGING METHODS==============================================================*/ @@ -267,8 +302,8 @@ class Logger extends Bus{ 511 => 'Network Authentication Required' ); - $this->logError('HTTP-error '.$errno.' called', 'FuzeWorks->Logger'); - $this->logInfo('Sending header HTTP/1.1 '.$errno.' '.$http_codes[$errno], 'FuzeWorks->Logger', __FILE__, __LINE__); + $this->logError('HTTP-error '.$errno.' called', 'Logger'); + $this->logInfo('Sending header HTTP/1.1 '.$errno.' '.$http_codes[$errno], 'Logger', __FILE__, __LINE__); header('HTTP/1.1 '.$errno.' '.$http_codes[$errno]); } diff --git a/Core/System/class.models.php b/Core/System/class.models.php index 0abf1e0..b414037 100644 --- a/Core/System/class.models.php +++ b/Core/System/class.models.php @@ -3,6 +3,14 @@ * @author FuzeNetwork */ +namespace FuzeWorks; + +/** + * Models Class + * + * Simple loader class for MVC Models. Typically loads models from Application/Models unless otherwise specified. + * If a model is not found, it will load a DatabaseModel type which will analyze the database and can directly be used. + */ class Models extends Bus{ private $models_array = array(); @@ -14,9 +22,10 @@ class Models extends Bus{ } public function loadModel($name, $directory = null){ - if($directory === null){ - $directory = FUZEPATH . "/Application/Models"; - } + // Model load event + $event = $this->events->fireEvent('modelLoadEvent', $name); + $directory = ($event->directory === null ? "Application/Models" : $event->directory); + $name = ($event->model === null ? $name : $event->model); $file = $directory.'/model.'.$name.'.php'; if (isset($this->model_types[$name])) { @@ -24,12 +33,12 @@ class Models extends Bus{ $this->models_array[$name] = $this->model_types[$name]; } elseif (file_exists($file)){ require_once($file); - $model = ucfirst($name); + $model = "\Model\\" . ucfirst($name); $this->logger->logInfo('Loading Model: '.$model, $model); $this->models_array[$name] = new $model($this->core); } else{ $this->logger->logWarning('The requested model: \''.$name.'\' could not be found. Loading empty model', 'Models'); - require_once(FUZEPATH . "/Core/System/Models/model.interpret.php"); + require_once("Core/System/Models/model.interpret.php"); $this->logger->logInfo('Loading Model: interprated databasemodel', 'Models'); $model = new Interpret($this->core); $model->table($name); diff --git a/Modules/database/class.database.php b/Modules/database/class.database.php new file mode 100644 index 0000000..bdc5c9a --- /dev/null +++ b/Modules/database/class.database.php @@ -0,0 +1,99 @@ +config->dbActive = true; + } + + public function connect($config = null) { + // If nothing is given, connect to database from the main config, otherwise use the served configuration + if (is_null($config)) { + $db = $this->mods->config->database; + } else { + $db = $config; + } + + // Get the DSN for popular types of databases or a custom DSN + switch (strtolower($db->type)) { + case 'mysql': + $dsn = "mysql:host=".$db->host.";"; + $dsn .= (!empty($db->database) ? "dbname=".$db->database.";" : ""); + break; + case 'custom': + $dsn = $db->dsn; + break; + } + + try { + $this->mods->logger->logInfo("Connecting to '".$dsn."'", "Database"); + // And create the connection + $this->DBH = new PDO($dsn, $db->username, $db->password, (isset($db->options) ? $db->options : null)); + $this->DBH->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $this->mods->logger->logInfo("Connected to database", "Database"); + + // And set the prefix + $this->prefix = $db->prefix; + } catch (Exception $e) { + throw (new Exception('Could not connect to the database: "'. $e->getMessage() . '"')); + } + } + + public function getPrefix() { + return $this->prefix; + } + + public function is_active() { + if($this->DBH === null){ + return false; + } else { + return true; + } + } + + public function __call($name, $params) { + if ($this->is_active()) { + return call_user_func_array(array($this->DBH, $name), $params); + } else { + $this->connect(); + return call_user_func_array(array($this->DBH, $name), $params); + } + } + + public function __get($name) { + if ($this->is_active()) { + return $this->DBH->$name; + } else { + $this->connect(); + return $this->DBH->$name; + } + } + + public function __set($name, $value) { + if ($this->is_active()) { + $this->DBH->$name = $value; + } else { + $this->connect(); + $this->DBH->$name = $value; + } + } +} + + +?> \ No newline at end of file diff --git a/Modules/database/moduleInfo.php b/Modules/database/moduleInfo.php new file mode 100644 index 0000000..705ba7c --- /dev/null +++ b/Modules/database/moduleInfo.php @@ -0,0 +1,23 @@ + 'Module\Database\Main', + 'module_file' => 'class.database.php', + 'module_name' => 'Database', + + 'abstract' => false, + 'dependencies' => array(), + 'events' => array(), + 'sections' => array(), + + 'name' => 'FuzeWorks Database Module', + 'description' => 'PDO Wrapper class for FuzeWorks', + 'author' => 'TechFuze', + 'version' => '1.0.0', + 'website' => 'http://fuzeworks.techfuze.net/', + + 'date_created' => '30-04-2015', + 'date_updated' => '30-04-2015', + + 'enabled' => true, +); diff --git a/Core/Mods/databasemodel/class.model.php b/Modules/databasemodel/class.model.php similarity index 97% rename from Core/Mods/databasemodel/class.model.php rename to Modules/databasemodel/class.model.php index 06dd053..90d72a5 100644 --- a/Core/Mods/databasemodel/class.model.php +++ b/Modules/databasemodel/class.model.php @@ -1,6 +1,11 @@ core->loadMod('database'); + } + + public function onLoad() {} + + public function giveModel($type) { + return new DatabaseModel($this->core); } public function select(){ diff --git a/Core/Mods/databasemodel/moduleInfo.php b/Modules/databasemodel/moduleInfo.php similarity index 72% rename from Core/Mods/databasemodel/moduleInfo.php rename to Modules/databasemodel/moduleInfo.php index aafc5dc..81b6c58 100644 --- a/Core/Mods/databasemodel/moduleInfo.php +++ b/Modules/databasemodel/moduleInfo.php @@ -1,11 +1,12 @@ 'DatabaseModel', + 'module_class' => 'Module\DatabaseModel', 'module_file' => 'class.model.php', 'module_name' => 'databasemodel', - 'abstract' => true, + 'abstract' => false, + 'dependencies' => array('techfuze/database'), 'name' => 'DatabaseModel', 'description' => 'Abstract type for easy database queries', @@ -15,4 +16,6 @@ return array( 'date_created' => '26-02-2015', 'date_updated' => '26-02-2015', + + 'enabled' => true, ); diff --git a/Core/Mods/router/class.router.php b/Modules/router/class.router.php similarity index 81% rename from Core/Mods/router/class.router.php rename to Modules/router/class.router.php index 1b1c7a4..cf30d71 100644 --- a/Core/Mods/router/class.router.php +++ b/Modules/router/class.router.php @@ -1,13 +1,16 @@ controllerName = ($event->controller === null || empty($event->controller) ? $this->config->main->default_controller : $event->controller); $this->function = ($event->function === null || empty($event->function) ? $this->config->main->default_function : $event->function); $this->parameters = $event->parameters; - $this->directory = ($event->directory === null || empty($event->directory) ? FUZEPATH . "/Application/Controller/" : $event->directory); - - // Load the controller - $this->loadController(); + $this->directory = ($event->directory === null || empty($event->directory) ? "Application/Controller/" : $event->directory); } /** @@ -100,6 +100,21 @@ class Router extends Bus { * @access public */ public function loadController() { + // Initate the controllerLoadEvent + $event = $this->events->fireEvent('controllerLoadEvent', + $this->route, + $this->controllerName, + $this->function, + $this->parameters, + $this->directory + ); + + $this->route = ($event->route === null ? $this->route : $event->route); + $this->controllerName = ($event->controllerName === null ? $this->controllerName : $event->controllerName); + $this->function = ($event->function === null ? $this->function : $event->function); + $this->parameters = ($event->parameters === null ? $this->parameters : $event->parameters); + $this->directory = ($event->directory === null ? $this->directory : $event->directory); + $file = $this->directory . "controller.".strtolower($this->controllerName).".php"; $this->logger->log("Loading controller from file: '".$file."'"); @@ -107,7 +122,7 @@ class Router extends Bus { if (!class_exists(ucfirst($this->controllerName))) require_once($file); - $this->controllerClass = ucfirst($this->controllerName); + $this->controllerClass = "\Controller\\" . ucfirst($this->controllerName); $this->controller = new $this->controllerClass($this->core); if (method_exists($this->controller, $this->function) || method_exists($this->controller, '__call')) { diff --git a/Modules/sections/class.sections.php b/Modules/sections/class.sections.php new file mode 100644 index 0000000..7c8222d --- /dev/null +++ b/Modules/sections/class.sections.php @@ -0,0 +1,246 @@ +config->loadConfigFile('sections', $this->getModulePath()); + + $this->logger->newLevel('Adding module sections', 'Sections'); + // Add the modules to the config + $section_register = array(); + foreach ($this->core->register as $key => $value) { + // Check if the sections value exists in the config file + if (isset($value['sections'])) { + // Check if it is empty + if (!empty($value['sections'])) { + // If not, cycle through all sections + foreach ($value['sections'] as $section_name => $section) { + // Check if the section is already set + if (isset($config->$section_name)) { + // If the priority is higher, replace the section + if ($section->priority > $config->$section_name->priority) { + $config->$section_name = $section; + $this->logger->log('Added section with higher priority: \''.$section_name.'\'', 'Sections'); + } + } else { + $config->$section_name = $section; + $this->logger->log('Added section: \''.$section_name.'\'', 'Sections'); + } + } + } + } + } + + // Apply the changes and log it + $this->cfg = $config; + $this->logger->log('Added all module sections to the config file', 'Sections'); + $this->logger->stopLevel(); + + // Register Events + $this->events->addListener(array($this, 'routerRouteEvent'), 'routerRouteEvent', EventPriority::LOWEST); + $this->events->addListener(array($this, 'layoutLoadEvent'), 'layoutLoadEvent', EventPriority::LOWEST); + $this->events->addListener(array($this, 'modelLoadevent'), 'modelLoadEvent', EventPriority::LOWEST); + } + + /** + * Redirects layouts to the new section + * @access public + * @param layoutLoadEvent Event + * @return layoutLoadEvent Event + */ + public function layoutLoadEvent($event) { + $layout_name = $event->layout; + if ($this->currentSection !== null) { + $section = $this->getSection($this->currentSection); + if ($section['module_section']) { + $mod = $this->core->loadMod($section['module_name']); + $event->directory = $mod->getModulePath() . '/Views/'; + } else { + $event->directory = $section['view_path']; + } + } + return $event; + } + + /** + * Redirects models to the new section + * @access public + * @param layoutLoadEvent Event + * @return layoutLoadEvent Event + */ + public function modelLoadEvent($event) { + $model_name = $event->model; + if ($this->currentSection !== null) { + $section = $this->getSection($this->currentSection); + if ($section['module_section']) { + $mod = $this->core->loadMod($section['module_name']); + $event->directory = $mod->getModulePath() . '/Models/'; + } else { + $event->directory = $section['model_path']; + } + } + return $event; + } + + /** + * Add a section to the config file + * @access public + * @param String section_name, name of the section + * @param Boolean module_section wether this is a module_section + * @param String module_name to use when this is a module_section + * @param String Controller_path, where to find the controllers for this section + * @param String Model_path, where to find the models for this section + * @param String View_path, where to find the views for this section + */ + public function addSection($name, $module_section = false, $module_name = null, $controller_path = null, $model_path = null, $view_path = null) { + if ($module_section) { + $m = $this->core->loadMod($module_name); + $m_dir = $m->getModulePath(); + $data = array( + 'name' => $name, + 'module_section' => $module_section, + 'module_name' => $module_name, + 'controller_path' => $m_dir . '/Controller/', + 'model_path' => $m_dir . '/Models/', + 'view_path' => $m_dir . '/Views/', + ); + } else { + $data = array( + 'name' => $name, + 'module_section' => $module_section, + 'module_name' => $module_name, + 'controller_path' => $controller_path, + 'model_path' => $model_path, + 'view_path' => $view_path, + ); + } + + $this->config->set('sections', $name, $data, $this->getModulePath()); + } + + /** + * Removes a section from the config file + * @access public + * @param String section_name, name of the section to remove + */ + public function removeSection($name) { + $this->config->set('sections', $name, null, $this->getModulePath()); + } + + /** + * Get's called on routerRouteEvent. Redirects when a section is found + * @access public + * @param routerRouteEvent Event + * @return routerRouteEvent Event + */ + public function routerRouteEvent($event) { + $name = $event->controller; + $controller = null; + $function = null; + $parameters = array(); + $section = $this->{$name}; + + if ($section !== null) { + // Section found + $this->logger->log("Section found with name: '".$name."'", 'Sections'); + $this->currentSection = $name; + + // Logic here, first for module sections + if ($section['module_section']) { + $mod = $this->core->loadMod($section['module_name']); + $event->directory = $mod->getModulePath() . "/Controller/"; + } else { + // Now for regular sections + $event->directory = $section['controller_path']; + } + + // Move the path so it matches the new regime + if (count($event->parameters) > 0) { + $function = $event->parameters[0]; + $parameters = array_slice($event->parameters, 1); + } else { + $function = $this->config->main->default_function; + } + + // And finally set the controller, if no parameters are set, load the default function + $controller = (!empty($event->function) ? $event->function : $this->config->main->default_controller ); + } else { + $this->logger->log("No section was found with name: '".$name."'", 'Sections'); + } + + if($controller !== null)$event->controller = $controller; + if($function !== null)$event->function = $function; + if(count($parameters) !== 0)$event->parameters = $parameters; + + return $event; + } + + /** + * Load a section file + * @access public + * @param String section name + * @return Array Section + */ + public function getSection($name) { + if (isset($this->cfg->$name)) { + return $this->cfg->$name; + } + } + + /** + * Retrieves section information from the config file + * @access public + * @param String section_name, name of the section + * @return Array section_information or null + */ + public function __get($name){ + // Something given? + if(empty($name)) { + // Currently in a section? + if($this->currentSection !== null){ + // Return that one then + return $this->cfg[$this->currentSection]; + } + return null; + } + + // Return element $name of the config file + if (isset($this->cfg->$name)) { + $section = $this->cfg->$name; + } else { + $section = null; + } + + return $section; + } + +} + +?> \ No newline at end of file diff --git a/Modules/sections/config.sections.php b/Modules/sections/config.sections.php new file mode 100644 index 0000000..9dd0e94 --- /dev/null +++ b/Modules/sections/config.sections.php @@ -0,0 +1 @@ + 'Module\Sections\Main', + 'module_file' => 'class.sections.php', + 'module_name' => 'Sections', + + 'abstract' => false, + 'dependencies' => array(), + 'events' => array('routerRouteEvent', 'layoutLoadEvent', 'modelLoadEvent'), + + 'name' => 'FuzeWorks Sections', + 'description' => 'Submodules for FuzeWorks', + 'author' => 'TechFuze', + 'version' => '1.0.0', + 'website' => 'http://fuzeworks.techfuze.net/', + + 'date_created' => '29-04-2015', + 'date_updated' => '29-04-2015', + + 'enabled' => true, +);