Implemented Helpers into FuzeWorks.

Helpers are small utilities that can be loaded to assist a performing certain functions. Helpers are simply global functions that get loaded when requesting them.

Helpers can be loaded using (\FuzeWorks\)Helpers::load('helperName'); Helpers can be put in the 'Core/Helpers' or the 'Application/Helpers' directory. The 'Applications/Helpers' directory is scanned first, so this one has priority over Core helpers.

It is possible to sort of 'extend' helpers. By putting a helper in the 'Application/Helpers' directory with the application prefix (found in config.main.php) you can load that helper first and then the helper in the core directory. This allows you to add or override functions without the need of copying the entire helper from the core. For example: there is a helper in the core directory named 'example_helper.php'. This one has a function named 'doSomething();' inside it. If you now create a helper in the application directory named 'MY_example_helper.php', then that one will be loaded first and can override the core class because the application helper is loaded first.

More detailed instructions will be provided in the documentation.
This commit is contained in:
Abel Hoogeveen 2016-05-14 14:06:04 +02:00
parent 2cbc431283
commit 2aef5abdd3
9 changed files with 392 additions and 24 deletions

View File

@ -8,5 +8,5 @@ return array(
'administrator_mail' => '',
'default_controller' => 'standard',
'default_function' => 'index',
'application_library_prefix' => 'MY_'
'application_prefix' => 'MY_'
);

View File

@ -0,0 +1,83 @@
<?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 - 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://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 helper is loaded.
*
* Use this to cancel the loading of a helper, or change the helper to be loaded
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class HelperLoadEvent extends Event
{
/**
* The name of the helper that gets loaded
*
* @var string
*/
public $helperName;
/**
* The directory of the helper that gets loaded
*
* @var string
*/
public $helperFile;
/**
* An optional extension helper name that can get loaded.
*
* @var string|null
*/
public $extendedHelperName = null;
/**
* The directory of an optional extension helper that can get loaded
*
* @var string|null
*/
public $extendedHelperFile = null;
public function init($helperName, $helperFile, $extendedHelperName = null, $extendedHelperFile = null)
{
$this->helperName = $helperName;
$this->helperFile = $helperFile;
$this->extendedHelperName = $extendedHelperName;
$this->extendedHelperFile = $extendedHelperFile;
}
}

View File

@ -0,0 +1,111 @@
<?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 - 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://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
/**
* CodeIgniter Array Helpers
*
* Converted to FuzeWorks.
*
* @package CodeIgniter
* @subpackage Helpers
* @category Helpers
* @author EllisLab Dev Team
* @link https://codeigniter.com/user_guide/helpers/array_helper.html
* @license http://opensource.org/licenses/MIT MIT License
*/
// ------------------------------------------------------------------------
if ( ! function_exists('element'))
{
/**
* Element
*
* Lets you determine whether an array index is set and whether it has a value.
* If the element is empty it returns NULL (or whatever you specify as the default value.)
*
* @param string
* @param array
* @param mixed
* @return mixed depends on what the array contains
*/
function element($item, array $array, $default = NULL)
{
return array_key_exists($item, $array) ? $array[$item] : $default;
}
}
// ------------------------------------------------------------------------
if ( ! function_exists('random_element'))
{
/**
* Random Element - Takes an array as input and returns a random element
*
* @param array
* @return mixed depends on what the array contains
*/
function random_element($array)
{
return is_array($array) ? $array[array_rand($array)] : $array;
}
}
// --------------------------------------------------------------------
if ( ! function_exists('elements'))
{
/**
* Elements
*
* Returns only the array items specified. Will return a default value if
* it is not set.
*
* @param array
* @param array
* @param mixed
* @return mixed depends on what the array contains
*/
function elements($items, array $array, $default = NULL)
{
$return = array();
is_array($items) OR $items = array($items);
foreach ($items as $item)
{
$return[$item] = array_key_exists($item, $array) ? $array[$item] : $default;
}
return $return;
}
}

View File

@ -137,6 +137,7 @@ class Core
include_once 'Core/System/class.abstract.module.php';
include_once 'Core/System/class.modules.php';
include_once 'Core/System/class.libraries.php';
include_once 'Core/System/class.helpers.php';
// Load the core classes
new Config();
@ -147,6 +148,7 @@ class Core
new Router();
new Modules();
new Libraries();
new Helpers();
self::$loaded = true;
}

View File

