Router and Logger merge with GF, and many more CI Tests
This commit is contained in:
parent
b4a574dadf
commit
dd93772704
8
Application/Config/config.routes.php
Normal file
8
Application/Config/config.routes.php
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?php
|
||||||
|
return array(
|
||||||
|
|
||||||
|
// '/^alias(|\-(?P<function>.*?))$/' => array(
|
||||||
|
// 'controller' => 'home'
|
||||||
|
// ),
|
||||||
|
'/^(?P<controller>.*?)(|\/(?P<function>.*?)(|\/(?P<parameters>.*?)))$/',
|
||||||
|
);
|
@ -11,13 +11,6 @@ class Standard extends Controller {
|
|||||||
public function index($path = null) {
|
public function index($path = null) {
|
||||||
$this->layout->view('maintenance');
|
$this->layout->view('maintenance');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function not_found($path = null) {
|
|
||||||
$this->logger->http_error(404);
|
|
||||||
$this->layout->assign('page', $_SERVER['REQUEST_URI']);
|
|
||||||
$this->layout->assign('mail', $this->config->main->administrator_mail);
|
|
||||||
$this->layout->view('404');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
41
Core/Events/event.routerLoadCallableEvent.php
Normal file
41
Core/Events/event.routerLoadCallableEvent.php
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use \FuzeWorks\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class routerLoadCallableEvent
|
||||||
|
*
|
||||||
|
* Called when a callable is about to be loaded
|
||||||
|
*
|
||||||
|
* @package System\Events
|
||||||
|
*/
|
||||||
|
class routerLoadCallableEvent extends Event{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var callable The callable
|
||||||
|
*/
|
||||||
|
public $callable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The controller-part of the route
|
||||||
|
*/
|
||||||
|
public $controller = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The function-part of the route
|
||||||
|
*/
|
||||||
|
public $function = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The parameter-part of the route
|
||||||
|
*/
|
||||||
|
public $parameters = null;
|
||||||
|
|
||||||
|
public function init($callable, $controller, $function, $parameters){
|
||||||
|
|
||||||
|
$this->callable = $callable;
|
||||||
|
$this->controller = $controller;
|
||||||
|
$this->function = $function;
|
||||||
|
$this->parameters = $parameters;
|
||||||
|
}
|
||||||
|
}
|
@ -2,18 +2,28 @@
|
|||||||
|
|
||||||
use \FuzeWorks\Event;
|
use \FuzeWorks\Event;
|
||||||
|
|
||||||
class RouterRouteEvent extends Event {
|
/**
|
||||||
|
* Class routerRouteEvent
|
||||||
|
*
|
||||||
|
* Fired after the router has extracted the path
|
||||||
|
*
|
||||||
|
* @package System\Events
|
||||||
|
*/
|
||||||
|
class routerRouteEvent extends Event{
|
||||||
|
|
||||||
public $controller;
|
/**
|
||||||
public $function;
|
* @var array The routing table
|
||||||
public $parameters;
|
*/
|
||||||
public $directory;
|
public $routes;
|
||||||
|
|
||||||
public function init($controller, $function, $parameters){
|
/**
|
||||||
$this->controller = $controller;
|
* @var boolean Whether the callable will be loaded directly after or not
|
||||||
$this->function = $function;
|
*/
|
||||||
$this->parameters = $parameters;
|
public $loadCallable;
|
||||||
|
|
||||||
|
public function init($routes, $loadCallable){
|
||||||
|
|
||||||
|
$this->routes = $routes;
|
||||||
|
$this->loadCallable = $loadCallable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
|
25
Core/Events/event.routerSetPathEvent.php
Normal file
25
Core/Events/event.routerSetPathEvent.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use \FuzeWorks\Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class routerSetPathEvent
|
||||||
|
*
|
||||||
|
* Fired when the router's path is changing
|
||||||
|
*
|
||||||
|
* @package System\Events
|
||||||
|
*/
|
||||||
|
class routerSetPathEvent extends Event{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The new path
|
||||||
|
*/
|
||||||
|
public $path;
|
||||||
|
|
||||||
|
public function init($path){
|
||||||
|
|
||||||
|
$this->path = $path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
?>
|
@ -6,7 +6,7 @@ abstract class Bus {
|
|||||||
protected $core;
|
protected $core;
|
||||||
protected $mods;
|
protected $mods;
|
||||||
|
|
||||||
protected $library;
|
protected $router;
|
||||||
protected $config;
|
protected $config;
|
||||||
protected $logger;
|
protected $logger;
|
||||||
protected $models;
|
protected $models;
|
||||||
@ -22,6 +22,7 @@ abstract class Bus {
|
|||||||
$this->models = &$core->mods->models;
|
$this->models = &$core->mods->models;
|
||||||
$this->layout = &$core->mods->layout;
|
$this->layout = &$core->mods->layout;
|
||||||
$this->events = &$core->mods->events;
|
$this->events = &$core->mods->events;
|
||||||
|
$this->router = &$core->mods->router;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config Class
|
* Config Class
|
||||||
|
@ -2,7 +2,6 @@
|
|||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
use \stdClass;
|
use \stdClass;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FuzeWorks Core
|
* FuzeWorks Core
|
||||||
@ -38,6 +37,9 @@ class Core {
|
|||||||
$this->buildRegister();
|
$this->buildRegister();
|
||||||
$this->mods->events->buildEventRegister();
|
$this->mods->events->buildEventRegister();
|
||||||
|
|
||||||
|
// And initialize the router paths
|
||||||
|
$this->mods->router->init();
|
||||||
|
|
||||||
$event = $this->mods->events->fireEvent('coreStartEvent');
|
$event = $this->mods->events->fireEvent('coreStartEvent');
|
||||||
if ($event->isCancelled()) {
|
if ($event->isCancelled()) {
|
||||||
return true;
|
return true;
|
||||||
@ -55,6 +57,7 @@ class Core {
|
|||||||
require_once("Core/System/class.abstract.model.php");
|
require_once("Core/System/class.abstract.model.php");
|
||||||
require_once("Core/System/class.abstract.controller.php");
|
require_once("Core/System/class.abstract.controller.php");
|
||||||
require_once("Core/System/class.abstract.eventPriority.php");
|
require_once("Core/System/class.abstract.eventPriority.php");
|
||||||
|
require_once("Core/System/class.exceptions.php");
|
||||||
|
|
||||||
// Load the core classes
|
// Load the core classes
|
||||||
require_once("Core/System/class.config.php");
|
require_once("Core/System/class.config.php");
|
||||||
@ -62,6 +65,7 @@ class Core {
|
|||||||
require_once("Core/System/class.models.php");
|
require_once("Core/System/class.models.php");
|
||||||
require_once("Core/System/class.layout.php");
|
require_once("Core/System/class.layout.php");
|
||||||
require_once("Core/System/class.events.php");
|
require_once("Core/System/class.events.php");
|
||||||
|
require_once("Core/System/class.router.php");
|
||||||
|
|
||||||
// Load them
|
// Load them
|
||||||
$this->mods->events = new Events ($this);
|
$this->mods->events = new Events ($this);
|
||||||
@ -69,6 +73,7 @@ class Core {
|
|||||||
$this->mods->logger = new Logger ($this);
|
$this->mods->logger = new Logger ($this);
|
||||||
$this->mods->models = new Models ($this);
|
$this->mods->models = new Models ($this);
|
||||||
$this->mods->layout = new Layout ($this);
|
$this->mods->layout = new Layout ($this);
|
||||||
|
$this->mods->router = new Router ($this);
|
||||||
|
|
||||||
$this->loaded = true;
|
$this->loaded = true;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,6 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event Class
|
* Event Class
|
||||||
|
45
Core/System/class.exceptions.php
Normal file
45
Core/System/class.exceptions.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FuzeWorks;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Exception
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class Exception extends \Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class LayoutException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class LayoutException extends Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class RouterException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class RouterException extends Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CoreException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class CoreException extends Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class EventException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class EventException extends Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DatabaseException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class DatabaseException extends Exception{}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ModuleException
|
||||||
|
* @package System\Core
|
||||||
|
*/
|
||||||
|
class ModuleException extends Exception{}
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Layout Class
|
* Layout Class
|
||||||
@ -131,7 +130,7 @@ class Layout extends Bus {
|
|||||||
|
|
||||||
// Throw error on failure
|
// Throw error on failure
|
||||||
$this->logger->logError('Could not load view '.$directory.'/'.$vw.' :: ' . $e->getMessage(), 'Layout', __FILE__, __LINE__);
|
$this->logger->logError('Could not load view '.$directory.'/'.$vw.' :: ' . $e->getMessage(), 'Layout', __FILE__, __LINE__);
|
||||||
throw new Exception\Layout('Could not load view '.$directory.'/'.$vw);
|
throw new LayoutException('Could not load view '.$directory.'/'.$vw);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +159,7 @@ class Layout extends Bus {
|
|||||||
|
|
||||||
// Throw error on failure
|
// Throw error on failure
|
||||||
$this->logger->logError('Could not load view '.$directory.'/view.'.$view.'.tpl :: ' . $e->getMessage(), 'Layout', __FILE__, __LINE__);
|
$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');
|
throw new LayoutException('Could not load view '.$directory.'/view.'.$view.'.tpl');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace FuzeWorks;
|
namespace FuzeWorks;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Logger Class
|
* Logger Class
|
||||||
@ -16,6 +15,7 @@ class Logger extends Bus{
|
|||||||
public $warningErrors = array();
|
public $warningErrors = array();
|
||||||
public $Logs = array();
|
public $Logs = array();
|
||||||
private $print_to_screen = false;
|
private $print_to_screen = false;
|
||||||
|
public $debug = false;
|
||||||
|
|
||||||
public function __construct(&$core) {
|
public function __construct(&$core) {
|
||||||
parent::__construct($core);
|
parent::__construct($core);
|
||||||
@ -27,6 +27,7 @@ class Logger extends Bus{
|
|||||||
error_reporting(false);
|
error_reporting(false);
|
||||||
}
|
}
|
||||||
$this->events->addListener(array($this, 'shutdown'), 'coreShutdownEvent', EventPriority::LOWEST);
|
$this->events->addListener(array($this, 'shutdown'), 'coreShutdownEvent', EventPriority::LOWEST);
|
||||||
|
$this->debug = $this->config->error->debug;
|
||||||
$this->newLevel("Logger Initiated");
|
$this->newLevel("Logger Initiated");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ class Logger extends Bus{
|
|||||||
$this->logInfo($this->backtrace());
|
$this->logInfo($this->backtrace());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->mods->config->error->debug == true || $this->print_to_screen) {
|
if ($this->debug == true || $this->print_to_screen) {
|
||||||
$this->log("Parsing debug log", "Logger");
|
$this->log("Parsing debug log", "Logger");
|
||||||
$this->logToScreen();
|
$this->logToScreen();
|
||||||
}
|
}
|
||||||
@ -263,7 +264,7 @@ class Logger extends Bus{
|
|||||||
return $type = 'Unknown error: '.$type;
|
return $type = 'Unknown error: '.$type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function http_error($errno){
|
public function http_error($errno = 500, $view = true){
|
||||||
|
|
||||||
$http_codes = array(
|
$http_codes = array(
|
||||||
|
|
||||||
@ -305,6 +306,22 @@ class Logger extends Bus{
|
|||||||
$this->logError('HTTP-error '.$errno.' called', 'Logger');
|
$this->logError('HTTP-error '.$errno.' called', 'Logger');
|
||||||
$this->logInfo('Sending header HTTP/1.1 '.$errno.' '.$http_codes[$errno], 'Logger', __FILE__, __LINE__);
|
$this->logInfo('Sending header HTTP/1.1 '.$errno.' '.$http_codes[$errno], 'Logger', __FILE__, __LINE__);
|
||||||
header('HTTP/1.1 '.$errno.' '.$http_codes[$errno]);
|
header('HTTP/1.1 '.$errno.' '.$http_codes[$errno]);
|
||||||
|
|
||||||
|
// Do we want the error-view with it?
|
||||||
|
if($view == false)
|
||||||
|
return;
|
||||||
|
|
||||||
|
$view = 'errors/'.$errno;
|
||||||
|
$this->logger->log('Loading view '.$view);
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
$this->layout->view($view);
|
||||||
|
}catch(LayoutException $exception){
|
||||||
|
|
||||||
|
// No error page could be found, just echo the result
|
||||||
|
echo "<h1>$errno</h1><h3>".$http_codes[$errno]."</h3>";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
397
Core/System/class.router.php
Normal file
397
Core/System/class.router.php
Normal file
@ -0,0 +1,397 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FuzeWorks;
|
||||||
|
|
||||||
|
class Router extends Bus{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The provided path
|
||||||
|
*/
|
||||||
|
private $path = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array Routes
|
||||||
|
*/
|
||||||
|
private $routes = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|mixed The callable
|
||||||
|
*/
|
||||||
|
private $callable = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The controller object
|
||||||
|
*/
|
||||||
|
private $controller= null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var null|string The extracted controller's function name
|
||||||
|
*/
|
||||||
|
private $function = null;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array The extracted parameters
|
||||||
|
*/
|
||||||
|
private $parameters = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The constructor adds the default route to the routing table
|
||||||
|
*
|
||||||
|
* @param Core $core Reference to the core object
|
||||||
|
*/
|
||||||
|
public function __construct(&$core){
|
||||||
|
parent::__construct($core);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the default routes to the routing table
|
||||||
|
*/
|
||||||
|
public function init() {
|
||||||
|
foreach($this->config->routes as $route => $callable){
|
||||||
|
|
||||||
|
if(is_int($route)) {
|
||||||
|
|
||||||
|
$route = $callable;
|
||||||
|
$callable = array($this, 'defaultCallable');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->addRoute($route, $callable, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current routing path
|
||||||
|
*
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
public function getPath(){
|
||||||
|
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array with all the routes
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getRoutes(){
|
||||||
|
|
||||||
|
return $this->routes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the currently loaded callable
|
||||||
|
* @return null|callable
|
||||||
|
*/
|
||||||
|
public function getCallable(){
|
||||||
|
|
||||||
|
return $this->callable;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the active controller
|
||||||
|
*
|
||||||
|
* @return null|Controller The controller object
|
||||||
|
*/
|
||||||
|
public function getController() {
|
||||||
|
|
||||||
|
return $this->controller;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the name of the function
|
||||||
|
*
|
||||||
|
* @return null|string The name of the function
|
||||||
|
*/
|
||||||
|
public function getFunction() {
|
||||||
|
|
||||||
|
return $this->function;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the routing parameters
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParameters(){
|
||||||
|
|
||||||
|
return $this->parameters;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the routing parameter at given index
|
||||||
|
*
|
||||||
|
* @param int $index
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getParameter($index = 0){
|
||||||
|
|
||||||
|
$parameters = $this->parameters;
|
||||||
|
$index = ($index >= 0 ? $index : count($parameters)+$index);
|
||||||
|
|
||||||
|
if(isset($parameters[$index]))
|
||||||
|
return $parameters[$index];
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the current routing path
|
||||||
|
*
|
||||||
|
* @param string $path The routing path (e.g. a/b/c/d/e)
|
||||||
|
* @return bool|string
|
||||||
|
*/
|
||||||
|
public function setPath($path){
|
||||||
|
// Fire the event to notify our modules
|
||||||
|
$event = $this->events->fireEvent('routerSetPathEvent', $path);
|
||||||
|
|
||||||
|
// The event has been cancelled
|
||||||
|
if($event->isCancelled()){
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove double slashes
|
||||||
|
$event->path = preg_replace('@[/]+@', '/', $event->path);
|
||||||
|
|
||||||
|
// Remove first slash
|
||||||
|
if(substr($event->path, 0, 1) == '/')
|
||||||
|
$event->path = substr($event->path, 1);
|
||||||
|
|
||||||
|
// Remove trailing slash
|
||||||
|
if(substr($event->path, -1, 1) == '/')
|
||||||
|
$event->path = substr($event->path, 0, strlen($event->path)-1);
|
||||||
|
|
||||||
|
return $this->path = $event->path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a route
|
||||||
|
*
|
||||||
|
* The path will be checked before custom routes before the default route(/controller/function/param1/param2/etc)
|
||||||
|
* When the given RegEx matches the current routing-path, the callable will be called.
|
||||||
|
*
|
||||||
|
* The callable will be called with three arguments:
|
||||||
|
*
|
||||||
|
* Callable($controller, $function, $parameters)
|
||||||
|
*
|
||||||
|
* These three variables will be extracted from the named groups of your RegEx. When one or more named groups are
|
||||||
|
* not matched, they will be set to NULL. The default RegEx is:
|
||||||
|
*
|
||||||
|
* /^(?P<controller>.*?)(|\/(?P<function>.*?)(|\/(?P<parameters>.*?)))$/
|
||||||
|
*
|
||||||
|
* ^ Named group 1 ^ Named group 2 ^ Named group 3
|
||||||
|
*
|
||||||
|
* Named group 1 is named 'controller' and thus will become $controller
|
||||||
|
* Named group 2 is named 'function' and thus will become $function
|
||||||
|
* Named group 3 is named 'parameters' and thus will become $parameters
|
||||||
|
*
|
||||||
|
* You can also add aliases with the following:
|
||||||
|
*
|
||||||
|
* '/^this-is-an-alias$/' => array(
|
||||||
|
* 'controller' => 'home',
|
||||||
|
* 'function' => 'index',
|
||||||
|
* 'parameters' => array()
|
||||||
|
* ),
|
||||||
|
*
|
||||||
|
* This will link '/this-is-an-alias/ to /home/index. It is also possible to use the three named capture groups
|
||||||
|
* for the function, parameters or controllers. Like this:
|
||||||
|
*
|
||||||
|
* '/^alias(|\-(?P<function>.*?))$/' => array(
|
||||||
|
* 'controller' => 'home'
|
||||||
|
* ),
|
||||||
|
*
|
||||||
|
* This will mask '/alias' to '/home' and '/alias-test' to 'home/test'.
|
||||||
|
*
|
||||||
|
* You do not *have* to use named groups, but when you don't the arguments will be left NULL; and you will need to
|
||||||
|
* extract the information from the routing-path yourself.
|
||||||
|
*
|
||||||
|
* @param string $route This is a RegEx of the route, Every capture group will be a parameter
|
||||||
|
* @param callable $callable The callable to execute
|
||||||
|
* @param bool $prepend Whether or not to insert at the beginning of the routing table
|
||||||
|
*/
|
||||||
|
public function addRoute($route, $callable, $prepend = true){
|
||||||
|
|
||||||
|
if($prepend)
|
||||||
|
$this->routes = array($route => $callable) + $this->routes;
|
||||||
|
else
|
||||||
|
$this->routes[$route] = $callable;
|
||||||
|
|
||||||
|
$this->logger->log('Route added at '.($prepend ? 'top' : 'bottom').': "'.$route.'"');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a route from the array based on the given route
|
||||||
|
*
|
||||||
|
* @param $route string The route to remove
|
||||||
|
*/
|
||||||
|
public function removeRoute($route){
|
||||||
|
|
||||||
|
unset($this->routes[$route]);
|
||||||
|
|
||||||
|
$this->logger->log('Route removed: '.$route);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extracts the routing path to controller, function and parameters
|
||||||
|
*
|
||||||
|
* @param boolean $loadCallable Immediate load the callable after routing
|
||||||
|
*/
|
||||||
|
public function route($loadCallable = true)
|
||||||
|
{
|
||||||
|
// Default values
|
||||||
|
$callable = null;
|
||||||
|
$args = array();
|
||||||
|
|
||||||
|
$controller = null;
|
||||||
|
$function = null;
|
||||||
|
$parameters = null;
|
||||||
|
|
||||||
|
// Fire the event to notify our modules
|
||||||
|
$event = $this->events->fireEvent('routerRouteEvent', $this->routes, $loadCallable);
|
||||||
|
|
||||||
|
// The event has been cancelled
|
||||||
|
if($event->isCancelled()){
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign everything to the object to make it accessible, but let modules check it first
|
||||||
|
$this->routes = $event->routes;
|
||||||
|
$loadCallable = $event->loadCallable;
|
||||||
|
|
||||||
|
//Check the custom routes
|
||||||
|
foreach ($this->routes as $r => $c) {
|
||||||
|
|
||||||
|
//A custom route is found
|
||||||
|
if(preg_match($r, $this->path, $matches)) {
|
||||||
|
|
||||||
|
$controller = !empty($matches['controller']) ? $matches['controller'] : null;
|
||||||
|
$function = !empty($matches['function']) ? $matches['function'] : null;
|
||||||
|
$parameters = !empty($matches['parameters']) ? explode('/', $matches['parameters']) : null;
|
||||||
|
|
||||||
|
$this->logger->log('Route matched: '.$r);
|
||||||
|
|
||||||
|
$callable = $c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->callable = $callable;
|
||||||
|
$this->controller = $controller;
|
||||||
|
$this->function = $function;
|
||||||
|
$this->parameters = $parameters;
|
||||||
|
|
||||||
|
// Check if we found a callable anyway
|
||||||
|
if($this->callable === null){
|
||||||
|
|
||||||
|
$this->logger->logWarning('No routes found for given path: "'.$this->path.'"', 'Router');
|
||||||
|
$this->logger->http_error(404);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($loadCallable)
|
||||||
|
$this->loadCallable();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load and execute the callable
|
||||||
|
*/
|
||||||
|
public function loadCallable(){
|
||||||
|
|
||||||
|
$this->logger->newLevel('Loading callable');
|
||||||
|
|
||||||
|
// Fire the event to notify our modules
|
||||||
|
$event = $this->events->fireEvent('routerLoadCallableEvent', $this->callable, $this->controller, $this->function, $this->parameters);
|
||||||
|
|
||||||
|
// The event has been cancelled
|
||||||
|
if($event->isCancelled()){
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign everything to the object to make it accessible, but let modules check it first
|
||||||
|
$this->callable = $event->callable;
|
||||||
|
$this->controller = $event->controller;
|
||||||
|
$this->function = $event->function;
|
||||||
|
$this->parameters = $event->parameters;
|
||||||
|
|
||||||
|
if(!is_callable($this->callable))
|
||||||
|
if(isset($this->callable['controller'])) {
|
||||||
|
|
||||||
|
$this->controller = isset($this->callable['controller']) ? $this->callable['controller'] : $this->controller;
|
||||||
|
$this->function = isset($this->callable['function']) ? $this->callable['function'] : $this->function;
|
||||||
|
$this->parameters = isset($this->callable['parameters']) ? $this->callable['parameters'] : $this->parameters;
|
||||||
|
|
||||||
|
$this->callable = array($this, 'defaultCallable');
|
||||||
|
}else{
|
||||||
|
|
||||||
|
$this->logger->log('The given callable is not callable!', E_ERROR);
|
||||||
|
$this->error->http_error(500);
|
||||||
|
$this->logger->stopLevel();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$args = array(
|
||||||
|
$this->controller,
|
||||||
|
$this->function,
|
||||||
|
$this->parameters,
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->logger->newLevel('Calling Callable');
|
||||||
|
$this->logger->log('Controller: '. ($args[0] === null ? 'null' : $args[0]));
|
||||||
|
$this->logger->log('Function: '. ($args[1] === null ? 'null' : $args[1]));
|
||||||
|
$this->logger->log('Parameters: '. (empty($args[2]) ? '[]' : implode(', ',$args[2])));
|
||||||
|
$this->logger->stopLevel();
|
||||||
|
|
||||||
|
call_user_func_array($this->callable, $args);
|
||||||
|
|
||||||
|
$this->logger->stopLevel();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default callable
|
||||||
|
*
|
||||||
|
* This callable will do the 'old skool' routing. It will load the controllers from the controller-directory
|
||||||
|
* in the application-directory.
|
||||||
|
*/
|
||||||
|
public function defaultCallable(){
|
||||||
|
|
||||||
|
$this->logger->log('Default callable called!');
|
||||||
|
|
||||||
|
$this->controller = $this->controller === null ? $this->config->main->default_controller : $this->controller;
|
||||||
|
$this->function = $this->function === null ? $this->config->main->default_function : $this->function;
|
||||||
|
|
||||||
|
// Construct file paths and classes
|
||||||
|
$class = '\Controller\\'.ucfirst($this->controller);
|
||||||
|
$file = 'Application/Controller/controller.'.$this->controller.'.php';
|
||||||
|
|
||||||
|
$this->logger->log('Loading controller '.$class.' from file: '.$file);
|
||||||
|
|
||||||
|
// Check if the file exists
|
||||||
|
if(file_exists($file)){
|
||||||
|
|
||||||
|
if(!class_exists($class))
|
||||||
|
require $file;
|
||||||
|
|
||||||
|
$this->callable = new $class($this->core);
|
||||||
|
|
||||||
|
// Check if method exists or if there is a caller function
|
||||||
|
if(method_exists($this->callable, $this->function) || method_exists($this->callable, '__call')){
|
||||||
|
|
||||||
|
// Execute the function on the controller
|
||||||
|
$this->callable->{$this->function}($this->parameters);
|
||||||
|
}else{
|
||||||
|
|
||||||
|
// Function could not be found
|
||||||
|
$this->logger->log('Could not find function '.$this->function.' on controller '.$class);
|
||||||
|
$this->error->http_error(404);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
|
||||||
|
// Controller could not be found
|
||||||
|
$this->logger->log('Could not find controller '.$class);
|
||||||
|
$this->logger->http_error(404);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -3,7 +3,6 @@
|
|||||||
namespace Module;
|
namespace Module;
|
||||||
use \FuzeWorks\Module;
|
use \FuzeWorks\Module;
|
||||||
use \FuzeWorks\ModelServer;
|
use \FuzeWorks\ModelServer;
|
||||||
use \Exception;
|
|
||||||
|
|
||||||
class DatabaseModel extends Module implements ModelServer {
|
class DatabaseModel extends Module implements ModelServer {
|
||||||
|
|
||||||
|
@ -1,156 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
use \FuzeWorks\Module;
|
|
||||||
|
|
||||||
class Router extends Module {
|
|
||||||
|
|
||||||
public $controller = null;
|
|
||||||
public $controllerName = null;
|
|
||||||
public $function = null;
|
|
||||||
public $route = array();
|
|
||||||
public $parameters = array();
|
|
||||||
public $directory;
|
|
||||||
public $path;
|
|
||||||
|
|
||||||
public function __construct(&$core) {
|
|
||||||
parent::__construct($core);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function onLoad() {}
|
|
||||||
|
|
||||||
public function setPath($path) {
|
|
||||||
if (substr($path, -1, 1) == '/')
|
|
||||||
$path = substr($path, 0, strlen($path)-1);
|
|
||||||
|
|
||||||
return $this->path = $path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getPath() {
|
|
||||||
return $this->path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getRoute($index = null) {
|
|
||||||
if ($index === null)
|
|
||||||
return $this->route;
|
|
||||||
return $this->route[$index];
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getParameters() {
|
|
||||||
return $this->parameters;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getParameter($index = 0) {
|
|
||||||
$parameters = $this->getParameters();
|
|
||||||
return ($index >= 0 ? $parameters[$index] : $parameters[count($parameters)+$index]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extracts the routing path to controller, function and parameters
|
|
||||||
*
|
|
||||||
* 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);
|
|
||||||
|
|
||||||
// If trailing slash was given or the last element was empty: remove it
|
|
||||||
if(end($path) == ''){
|
|
||||||
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) {
|
|
||||||
$FUNCTION = $path[1];
|
|
||||||
if ($path_size >= 3) {
|
|
||||||
$PARAMS = array_slice($path, 2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Default controller, default function, no arguments
|
|
||||||
$CONTROLLER = 'standard';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fire the event to notify our modules
|
|
||||||
$event = $this->events->fireEvent('routerRouteEvent', $CONTROLLER, $FUNCTION, $PARAMS);
|
|
||||||
|
|
||||||
// The event has been cancelled
|
|
||||||
if($event->isCancelled()){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Assign everything to the object to make it accessible, but let modules check it first
|
|
||||||
$this->route = $path;
|
|
||||||
$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) ? "Application/Controller/" : $event->directory);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Load a controller
|
|
||||||
* @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."'");
|
|
||||||
|
|
||||||
if (file_exists($file)) {
|
|
||||||
if (!class_exists(ucfirst($this->controllerName)))
|
|
||||||
require_once($file);
|
|
||||||
|
|
||||||
$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')) {
|
|
||||||
$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->logger->logError("Could not load not_found function. Aborting");
|
|
||||||
// totally not found
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
?>
|
|
@ -18,5 +18,5 @@ return array(
|
|||||||
'date_created' => '29-04-2015',
|
'date_created' => '29-04-2015',
|
||||||
'date_updated' => '29-04-2015',
|
'date_updated' => '29-04-2015',
|
||||||
|
|
||||||
'enabled' => true,
|
'enabled' => false,
|
||||||
);
|
);
|
||||||
|
@ -5,9 +5,7 @@ require_once( dirname(__FILE__) . "/Core/System/class.core.php");
|
|||||||
// Load it
|
// Load it
|
||||||
$core = new \FuzeWorks\Core();
|
$core = new \FuzeWorks\Core();
|
||||||
$core->init();
|
$core->init();
|
||||||
$core->loadMod('router');
|
|
||||||
$core->mods->router->setPath( (isset($_GET['path']) ? $_GET['path'] : null) );
|
$core->mods->router->setPath( (isset($_GET['path']) ? $_GET['path'] : null) );
|
||||||
$core->mods->router->route();
|
$core->mods->router->route();
|
||||||
$core->mods->router->loadController();
|
|
||||||
|
|
||||||
?>
|
?>
|
126
tests/core_routerTest.php
Normal file
126
tests/core_routerTest.php
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class RouterTest
|
||||||
|
*
|
||||||
|
* This test will test the router
|
||||||
|
*/
|
||||||
|
class RouterTest extends CoreTestAbstract
|
||||||
|
{
|
||||||
|
public function testParsePath(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
// Act and assert
|
||||||
|
$core->mods->router->setPath('a/b/c/d/');
|
||||||
|
$this->assertEquals('a/b/c/d', $core->mods->router->getPath());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('//a//b//c');
|
||||||
|
$this->assertEquals('a/b/c', $core->mods->router->getPath());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('/');
|
||||||
|
$this->assertEquals('', $core->mods->router->getPath());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('');
|
||||||
|
$this->assertEquals('', $core->mods->router->getPath());
|
||||||
|
|
||||||
|
$core->mods->router->setPath(false);
|
||||||
|
$this->assertEquals('', $core->mods->router->getPath());
|
||||||
|
|
||||||
|
$core->mods->router->setPath(null);
|
||||||
|
$this->assertEquals('', $core->mods->router->getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testParsePath
|
||||||
|
*/
|
||||||
|
public function testDoRoute(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
// Act
|
||||||
|
$core->mods->router->setPath('a/b/c/d/');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
// Whole route
|
||||||
|
$this->assertEquals(array('a','b',array('c','d')), array($core->mods->router->getController(), $core->mods->router->getFunction(), $core->mods->router->getParameters()));
|
||||||
|
$this->assertEquals('a', $core->mods->router->getController());
|
||||||
|
$this->assertEquals('d', $core->mods->router->getParameter(-1));
|
||||||
|
$this->assertEquals(null, $core->mods->router->getParameter(5));
|
||||||
|
|
||||||
|
// Parameters
|
||||||
|
$this->assertEquals(array('c','d'), $core->mods->router->getParameters());
|
||||||
|
$this->assertEquals('c', $core->mods->router->getParameter(0));
|
||||||
|
$this->assertEquals('d', $core->mods->router->getParameter(-1));
|
||||||
|
|
||||||
|
// Function and controller
|
||||||
|
$this->assertEquals('a', $core->mods->router->getController());
|
||||||
|
$this->assertEquals('b', $core->mods->router->getFunction());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testDoRoute
|
||||||
|
*/
|
||||||
|
public function testOddRoutes(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
// Empty path
|
||||||
|
$core->mods->router->setPath(null);
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(null, $core->mods->router->getController());
|
||||||
|
|
||||||
|
// Double slashes
|
||||||
|
$core->mods->router->setPath('a///b');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(array('a','b'), array($core->mods->router->getController(), $core->mods->router->getFunction()));
|
||||||
|
|
||||||
|
// Escaped path path
|
||||||
|
$core->mods->router->setPath('/a\/b\/c/');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(array('a\\','b\\','c'), array($core->mods->router->getController(), $core->mods->router->getFunction(), $core->mods->router->getParameter(0)));
|
||||||
|
$this->assertNotEquals('a', $core->mods->router->getController());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomRoute(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$core->mods->router->addRoute('/test1/test2/', 'callable');
|
||||||
|
$this->assertArraySubset(array('/test1/test2/' => 'callable'), $core->mods->router->getRoutes());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('test1/test2');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(array('test1', 'test2'), array($core->mods->router->getController(), $core->mods->router->getFunction()));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testCustomRouteWithParameters(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$core->mods->router->addRoute('/^b\/(?P<controller>[^\/]+)\/?(?P<function>.+?)$/', 'callable');
|
||||||
|
$core->mods->router->addRoute('/e\/(?P<function>[^\/]+)/', 'callable');
|
||||||
|
$core->mods->router->addRoute('/b\/b$/', 'callable');
|
||||||
|
|
||||||
|
$core->mods->router->setPath('b/controller_a/function_a');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals('controller_a', $core->mods->router->getController());
|
||||||
|
$this->assertEquals('function_a', $core->mods->router->getFunction());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('e/function_b/c');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(null, $core->mods->router->getController());
|
||||||
|
$this->assertEquals('function_b', $core->mods->router->getFunction());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('b/b');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals(null, $core->mods->router->getController());
|
||||||
|
$this->assertEquals(null, $core->mods->router->getFunction());
|
||||||
|
|
||||||
|
$core->mods->router->setPath('a/b');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
$this->assertEquals('a', $core->mods->router->getController());
|
||||||
|
$this->assertEquals('b', $core->mods->router->getFunction());
|
||||||
|
}
|
||||||
|
}
|
82
tests/event_routerLoadCallableEventTest.php
Normal file
82
tests/event_routerLoadCallableEventTest.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class RouterLoadCallableEventTest
|
||||||
|
*/
|
||||||
|
class RouterLoadCallableEventTest extends CoreTestAbstract{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the event is fired when it should be
|
||||||
|
*/
|
||||||
|
public function test_basic(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||||
|
$mock->expects($this->once())->method('mockMethod')->with(
|
||||||
|
$this->isInstanceOf('\routerLoadCallableEvent')
|
||||||
|
);
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
//Prevent ouputting HTML
|
||||||
|
ob_start();
|
||||||
|
$core->mods->router->route();
|
||||||
|
ob_end_clean();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercept and change
|
||||||
|
* @todo Make this test correct
|
||||||
|
*/
|
||||||
|
public function test_change(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_change'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('x/y/z');
|
||||||
|
ob_start();
|
||||||
|
$core->mods->router->route(true);
|
||||||
|
ob_end_clean();
|
||||||
|
|
||||||
|
$this->assertNotNull($core->mods->router->getCallable());
|
||||||
|
$this->assertInstanceOf('\FuzeWorks\Router', $core->mods->router->getCallable()[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change title from new to other
|
||||||
|
public function listener_change(\routerLoadCallableEvent $event){
|
||||||
|
|
||||||
|
// This controller should not exist
|
||||||
|
$this->assertEquals('x', $event->controller);
|
||||||
|
$this->assertEquals('y', $event->function);
|
||||||
|
|
||||||
|
// It should exist now
|
||||||
|
$event->controller = 'home';
|
||||||
|
$event->function = 'index';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancel events
|
||||||
|
*/
|
||||||
|
public function test_cancel(){
|
||||||
|
|
||||||
|
// When the callable may execute, the callable will change to the controller
|
||||||
|
// (because '' will trigger the default callable')
|
||||||
|
$core = $this->createCore();
|
||||||
|
$core->mods->router->setPath('');
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerLoadCallableEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->route();
|
||||||
|
$this->assertTrue(is_callable($core->mods->router->getCallable()));
|
||||||
|
|
||||||
|
// When disabled, the default controller will be loaded and the callable will be overwritten
|
||||||
|
$core = $this->createCore();
|
||||||
|
$core->mods->router->setPath('');
|
||||||
|
$core->mods->router->route();
|
||||||
|
$this->assertFalse(is_callable($core->mods->router->getCallable()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel all calls
|
||||||
|
public function listener_cancel(\routerLoadCallableEvent $event){
|
||||||
|
|
||||||
|
$event->setCancelled(true);
|
||||||
|
}
|
||||||
|
}
|
45
tests/event_routerRouteEventTest.php
Normal file
45
tests/event_routerRouteEventTest.php
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class RouterRouteEventTest
|
||||||
|
*/
|
||||||
|
class RouterRouteEventTest extends CoreTestAbstract{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the event is fired when it should be
|
||||||
|
*/
|
||||||
|
public function test_basic(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||||
|
$mock->expects($this->once())->method('mockMethod')->with(
|
||||||
|
$this->isInstanceOf('\routerRouteEvent')
|
||||||
|
);
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerRouteEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancel events
|
||||||
|
*/
|
||||||
|
public function test_cancel(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerRouteEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->route(false);
|
||||||
|
|
||||||
|
$this->assertNotEquals('a', $core->mods->router->getController());
|
||||||
|
$this->assertNotEquals('b', $core->mods->router->getFunction());
|
||||||
|
$this->assertNotEquals(array('c'), $core->mods->router->getParameters());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel all calls
|
||||||
|
public function listener_cancel(\System\Events\routerRouteEvent $event){
|
||||||
|
|
||||||
|
$event->setCancelled(true);
|
||||||
|
}
|
||||||
|
}
|
83
tests/event_routerRouterSetPathEventTest.php
Normal file
83
tests/event_routerRouterSetPathEventTest.php
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Class RouterSetPathEventTest
|
||||||
|
*/
|
||||||
|
class RouterSetPathEventTest extends CoreTestAbstract{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the event is fired when it should be
|
||||||
|
*/
|
||||||
|
public function testRouterSetPathEvent(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$mock = $this->getMock('MockEvent', array('mockMethod'));
|
||||||
|
$mock->expects($this->once())->method('mockMethod')->with(
|
||||||
|
$this->isInstanceOf('\routerSetPathEvent')
|
||||||
|
);
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($mock, 'mockMethod'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Intercept and change
|
||||||
|
*/
|
||||||
|
public function testRouterSetPathEvent_change(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_change'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
|
||||||
|
$this->assertEquals('x/y/z', $core->mods->router->getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Change title from new to other
|
||||||
|
public function listener_change(\routerSetPathEvent $event){
|
||||||
|
|
||||||
|
$this->assertEquals('a/b/c', $event->path);
|
||||||
|
$event->path = 'x/y/z';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancel events
|
||||||
|
*/
|
||||||
|
public function testLayoutFunctionCallEvent_cancel(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_cancel'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('x/y/z');
|
||||||
|
|
||||||
|
$this->assertEquals('a/b/c', $core->mods->router->getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel all calls
|
||||||
|
public function listener_cancel(\routerSetPathEvent $event){
|
||||||
|
|
||||||
|
$event->setCancelled(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Do not cancel events
|
||||||
|
*/
|
||||||
|
public function testLayoutFunctionCallEvent_dontcancel(){
|
||||||
|
|
||||||
|
$core = $this->createCore();
|
||||||
|
$core->mods->router->setPath('a/b/c');
|
||||||
|
|
||||||
|
$core->mods->events->addListener(array($this, 'listener_dontcancel'), 'routerSetPathEvent', \FuzeWorks\EventPriority::NORMAL);
|
||||||
|
$core->mods->router->setPath('x/y/z');
|
||||||
|
|
||||||
|
$this->assertEquals('x/y/z', $core->mods->router->getPath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cancel all calls
|
||||||
|
public function listener_dontcancel(\routerSetPathEvent $event){
|
||||||
|
|
||||||
|
$event->setCancelled(false);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user