Many Misc jobs:

- Modules can now be loaded using routes by adding a routes[] array to the moduleInfo.php. When it matches, the module gets loaded, and a route() function in the main class gets called. (Fixes #79)
- Composer can now load from a different file
- License headers have been added to all core files (Fixes #66)
- Table model has been renamed to sqltable. Interpret model has been removed
- layoutLoadViewEvent added
- Controllers now extend the \FuzeWorks\ControllerAbstract class
- Controllers are now in the \Application\Controller namespace
- Models are now in the \Application\Model namespace
- Events are now in the \FuzeWorks\Event namespace
- Moved some classes in a different namespace for a better overview
- Events can now properly load function listeners (fixes #81)
- Documentation added to more classes. (Partially fixes #58)
- Added replace() command to DatabaseUtils Abstract Model
- Added more unit tests for router, query and events
This commit is contained in:
Abel Hoogeveen 2015-10-11 19:14:49 +01:00
parent d982534cf0
commit 9d665e2ae1
40 changed files with 1398 additions and 305 deletions

View File

@ -1,4 +1,4 @@
<?php return array (
'enable_composer' => true
'enable_composer' => true,
'composer_autoloader' => ''
) ;

View File

@ -1,11 +1,56 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace Controller;
use \FuzeWorks\Controller;
namespace Application\Controller;
use \FuzeWorks\ControllerAbstract;
use \FuzeWorks\Layout;
class Standard extends Controller {
/**
* The default controller of FuzeWorks.
*
* This controller gets loaded when the '/' path is provided, eg. the home page.
* By default, the index function gets loaded.
*
* @package net.techfuze.fuzeworks.core
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Standard extends ControllerAbstract {
/**
* The default function of FuzeWorks.
*
* This code gets loaded for the Home page
* @param array $path The path provided by the HTTP Server
* @return void
*/
public function index($path = null) {
Layout::view('home');
}

View File

@ -1,8 +1,44 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace Model;
namespace Application\Model;
use \FuzeWorks\Model;
/**
* Example model to show how to use models
*
* This model connects to an example table if present in the database
* @package net.techfuze.fuzeworks.application.model
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Example extends Model{
public function __construct(&$core){

View File

@ -28,27 +28,25 @@
* @version Version 0.0.1
*/
namespace FuzeWorks;
namespace Application\Model;
use \FuzeWorks\Model;
/**
* Interpret Class.
* SQLTable model
*
* This Model is able to automatically select a SQL database as its source.
* @package net.techfuze.fuzeworks.core
* This model connects to the querybuilder and is used to quickly interact with SQL tables.
*
* Start building a query AND DON'T FORGET TO USE setTable() before executing the query
* @package net.techfuze.fuzeworks.application.model
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Interpret extends Model {
class Sqltable extends Model{
public function __construct(){
$this->setType('techfuze/databaseutils', 'Model');
$this->table = '';
$this->setType('core/databaseutils', 'Model');
$this->fields = '*';
$this->table = 'table';
}
public function table($name) {
$this->table = $name;
}
}
?>
}

View File

@ -1,13 +0,0 @@
<?php
namespace Model;
use \FuzeWorks\Model;
class Table extends Model{
public function __construct(){
$this->setType('core/databaseutils', 'Model');
$this->fields = '*';
$this->table = 'table';
}
}

View File

@ -1,3 +1,34 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
?>
<html>
<head>
<title>Page not found</title>

View File

@ -1,3 +1,34 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
?>
<html>
<head>
<title>FuzeWorks - Home</title>

View File

@ -1,22 +0,0 @@
<?php
use \FuzeWorks\Event;
class ControllerLoadEvent extends Event {
public $route;
public $controllerName;
public $function;
public $parameters;
public $directory;
public function init($route, $controllerName, $function, $parameters, $directory) {
$this->route = $route;
$this->controllerName = $controllerName;
$this->function = $function;
$this->parameters = $parameters;
$this->directory = $directory;
}
}
?>

View File

@ -1,15 +0,0 @@
<?php
use \FuzeWorks\Event;
class LayoutLoadEvent extends Event {
public $directory;
public $layout;
public function init($layout){
$this->layout = $layout;
}
}
?>

View File

@ -0,0 +1,77 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace FuzeWorks\Event;
use \FuzeWorks\Event;
/**
* Event that gets loaded when a view is loaded.
*
* Use this to cancel the loading of a view, or change the file or engine of a view
*
* @package net.techfuze.fuzeworks.core.event
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class LayoutLoadViewEvent extends Event {
/**
* The directory of the view to be loaded
* @var string
*/
public $directory;
/**
* The file of the view to be loaded
* @var string
*/
public $file;
/**
* The engine the file will be loaded with
* @var object
*/
public $engine;
/**
* The assigned variables to the template
* @var array
*/
public $assigned_variables;
public function init($file, $directory, $engine, $assigned_variables){
$this->file = $file;
$this->directory = $directory;
$this->engine = $engine;
$this->assigned_variables = $assigned_variables;
}
}
?>

View File

@ -1,11 +1,58 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace FuzeWorks\Event;
use \FuzeWorks\Event;
/**
* Event that gets loaded when a model is loaded.
*
* Use this to cancel the loading of a model, or change the model to be loaded
*
* @package net.techfuze.fuzeworks.core.event
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class ModelLoadEvent extends Event {
public $directory;
public $model;
/**
* The directory the model gets loaded from
* @var string|null
*/
public $directory = null;
/**
* The name of the model to be loaded
* @var string|null
*/
public $model = null;
public function init($model, $directory){
$this->model = $model;

View File

@ -1,5 +1,34 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace FuzeWorks\Event;
use \FuzeWorks\Event;
/**
@ -7,7 +36,9 @@ use \FuzeWorks\Event;
*
* Called when a callable is about to be loaded
*
* @package net.techfuze.fuzeworks.events
* @package net.techfuze.fuzeworks.core.event
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class routerLoadCallableEvent extends Event{
@ -17,25 +48,20 @@ class routerLoadCallableEvent extends Event{
public $callable;
/**
* @var null|string The controller-part of the route
* @var array The matches with the routing path
*/
public $controller = null;
public $matches = array();
/**
* @var null|string The function-part of the route
* The route which was matched
* @var null|string
*/
public $function = null;
public $route = null;
/**
* @var null|string The parameter-part of the route
*/
public $parameters = null;
public function init($callable, $matches, $route){
public function init($callable, $controller, $function, $parameters){
$this->callable = $callable;
$this->controller = $controller;
$this->function = $function;
$this->parameters = $parameters;
$this->callable = $callable;
$this->matches = $matches;
$this->route = $route;
}
}

View File

@ -1,13 +1,46 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace FuzeWorks\Event;
use \FuzeWorks\Event;
/**
* Class routerRouteEvent
*
* Fired after the router has extracted the path
* Fired after the router has extracted the path, and is about to find out what route matches the path.
*
* @package net.techfuze.fuzeworks.events
* This Event is usefull for adding routes.
*
* @package net.techfuze.fuzeworks.core.event
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class routerRouteEvent extends Event{
@ -21,9 +54,16 @@ class routerRouteEvent extends Event{
*/
public $loadCallable;
public function init($routes, $loadCallable){
/**
* The current path input to FuzeWorks
* @var null|string
*/
public $path;
public function init($routes, $loadCallable, $path){
$this->routes = $routes;
$this->loadCallable = $loadCallable;
$this->path = $path;
}
}

View File

@ -1,5 +1,34 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
namespace FuzeWorks\Event;
use \FuzeWorks\Event;
/**
@ -7,11 +36,14 @@ use \FuzeWorks\Event;
*
* Fired when the router's path is changing
*
* @package net.techfuze.fuzeworks.events
* @package net.techfuze.fuzeworks.core.event
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class routerSetPathEvent extends Event{
/**
* The path to be set to the router
* @var string The new path
*/
public $path;

View File

@ -31,11 +31,11 @@
namespace FuzeWorks;
/**
* Abstract class Controller
* Abstract class ControllerAbstract
*
* At this point does nothing, can be extended in the future to allow special controller functions
* @package net.techfuze.fuzeworks.core
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
abstract class Controller {}
abstract class ControllerAbstract {}

View File

@ -61,12 +61,14 @@ class Event {
}
}
namespace FuzeWorks\Event;
/**
* Simple event which will notify components of an event, but does not contain any data
* @package net.techfuze.fuzeworks.core
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class NotifierEvent extends Event {}
class NotifierEvent extends \FuzeWorks\Event {}
?>

View File

@ -29,8 +29,9 @@
*/
namespace FuzeWorks;
use \Iterator;
use \PDOException;
use \FuzeWorks\ORM\ConfigFileORM;
use \FuzeWorks\ORM\ConfigDatabaseORM;
/**
@ -63,7 +64,7 @@ class Config {
* @param String config file name
* @param String directory, default is Application/Config
* @throws \Exception on file not found
* @return \FuzeWorks\ConfigORM of config
* @return \FuzeWorks\ORM\ConfigORM of config
*/
public static function loadConfigFile($name, $directory = null) {
$dir = (isset($directory) ? $directory : "Application/Config/");
@ -104,13 +105,16 @@ class Config {
* Magic config getter
* @access public
* @param String config file name
* @return \FuzeWorks\ConfigORM of config
* @return \FuzeWorks\ORM\ConfigORM of config
*/
public static function get($name) {
return self::loadConfigFile($name);
}
}
namespace FuzeWorks\ORM;
use \Iterator;
/**
* Abstract ConfigORM class.
*

View File

@ -71,17 +71,23 @@ class Core {
// Load core functionality
self::loadStartupFiles();
Logger::init();
// Load the config file of the FuzeWorks core
$config = Config::get('core');
Modules::buildRegister();
Events::buildEventRegister();
// Load the logger
Logger::init();
// And initialize the router paths
Router::init();
// Build all the registers for correct operation
Modules::buildRegister();
Events::buildEventRegister();
// Load Composer
if (Config::get('core')->enable_composer) {
self::loadComposer();
if ($config->enable_composer) {
$file = ($config->composer_autoloader != '' ? $config->composer_autoloader : 'vendor/autoload.php');
self::loadComposer($file);
}
$event = Events::fireEvent('coreStartEvent');
@ -90,7 +96,10 @@ class Core {
}
}
public static function loadStartupFiles($config = array()) {
/**
* Load all the files of the FuzeWorks Framework.
*/
private static function loadStartupFiles() {
if (self::$loaded)
return;
@ -106,7 +115,7 @@ class Core {
require_once("Core/System/class.abstract.model.php");
require_once("Core/System/class.models.php");
require_once("Core/System/class.layout.php");
require_once("Core/System/class.abstract.controller.php");
require_once("Core/System/class.abstract.controllerabstract.php");
require_once("Core/System/class.router.php");
require_once("Core/System/class.abstract.module.php");
require_once("Core/System/class.modules.php");
@ -123,6 +132,11 @@ class Core {
self::$loaded = true;
}
/**
* Stop FuzeWorks and run all shutdown functions.
*
* Afterwards run the Logger shutdown function in order to possibly display the log
*/
public static function shutdown() {
Events::fireEvent('coreShutdownEvent');
Logger::shutdown();
@ -132,11 +146,16 @@ class Core {
* Load composer if it is present
* @access private
* @param String directory of composer autoload file (optional)
* @return boolean true on success, false on failure
*/
private static function loadComposer($file = "vendor/autoload.php") {
if (file_exists($file)) {
require($file);
Logger::log('Loaded Composer');
return true;
}
Logger::log('Failed to load Composer. File \''.$file.'\' not found');
return false;
}

View File

@ -110,7 +110,13 @@ class Events {
}
}
## EVENTS
/**
* Fires an Event
*
* The Event gets created, passed around and then returned to the issuer.
* @param Mixed $input Object for direct event, string for system event or notifierEvent
* @return \FuzeWorks\Event The Event
*/
public static function fireEvent($input) {
if (is_string($input)) {
// If the input is a string
@ -121,12 +127,13 @@ class Events {
$file = "Core/Events/event.".$eventName.".php";
if(file_exists($file)){
// Load the file
$eventClass = "\FuzeWorks\Event\\" . $eventClass;
require_once($file);
}else{
// No event arguments? Looks like a notify-event
if(func_num_args() == 1){
// Load notify-event-class
$eventClass = '\FuzeWorks\NotifierEvent';
$eventClass = '\FuzeWorks\Event\NotifierEvent';
}else{
// No notify-event: we tried all we could
throw new Exception("Event ".$eventName." could not be found!");
@ -178,11 +185,12 @@ class Events {
Logger::newLevel('Found listeners with priority ' . EventPriority::getPriority($priority));
//Fire the event to each listener
foreach ($listeners as $callback) {
if(!is_string($callback[0]))
Logger::log('Firing ' . get_class($callback[0]) . '->' . $callback[1]);
if (is_callable($callback)) {
Logger::newLevel('Firing function');
} elseif(!is_string($callback[0]))
Logger::newLevel('Firing ' . get_class($callback[0]) . '->' . $callback[1]);
else
Logger::log('Firing ' . join('->', $callback));
Logger::newLevel('');
Logger::newLevel('Firing ' . join('->', $callback));
try {
call_user_func($callback, $event);
} catch (ModuleException $e) {
@ -200,7 +208,12 @@ class Events {
return $event;
}
// Event Preparation:
/**
* Build a register which says which module listens for what event.
*
* This way not all modules have to be loaded in order to listen to all events.
* @return boolean true on success
*/
public static function buildEventRegister() {
$event_register = array();

View File

@ -30,6 +30,10 @@
namespace FuzeWorks;
use \Smarty;
use \FuzeWorks\TemplateEngine\JSONEngine;
use \FuzeWorks\TemplateEngine\PHPEngine;
use \FuzeWorks\TemplateEngine\SmartyEngine;
use \FuzeWorks\TemplateEngine\TemplateEngine;
/**
* Layout and Template Manager for FuzeWorks.
@ -128,10 +132,23 @@ class Layout {
self::$current_engine->setDirectory($directory);
// And run an Event to see what other parts have to say about it
$event = Events::fireEvent('layoutLoadViewEvent', $file, $directory, self::$current_engine, self::$assigned_variables);
// The event has been cancelled
if($event->isCancelled()){
return false;
}
// And refetch the data from the event
self::$current_engine = $event->engine;
self::$assigned_variables = $event->assigned_variables;
Logger::stopLevel();
// And finally run it
return self::$current_engine->get($file, self::$assigned_variables);
return self::$current_engine->get($event->file, self::$assigned_variables);
}
/**
@ -356,6 +373,9 @@ class Layout {
}
}
namespace FuzeWorks\TemplateEngine;
use \FuzeWorks\LayoutException;
/**
* Interface that all Template Engines must follow
* @package net.techfuze.fuzeworks.core

View File

@ -41,13 +41,47 @@ namespace FuzeWorks;
*/
class Logger {
/**
* Log entries which display information entries
* @var array
*/
public static $infoErrors = array();
/**
* Log entries which display critical error entries
* @var array
*/
public static $criticalErrors = array();
/**
* Log entries which display warning entries
* @var array
*/
public static $warningErrors = array();
/**
* All log entries, unsorted
* @var array
*/
public static $Logs = array();
/**
* Wether to output the log after FuzeWorks has run
* @var boolean
*/
private static $print_to_screen = false;
/**
* Wether to output the log after FuzeWorks has run, regardless of conditions
* @var boolean
*/
public static $debug = false;
/**
* Initiates the Logger.
*
* Registers the error and exception handler, when required to do so by configuration
*/
public static function init() {
// Register the error handler
if (Config::get('error')->error_reporting == true) {
@ -59,6 +93,11 @@ class Logger {
self::newLevel("Logger Initiated");
}
/**
* Function to be run upon FuzeWorks shutdown.
*
* Logs a fatal error and outputs the log when configured or requested to do so
*/
public static function shutdown() {
// Load last error if thrown
$errfile = "Unknown file";
@ -79,8 +118,8 @@ class Logger {
}
if (self::$debug == true || self::$print_to_screen) {
self::log("Parsing debug log", "Logger");
self::logToScreen();
self::log("Parsing debug log");
echo self::logToScreen();
}
}
@ -126,6 +165,10 @@ class Logger {
self::logError("Exception thrown: " . $message . " | " . $code, null, $file, $line);
}
/**
* Output the entire log to the screen. Used for debugging problems with your code.
* @return String Output of the log
*/
public static function logToScreen() {
// Send a screenLogEvent, allows for new screen log designs
$event = Events::fireEvent('screenLogEvent');
@ -134,7 +177,7 @@ class Logger {
}
// Otherwise just load it
echo '<h3>FuzeWorks debug log</h3>';
$string = '<h3>FuzeWorks debug log</h3>';
$layer = 0;
for($i = 0; $i < count(self::$Logs); $i++){
@ -142,23 +185,29 @@ class Logger {
if($log['type'] == 'LEVEL_START'){
$layer++;
$color = 255-($layer*25);
echo '<div style="background: rgb(188 , 232 ,'.$color.');border: 1px black solid;margin: 5px 0;padding: 5px 20px;">';
echo '<div style="font-weight: bold; font-size: 11pt;">'.$log['message'].'<span style="float: right">'.(!empty($log['runtime']) ? "(".round($log['runtime']*1000, 4).'ms)' : "").'</span></div>';
$string .= '<div style="background: rgb(188 , 232 ,'.$color.');border: 1px black solid;margin: 5px 0;padding: 5px 20px;">';
$string .= '<div style="font-weight: bold; font-size: 11pt;">'.$log['message'].'<span style="float: right">'.(!empty($log['runtime']) ? "(".round($log['runtime']*1000, 4).'ms)' : "").'</span></div>';
} elseif ($log['type'] == "LEVEL_STOP") {
$layer--;
echo "</div>";
$string .= "</div>";
} elseif ($log['type'] == "ERROR") {
echo '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt; background-color:#f56954;">['.$log['type'].']'.(!empty($log['context']) && is_string($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'
$string .= '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt; background-color:#f56954;">['.$log['type'].']'.(!empty($log['context']) && is_string($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'
<span style="float: right">'.(!empty($log['logFile']) ? $log['logFile'] : "")." : ".(!empty($log['logLine']) ? $log['logLine'] : "").'('.round($log['runtime']*1000, 4).' ms)</span></div>';
} elseif ($log['type'] == "WARNING") {
echo '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt; background-color:#f39c12;">['.$log['type'].']'.(!empty($log['context']) && is_string($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'
$string .= '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt; background-color:#f39c12;">['.$log['type'].']'.(!empty($log['context']) && is_string($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'
<span style="float: right">'.(!empty($log['logFile']) ? $log['logFile'] : "")." : ".(!empty($log['logLine']) ? $log['logLine'] : "").'('.round($log['runtime']*1000, 4).' ms)</span></div>';
} elseif ($log['type'] == "INFO") {
echo '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt;">'.(!empty($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'<span style="float: right">('.round($log['runtime']*1000, 4).' ms)</span></div>';
$string .= '<div style="'.($layer == 0 ? 'padding-left: 21px;' : "").'font-size: 11pt;">'.(!empty($log['context']) ? '<u>['.$log['context'].']</u>' : "").' '.$log["message"].'<span style="float: right">('.round($log['runtime']*1000, 4).' ms)</span></div>';
}
}
return $string;
}
/**
* Backtrace a problem to the source using the trace of an Exception
* @return string HTML backtrace
*/
public static function backtrace() {
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
@ -179,11 +228,26 @@ class Logger {
/* =========================================LOGGING METHODS==============================================================*/
/**
* Create a information log entry
* @param String $msg The information to be logged
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function log($msg, $mod = null, $file = 0, $line = 0) {
self::logInfo($msg, $mod, $file, $line);
}
/**
* Create a information log entry
* @param String $msg The information to be logged
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function logInfo($msg, $mod = null, $file = 0, $line = 0) {
$LOG = array('type' => 'INFO',
'message' => (!is_null($msg) ? $msg : ""),
@ -196,6 +260,14 @@ class Logger {
self::$Logs[] = $LOG;
}
/**
* Create a error log entry
* @param String $msg The information to be logged
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function logError($msg, $mod = null, $file = 0, $line = 0) {
$LOG = array('type' => 'ERROR',
'message' => (!is_null($msg) ? $msg : ""),
@ -208,6 +280,14 @@ class Logger {
self::$Logs[] = $LOG;
}
/**
* Create a warning log entry
* @param String $msg The information to be logged
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function logWarning($msg, $mod = null, $file = 0, $line = 0) {
$LOG = array('type' => 'WARNING',
'message' => (!is_null($msg) ? $msg : ""),
@ -220,6 +300,14 @@ class Logger {
self::$Logs[] = $LOG;
}
/**
* Create a new Level log entry. Used to categorise logs
* @param String $msg The name of the new level
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function newLevel($msg, $mod = null, $file = null, $line = null) {
$LOG = array('type' => 'LEVEL_START',
'message' => (!is_null($msg) ? $msg : ""),
@ -231,6 +319,14 @@ class Logger {
self::$Logs[] = $LOG;
}
/**
* Create a stop Level log entry. Used to close log categories
* @param String $msg The name of the new level
* @param String $mod The name of the module
* @param String $file The file where the log occured
* @param integer $line The line where the log occured
* @return void
*/
public static function stopLevel($msg = null, $mod = null, $file = null, $line = null) {
$LOG = array('type' => 'LEVEL_STOP',
'message' => (!is_null($msg) ? $msg : ""),
@ -292,6 +388,11 @@ class Logger {
return $type = 'Unknown error: '.$type;
}
/**
* Calls an HTTP error, sends it as a header, and loads a template if required to do so.
* @param integer $errno HTTP error code
* @param boolean $view true to view error on website
*/
public static function http_error($errno = 500, $view = true){
$http_codes = array(
@ -331,22 +432,22 @@ class Logger {
511 => 'Network Authentication Required'
);
self::logError('HTTP-error '.$errno.' called', 'Logger');
self::log('Sending header HTTP/1.1 '.$errno.' '.$http_codes[$errno], 'Logger', __FILE__, __LINE__);
self::logError('HTTP-error '.$errno.' called');
self::log('Sending 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;
// Load the view
$view = 'errors/'.$errno;
self::log('Loading view '.$view);
// Try and load the view, if impossible, load HTTP code instead.
try{
Layout::view($view);
}catch(LayoutException $exception){
} catch(LayoutException $exception){
// No error page could be found, just echo the result
echo "<h1>$errno</h1><h3>".$http_codes[$errno]."</h3>";
}
@ -368,6 +469,12 @@ class Logger {
self::$print_to_screen = false;
}
/**
* Get the relative time since the framework started.
*
* Used for debugging timings in FuzeWorks
* @return int Time passed since FuzeWorks init
*/
private static function getRelativeTime() {
$startTime = STARTTIME;
$time = microtime(true) - $startTime;

View File

@ -41,10 +41,24 @@ namespace FuzeWorks;
*/
class Models {
/**
* Array of all the loaded models
* @var array
*/
private static $models_array = array();
private static $model_types = array();
private static $models_loaded = false;
/**
* Array of all the existing model types (classes)
* @var array
*/
private static $model_types = array();
/**
* Load a model.
* @param String $name Name of the model
* @param String $directory Optional directory of the model
* @return Object The Model object.
*/
public static function loadModel($name, $directory = null){
// Model load event
$event = Events::fireEvent('modelLoadEvent', $name, $directory);
@ -57,19 +71,19 @@ class Models {
self::$models_array[$name] = self::$model_types[$name];
} elseif (file_exists($file)){
require_once($file);
$model = "\Model\\" . ucfirst($name);
$model = "\Application\Model\\" . ucfirst($name);
Logger::log('Loading Model: '.$model, $model);
self::$models_array[$name] = new $model();
} else{
Logger::logWarning('The requested model: \''.$name.'\' could not be found. Loading empty model', 'Models');
require_once("Core/System/Models/model.interpret.php");
Logger::log('Loading Model: interprated databasemodel', 'Models');
$model = new Interpret();
$model->table($name);
self::$models_array[$name] = $model;
throw new ModelException("The requested model: \''.$name.'\' could not be found", 1);
}
}
/**
* Retrieve a model
* @param String $name Name of the model
* @return Object The Model object
*/
public static function get($name){
if (isset(self::$models_array[strtolower($name)])) {
return self::$models_array[strtolower($name)];

View File

@ -39,22 +39,40 @@ use \stdClass;
*/
class Modules {
/**
* A register of all the existing module headers.
*
* The module headers contain information required to loading the module
* @var Array
*/
public static $register;
/**
* An array of all the loaded modules
* @var array
*/
public static $modules = array();
/**
* An array which modules are loaded, and should not be loaded again
* An array with the names of all modules that are loaded, and should not be loaded again
* @access private
* @var Array of module names
*/
private static $loaded_modules = array();
/**
* An array which holds the routes to module to load them quickly
* @access private
* @var array
*/
private static $module_routes = array();
/**
* Retrieves a module and returns it.
* If a module is already loaded, it returns a reference to the loaded version
* @param String $name Name of the module
* @return \FuzeWorks\Module Module The module
* @throws FuzeWorks\ModuleException
* @throws \FuzeWorks\ModuleException
*/
public static function get($name) {
// Where the modules are
@ -154,6 +172,12 @@ class Modules {
}
}
/**
* Set the value of a module config or moduleInfo.php
* @param String $file File to edit
* @param String $key Key to edit
* @param Mixed $value Value to set
*/
private static function setModuleValue($file, $key, $value) {
if (file_exists($file) && is_writable($file)) {
$cfg = require($file);
@ -164,6 +188,9 @@ class Modules {
}
/**
* Add a module using a moduleInfo.php file
*
* @param String Path to moduleInfo.php file
* @throws FuzeWorks\ModuleException
*/
public static function addModule($moduleInfo_file) {
@ -210,6 +237,11 @@ class Modules {
}
/**
* Enables a module when it is disabled
*
* @access public
* @param String Module name
* @param boolean true for permanent enable
* @throws FuzeWorks\ModuleException
*/
public static function enableModule($name, $permanent = true) {
@ -246,6 +278,11 @@ class Modules {
}
/**
* Disableds a module when it is enabled
*
* @access public
* @param String Module name
* @param boolean true for permanent disable
* @throws FuzeWorks\ModuleException
*/
public static function disableModule($name, $permanent = true) {
@ -281,6 +318,12 @@ class Modules {
}
/**
* Create a register with all the module headers from all the existing modules.
*
* Used to correctly load all modules
* @return void
*/
public static function buildRegister() {
Logger::newLevel("Loading Module Headers", 'Core');
@ -321,6 +364,16 @@ class Modules {
Logger::log("&nbsp;&nbsp;&nbsp;'".$alias."' (alias of '".$name."')");
}
}
// If routes are present, add them to the router
if (isset($cfg->routes)) {
foreach ($cfg->routes as $route) {
// Create the route and callable and parse them
$callable = array('\FuzeWorks\Modules', 'moduleCallable');
Router::addRoute($route, $callable, true);
self::$module_routes[$route] = $name;
}
}
} else {
// If not, copy all the basic data so that it can be enabled in the future
$cfg2 = new StdClass();
@ -377,6 +430,38 @@ class Modules {
}
/**
* The Module Callable
*
* When a module listens for a specific routing path, this callable get's called.
* After this the module can handle the request with the route() function in the module's root directory
* @access public
* @param array Regex matches
* @return void
*/
public static function moduleCallable($matches = array()){
// First detect what module is attached to this route
Logger::newLevel('Module callable called!');
// Get the route
$route = !empty($matches['route']) ? $matches['route'] : null;
// See if the route exists
if (isset(self::$module_routes[$route])) {
Logger::log("Module '".self::$module_routes[$route]."' matched given route");
// Load the module
$mod = self::get(self::$module_routes[$route]);
unset($matches['route']);
$mod->route($matches);
} else {
Logger::logError("Route did not match known module. Fatal error");
return Logger::http_error(500);
}
Logger::stopLevel();
}
}
?>

View File

@ -34,25 +34,32 @@ use \Application\Init;
/**
* Class Router
*
* This class handles the framework's routing. The router determines which controller should be called.
* This class handles the framework's routing. The router determines which system should be loaded and called.
* The overall structure of the routing is as follows:
*
* The routes-array will hold a list of RegEx-strings. When the route-method is called, the framework will try
* to match the current path against all the RegEx's. When a RegEx matches, the linked callable will be called.
*
* The default route works as follows:
* Every module can register routes and add their own callables. By default, two callables are used:
* The defaultCallable and the moduleCallable.
*
* The defaultCallable is a traditional MVC controller loader. Loading an URL using a default route works as follows:
*
* Let's say the visitor requests /A/B/C
*
* A would be the 'controller' (default: home)
* A would be the 'controller' (default: standard)
* B would be the function to be called in the 'controller' (default: index)
* C would be the first parameter
*
* All controllers are to be placed in the /Application/controller-directory.
*
* This is the default behaviour by adding routes to the config.routes.php. It is also possible to load Modules using routes.
* To load a Module using a route, add the route to the moduleInfo.php in a routes array.
* When this route is matched, a moduleCallable gets loaded which loads the module and loads either a controller file, or a routing function.
*
* But because of this RegEx-table, modules can easily listen on completely different paths. You can, for example, make
* a module that only triggers when /admin/<controller>/<function>/.. is accessed. Or even complexer structure are
* available, e.g: /webshop/product-<controller>/view/<function>.
* a module that only triggers when /admin/<page>/<component>/.. is accessed. Or even complexer structure are
* available, e.g: /webshop/product-<id>/view/<detailID>.
*
* BE AWARE:
*
@ -72,7 +79,7 @@ use \Application\Init;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
*/
class Router{
class Router {
/**
* @var null|string The provided path
@ -90,19 +97,9 @@ class Router{
private static $callable = null;
/**
* @var null|string The extracted controller's name
* @var null|array The extracted matches from the regex
*/
private static $controller = null;
/**
* @var null|string The extracted controller's function name
*/
private static $function = null;
/**
* @var array The extracted parameters
*/
private static $parameters = array();
private static $matches = null;
/**
* The constructor adds the default route to the routing table
@ -150,50 +147,12 @@ class Router{
}
/**
* Returns the active controller's name
*
* @return null|string
* Returns all the matches with the RegEx route
* @return null|array
*/
public static function getController() {
public static function getMatches() {
return self::$controller;
}
/**
* Returns the name of the active controller's function
*
* @return null|string The name of the function
*/
public static function getFunction() {
return self::$function;
}
/**
* Returns the routing parameters
*
* @return array
*/
public static function getParameters(){
return self::$parameters;
}
/**
* Returns the routing parameter at given index
*
* @param int $index
* @return array
*/
public static function getParameter($index = 0){
$parameters = self::$parameters;
$index = ($index >= 0 ? $index : count($parameters)+$index);
if(isset($parameters[$index]))
return $parameters[$index];
return null;
return self::$matches;
}
/**
@ -214,7 +173,7 @@ class Router{
}
// Remove double slashes
$path = preg_replace('@[/]+@', '/', $path);
$path = preg_replace('@[/]+@', '/', $event->path);
// Remove first slash
if(substr($path, 0, 1) == '/')
@ -227,43 +186,17 @@ class Router{
return self::$path = $path;
}
/**
* Set the controller name
*
* Until we decide what to do with name giving of controllers/functions,
* this function stays here.
*
* @param string $controller the name of the controller
*/
public static function setController($controller){
self::$controller = $controller;
}
/**
* Set the function name
*
* Until we decide what to do with name giving of controllers/functions,
* this function stays here.
*
* @param string $function the name of the controller
*/
public static function setFunction($function){
self::$function = $function;
}
/**
* 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
*
* Callable($regex_matches = array())
*
* The variables in the array will be 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>.*?)))$/
@ -321,15 +254,17 @@ class Router{
}
/**
* Extracts the routing path to controller, function and parameters
* Extracts the routing path from the URL using the routing table.
*
* @TODO: $loadCallable might not be needed anymore
* Determines what callable should be loaded and what data matches the route regex.
*
* @todo: Add path to routerRouteEvent
* @param boolean $loadCallable Immediate load the callable when it's route matches
*/
public static function route($loadCallable = true)
{
// Fire the event to notify our modules
$event = Events::fireEvent('routerRouteEvent', self::$routes, $loadCallable);
$event = Events::fireEvent('routerRouteEvent', self::$routes, $loadCallable, self::$path);
// The event has been cancelled
if($event->isCancelled()){
@ -338,23 +273,21 @@ class Router{
}
// Assign everything to the object to make it accessible, but let modules check it first
//self::$routes = $event->routes;
//$loadCallable = $event->loadCallable;
$routes = $event->routes;
$loadCallable = $event->loadCallable;
//Check the custom routes
foreach (self::$routes as $r => $c) {
foreach ($routes as $r => $c) {
//A custom route is found
if(preg_match($r, self::$path, $matches)) {
self::$controller = !empty($matches['controller']) ? $matches['controller'] : null;
self::$function = !empty($matches['function']) ? $matches['function'] : null;
self::$parameters = !empty($matches['parameters']) ? explode('/', $matches['parameters']) : null;
if(preg_match($r, $event->path, $matches)) {
Logger::log('Route matched: '.$r);
// Add the matches to the current class
self::$matches = $matches;
self::$callable = $c;
if(!$loadCallable || !self::loadCallable())
if(!$loadCallable || !self::loadCallable($matches, $r))
break;
}
}
@ -362,34 +295,47 @@ class Router{
// Check if we found a callable anyway
if(self::$callable === null){
Logger::log('No routes found for given path: "'.self::$path.'"', E_WARNING);
Logger::log('No routes found for given path: "'.$event->path.'"', E_WARNING);
Logger::http_error(404);
return;
}
}
/**
* Load and execute the callable
* Load the callable to which the route matched.
*
* First it checks if it is possible to call the callable. If not, the default callable gets selected and a controller, function and parameters get selected.
*
* Then the arguments get prepared and finally the callable is called.
*
* @param array Preg matches with the routing path
* @param string The route that matched
* @return boolean Whether or not the callable was satisfied
* @todo add route to routerLoadCallableEvent
*/
public static function loadCallable(){
public static function loadCallable($matches = array(), $route){
Logger::newLevel('Loading callable');
// Fire the event to notify our modules
$event = Events::fireEvent('routerLoadCallableEvent', self::$callable, self::$controller, self::$function, self::$parameters);
$event = Events::fireEvent('routerLoadCallableEvent', self::$callable, $matches, $route);
// The event has been cancelled
if($event->isCancelled())
return false;
if(!is_callable(self::$callable))
// Prepare the arguments and add the route
$args = $event->matches;
$args['route'] = $event->route;
if(!is_callable($event->callable))
if(isset(self::$callable['controller'])) {
self::$controller = isset(self::$callable['controller']) ? self::$callable['controller'] : self::$controller;
self::$function = isset(self::$callable['function']) ? self::$callable['function'] : self::$function;
self::$parameters = isset(self::$callable['parameters']) ? self::$callable['parameters'] : self::$parameters;
// Reset the arguments and fetch from custom callable
$args = array();
$args['controller'] = isset(self::$callable['controller']) ? self::$callable['controller'] : (isset($matches['controller']) ? $matches['controller'] : null);
$args['function'] = isset(self::$callable['function']) ? self::$callable['function'] : (isset($matches['function']) ? $matches['function'] : null);
$args['parameters'] = isset(self::$callable['parameters']) ? self::$callable['parameters'] : (isset($matches['parameters']) ? explode('/', $matches['parameters']) : null);
self::$callable = array('\FuzeWorks\Router', 'defaultCallable');
}else{
@ -400,19 +346,14 @@ class Router{
return true;
}
$args = array(
self::$controller,
self::$function,
self::$parameters,
);
// And log the input to the logger
Logger::newLevel('Calling callable');
Logger::log('Controller: '. ($args[0] === null ? 'null' : $args[0]));
Logger::log('Function: '. ($args[1] === null ? 'null' : $args[1]));
Logger::log('Parameters: '. (empty($args[2]) ? '[]' : implode(', ',$args[2])));
foreach ($args as $key => $value) {
Logger::log($key.": ".var_export($value,true)."");
}
Logger::stopLevel();
$skip = call_user_func_array(self::$callable, $args) === false;
$skip = call_user_func_array(self::$callable, array($args)) === false;
if($skip)
Logger::log('Callable not satisfied, skipping to next callable');
@ -427,16 +368,17 @@ class Router{
* This callable will do the 'old skool' routing. It will load the controllers from the controller-directory
* in the application-directory.
*/
public static function defaultCallable(){
public static function defaultCallable($arguments = array()){
Logger::log('Default callable called!');
self::$controller = self::$controller === null ? Config::get('main')->default_controller : self::$controller;
self::$function = self::$function === null ? Config::get('main')->default_function : self::$function;
$controller = empty($arguments['controller']) ? Config::get('main')->default_controller : $arguments['controller'];
$function = empty($arguments['function']) ? Config::get('main')->default_function : $arguments['function'];
$parameters = empty($arguments['parameters']) ? null : $arguments['parameters'];
// Construct file paths and classes
$class = '\Controller\\'.ucfirst(self::$controller);
$file = 'Application/Controller/controller.'.self::$controller.'.php';
$class = '\Application\Controller\\'.ucfirst($controller);
$file = 'Application/Controller/controller.'.$controller.'.php';
Logger::log('Loading controller '.$class.' from file: '.$file);
@ -458,14 +400,14 @@ class Router{
}
// Check if method exists or if there is a caller function
if(method_exists(self::$callable, self::$function) || method_exists(self::$callable, '__call')){
if(method_exists(self::$callable, $function) || method_exists(self::$callable, '__call')){
// Execute the function on the controller
echo self::$callable->{self::$function}(self::$parameters);
echo self::$callable->{$function}($parameters);
}else{
// Function could not be found
Logger::log('Could not find function '.self::$function.' on controller '.$class);
Logger::log('Could not find function '.$function.' on controller '.$class);
Logger::http_error(404);
}
}else{

View File

@ -31,6 +31,7 @@
namespace Module\DatabaseUtils;
use \FuzeWorks\Module;
use \FuzeWorks\Modules;
use \FuzeWorks\Bus;
use \FuzeWorks\ModelServer;
use \FuzeWorks\DatabaseException;
@ -182,6 +183,23 @@ class Model {
return $queryBuilder;
}
/**
* The default table will be set to $this->table
* @see Query::replace
* @param $array Array with values
* @return Query
* @throws Exception
*/
public function replace($array){
$queryBuilder = new Query();
$queryBuilder->setTable($this->table);
call_user_func_array(array($queryBuilder, 'replace'), func_get_args());
return $queryBuilder;
}
/**
* Return latest insert id
*
@ -192,6 +210,6 @@ class Model {
}
public function __call($name, $params) {
return call_user_func_array(array($this->mods->database, $name), $params);
return call_user_func_array(array(Modules::get('core/database'), $name), $params);
}
}

View File

@ -779,7 +779,7 @@ class Query extends Module {
try{
$this->sth = Modules::get('core/database')->getConnection()->prepare($this->getSql());
$this->sth = Modules::get('core/database')->prepare($this->getSql());
if(count($this->getBinds()) === 0){
$this->sth->execute();
@ -832,7 +832,7 @@ class Query extends Module {
*/
public function getLastInsertId(){
return Modules::get('core/database')->getConnection()->lastInsertId();
return Modules::get('core/database')->lastInsertId();
}

View File

@ -99,6 +99,17 @@ class Main extends Module {
return $result;
}
/**
* Gets called when the path matches the regex of this module.
* @access public
* @param array Regex matches
* @return void
*/
public function route($matches = array()) {
// Just print the inputted data:
echo "<h3>Input data: ".$matches['data']."</h3>";
}
}
class ExampleEvent extends Event {

View File

@ -51,6 +51,11 @@ return array(
// Events that this module listens for. When the exampleEvent is fired, this module will be loaded so the module can handle the event
'events' => array('exampleEvent'),
// Routes that this module listens on. If the URL /example/ gets called, this module will be loaded
// Everything after /example/ will be sent to the route() function in the matches array under the 'data' key
// A route must ALWAYS have the module capturing group. Otherwise it will fail
'routes' => array('/^example(|\/(?P<data>.*?))$/'),
// The name of the module as it will be logged. This does not affect usage of the module in any way
'name' => 'FuzeWorks Example Module',

View File

@ -35,7 +35,6 @@ use \FuzeWorks\Router;
require_once( dirname(__FILE__) . "/Core/System/class.core.php");
// Load it
new Core();
Core::init();
Router::setPath( (isset($_GET['path']) ? $_GET['path'] : null) );

View File

@ -1,9 +0,0 @@
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /fw/index.php?path=$1 last;
}
}
location ~* (\.enc.cfg)$ {
return 404;
}

View File

@ -1,4 +1,33 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use FuzeWorks\Events;
/**

View File

@ -1,4 +1,32 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
// Load the abstract
use \FuzeWorks\Config;

View File

@ -1,4 +1,32 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
/**
* Class CoreTest

81
tests/core_modelTest.php Normal file
View File

@ -0,0 +1,81 @@
<?php
use \FuzeWorks\Core;
use \FuzeWorks\Models;
/**
* Class ModelTest
*
* Core model testing suite, will test basic model functionality
*
*/
class ModelTest extends CoreTestAbstract
{
/**
* Select
*/
public function testSelectSimple()
{
$query = Models::get('sqltable')->select();
$this->assertEquals('SELECT * FROM table', $query->getSql());
}
public function testSelectSimpleOneField(){
$query = Models::get('sqltable')->select('field1');
$this->assertEquals('SELECT field1 FROM table', $query->getSql());
}
public function testSelectSimpleTwoFields(){
$query = Models::get('sqltable')->select('field1', 'field2');
$this->assertEquals('SELECT field1, field2 FROM table', $query->getSql());
}
/**
* Delete
*/
public function testDeleteSimple(){
$query = Models::get('sqltable')->delete()->from('table');
$this->assertEquals('DELETE FROM table', $query->getSql());
}
/**
* Insert
*/
public function testInsertSimple(){
$query = Models::get('sqltable')->insert(array('field' => 'value'));
$this->assertEquals('INSERT INTO table (field) VALUES (?)', $query->getSql());
$this->assertEquals(array('value'), $query->getBinds());
}
public function testInsertMultiple(){
$query = Models::get('sqltable')->insert(array('field1' => 'value1', 'field2' => 'value2'), 'table');
$this->assertEquals('INSERT INTO table (field1,field2) VALUES (?,?)', $query->getSql());
$this->assertEquals(array('value1', 'value2'), $query->getBinds());
}
/**
* Replace
*/
public function testReplaceSimple(){
$query = Models::get('sqltable')->replace(array('field' => 'value'));
$this->assertEquals('REPLACE INTO table (field) VALUES (?)', $query->getSql());
$this->assertEquals(array('value'), $query->getBinds());
}
public function testReplaceMultiple(){
$query = Models::get('sqltable')->replace(array('field1' => 'value1', 'field2' => 'value2'), 'table');
$this->assertEquals('REPLACE INTO table (field1,field2) VALUES (?,?)', $query->getSql());
$this->assertEquals(array('value1', 'value2'), $query->getBinds());
}
}

View File

@ -1,4 +1,33 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Modules;
use \Module\DatabaseUtils\Query;

View File

@ -1,4 +1,33 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Core;
use \FuzeWorks\Router;
@ -42,19 +71,15 @@ class RouterTest extends CoreTestAbstract
// Assert
// Whole route
$this->assertEquals(array('a','b',array('c','d')), array(Router::getController(), Router::getFunction(), Router::getParameters()));
$this->assertEquals('a', Router::getController());
$this->assertEquals('d', Router::getParameter(-1));
$this->assertEquals(null, Router::getParameter(5));
$this->assertEquals(array('a','b','c/d'), array(Router::getMatches()['controller'], Router::getMatches()['function'], Router::getMatches()['parameters']));
$this->assertEquals('a', Router::getMatches()['controller']);
// Parameters
$this->assertEquals(array('c','d'), Router::getParameters());
$this->assertEquals('c', Router::getParameter(0));
$this->assertEquals('d', Router::getParameter(-1));
$this->assertEquals('c/d', Router::getMatches()['parameters']);
// Function and controller
$this->assertEquals('a', Router::getController());
$this->assertEquals('b', Router::getFunction());
$this->assertEquals('a', Router::getMatches()['controller']);
$this->assertEquals('b', Router::getMatches()['function']);
}
/**
@ -65,18 +90,18 @@ class RouterTest extends CoreTestAbstract
// Empty path
Router::setPath(null);
Router::route(false);
$this->assertEquals(null, Router::getController());
$this->assertEquals(null, Router::getMatches()['controller']);
// Double slashes
Router::setPath('a///b');
Router::route(false);
$this->assertEquals(array('a','b'), array(Router::getController(), Router::getFunction()));
$this->assertEquals(array('a','b'), array(Router::getMatches()['controller'], Router::getMatches()['function']));
// Escaped path path
Router::setPath('/a\/b\/c/');
Router::route(false);
$this->assertEquals(array('a\\','b\\','c'), array(Router::getController(), Router::getFunction(), Router::getParameter(0)));
$this->assertNotEquals('a', Router::getController());
$this->assertEquals(array('a\\','b\\','c'), array(Router::getMatches()['controller'], Router::getMatches()['function'], Router::getMatches()['parameters']));
$this->assertNotEquals('a', Router::getMatches()['controller']);
}
public function testCustomRoute(){
@ -93,22 +118,22 @@ class RouterTest extends CoreTestAbstract
Router::setPath('b/controller_a/function_a');
Router::route(false);
$this->assertEquals('controller_a', Router::getController());
$this->assertEquals('function_a', Router::getFunction());
$this->assertEquals('controller_a', Router::getMatches()['controller']);
$this->assertEquals('function_a', Router::getMatches()['function']);
Router::setPath('e/function_b/c');
Router::route(false);
$this->assertEquals(null, Router::getController());
$this->assertEquals('function_b', Router::getFunction());
$this->assertEquals(null, Router::getMatches()['controller']);
$this->assertEquals('function_b', Router::getMatches()['function']);
Router::setPath('b/b');
Router::route(false);
$this->assertEquals(null, Router::getController());
$this->assertEquals(null, Router::getFunction());
$this->assertEquals(null, Router::getMatches()['controller']);
$this->assertEquals(null, Router::getMatches()['function']);
Router::setPath('a/b');
Router::route(false);
$this->assertEquals('a', Router::getController());
$this->assertEquals('b', Router::getFunction());
$this->assertEquals('a', Router::getMatches()['controller']);
$this->assertEquals('b', Router::getMatches()['function']);
}
}

View File

@ -1,4 +1,33 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Core;
use \FuzeWorks\Events;
use \FuzeWorks\EventPriority;

View File

@ -0,0 +1,120 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Router;
use \FuzeWorks\EventPriority;
/**
* Class RouterLoadCallableEventTest
*/
class RouterLoadCallableEventTest extends CoreTestAbstract{
/**
* Check if the event is fired when it should be
*/
public function test_basic(){
$mock = $this->getMock('MockEvent', array('mockMethod'));
$mock->expects($this->once())->method('mockMethod');
Router::setPath('/');
Events::addListener(function($event){$event->setCancelled(true);}, 'layoutLoadViewEvent', EventPriority::HIGHEST);
Events::addListener(array($mock, 'mockMethod'), 'routerLoadCallableEvent', EventPriority::NORMAL);
//Prevent ouputting HTML
ob_start();
Router::route();
ob_end_clean();
}
/**
* Intercept and change
*/
public function test_change(){
Events::addListener(function($event){$event->setCancelled(true);}, 'layoutLoadViewEvent', EventPriority::HIGHEST);
Events::addListener(array($this, 'listener_change'), 'routerLoadCallableEvent', EventPriority::NORMAL);
Router::setPath('x/y/z');
Router::route(true);
Events::$listeners = array();
Events::addListener(function($event){$event->setCancelled(true);}, 'layoutLoadViewEvent', EventPriority::HIGHEST);
Events::addListener(array($this, 'listener_change'), 'routerLoadCallableEvent', EventPriority::NORMAL);
Router::setPath('x/y/z');
Router::route(true);
$this->assertNotNull(Router::getCallable());
$this->assertInstanceOf('\Application\Controller\Standard', Router::getCallable());
}
// Change title from new to other
public function listener_change($event){
// This controller should not exist
$this->assertEquals('x', $event->matches['controller']);
$this->assertEquals('y', $event->matches['function']);
// It should exist now
$event->matches['controller'] = 'standard';
$event->matches['function'] = 'index';
return $event;
}
/**
* Cancel events
*/
public function test_cancel(){
ob_start();
// When the callable may execute, the callable will change to the controller
// (because '' will trigger the default callable)
Router::setPath('');
Events::addListener(array($this, 'listener_cancel'), 'routerLoadCallableEvent', EventPriority::NORMAL);
Router::route();
$this->assertTrue(is_callable(Router::getCallable()));
// When disabled, the default controller will be loaded and the callable will be overwritten
// Remove the listener
Events::$listeners = array();
Events::addListener(function($event){$event->setCancelled(true);}, 'layoutLoadViewEvent', EventPriority::HIGHEST);
Router::route();
$this->assertFalse(is_callable(Router::getCallable()));
ob_end_clean();
}
// Cancel all calls
public function listener_cancel($event){
$event->setCancelled(true);
}
}

View File

@ -0,0 +1,73 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Router;
use \FuzeWorks\EventPriority;
/**
* Class RouterRouteEventTest
*/
class RouterRouteEventTest extends CoreTestAbstract{
/**
* Check if the event is fired when it should be
*/
public function test_basic(){
$mock = $this->getMock('MockEvent', array('mockMethod'));
$mock->expects($this->once())->method('mockMethod');
Events::addListener(array($mock, 'mockMethod'), 'routerRouteEvent', EventPriority::NORMAL);
Router::setPath('a/b/c');
Router::route(false);
}
/**
* Cancel events
*/
public function test_cancel(){
Router::setPath('x/y/z');
Events::addListener(array($this, 'listener_cancel'), 'routerRouteEvent', EventPriority::NORMAL);
Router::route(false);
$this->assertNotEquals('x', Router::getMatches()['controller']);
$this->assertNotEquals('y', Router::getMatches()['function']);
$this->assertNotEquals('z', Router::getMatches()['parameters']);
}
// Cancel all calls
public function listener_cancel($event){
$event->setCancelled(true);
}
}

View File

@ -0,0 +1,108 @@
<?php
/**
* FuzeWorks
*
* The FuzeWorks MVC PHP FrameWork
*
* Copyright (C) 2015 TechFuze
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
* @copyright Copyright (c) 1996 - 2015, Free Software Foundation, Inc. (http://www.fsf.org/)
* @license http://opensource.org/licenses/GPL-3.0 GPLv3 License
* @link http://fuzeworks.techfuze.net
* @since Version 0.0.1
* @version Version 0.0.1
*/
use \FuzeWorks\Events;
use \FuzeWorks\Router;
use \FuzeWorks\EventPriority;
/**
* Class RouterSetPathEventTest
*/
class RouterSetPathEventTest extends CoreTestAbstract{
/**
* Check if the event is fired when it should be
*/
public function testRouterSetPathEvent(){
$mock = $this->getMock('MockEvent', array('mockMethod'));
$mock->expects($this->once())->method('mockMethod');
Events::addListener(array($mock, 'mockMethod'), 'routerSetPathEvent', EventPriority::NORMAL);
Router::setPath('a/b/c');
}
/**
* Intercept and change
*/
public function testRouterSetPathEvent_change(){
Events::addListener(array($this, 'listener_change'), 'routerSetPathEvent', EventPriority::NORMAL);
Router::setPath('a/b/c');
$this->assertEquals('x/y/z', Router::getPath());
}
// Change title from new to other
public function listener_change($event){
$this->assertEquals('a/b/c', $event->path);
$event->path = 'x/y/z';
}
/**
* Cancel events
*/
public function testLayoutFunctionCallEvent_cancel(){
Router::setPath('a/b/c');
Events::addListener(array($this, 'listener_cancel'), 'routerSetPathEvent', EventPriority::NORMAL);
Router::setPath('x/y/z');
$this->assertEquals('a/b/c', Router::getPath());
}
// Cancel all calls
public function listener_cancel($event){
$event->setCancelled(true);
}
/**
* Do not cancel events
*/
public function testLayoutFunctionCallEvent_dontcancel(){
Router::setPath('a/b/c');
Events::addListener(array($this, 'listener_dontcancel'), 'routerSetPathEvent', EventPriority::NORMAL);
Router::setPath('x/y/z');
$this->assertEquals('x/y/z', Router::getPath());
}
// Cancel all calls
public function listener_dontcancel($event){
$event->setCancelled(false);
}
}