Changed the way sections work. From now on it is a separate module called by events. The router has been made far more efficient

This commit is contained in:
Abel Hoogeveen 2015-02-24 14:19:57 +01:00
parent 3c54281092
commit f894727e40
5 changed files with 139 additions and 93 deletions

View File

@ -1 +0,0 @@
<?php return 1 ;

View File

@ -5,13 +5,12 @@ class RouterRouteEvent extends Event {
public $controller;
public $function;
public $parameters;
public $directory;
public $directory;
public function init($controller, $function, $parameters, $directory){
public function init($controller, $function, $parameters){
$this->controller = $controller;
$this->function = $function;
$this->parameters = $parameters;
$this->directory = $directory;
}
}

View File

@ -56,58 +56,15 @@ class Router extends Bus {
array_pop($path);
}
// Load the Section config file
$cfg = $this->config->sections;
// Perform a routing check
// Prepare CONTROLLER, FUNCTION and PARAMS variables
$MODULE = false;
$CONTROLLER = "";
$FUNCTION = "";
$PARAMS = array();
$DIRECTORY = null;
// First check if anything is given
if ($path_size >= 1) {
$first = strtolower($path[0]);
// Check if a section exists with this parameter
if (isset($cfg->$first)) {
$section = $cfg->$first;
// Load the section dependencies
if (!empty($section['dependencies'])) {
for ($i=0; $i < count($section['dependencies']); $i++) {
$this->core->loadMod($section['dependencies'][$i]);
}
}
// Load the module if it is a module section
if ($section['module_section']) {
$mod_name = strtolower($section['module_name']);
$this->core->loadMod($mod_name);
$mod_path = $this->mods->{$mod_name}->getModulePath();
$DIRECTORY = $mod_path . "/Controller/";
$model_dir = $mod_path . "/Models/";
$view_dir = $mod_path . "/Views/";;
}
// Set the switch for the rest of the routing
$MODULE = true;
array_shift($path);
$path_size = count($path);
// And append the controller if found
if ($path_size >= 1) {
$CONTROLLER = $path[0];
} else {
$CONTROLLER = 'standard';
}
} else {
// Regular routing
$CONTROLLER = $path[0];
}
$CONTROLLER = $path[0];
if ($path_size >= 2) {
$FUNCTION = $path[1];
if ($path_size >= 3) {
@ -120,7 +77,7 @@ class Router extends Bus {
}
// Fire the event to notify our modules
$event = $this->events->fireEvent('routerRouteEvent', $CONTROLLER, $FUNCTION, $PARAMS, $DIRECTORY);
$event = $this->events->fireEvent('routerRouteEvent', $CONTROLLER, $FUNCTION, $PARAMS);
// The event has been cancelled
if($event->isCancelled()){
@ -130,12 +87,12 @@ class Router extends Bus {
// Assign everything to the object to make it accessible, but let modules check it first
$this->route = $path;
$this->controllerName = $event->controller === null ? $this->config->main->default_controller : $event->controller;
$this->function = $event->function === null ? $this->config->main->default_function : $event->function;
$this->function = $event->function === null || empty($this->function) ? $this->config->main->default_function : $event->function;
$this->parameters = $event->parameters;
$this->directory = $event->directory === null ? FUZEPATH . "/Application/Controller/" : $event->directory;
// Load the controller
$dir = (isset($event->directory) ? $event->directory : null);
$CLASS = $this->loadController($CONTROLLER, $FUNCTION, $PARAMS, $dir);
$this->loadController();
}
/**
@ -145,54 +102,36 @@ class Router extends Bus {
* @param String function name
* @param Array Parameters
*/
public function loadController($controller, $function = '', $params = array(), $directory = null) {
$path = ($function != '' ? array($controller, $function) : array($controller));
$path = (!empty($params) ? array_merge($path, $params) : $path );
public function loadController() {
$file = $this->directory . "controller.".strtolower($this->controllerName).".php";
$this->logger->log("Loading controller from file: '".$file."'");
// The directory where to find the controller files
$dir = (isset($directory) ? $directory : FUZEPATH . "/Application/Controller/" );
if (file_exists($file)) {
if (!class_exists(ucfirst($this->controllerName)))
require_once($file);
// Select the proper functions and parameters
$FUNCTION = ($function != '' ? $function : "index");
$PARAMS = (!empty($params) ? $params : array());
$this->controllerClass = ucfirst($this->controllerName);
$this->controller = new $this->controllerClass($this->core);
$CONTROLLER_NAME = ucfirst($controller);
$CONTROLLER_FILE = $dir . 'controller.'.strtolower($controller).".php";
if (file_exists($CONTROLLER_FILE)) {
require_once($CONTROLLER_FILE);
array_shift($path);
} else {
$CONTROLLER_NAME = 'Standard';
$CONTROLLER_FILE = $dir . 'controller.standard.php';
$FUNCTION = 'not_found';
if (file_exists($CONTROLLER_FILE)) {
require_once($CONTROLLER_FILE);
if (method_exists($this->controller, $this->function) || method_exists($this->controller, '__call')) {
$this->controller->{$this->function}($this->parameters);
} elseif (method_exists($this->controller, 'not_found')) {
// Trying last resort
$this->logger->log("Function was not found, trying Controllers not_found function");
$this->controller->not_found($this->parameters);
} else {
$this->mods->logger->logError("Failed to load standard controller. Controller loading can not continue!", "Web");
$this->mods->logger->enable();
return false;
$this->logger->logError("Could not load not_found function. Aborting");
// totally not found
}
}
$this->mods->logger->log("Loading controller <b>'".$CONTROLLER_NAME."'</b>", "Web");
$CLASS = new $CONTROLLER_NAME($this->core);
if (method_exists($CLASS, $FUNCTION) || method_exists($CLASS, '__call')) {
array_shift($path);
call_user_func(array($CLASS, $FUNCTION), $path);
} elseif (method_exists($CLASS, 'not_found')) {
$this->mods->logger->logInfo("Function from URL not found. Trying not_found function", $CONTROLLER_NAME, __FILE__, __LINE__);
call_user_func(array($CLASS, 'not_found'), $path);
} elseif (method_exists($CLASS, 'index')) {
$this->mods->logger->logInfo("Function from URL not found. Last try. Trying index function", $CONTROLLER_NAME, __FILE__, __LINE__);
call_user_func(array($CLASS, 'index'), $path);
} else {
$this->mods->logger->logError("Function from URL not found. No index function present. Ignoring request", $CONTROLLER_NAME, __FILE__, __LINE__);
$this->mods->logger->enable();
return false;
$this->logger->logError("Could not find class. Reverting to default controller not_found");
$file = $this->directory . "controller.".strtolower($this->config->main->default_controller).".php";
if (file_exists($file))
require_once($file);
$this->controllerClass = ucfirst($this->config->main->default_controller);
$this->controller = new $this->controllerClass($this->core);
$this->controller->not_found($this->parameters);
}
return $CLASS;
}
}

View File

@ -0,0 +1,108 @@
<?php
/**
* @author TechFuze
*/
class Sections extends Module {
/**
* The config holder for this module. Holds an StdObject
* @access public
* @var StdObject Module Config
*/
public $cfg;
/**
* The current section that we are using right now!
* @access private
* @var String name of section or null
*/
private $currentSection = null;
public function __construct(&$core) {
parent::__construct($core);
}
public function onLoad() {
// Load module configuration
$this->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);
}
public function eventRegisterBuild($event) {
$event->addEvent('sections', 'routerRouteEvent');
return $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) {
$this->core->loadMod($section->module_name);
$event->directory = $this->mods->{$section->module_name}->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 );
}
if($controller !== null)$event->controller = $controller;
if($function !== null)$event->function = $function;
if(count($parameters) !== 0)$event->parameters = $parameters;
return $event;
}
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];
}
// Emptiness...
return null;
}
// Return element $name of the config file
if (isset($this->cfg->$name)) {
$section = $this->cfg->$name;
} else {
$section = null;
}
return $section;
}
}
?>

View File

@ -0,0 +1 @@
<?php return 1;