@ -141,3 +141,13 @@ class ModuleException extends Exception
class LibraryException extends Exception
{
}
/**
* Class HelperException.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class HelperException extends Exception
{
}

View File

@ -0,0 +1,159 @@
<?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 - 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://fuzeworks.techfuze.net
* @since Version 0.0.1
*
* @version Version 0.0.1
*/
namespace FuzeWorks;
/**
* Helpers Class.
*
* @todo Add documentation
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class Helpers
{
protected static $helpers = array();
protected static $helperPaths = array('Application'.DS.'Helpers', 'Core'.DS.'Helpers');
public static function load($helperName, $directory = null)
{
// First determine the name of the helper
$helperName = strtolower(str_replace(array('_helper', '.php'), '', $helperName).'_helper');
// Determine what directories should be checked
$directories = (is_null($directory) ? self::$helperPaths : array($directory));
// Check it is already loaded
if (isset($helpers[$helperName]))
{
Logger::log("Helper '".$helperName."' is already loaded. Skipping");
return false;
}
// First check if there is an 'extension' class
$extendedHelper = Config::get('main')->application_prefix . $helperName;
$extendedHelperLoaded = false;
foreach ($directories as $helperPath)
{
$file = $helperPath . DS . $extendedHelper . '.php';
if (file_exists($file))
{
$extendedHelperLoaded = true;
$extendedHelperFile = $file;
}
}
// If an extension is loaded there needs to be a base helper
if ($extendedHelperLoaded)
{
$baseHelper = 'Core'.DS.'Helpers'.$helperName.'.php';
if (!file_exists($baseHelper))
{
throw new HelperException("Could not load helper. Base Helper not found while Extension loaded", 1);
}
// Fire the associated event
$event = Events::fireEvent('helperLoadEvent', $helperName, $baseHelper, $extendedHelper, $extendedHelperFile);
if ($event->isCancelled())
{
Logger::log("Not loading helper. Aborted by event");
return false;
}
include_once($event->extendedHelperFile);
include_once($event->helperFile);
self::$helpers[$event->helperName] = true;
Logger::log("Loading base helper '".$event->helperName."' and extended helper '".$event->extendedHelperName."'");
return true;
}
// If no extension exists, try loading a regular helper
foreach ($directories as $helperPath)
{
$file = $helperPath . DS . $helperName . '.php';
if (file_exists($file))
{
// Fire the associated event
$event = Events::fireEvent('helperLoadEvent', $helperName, $file);
if ($event->isCancelled())
{
Logger::log("Not loading helper. Aborted by event");
return false;
}
include_once($event->helperFile);
self::$helpers[$event->helperName] = true;
Logger::log("Loading helper '".$event->helperName."'");
return true;
}
}
throw new HelperException("Could not load helper. Helper not found.", 1);
}
public static function addHelperPath($directory)
{
if (!in_array($directory, $directories))
{
$directories[] = $directory;
}
}
public static function removeHelperPath($directory)
{
if (($key = array_search($directory, $directories)) !== false)
{
unset($directories[$key]);
}
}
public static function getHelperPaths()
{
return $directories;
}
}

View File

@ -32,9 +32,6 @@
namespace FuzeWorks;
use FuzeWorks\Logger;
use FuzeWorks\Config;
/**
* @todo add documentation
*
@ -101,7 +98,7 @@ class Libraries
// Retrieve the subclass prefix
$corePrefix = '\FuzeWorks\Library\FW_';
$appPrefix = '\Application\Library\\' . Config::get('main')->application_library_prefix;
$appPrefix = '\Application\Library\\' . Config::get('main')->application_prefix;
$prefix = $corePrefix;
// Perform a check to see if the library is already loaded
@ -151,7 +148,7 @@ class Libraries
include_once('Core'.DS.'Libraries'.DS.$subdir.$class.'.php');
// Now let's check for extensions
$subclass = Config::get('main')->application_library_prefix . $class;
$subclass = Config::get('main')->application_prefix . $class;
foreach ($directories as $directory)
{
$file = $directory . DS . $subdir . $subclass . '.php';
@ -251,24 +248,25 @@ class Libraries
Logger::log("Loaded Library: ".$class);
return $c = self::$libraries[$class];
}
}
public static function addLibraryPath($directory)
{
if (!in_array($directory, self::$libraryPaths))
{
self::$libraryPaths[] = $directory;
}
}
public static function removeLibraryPath($directory)
{
if (($key = array_search($directory, self::$libraryPaths)) !== false)
{
unset(self::$libraryPaths[$key]);
}
}
public static function getLibraryPaths()
{
return self::$libraryPaths;
}
}

View File

@ -400,6 +400,11 @@ class Router
$parameters
);
// Cancel if requested to do so
if ($event->isCancelled()) {
return;
}
Logger::log('Loading controller '.$event->className.' from file: '.$event->file);
// Check if the file exists