From d086923f832ee1b78098d13924fc106a2706347b Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Fri, 14 Jul 2017 16:11:35 +0200 Subject: [PATCH] Modernized the framework to work with PHP 7.1. - Events have been moved and are now loaded through autoloading. - FuzeWorks\Configurator now supports a setDebugEmail() method which will pass an email address to Tracy. - The FuzeWorks Module system has been removed - FuzeWorks\Layout is no longer a static class - Fixed multiple bugs where paths were case-sensitive and did not work under Linux - Latte engine is now able to return layouts and not only view them --- composer.json | 8 +- src/FuzeWorks/Configurator.php | 17 +- src/FuzeWorks/Core.php | 13 +- .../Event/DatabaseLoadDriverEvent.php | 85 +++ .../Event/DatabaseLoadForgeEvent.php | 77 +++ src/FuzeWorks/Event/DatabaseLoadUtilEvent.php | 77 +++ .../Event/HelperLoadEvent.php} | 0 .../Event/LayoutLoadViewEvent.php} | 0 .../Event/ModelLoadEvent.php} | 0 .../Event/RouterLoadCallableEvent.php} | 0 .../Event/RouterLoadControllerEvent.php} | 0 .../Event/RouterRouteEvent.php} | 0 src/FuzeWorks/Events.php | 105 ++- src/FuzeWorks/Exception/ModuleException.php | 45 -- src/FuzeWorks/Factory.php | 1 - src/FuzeWorks/Layout.php | 183 +++--- src/FuzeWorks/Logger.php | 15 +- src/FuzeWorks/LoggerTracyBridge.php | 4 +- src/FuzeWorks/Module.php | 204 ------ src/FuzeWorks/Modules.php | 598 ------------------ src/FuzeWorks/TemplateEngine/LatteEngine.php | 2 + src/FuzeWorks/URI.php | 2 + 22 files changed, 413 insertions(+), 1023 deletions(-) create mode 100644 src/FuzeWorks/Event/DatabaseLoadDriverEvent.php create mode 100644 src/FuzeWorks/Event/DatabaseLoadForgeEvent.php create mode 100644 src/FuzeWorks/Event/DatabaseLoadUtilEvent.php rename src/{Events/event.helperLoadEvent.php => FuzeWorks/Event/HelperLoadEvent.php} (100%) rename src/{Events/event.layoutLoadViewEvent.php => FuzeWorks/Event/LayoutLoadViewEvent.php} (100%) rename src/{Events/event.modelLoadEvent.php => FuzeWorks/Event/ModelLoadEvent.php} (100%) rename src/{Events/event.routerLoadCallableEvent.php => FuzeWorks/Event/RouterLoadCallableEvent.php} (100%) rename src/{Events/event.routerLoadControllerEvent.php => FuzeWorks/Event/RouterLoadControllerEvent.php} (100%) rename src/{Events/event.routerRouteEvent.php => FuzeWorks/Event/RouterRouteEvent.php} (100%) delete mode 100644 src/FuzeWorks/Exception/ModuleException.php delete mode 100644 src/FuzeWorks/Module.php delete mode 100644 src/FuzeWorks/Modules.php diff --git a/composer.json b/composer.json index 583e695..ed4a37f 100644 --- a/composer.json +++ b/composer.json @@ -24,11 +24,11 @@ "tracy/tracy": "Allows for extensive debugging" }, "require-dev": { - "phpunit/phpunit": "5.3.*", - "apigen/apigen": "^4.1", + "phpunit/phpunit": "6.2.*", "mikey179/vfsStream": "1.1.*", - "smarty/smarty": "~3.1", - "latte/latte": "*" + "smarty/smarty": "3.1.*", + "latte/latte": "2.4.*", + "tracy/tracy": "2.4.*" }, "autoload": { "psr-4": { diff --git a/src/FuzeWorks/Configurator.php b/src/FuzeWorks/Configurator.php index e05899e..f66bc37 100644 --- a/src/FuzeWorks/Configurator.php +++ b/src/FuzeWorks/Configurator.php @@ -151,6 +151,15 @@ class Configurator return $this; } + /** + * Set the email to send logs to from Tracy + * @param string + */ + public function setDebugEmail($email) + { + $this->parameters['debugEmail'] = $email; + } + /** * Detects debug mode by IP address. * @param string|array IP addresses or computer names whitelist detection @@ -180,7 +189,7 @@ class Configurator * Due to the static nature of FuzeWorks, this is not yet possible. * When issue #101 is completed, this should be resolved. * - * @return void + * @return FuzeWorks\Factory */ public function createContainer() { @@ -204,10 +213,16 @@ class Configurator if (class_exists('Tracy\Debugger', true)) { Debugger::enable(!$this->parameters['debugMode'], realpath($this->parameters['logDir'])); + if (isset($this->parameters['debugEmail'])) + { + Debugger::$email = $this->parameters['debugEmail']; + } Logger::$useTracy = true; } // Then load the framework Core::init(); + + return Factory::getInstance(); } } \ No newline at end of file diff --git a/src/FuzeWorks/Core.php b/src/FuzeWorks/Core.php index 0ed5fd5..c864284 100644 --- a/src/FuzeWorks/Core.php +++ b/src/FuzeWorks/Core.php @@ -100,7 +100,7 @@ class Core register_shutdown_function(array('\FuzeWorks\Core', 'shutdown')); // Load core functionality - new Factory(); + $container = new Factory(); // Load the config file of the FuzeWorks core $config = Config::get('core'); @@ -111,15 +111,6 @@ class Core Events::disable(); } - // Build all the registers for correct operation, if modules are enabled - if ($config->enable_modules) - { - Modules::buildRegister($config->registry_caching, - $config->registry_caching_method, - $config->registry_caching_time - ); - } - // And fire the coreStartEvent $event = Events::fireEvent('coreStartEvent'); if ($event->isCancelled()) { @@ -127,7 +118,7 @@ class Core } // And initialize multiple classes - Layout::init(); + $container->layout->init(); Language::init(); } diff --git a/src/FuzeWorks/Event/DatabaseLoadDriverEvent.php b/src/FuzeWorks/Event/DatabaseLoadDriverEvent.php new file mode 100644 index 0000000..2b6fc82 --- /dev/null +++ b/src/FuzeWorks/Event/DatabaseLoadDriverEvent.php @@ -0,0 +1,85 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2017, 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://techfuze.net/fuzeworks + * @since Version 1.0.4 + * + * @version Version 1.0.4 + */ + +namespace FuzeWorks\Event; + +use FuzeWorks\Event; + +/** + * Event that gets loaded when a database driver is loaded + * + * Use this to cancel the loading of a database, or change the provided database + * + * @author Abel Hoogeveen + * @copyright Copyright (c) 2013 - 2017, Techfuze. (http://techfuze.net) + */ +class DatabaseLoadDriverEvent extends Event +{ + /** + * A possible database that can be loaded. + * + * Provide a database in this variable and it will be loaded. It shall be identified as default if + * the parameters variable is empty. If there is a string in parameters this database shall be identified as + * such. + * + * @var FW_DB|null + */ + public $database = null; + + /** + * Parameters of the database to be loaded + * + * @var string + */ + public $parameters; + + /** + * Whether a database instance shall be cloned if existing + * + * @var bool + */ + public $newInstance; + + /** + * Whether to attach the queryBuilder to the database driver + * + * @var null|bool + */ + public $queryBuilder; + + public function init($parameters = '', $newInstance = false, $queryBuilder = null) + { + $this->parameters = $parameters; + $this->newInstance = $newInstance; + $this->queryBuilder = $queryBuilder; + } +} diff --git a/src/FuzeWorks/Event/DatabaseLoadForgeEvent.php b/src/FuzeWorks/Event/DatabaseLoadForgeEvent.php new file mode 100644 index 0000000..ab2d8cc --- /dev/null +++ b/src/FuzeWorks/Event/DatabaseLoadForgeEvent.php @@ -0,0 +1,77 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2017, 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://techfuze.net/fuzeworks + * @since Version 1.0.4 + * + * @version Version 1.0.4 + */ + +namespace FuzeWorks\Event; + +use FuzeWorks\Event; + +/** + * Event that gets loaded when a database forge is loaded + * + * Use this to cancel the loading of a forge, or change the provided database + * + * @author Abel Hoogeveen + * @copyright Copyright (c) 2013 - 2017, Techfuze. (http://techfuze.net) + */ +class DatabaseLoadForgeEvent extends Event +{ + /** + * A possible forge that can be loaded. + * + * Provide a forge in this variable and it will be loaded. + * + * @var FW_DB_forge|null + */ + public $forge = null; + + /** + * Database to be used by the forge. + * + * If no database is provided, FuzeWorks\Database shall provide the default database + * + * @var FW_DB|null + */ + public $database = null; + + /** + * Whether a database instance shall be cloned if existing + * + * @var bool + */ + public $newInstance; + + public function init($database = null, $newInstance = false) + { + $this->database = $database; + $this->newInstance = $newInstance; + } +} diff --git a/src/FuzeWorks/Event/DatabaseLoadUtilEvent.php b/src/FuzeWorks/Event/DatabaseLoadUtilEvent.php new file mode 100644 index 0000000..bde7470 --- /dev/null +++ b/src/FuzeWorks/Event/DatabaseLoadUtilEvent.php @@ -0,0 +1,77 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2017, 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://techfuze.net/fuzeworks + * @since Version 1.0.4 + * + * @version Version 1.0.4 + */ + +namespace FuzeWorks\Event; + +use FuzeWorks\Event; + +/** + * Event that gets loaded when a database utility is loaded + * + * Use this to cancel the loading of a util, or change the provided database + * + * @author Abel Hoogeveen + * @copyright Copyright (c) 2013 - 2017, Techfuze. (http://techfuze.net) + */ +class DatabaseLoadUtilEvent extends Event +{ + /** + * A possible util that can be loaded. + * + * Provide a util in this variable and it will be loaded. + * + * @var FW_DB_util|null + */ + public $util = null; + + /** + * Database to be used by the util. + * + * If no database is provided, FuzeWorks\Database shall provide the default database + * + * @var FW_DB_utility|null + */ + public $database = null; + + /** + * Whether a database instance shall be cloned if existing + * + * @var bool + */ + public $newInstance; + + public function init($database = null, $newInstance = false) + { + $this->database = $database; + $this->newInstance = $newInstance; + } +} diff --git a/src/Events/event.helperLoadEvent.php b/src/FuzeWorks/Event/HelperLoadEvent.php similarity index 100% rename from src/Events/event.helperLoadEvent.php rename to src/FuzeWorks/Event/HelperLoadEvent.php diff --git a/src/Events/event.layoutLoadViewEvent.php b/src/FuzeWorks/Event/LayoutLoadViewEvent.php similarity index 100% rename from src/Events/event.layoutLoadViewEvent.php rename to src/FuzeWorks/Event/LayoutLoadViewEvent.php diff --git a/src/Events/event.modelLoadEvent.php b/src/FuzeWorks/Event/ModelLoadEvent.php similarity index 100% rename from src/Events/event.modelLoadEvent.php rename to src/FuzeWorks/Event/ModelLoadEvent.php diff --git a/src/Events/event.routerLoadCallableEvent.php b/src/FuzeWorks/Event/RouterLoadCallableEvent.php similarity index 100% rename from src/Events/event.routerLoadCallableEvent.php rename to src/FuzeWorks/Event/RouterLoadCallableEvent.php diff --git a/src/Events/event.routerLoadControllerEvent.php b/src/FuzeWorks/Event/RouterLoadControllerEvent.php similarity index 100% rename from src/Events/event.routerLoadControllerEvent.php rename to src/FuzeWorks/Event/RouterLoadControllerEvent.php diff --git a/src/Events/event.routerRouteEvent.php b/src/FuzeWorks/Event/RouterRouteEvent.php similarity index 100% rename from src/Events/event.routerRouteEvent.php rename to src/FuzeWorks/Event/RouterRouteEvent.php diff --git a/src/FuzeWorks/Events.php b/src/FuzeWorks/Events.php index cea6c9b..bc5bdb1 100644 --- a/src/FuzeWorks/Events.php +++ b/src/FuzeWorks/Events.php @@ -37,8 +37,8 @@ use FuzeWorks\Exception\ModuleException; /** * Class Events. * - * FuzeWorks is built in a way that almost every core-event can be manipulated by modules. This class provides various ways to hook into the core (or other modules) - * and manipulate the outcome of the functions. Modules and core actions can 'fire' an event and modules can 'hook' into that event. Let's take a look at the example below: + * FuzeWorks is built in a way that almost every core-event can be manipulated. This class provides various ways to hook into the core or other components + * and manipulate the outcome of the functions. Components and core actions can 'fire' an event and components can 'hook' into that event. Let's take a look at the example below: * * If we want to add the current time at the end of each page title, we need to hook to the corresponding event. Those events are found in the 'events' directory in the system directory. * The event that will be fired when the title is changing is called layoutSetTitleEvent. So if we want our module to hook to that event, we add the following to the constructor: @@ -71,13 +71,6 @@ class Events */ private static $enabled = true; - /** - * A register with all the events and associated modules which should be loaded upon eventFire. - * - * @var array - */ - public static $register; - /** * Adds a function as listener. * @@ -134,11 +127,7 @@ class Events throw new EventException('Unknown priority '.$priority); } - if (!isset(self::$listeners[$eventName])) { - return; - } - - if (!isset(self::$listeners[$eventName][$priority])) { + if (!isset(self::$listeners[$eventName]) || !isset(self::$listeners[$eventName][$priority])) { return; } @@ -164,47 +153,56 @@ class Events */ public static function fireEvent($input) { - if (is_string($input)) { - // If the input is a string - $eventClass = $input; - $eventName = $input; - if (!class_exists($eventClass)) { - // Check if the file even exists - $file = Core::$coreDir . DS . 'Events' . DS . 'event.'.$eventName.'.php'; - if (file_exists($file)) { - // Load the file - $eventClass = "\FuzeWorks\Event\\".$eventClass; - include_once $file; - } else { - // No event arguments? Looks like a notify-event - if (func_num_args() == 1) { - // Load notify-event-class - $eventClass = '\FuzeWorks\Event\NotifierEvent'; - } else { - // No notify-event: we tried all we could - throw new EventException('Event '.$eventName.' could not be found!'); - } - } - } - - $event = new $eventClass($this); - } elseif (is_object($input)) { + // First try and see if the object is an Event + if (is_object($input)) + { $eventName = get_class($input); $eventName = explode('\\', $eventName); $eventName = end($eventName); $event = $input; - } else { - // INVALID EVENT - return false; + } + // Otherwise try to load an event based on the input string + elseif (is_string($input)) + { + $eventClass = ucfirst($input); + $eventName = $input; + + // Try a direct class + if (class_exists($eventClass, true)) + { + $event = new $eventClass(); + } + + // Try a core event + elseif (class_exists("\FuzeWorks\Event\\".$eventClass, true)) + { + $class = "\FuzeWorks\Event\\".$eventClass; + $event = new $class(); + } + + // Try a notifier event + elseif (func_num_args() == 1) + { + $class = "\FuzeWorks\Event\NotifierEvent"; + $event = new $class(); + } + + // Or throw an exception on failure + else + { + throw new EventException('Event '.$eventName.' could not be found!', 1); + } + } + else + { + throw new EventException('Event could not be loaded. Invalid variable provided.', 1); } if (self::$enabled) { - Logger::newLevel("Firing Event: '".$eventName."'"); - Logger::log('Initializing Event'); + Logger::newLevel("Firing Event: '".$eventName."'"); } - if (func_num_args() > 1) { call_user_func_array(array($event, 'init'), array_slice(func_get_args(), 1)); } @@ -216,14 +214,6 @@ class Events Logger::log('Checking for Listeners'); - // Read the event register for listeners - $register = self::$register; - if (isset($register[$eventName])) { - for ($i = 0; $i < count($register[$eventName]); ++$i) { - Modules::get($register[$eventName][$i]); - } - } - //There are listeners for this event if (isset(self::$listeners[$eventName])) { //Loop from the highest priority to the lowest @@ -234,6 +224,7 @@ class Events Logger::newLevel('Found listeners with priority '.EventPriority::getPriority($priority)); //Fire the event to each listener foreach ($listeners as $callback) { + // @codeCoverageIgnoreStart if (is_callable($callback)) { Logger::newLevel('Firing function'); } elseif (!is_string($callback[0])) { @@ -241,11 +232,9 @@ class Events } else { Logger::newLevel('Firing '.implode('->', $callback)); } - try { - call_user_func($callback, $event); - } catch (ModuleException $e) { - Logger::exceptionHandler($e); - } + // @codeCoverageIgnoreEnd + + call_user_func($callback, $event); Logger::stopLevel(); } diff --git a/src/FuzeWorks/Exception/ModuleException.php b/src/FuzeWorks/Exception/ModuleException.php deleted file mode 100644 index cad5e13..0000000 --- a/src/FuzeWorks/Exception/ModuleException.php +++ /dev/null @@ -1,45 +0,0 @@ -. - * - * @author TechFuze - * @copyright Copyright (c) 2013 - 2016, 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://techfuze.net/fuzeworks - * @since Version 0.0.1 - * - * @version Version 1.0.1 - */ - -namespace FuzeWorks\Exception; - -/** - * Class ModuleException. - * - * @author Abel Hoogeveen - * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) - */ -class ModuleException extends Exception -{ -} - -?> \ No newline at end of file diff --git a/src/FuzeWorks/Factory.php b/src/FuzeWorks/Factory.php index d148134..08a47a3 100644 --- a/src/FuzeWorks/Factory.php +++ b/src/FuzeWorks/Factory.php @@ -94,7 +94,6 @@ class Factory $this->instances['Events'] = new Events(); $this->instances['Models'] = new Models(); $this->instances['Layout'] = new Layout(); - $this->instances['Modules'] = new Modules(); $this->instances['Libraries'] = new Libraries(); $this->instances['Helpers'] = new Helpers(); $this->instances['Database'] = new Database(); diff --git a/src/FuzeWorks/Layout.php b/src/FuzeWorks/Layout.php index a479fac..3e8d61e 100644 --- a/src/FuzeWorks/Layout.php +++ b/src/FuzeWorks/Layout.php @@ -4,7 +4,7 @@ * * The FuzeWorks MVC PHP FrameWork * - * Copyright (C) 2015 TechFuze + * Copyright (C) 2017 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 @@ -20,14 +20,14 @@ * along with this program. If not, see . * * @author TechFuze - * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) + * @copyright Copyright (c) 2013 - 2017, 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://techfuze.net/fuzeworks * @since Version 0.0.1 * - * @version Version 1.0.0 + * @version Version 1.0.4 */ namespace FuzeWorks; @@ -43,8 +43,7 @@ use FuzeWorks\Exception\LayoutException; * Layout and Template Manager for FuzeWorks. * * @author Abel Hoogeveen - * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) - * @todo Make Object, remove static stuff + * @copyright Copyright (c) 2013 - 2017, Techfuze. (http://techfuze.net) */ class Layout { @@ -53,53 +52,53 @@ class Layout * * @var null|string */ - public static $file = null; + public $file = null; /** * The directory of the file to be loaded by the layout manager. * * @var string */ - public static $directory; + public $directory; /** * All assigned currently assigned to the template. * * @var array Associative Assigned Variable Array */ - private static $assigned_variables = array(); + private $assigned_variables = array(); /** * All engines that can be used for templates. * * @var array of engines */ - private static $engines = array(); + private $engines = array(); /** * All file extensions that can be used and are bound to a template engine. * * @var array of names of engines */ - private static $file_extensions = array(); + private $file_extensions = array(); /** * whether the template engines are already called. * * @var bool True if loaded */ - private static $engines_loaded = false; + private $engines_loaded = false; /** * The currently selected template engine. * * @var string name of engine */ - private static $current_engine; + private $current_engine; - public static function init() + public function init() { - self::$directory = Core::$appDir . DS .'Views'; + $this->directory = Core::$appDir . DS .'Views'; } /** @@ -116,18 +115,18 @@ class Layout * * @throws LayoutException On error */ - public static function view($file, $directory = null, $directOutput = false) + public function view($file, $directory = null, $directOutput = false) { $output = Factory::getInstance()->output; - $directory = (is_null($directory) ? self::$directory : $directory); + $directory = (is_null($directory) ? $this->directory : $directory); if ($directOutput === true) { - echo self::get($file, $directory); + echo $this->get($file, $directory); } else { - $output->append_output(self::get($file, $directory)); + $output->append_output($this->get($file, $directory)); } return; @@ -148,37 +147,37 @@ class Layout * * @throws LayoutException On error */ - public static function get($file, $directory = null) + public function get($file, $directory = null) { - $directory = (is_null($directory) ? self::$directory : $directory); + $directory = (is_null($directory) ? $this->directory : $directory); Logger::newLevel("Loading template file '".$file."' in '".$directory."'"); // First load the template engines - self::loadTemplateEngines(); + $this->loadTemplateEngines(); // First retrieve the filepath - if (is_null(self::$current_engine)) { - self::setFileFromString($file, $directory, array_keys(self::$file_extensions)); + if (is_null($this->current_engine)) { + $this->setFileFromString($file, $directory, array_keys($this->file_extensions)); } else { - self::setFileFromString($file, $directory, self::$current_engine->getFileExtensions()); + $this->setFileFromString($file, $directory, $this->current_engine->getFileExtensions()); } // Then assign some basic variables for the template - self::$assigned_variables['wwwDir'] = Config::get('main')->base_url; - self::$assigned_variables['siteURL'] = Config::get('main')->base_url; - self::$assigned_variables['serverName'] = Config::get('main')->server_name; - self::$assigned_variables['adminMail'] = Config::get('main')->administrator_mail; - self::$assigned_variables['contact'] = Config::get('contact')->toArray(); + $this->assigned_variables['wwwDir'] = Config::get('main')->base_url; + $this->assigned_variables['siteURL'] = Config::get('main')->base_url; + $this->assigned_variables['serverName'] = Config::get('main')->server_name; + $this->assigned_variables['adminMail'] = Config::get('main')->administrator_mail; + $this->assigned_variables['contact'] = Config::get('contact')->toArray(); // Select an engine if one is not already selected - if (is_null(self::$current_engine)) { - self::$current_engine = self::getEngineFromExtension(self::getExtensionFromFile(self::$file)); + if (is_null($this->current_engine)) { + $this->current_engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file)); } - self::$current_engine->setDirectory(self::$directory); + $this->current_engine->setDirectory($this->directory); // And run an Event to see what other parts have to say about it - $event = Events::fireEvent('layoutLoadViewEvent', self::$file, self::$directory, self::$current_engine, self::$assigned_variables); + $event = Events::fireEvent('layoutLoadViewEvent', $this->file, $this->directory, $this->current_engine, $this->assigned_variables); // The event has been cancelled if ($event->isCancelled()) { @@ -186,14 +185,14 @@ class Layout } // And refetch the data from the event - self::$current_engine = $event->engine; - self::$assigned_variables = $event->assigned_variables; + $this->current_engine = $event->engine; + $this->assigned_variables = $event->assigned_variables; Logger::stopLevel(); // And finally run it if (file_exists($event->file)) { - return self::$current_engine->get($event->file, self::$assigned_variables); + return $this->current_engine->get($event->file, $this->assigned_variables); } throw new LayoutException('The requested file was not found', 1); @@ -206,10 +205,10 @@ class Layout * * @return object Template Engine */ - public static function getEngineFromExtension($extension) + public function getEngineFromExtension($extension) { - if (isset(self::$file_extensions[strtolower($extension)])) { - return self::$engines[ self::$file_extensions[strtolower($extension)]]; + if (isset($this->file_extensions[strtolower($extension)])) { + return $this->engines[ $this->file_extensions[strtolower($extension)]]; } throw new LayoutException('Could not get Template Engine. No engine has corresponding file extension', 1); @@ -222,7 +221,7 @@ class Layout * * @return string Extension of the file */ - public static function getExtensionFromFile($fileString) + public function getExtensionFromFile($fileString) { return substr($fileString, strrpos($fileString, '.') + 1); } @@ -240,9 +239,9 @@ class Layout * * @throws LayoutException On error */ - public static function getFileFromString($string, $directory, $extensions = array()) + public function getFileFromString($string, $directory, $extensions = array()) { - $directory = preg_replace('#/+#', '/', (!is_null($directory) ? $directory : self::$directory).'/'); + $directory = preg_replace('#/+#', '/', (!is_null($directory) ? $directory : $this->directory).'/'); if (strpbrk($directory, "\\/?%*:|\"<>") === TRUE || strpbrk($string, "\\/?%*:|\"<>") === TRUE) { @@ -310,10 +309,10 @@ class Layout * * @throws LayoutException On error */ - public static function setFileFromString($string, $directory, $extensions = array()) + public function setFileFromString($string, $directory, $extensions = array()) { - self::$file = self::getFileFromString($string, $directory, $extensions); - self::$directory = preg_replace('#/+#', '/', (!is_null($directory) ? $directory : self::$directory).'/'); + $this->file = $this->getFileFromString($string, $directory, $extensions); + $this->directory = preg_replace('#/+#', '/', (!is_null($directory) ? $directory : $this->directory).'/'); } /** @@ -321,9 +320,9 @@ class Layout * * @return null|string Path to the file */ - public static function getFile() + public function getFile() { - return self::$file; + return $this->file; } /** @@ -331,9 +330,9 @@ class Layout * * @param string $file Path to the file */ - public static function setFile($file) + public function setFile($file) { - self::$file = $file; + $this->file = $file; } /** @@ -341,9 +340,9 @@ class Layout * * @return null|string Path to the directory */ - public static function getDirectory() + public function getDirectory() { - return self::$directory; + return $this->directory; } /** @@ -351,9 +350,9 @@ class Layout * * @param string $directory Path to the directory */ - public static function setDirectory($directory) + public function setDirectory($directory) { - self::$directory = $directory; + $this->directory = $directory; } /** @@ -362,9 +361,9 @@ class Layout * @param string $key Key of the variable * @param mixed $value Value of the variable */ - public static function assign($key, $value) + public function assign($key, $value) { - self::$assigned_variables[$key] = $value; + $this->assigned_variables[$key] = $value; } /** @@ -372,9 +371,9 @@ class Layout * * @param string $title title of the template */ - public static function setTitle($title) + public function setTitle($title) { - self::$assigned_variables['title'] = $title; + $this->assigned_variables['title'] = $title; } /** @@ -382,13 +381,13 @@ class Layout * * @return string title of the template */ - public static function getTitle() + public function getTitle() { - if (!isset(self::$assigned_variables['title'])) + if (!isset($this->assigned_variables['title'])) { return false; } - return self::$assigned_variables['title']; + return $this->assigned_variables['title']; } /** @@ -400,11 +399,11 @@ class Layout * * @throws \FuzeWorks\LayoutException on error */ - public static function setEngine($name) + public function setEngine($name) { - self::loadTemplateEngines(); - if (isset(self::$engines[$name])) { - self::$current_engine = self::$engines[$name]; + $this->loadTemplateEngines(); + if (isset($this->engines[$name])) { + $this->current_engine = $this->engines[$name]; Logger::log('Set the Template Engine to '.$name); return true; @@ -419,11 +418,11 @@ class Layout * * @return object Object that implements \FuzeWorks\TemplateEngine */ - public static function getEngine($name) + public function getEngine($name) { - self::loadTemplateEngines(); - if (isset(self::$engines[$name])) { - return self::$engines[$name]; + $this->loadTemplateEngines(); + if (isset($this->engines[$name])) { + return $this->engines[$name]; } throw new LayoutException('Could not return engine. Engine does not exist', 1); } @@ -439,17 +438,17 @@ class Layout * * @throws \FuzeWorks\LayoutException On error */ - public static function registerEngine($engineClass, $engineName, $engineFileExtensions = array()) + public function registerEngine($engineClass, $engineName, $engineFileExtensions = array()) { // First check if the engine already exists - if (isset(self::$engines[$engineName])) { + if (isset($this->engines[$engineName])) { throw new LayoutException("Could not register engine. Engine '".$engineName."' already registered", 1); } // Then check if the object is correct if ($engineClass instanceof TemplateEngine) { // Install it - self::$engines[$engineName] = $engineClass; + $this->engines[$engineName] = $engineClass; // Then define for what file extensions this Template Engine will work if (!is_array($engineFileExtensions)) { @@ -458,12 +457,12 @@ class Layout // Then install them foreach ($engineFileExtensions as $extension) { - if (isset(self::$file_extensions[strtolower($extension)])) { + if (isset($this->file_extensions[strtolower($extension)])) { throw new LayoutException('Could not register engine. File extension already bound to engine', 1); } // And add it - self::$file_extensions[strtolower($extension)] = $engineName; + $this->file_extensions[strtolower($extension)] = $engineName; } // And log it @@ -478,17 +477,17 @@ class Layout /** * Load the template engines by sending a layoutLoadEngineEvent. */ - public static function loadTemplateEngines() + public function loadTemplateEngines() { - if (!self::$engines_loaded) { + if (!$this->engines_loaded) { Events::fireEvent('layoutLoadEngineEvent'); // Load the engines provided in this file - self::registerEngine(new PHPEngine(), 'PHP', array('php')); - self::registerEngine(new JsonEngine(), 'JSON', array('json')); - self::registerEngine(new SmartyEngine(), 'Smarty', array('tpl')); - self::registerEngine(new LatteEngine(), 'Latte', array('latte')); - self::$engines_loaded = true; + $this->registerEngine(new PHPEngine(), 'PHP', array('php')); + $this->registerEngine(new JsonEngine(), 'JSON', array('json')); + $this->registerEngine(new SmartyEngine(), 'Smarty', array('tpl')); + $this->registerEngine(new LatteEngine(), 'Latte', array('latte')); + $this->engines_loaded = true; } } @@ -503,11 +502,11 @@ class Layout public static function __callStatic($name, $params) { // First load the template engines - self::loadTemplateEngines(); + $this->loadTemplateEngines(); - if (!is_null(self::$current_engine)) { + if (!is_null($this->current_engine)) { // Call user func array here - return call_user_func_array(array(self::$current_engine, $name), $params); + return call_user_func_array(array($this->current_engine, $name), $params); } throw new LayoutException('Could not access Engine. Engine not loaded', 1); } @@ -515,20 +514,20 @@ class Layout /** * Resets the layout manager to its default state. */ - public static function reset() + public function reset() { - if (!is_null(self::$current_engine)) { - self::$current_engine->reset(); + if (!is_null($this->current_engine)) { + $this->current_engine->reset(); } // Unload the engines - self::$engines = array(); - self::$engines_loaded = false; - self::$file_extensions = array(); + $this->engines = array(); + $this->engines_loaded = false; + $this->file_extensions = array(); - self::$current_engine = null; - self::$assigned_variables = array(); - self::$directory = Core::$appDir . DS . 'Views'; + $this->current_engine = null; + $this->assigned_variables = array(); + $this->directory = Core::$appDir . DS . 'Views'; Logger::log('Reset the layout manager to its default state'); } } \ No newline at end of file diff --git a/src/FuzeWorks/Logger.php b/src/FuzeWorks/Logger.php index b7ebfeb..be842e2 100644 --- a/src/FuzeWorks/Logger.php +++ b/src/FuzeWorks/Logger.php @@ -216,8 +216,8 @@ class Logger { self::logError('Exception thrown: ' . $message . ' | ' . $code, null, $file, $line); - // And return a 500 because this error was fatal - self::http_error('500'); + // And return a 500 because this error was fatal + self::http_error('500'); } /** @@ -244,7 +244,7 @@ class Logger { } $logs = self::$Logs; - require(dirname(__DIR__) . '/views/view.' . self::$logger_template . '.php'); + require(dirname(__DIR__) . '/Views/view.' . self::$logger_template . '.php'); } /** @@ -255,7 +255,7 @@ class Logger { { ob_start(function () {}); $logs = self::$Logs; - require(dirname(__DIR__) . '/views/view.logger_cli.php'); + require(dirname(__DIR__) . '/Views/view.logger_cli.php'); $contents = ob_get_clean(); $file = Core::$logDir .DS. 'Logs'.DS.'log_latest.php'; if (is_writable($file)) @@ -516,12 +516,13 @@ class Logger { self::log('Loading view ' . $view); // Try and load the view, if impossible, load HTTP code instead. + $factory = Factory::getInstance(); try { - Layout::reset(); - Layout::view($view); + $factory->Layout->reset(); + $factory->Layout->view($view); } catch (LayoutException $exception) { // No error page could be found, just echo the result - Factory::getInstance()->output->set_output("

$errno

" . $http_codes[$errno] . '

'); + $factory->output->set_output("

$errno

" . $http_codes[$errno] . '

'); } } diff --git a/src/FuzeWorks/LoggerTracyBridge.php b/src/FuzeWorks/LoggerTracyBridge.php index 4e1a37f..dce058e 100644 --- a/src/FuzeWorks/LoggerTracyBridge.php +++ b/src/FuzeWorks/LoggerTracyBridge.php @@ -73,7 +73,7 @@ class LoggerTracyBridge implements IBarPanel { public function getTab() { ob_start(function () {}); - require dirname(__DIR__) . '/views/view.tracyloggertab.php'; + require dirname(__DIR__) . '/Views/view.tracyloggertab.php'; return ob_get_clean(); } @@ -101,7 +101,7 @@ class LoggerTracyBridge implements IBarPanel { // Parse the panel ob_start(function () {}); - require dirname(__DIR__) . '/views/view.tracyloggerpanel.php'; + require dirname(__DIR__) . '/Views/view.tracyloggerpanel.php'; return ob_get_clean(); } diff --git a/src/FuzeWorks/Module.php b/src/FuzeWorks/Module.php deleted file mode 100644 index ee6daaf..0000000 --- a/src/FuzeWorks/Module.php +++ /dev/null @@ -1,204 +0,0 @@ -. - * - * @author TechFuze - * @copyright Copyright (c) 2013 - 2016, 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://techfuze.net/fuzeworks - * @since Version 0.0.1 - * - * @version Version 1.0.0 - */ - -namespace FuzeWorks; - -/** - * Trait Module. - * - * Contains all the methods modules should have - * - * @author Abel Hoogeveen - * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) - * @deprecated - */ -trait Module -{ - /** - * @var null|string Relative path to the module - */ - protected static $modulePath = null; - - /** - * @var string Internal name of the module - */ - protected static $moduleName = 'placeholder'; - - /** - * @var string name used in the mod array - */ - protected static $linkName = 'placeholder'; - - /** - * @var moduleInfo object of the module - */ - protected static $cfg; - - /** - * @var array Advertisements send from other modules - */ - protected static $advertisements = array(); - - /** - * Returns the name of the module. - * - * @return string Returns the name of the module - */ - public static function getModuleName() - { - return self::$moduleName; - } - - /** - * Returns the path to the module. - * - * @return null|string - */ - public static function getModulePath() - { - return self::$modulePath; - } - - /** - * Returns the config of the module (moduleInfo.php). - * - * @return stdClass module config - */ - public static function getModuleConfig() - { - return self::$cfg; - } - - /** - * Changes the path to the location of the module. - * - * This function can only be executed once, because when the path has been set this function won't save changes anymore. - * This prevents modules or other systems to mess with modules and their stability. - * - * @param string $modulePath - */ - public static function setModulePath($modulePath = null) - { - // Only allow one change of this variable from outside - if (self::$modulePath === null) { - self::$modulePath = $modulePath; - } - } - - /** - * Set the link name of the module. The link name is the address in the module array so that the module can self reference. - * - * @param string link name - */ - public static function setModuleLinkName($linkName) - { - self::$linkName = $linkName; - } - - /** - * The name that is required to load itself, eg 'exampleauthor/examplemodulename' or 'techfuze/cms'. - * - * @param string module name - */ - public static function setModuleName($modName) - { - self::$moduleName = $modName; - } - - /** - * Add the moduleInfo.php to the module for direct interaction. - * - * @param stdClass module config - */ - public static function setModuleConfig($config) - { - self::$cfg = $config; - } - - /** - * Set a value in the modules moduleInfo.php. - * - * @param mixed config Key - * @param mixed config value - */ - public static function setConfigValue($key, $value) - { - $file = self::getModulePath().'moduleInfo.php'; - self::$cfg->$key = $value; - - // Check if the module path is set yet - if (self::getModulePath() == null) { - Logger::logWarning('Could not write module config. ModulePath is not set', get_class($this)); - - return false; - } - - if (file_exists($file) && is_writable($file)) { - $config = var_export($this->cfg, true); - file_put_contents($file, "$key; - } -} diff --git a/src/FuzeWorks/Modules.php b/src/FuzeWorks/Modules.php deleted file mode 100644 index 7971596..0000000 --- a/src/FuzeWorks/Modules.php +++ /dev/null @@ -1,598 +0,0 @@ -. - * - * @author TechFuze - * @copyright Copyright (c) 2013 - 2016, 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://techfuze.net/fuzeworks - * @since Version 0.0.1 - * - * @version Version 1.0.0 - */ - -namespace FuzeWorks; - -use FuzeWorks\Exception\ModuleException; -use stdClass; - -/** - * Modules Class. - * - * @author Abel Hoogeveen - * @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net) - * @deprecated - */ -class Modules -{ - /** - * A register of all the existing module headers. - * - * The module headers contain information required to loading the module - * - * @var array - */ - private static $register; - - /** - * A register which holds all the module advertisements by key. - * - * @var array - */ - private static $advertiseRegister = array(); - - /** - * An array of all the loaded modules. - * - * @var array - */ - public static $modules = array(); - - /** - * An array with the names of all modules that are loaded, and should not be loaded again. - * - * @var array of module names - */ - private static $loaded_modules = array(); - - /** - * An array which holds the routes to module to load them quickly. - * - * @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 - */ - public static function get($name) - { - // Where the modules are - $path = Core::$appDir . DS . 'Modules/'; - - // Check if the requested module is registered - if (isset(self::$register[$name])) { - if (!empty(self::$register[$name])) { - // Load the moduleInfo - $cfg = (object) self::$register[$name]; - - // Check if the module is disabled - if (isset($cfg->meta)) { - throw new ModuleException("Could not load module. Module '".$name."' is not enabled", 1); - } - - // Check if the module is already loaded. If so, only return a reference, if not, load the module - if (in_array(strtolower($name), self::$loaded_modules)) { - // return the link - $c = self::$modules[strtolower($cfg->module_name)]; - - return $c; - } else { - // Load the module - $file = $cfg->directory.'/'.$cfg->module_file; - - // Load the dependencies before the module loads - $deps = (isset($cfg->dependencies) ? $cfg->dependencies : array()); - for ($i = 0; $i < count($deps); ++$i) { - self::get($deps[$i]); - } - - // Check if the file exists - if (file_exists($file)) { - // And load it - include_once $file; - $class_name = $cfg->module_class; - $msg = "Loading Module '".ucfirst((isset($cfg->name) ? $cfg->name : $cfg->module_name))."'"; - $msg .= (isset($cfg->version) ? '; version: '.$cfg->version : ''); - $msg .= (isset($cfg->author) ? '; made by '.$cfg->author : ''); - $msg .= (isset($cfg->website) ? '; from '.$cfg->website : ''); - Logger::log($msg); - } else { - // Throw Exception if the file does not exist - throw new ModuleException("Could not load module. Module '".$name."' class file was not found.", 1); - - return false; - } - - // If it is an abstract module, load an StdClass for the module address - if (isset($cfg->abstract)) { - if ($cfg->abstract) { - $CLASS = new stdClass(); - - return self::$modules[strtolower($cfg->module_name)] = &$CLASS; - } - } - - // Load the module class - $class_name = $cfg->module_class; - $CLASS = new $class_name(); - - // Apply default methods - if (method_exists($CLASS, 'setModulePath')) { - $CLASS::setModulePath($cfg->directory); - } - - if (method_exists($CLASS, 'setModuleLinkName')) { - $CLASS::setModuleLinkName(strtolower($cfg->module_name)); - } - - if (method_exists($CLASS, 'setModuleName')) { - $CLASS::setModuleName($name); - } - - // Send all advertisements - if (isset($cfg->listenFor)) { - $listenFor = $cfg->listenFor; - if (method_exists($CLASS, 'setAdvertisements')) { - foreach ($listenFor as $advertiseName) { - if (isset(self::$advertiseRegister[$advertiseName])) { - $CLASS::setAdvertisements($advertiseName, self::$advertiseRegister[$advertiseName]); - } - } - } else { - throw new ModuleException("Could not load module. Module '".$name."' listens for advertisement but does not implement setAdvertisements() method.", 1); - } - } - - // Send the moduleConfig if possible - if (method_exists($CLASS, 'setModuleConfig')) { - // Append the config file to the module CFG (accessable through $this->cfg) - if (file_exists($cfg->directory.'/'.'config.'.strtolower($cfg->module_name).'.php')) { - $data = (object) include $cfg->directory.'/'.'config.'.strtolower($cfg->module_name).'.php'; - foreach ($data as $key => $value) { - $cfg->$key = $value; - } - } - - $CLASS::setModuleConfig($cfg); - } - - // And finally check if it can be loaded - if (!method_exists($CLASS, 'onLoad')) { - throw new ModuleException("Could not load module. Module '".$name."' does not have an onLoad() method", 1); - } - - // Prepare onLoad call - $args = func_get_args(); - array_shift($args); - - // And call onLoad - call_user_func_array(array($CLASS, 'onLoad'), $args); - - // Add to the loaded modules - self::$loaded_modules[] = strtolower($name); - - // Return a reference - return self::$modules[strtolower($cfg->module_name)] = &$CLASS; - } - } else { - throw new ModuleException("Could not load module. Module '".$name."' has an invalid config", 1); - } - } else { - throw new ModuleException("Module not found: '".$name."'", 1); - } - } - - /** - * 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 = include $file; - $cfg[$key] = $value; - $config = var_export($cfg, true); - file_put_contents($file, "directory = $directory; - - // Define the module name - $name = ''; - $name .= (!empty($cfg->author) ? strtolower($cfg->author).'/' : ''); - $name .= strtolower($cfg->module_name); - - Logger::log("Adding module: '".$name."'"); - if (isset(self::$register[$name])) { - Logger::logError("Module '".$name."' can not be added. Module is already loaded"); - - return false; - } - - // Check whether the module is enabled or no - if (isset($cfg->enabled)) { - if ($cfg->enabled) { - // Copy all the data into the register and enable - self::$register[$name] = (array) $cfg; - Logger::log("[ON] '".$name."'"); - } else { - // If not, copy all the basic data so that it can be enabled in the future - $cfg2 = new StdClass(); - $cfg2->module_name = $cfg->module_name; - $cfg2->directory = $cfg->directory; - $cfg2->meta = $cfg; - self::$register[$name] = (array) $cfg2; - Logger::log("[OFF] '".$name."'"); - } - } else { - // Copy all the data into the register and enable - self::$register[$name] = (array) $cfg; - Logger::log("[ON] '".$name."'"); - } - } else { - throw new ModuleException("Could not add module. '$moduleInfo_file' does not exist", 1); - } - } - - /** - * Enables a module when it is disabled. - * - * @param string Module name - * @param bool true for permanent enable - * - * @throws FuzeWorks\ModuleException - */ - public static function enableModule($name, $permanent = true) - { - if (isset(self::$register[$name])) { - // Change the register - $info = (object) self::$register[$name]; - - // Do nothing if it is already enabled - if (isset($info->enabled)) { - if ($info->enabled) { - Logger::logWarning("Could not enable module '".$name."'. Module is already enabled."); - - return false; - } - } - - // Otherwise move data from meta to the module config - $info = $info->meta; - $info->enabled = true; - self::$register[$name] = (array) $info; - - Logger::log("Enabled module '".$name."'"); - - // Enable it permanently if so desired - if ($permanent) { - $file = $info->directory.'/moduleInfo.php'; - self::setModuleValue($file, 'enabled', true); - } - - // Reload the eventRegister - Events::buildEventRegister(); - } else { - throw new ModuleException("Could not enable module '".$name."'. Module does not exist.", 1); - } - } - - /** - * Disableds a module when it is enabled. - * - * @param string Module name - * @param bool true for permanent disable - * - * @throws FuzeWorks\ModuleException - */ - public static function disableModule($name, $permanent = true) - { - if (isset(self::$register[$name])) { - $info = (object) self::$register[$name]; - - // Do nothing if it is already disabled - if (isset($info->meta)) { - Logger::logWarning("Could not disable module '".$name."'. Module is already disabled."); - - return false; - } - - $disabled = new StdClass(); - $disabled->meta = $info; - $disabled->directory = $info->directory; - $disabled->module_name = $info->module_name; - - self::$register[$name] = (array) $disabled; - Logger::log("Disabled module '".$name."'"); - if ($permanent) { - $file = $info->directory.'/moduleInfo.php'; - self::setModuleValue($file, 'enabled', false); - } - - // Reload the eventRegister - Events::buildEventRegister(); - - // Remove the existence of the module - unset(self::$modules[strtolower($cfg->module_name)]); - } else { - throw new ModuleException("Could not disable module '".$name."'. Module does not exist.", 1); - } - } - - /** - * Create a register with all the module headers from all the existing modules. - * - * Used to correctly load all modules - * @param bool $cache true if loading from cache - * @param string $cachingMethod the driver used in the caching library - * @param int $cachingTime The time the registers are cached - */ - public static function buildRegister($cache = false, $cachingMethod = 'file', $cachingTime = 300) - { - // First check if the caching engine can be used - if ($cache) - { - // Retrieve the cache if possible - $cache = Factory::getInstance()->libraries->getDriver('cache'); - $cacheData = $cache->$cachingMethod->get('moduleRegisters'); - - if ( ! is_bool($cacheData) ) - { - // Separate the data - $moduleRegister = $cacheData['moduleRegister']; - $eventRegister = $cacheData['eventRegister']; - $routeRegister = $cacheData['routeRegister']; - $advertiseRegister = $cacheData['advertiseRegister']; - - // And register it all - Logger::newLevel("Loadig Module Headers from Cache"); - self::$register = $moduleRegister; - Events::$register = $eventRegister; - self::$advertiseRegister = $advertiseRegister; - self::$module_routes = $routeRegister; - foreach ($routeRegister as $route => $name) { - Factory::getInstance()->router->addRoute($route, array('callable' => array('\FuzeWorks\Modules', 'moduleCallable')), true); - } - Logger::stopLevel(); - return true; - } - } - - Logger::newLevel('Loading Module Headers', 'Core'); - - // Get all the module directories - $dir = Core::$appDir . DS . 'Modules/'; - $mod_dirs = array(); - $mod_dirs = array_values(array_diff(scandir($dir), array('..', '.'))); - - // Build the module and event register - $register = array(); - $event_register = array(); - - // Cycle through all module directories - for ($i = 0; $i < count($mod_dirs); ++$i) { - $mod_dir = $dir.$mod_dirs[$i].'/'; - // If a moduleInfo.php exists, load it - if (file_exists($mod_dir.'/moduleInfo.php')) { - // Load the configuration file - $cfg = (object) include $mod_dir.'/moduleInfo.php'; - - // Set enabled for now - $enabled = true; - - // Define the module name - $name = ''; - $name .= (!empty($cfg->author) ? strtolower($cfg->author).'/' : ''); - $name .= strtolower($cfg->module_name); - - // Get the module directory - $cfg->directory = $mod_dir; - - // Check whether the module is disabled - if (isset($cfg->enabled)) { - if (!$cfg->enabled) { - // If disabled, set the variable - $enabled = false; - - // If disabled, a holder will be placed so it might be enabled in the future - $mock = new StdClass(); - $mock->module_name = $cfg->module_name; - $mock->directory = $cfg->directory; - $mock->meta = $cfg; - $mock->aliases = $cfg->aliases; - - // Important, change the configuration to the mock, so we can duplicate it afterwards - $cfg = $mock; - } - } - - // Copy all the data into the register and enable - $register[$name] = (array) $cfg; - - // Log the name for enabled and disabled - if (!$enabled) { - Logger::newLevel("[OFF] '".$name."'"); - } else { - Logger::newLevel("[ON] '".$name."'"); - } - - // And possibly some aliases - if (isset($cfg->aliases)) { - foreach ($cfg->aliases as $alias) { - $register[$alias] = (array) $cfg; - unset($register[$alias]['events']); - Logger::log("   '".$alias."' (alias of '".$name."')"); - } - } - - // If not enabled, log it, wrap it and off to the next one - if (!$enabled) { - Logger::stopLevel(); - continue; - } - - // Otherwise continue and add routing paths - if (isset($cfg->routes)) { - // Get routes and add them - foreach ($cfg->routes as $route) { - // Create the route and callable and parse them - $callable = array('\FuzeWorks\Modules', 'moduleCallable'); - Factory::getInstance()->router->addRoute($route, array('callable' => $callable), true); - self::$module_routes[$route] = $name; - } - } - - // And for the events - if (isset($cfg->events)) { - // Get the events and add them - foreach ($cfg->events as $event) { - // First check if the event already exists, if so, append it - if (isset($event_register[$event])) { - $event_register[$event][] = $name; - } else { - $event_register[$event] = array($name); - } - - // Log the event - Logger::Log('Event added: \''.$event.'\''); - } - } - - // And check for an advertisement tag - if (isset($cfg->advertise)) { - // Cycle through advertisements - foreach ($cfg->advertise as $advertiseName => $advertiseData) { - // Log advertisement - Logger::log('Advertisement added: \''.$advertiseName.'\''); - - // Add to advertiseRegister - self::$advertiseRegister[$advertiseName][$name] = $advertiseData; - } - } - - Logger::stopLevel(); - } else { - // If no details are specified, create a basic mock module - $name = $mod_dirs[$i]; - - // Build a default mock module config - $mock = new stdClass(); - $mock->module_class = ucfirst($name); - $mock->module_file = 'class.'.strtolower($name).'.php'; - $mock->module_name = $name; - $mock->dependencies = array(); - $mock->versions = array(); - $mock->directory = $mod_dir; - - // Apply it - $register[$name] = (array) $mock; - Logger::newLevel("[ON] '".$name."'"); - Logger::stopLevel(); - } - } - - if ($cache) - { - Logger::log("Saving registry to cache"); - $cacheData = array( - 'moduleRegister' => $register, - 'eventRegister' => $event_register, - 'routeRegister' => self::$module_routes, - 'advertiseRegister' => self::$advertiseRegister - ); - $cache->$cachingMethod->save('moduleRegisters', $cacheData, $cachingTime); - } - - // And apply the registers to their dedicate location - self::$register = $register; - Events::$register = $event_register; - Logger::stopLevel(); - } - - /** - * 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 - * - * @param array Regex matches - */ - 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(); - } -} diff --git a/src/FuzeWorks/TemplateEngine/LatteEngine.php b/src/FuzeWorks/TemplateEngine/LatteEngine.php index 4a88c8d..3c6e3ce 100644 --- a/src/FuzeWorks/TemplateEngine/LatteEngine.php +++ b/src/FuzeWorks/TemplateEngine/LatteEngine.php @@ -81,7 +81,9 @@ class LatteEngine implements TemplateEngine */ public function get($file, $assigned_variables) { + ob_start(function () {}); $this->latte->render($file, $assigned_variables); + return ob_get_clean(); } /** diff --git a/src/FuzeWorks/URI.php b/src/FuzeWorks/URI.php index 4e14ee5..e3eacbc 100644 --- a/src/FuzeWorks/URI.php +++ b/src/FuzeWorks/URI.php @@ -356,6 +356,8 @@ class URI { { throw new UriException('The URI you submitted has disallowed characters.', 1); } + + return true; } // --------------------------------------------------------------------