Merge branch 'master' into 'master'

Sections

Implemented sections and various fixes on the router

See merge request !3
This commit is contained in:
Abel Hoogeveen 2015-02-24 15:30:41 +01:00
commit e7fdee40d5
6 changed files with 151 additions and 49 deletions

View File

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

View File

@ -47,6 +47,7 @@ class Router extends Bus {
* Path structure: /controller/function/par1/par2...
*/
public function route(){
// Retrieve the path and convert it to a proper format
$path = (!empty($this->getPath()) ? explode('/', preg_replace('#/+#','/',$this->getPath())) : array());
$path_size = count($path);
@ -55,9 +56,13 @@ class Router extends Bus {
array_pop($path);
}
// Perform a routing check
// Prepare CONTROLLER, FUNCTION and PARAMS variables
$CONTROLLER = "";
$FUNCTION = "";
$PARAMS = array();
// First check if anything is given
if ($path_size >= 1) {
$CONTROLLER = $path[0];
if ($path_size >= 2) {
@ -81,67 +86,54 @@ 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->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
$dir = (isset($event->directory) ? $event->directory : null);
$this->loadController($CONTROLLER, $FUNCTION, $PARAMS, $dir);
$this->loadController();
}
/**
* Load a controller
* @access public
* @param String controller name
* @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");
// Add the function to the parameters just because it's usefull
array_unshift($this->parameters, $this->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);
// Add the function to the parameters just because it's usefull
array_unshift($this->parameters, $this->function);
$this->controller->not_found($this->parameters);
}
}
}

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;

View File

@ -29,7 +29,6 @@ class Core {
// Load core abstracts
require_once(FUZESYSPATH . "/class.abstract.bus.php");
//require_once(FUZESYSPATH . "/class.abstract.model.php");
require_once(FUZESYSPATH . "/class.abstract.event.php");
require_once(FUZESYSPATH . "/class.abstract.module.php");
require_once(FUZESYSPATH . "/class.abstract.eventPriority.php");

View File

@ -72,7 +72,7 @@ class Layout extends Bus {
return $this->title;
}
public function view($view = "default", $dir = "") {
public function view($view = "default", $dir = null) {
// Chech if Smarty is loaded
if (!$this->loaded)
$this->load();
@ -98,13 +98,14 @@ class Layout extends Bus {
}
// Set the directory
$dir = ($dir == "" ? FUZEPATH . "/Application/" . '/Views' : $dir);
$dir = (!isset($dir) ? FUZEPATH . "/Application/" . '/Views' : $dir);
$this->Smarty['main']->setTemplateDir($dir);
// 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);