Implemented a (failed) config and a renewed Helpers class.

Helpers class is now fully supported by continuous integration.
This commit is contained in:
Abel Hoogeveen 2016-05-27 13:55:31 +02:00
parent 0adcdb7449
commit 68afa1ef1b
15 changed files with 578 additions and 243 deletions

View File

@ -30,8 +30,8 @@
* @version Version 0.0.1
*/
use FuzeWorks\Helpers;
use FuzeWorks\Logger;
use Fuzeworks\Factory;
/**
* Database Cache Class
@ -64,6 +64,13 @@ class FW_DB_Cache {
*/
public $db;
/**
* The FuzeWorks factory class
*
* @var Fuzeworks\Factory;
*/
private $factory;
// --------------------------------------------------------------------
/**
@ -75,7 +82,8 @@ class FW_DB_Cache {
public function __construct(&$db)
{
// Assign the main CI object to $this->CI and load the file helper since we use it a lot
Helpers::load('file');
$this->factory = Factory::getInstance();
$this->factory->getHelpers()->load('file');
$this->check_path();
}

View File

@ -32,7 +32,7 @@
use FuzeWorks\Logger;
use FuzeWorks\DatabaseException;
use FuzeWorks\Helpers;
use FuzeWorks\Utf8;
use FuzeWorks\Language;
/**
@ -373,6 +373,8 @@ abstract class FW_DB_driver {
}
}
$this->factory = Factory::getInstance();
Logger::log('Database Driver Class Initialized');
}
@ -1181,8 +1183,7 @@ abstract class FW_DB_driver {
*/
protected function _escape_str($str)
{
Helpers::load('common');
return str_replace("'", "''", remove_invisible_characters($str));
return str_replace("'", "''", Utf8::remove_invisible_characters($str));
}
// --------------------------------------------------------------------

View File

@ -78,6 +78,13 @@ abstract class FW_DB_utility {
*/
protected $_repair_table = FALSE;
/**
* The FuzeWorks factory class
*
* @var Fuzeworks\Factory;
*/
private $factory;
// --------------------------------------------------------------------
/**
@ -89,6 +96,7 @@ abstract class FW_DB_utility {
public function __construct(&$db)
{
$this->db =& $db;
$this->factory = Factory::getInstance();
Logger::log('Database Utility Class Initialized');
}
@ -296,7 +304,7 @@ abstract class FW_DB_utility {
extract($params);
// Load the xml helper
Helpers::load('xml');
$this->factory->getHelpers()->load('xml');
// Generate the result
$xml = '<'.$root.'>'.$newline;

View File

@ -30,7 +30,7 @@
* @version Version 0.0.1
*/
use FuzeWorks\Helpers;
use FuzeWorks\Utf8;
/**
* ODBC Database Adapter Class
@ -212,8 +212,7 @@ class FW_DB_odbc_driver extends FW_DB {
*/
protected function _escape_str($str)
{
Helpers::load('common');
return remove_invisible_characters($str);
return Utf8::remove_invisible_characters($str);
}
// --------------------------------------------------------------------

View File

@ -339,8 +339,8 @@ if ( ! function_exists('get_mime_by_extension'))
if ( ! is_array($mimes))
{
FuzeWorks\Helpers::load('common');
$mimes = get_mimes();
$factory = Factory::getInstance();
$mimes = $factory->getConfig()->get('mimes');
if (empty($mimes))
{

View File

@ -31,8 +31,7 @@
*/
namespace FuzeWorks\Library;
use FuzeWorks\Helpers;
use FuzeWorks\Config;
use FuzeWorks\Factory;
/**
* FuzeWorks File Caching Class
@ -55,6 +54,13 @@ class FW_Cache_file extends FW_Driver {
*/
protected $_cache_path;
/**
* The FuzeWorks factory class
*
* @var Fuzeworks\Factory;
*/
private $factory;
/**
* Initialize file-based cache
*
@ -62,10 +68,13 @@ class FW_Cache_file extends FW_Driver {
*/
public function __construct()
{
// Load the required helpers
Helpers::load('file');
// Load the factory
$this->factory = Factory::getInstance();
$path = Config::get('cache')->cache_file_path;
// Load the required helpers
$this->factory->getHelpers()->load('file');
$path = $this->factory->getConfig()->get('cache')->cache_file_path;
$this->_cache_path = ($path === '') ? 'Application'.DS.'Cache/' : $path;
}

View File

@ -32,7 +32,6 @@
namespace FuzeWorks\Library;
use FuzeWorks\Logger;
use FuzeWorks\Helpers;
/**
* FuzeWorks Zip Compression Class
@ -105,6 +104,13 @@ class FW_Zip {
*/
public $compression_level = 2;
/**
* The FuzeWorks factory class
*
* @var Fuzeworks\Factory;
*/
private $factory;
/**
* Initialize zip compression class
*
@ -113,6 +119,7 @@ class FW_Zip {
public function __construct()
{
$this->now = time();
$this->factory = Factory::getInstance();
Logger::log('Zip Compression Class Initialized');
}
@ -454,7 +461,7 @@ class FW_Zip {
$filename .= '.zip';
}
Helpers::load('download');
$this->factory->getHelpers()->load('download');
$get_zip = $this->get_zip();
$zip_content =& $get_zip;

View File

@ -33,8 +33,8 @@
namespace FuzeWorks;
use PDOException;
use FuzeWorks\ORM\ConfigFileORM;
use FuzeWorks\ORM\ConfigDatabaseORM;
use FuzeWorks\ConfigORM\ConfigORMAbstract;
use FuzeWorks\ConfigORM\ConfigFileORM;
/**
* Config Class.
@ -44,6 +44,7 @@ use FuzeWorks\ORM\ConfigDatabaseORM;
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
* @todo Fix the whole thing, it works, terribly
*/
class Config
{
@ -61,19 +62,191 @@ class Config
*/
private static $cfg = array();
/**
* All registered ORMs
*
* @var array of ORM names and objects
*/
private $ORMs = array();
/**
* The config tree, this is where all configs are stored whatever ORM they come from.
*
* @var array
*/
private $tree = array();
private $scope = 'Application/Config';
private static $factory;
public function __construct()
{
$this->loadDefaultORM();
}
public function getConfig($identifier, $scope = null, $appendRoot = true)
{
$scope = (is_null($scope) ? $this->scope : $scope);
$object = $this->getEndpointFromString($identifier, $appendRoot);
// Now check if we have an ORM or a result object
if ($object->isLoaded() === false)
{
$object->setScope($scope);
$object->retrieve();
}
return $object;
}
public function getEndpointFromString($string, $appendRoot = true)
{
// If the root directory needs to be appended, do so
if ($appendRoot === true)
{
$string = './' . str_replace(array('./'), '', $string);
}
// First sanitize the string
$string = preg_replace('#/+#', '/', $string);
// Check if the string is a valid filesystem string (which is the criteria for this system)
if (strpbrk($string, "\\/?%*:|\"<>") === TRUE)
{
throw new ConfigException('Could not get Config. Invalid string', 1);
}
// Prepare all variables for the search of the endpoint
$endpoints = explode('/', $string);
$ORM = null;
$ResultObject = null;
$tree = $this->tree;
$ORMTree = $endpoints;
// Cycle through the tree
for ($i=0; $i < count($endpoints); $i++)
{
// If this position in the tree is found, check all posibilities
$key = $endpoints[$i];
if (isset($tree[$key]))
{
// Is an ORM found? Register it as the last ORM for now
if (isset($tree[$key]['ORM']) && is_object($tree[$key]['ORM']))
{
$ORM = $tree[$key]['ORM'];
if ($ORM->isLoaded() === false)
{
$ORM = clone $ORM;
}
array_shift($ORMTree);
}
}
else
{
// Not found, use last found ORM
break;
}
// Update the working tree
$tree = $tree[$key];
}
// If absolutely nothing is found, throw an exception
if (is_null($ORM))
{
throw new ConfigException("Could not get Config. '".$string."'. No Object Mapper found.");
}
// And add the ORM to the final point
$last = end($endpoints);
reset($endpoints);
$endpoints[$last]['ORM'] = $ORM;
$this->tree = array_merge($this->tree, $endpoints);
// Determine the object to be loaded
if ($ORM->isLoaded() === false)
{
$ORM->setTree($ORMTree);
}
return $ORM;
}
public function registerEndpoint($endpointString, $ORMName, $appendRoot = true)
{
if (!isset($this->ORMs[$ORMName]))
{
throw new ConfigException("Could not register endpoint. ORM '".$ORMName."' is not known (not registered?)", 1);
}
// If the root directory needs to be appended, do so
if ($appendRoot === true)
{
$endpointString = './' . str_replace(array('./'), '', $endpointString);
}
// First sanitize the string
$endpointString = preg_replace('#/+#', '/', $endpointString);
// Check if the string is a valid filesystem string (which is the criteria for this system)
if (strpbrk($endpointString, "\\/?%*:|\"<>") === TRUE)
{
throw new ConfigException('Could not register endpoint. Invalid string', 1);
}
// Prepare all variables for the search of the endpoint
$endpoints = explode('/', $endpointString);
// Check for illegal tags
if (in_array('ORM', $endpoints) || in_array('ResultObject', $endpoints))
{
throw new ConfigException('Could not register endpoint. Endpoint can not contain \'ORM\' or \'ResultObject\' values.');
}
// And add the ORM to the final point
$last = end($endpoints);
reset($endpoints);
$endpoints[$last]['ORM'] = clone $this->ORMs[$ORMName];
// And finally merge the tree
$this->tree = array_merge($this->tree, $endpoints);
}
public function registerORM(ConfigORMAbstract $ORMObject, $ORMName, $appendRoot = true)
{
// First check if the ORM already exists
if (isset($this->ORMs[$ORMName]))
{
throw new ConfigException("Could not register ORM. ORM '".$ORMName."' already registered", 1);
}
// Then validate the ORM
if ($ORMObject instanceof ConfigORMAbstract)
{
$this->ORMs[$ORMName] = $ORMObject;
}
// And log it
return true;
}
/**
* Loads a config file and returns it as an object.
*
* @param string config file name
* @param string directory, default is Application/Config
*
* @throws \Exception on file not found
* @throws FuzeWorks\ConfigException on file not found
*
* @return \FuzeWorks\ORM\ConfigORM of config
* @return FuzeWorks\ORM\ConfigORM of config
*/
public static function loadConfigFile($name, $directory = null)
public static function loadConfigFile($name, $scope = null)
{
$dir = (isset($directory) ? $directory : 'Application/Config/');
$dir = (isset($scope) ? $scope : 'Application/Config/');
$file = $dir.'config.'.strtolower($name).'.php';
// If already loaded, return a reference to the ORM
@ -104,22 +277,71 @@ class Config
}
}
public function loadDefaultORM()
{
$this->registerORM(new ConfigFileORM(), 'File');
$this->registerEndpoint('.', 'File', false);
}
public function reset()
{
// Reset the variables
$this->tree = array();
$this->ORMs = array();
}
public function getDirectory()
{
return $this->scope;
}
public function setDirectory($scope)
{
// Check if the string is a valid filesystem string (which is the criteria for this system)
if (strpbrk($scope, "\\/?%*:|\"<>") === TRUE)
{
throw new ConfigException('Could not set directory. Invalid string', 1);
}
$this->scope = $scope;
}
/**
* Retrieves a config file from the Application folder
*
* @param string config file name
*
* @return \FuzeWorks\ORM\ConfigORM of config
* @return FuzeWorks\ORM\ConfigORM of config
*/
public static function get($name)
{
return self::loadConfigFile($name);
if (!is_object(self::$factory))
{
self::$factory = Factory::getInstance();
}
$config = self::$factory->getConfig();
return $config->getConfig($name);
}
}
namespace FuzeWorks\ORM;
namespace FuzeWorks\ConfigORM;
use Iterator;
use FuzeWorks\ConfigException;
interface ConfigORMInterface {
public function setTree($tree);
public function commit();
public function setScope($scope);
public function retrieve();
/**
* Whether the ConfigORM is already loaded.
*
* If true, the config should not be reloaded
*/
public function isLoaded();
}
/**
* Abstract ConfigORM class.
@ -130,7 +352,7 @@ use Iterator;
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
abstract class ConfigORM implements Iterator
abstract class ConfigORMAbstract implements Iterator
{
/**
* The original state of a config file. Can be reverted to using revert().
@ -250,162 +472,6 @@ abstract class ConfigORM implements Iterator
}
}
/**
* ORM class for config files in a database.
*
* Handles entries in the database of FuzeWorks and is able to dynamically update them when requested
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class ConfigDatabaseORM extends ConfigORM
{
/**
* The current connection to the database.
*
* @var \FuzeWorks\Database Database Connection
*/
private $dbh;
/**
* whether the database connection has been successfully established.
*
* @var bool true on success
*/
public $success = false;
/**
* The current filename.
*
* @var string filename
*/
private $file;
/**
* Sets up the class and the connection to the database.
*
* @param \FuzeWorks\Database $db The Database connection
* @param string $filename The current filename
*
* @throws ConfigException on fatal error
*/
public function __construct($db, $filename)
{
$this->dbh = $db;
$this->cfg = $this->openDb($filename);
$this->originalCfg = $this->cfg;
$this->file = $filename;
}
/**
* Opens up a database connection with the requested filename.
*
* @param string $name Name of the file
*
* @return array Content of the file
*
* @throws ConfigException on fatal error
*/
private function openDb($name)
{
$prefix = $this->dbh->getPrefix();
try {
$stmnt = $this->dbh->prepare('SELECT * FROM '.$prefix.'config WHERE `file` = ?');
$stmnt->execute(array($name));
} catch (PDOException $e) {
throw new ConfigException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
}
// Fetch results
$result = $stmnt->fetchAll(\PDO::FETCH_ASSOC);
$return = array();
for ($i = 0; $i < count($result); ++$i) {
$return[ $result[$i]['key'] ] = $result[$i]['value'];
}
// Return if found in DB
if (!empty($return)) {
$this->success = true;
return (array) $return;
}
}
/**
* Write config updates to the database.
*
* @throws ConfigException on fatal error
*/
private function writeDb()
{
// First arrays of all the fields that need to change
$changed_fields = array();
$removed_fields = array();
$new_fields = array();
// First check for changed and new feeds
foreach ($this->cfg as $key => $value) {
if (isset($this->originalCfg[$key])) {
if ($this->originalCfg[$key] != $value) {
// Changed field
$changed_fields[$key] = $value;
}
} else {
// New field
$new_fields[$key] = $value;
}
}
// Then check for removed fields
foreach ($this->originalCfg as $key => $value) {
if (!isset($this->cfg[$key])) {
$removed_fields[$key] = $value;
}
}
// First for the removed values
$prefix = $this->dbh->getPrefix();
try {
$stmnt = $this->dbh->prepare('DELETE FROM '.$prefix.'config WHERE `file` = :file AND `key` = :key');
foreach ($removed_fields as $key => $value) {
$stmnt->execute(array('file' => $this->file, 'key' => $key));
}
} catch (PDOException $e) {
throw new ConfigException('Could not change config due to PDOException: '.$e->getMessage(), 1);
}
// Then for the changed values
try {
$stmnt = $this->dbh->prepare('UPDATE '.$prefix.'config SET `value` = :value WHERE `file` = :file AND `key` = :key');
foreach ($changed_fields as $key => $value) {
$stmnt->execute(array('file' => $this->file, 'key' => $key, 'value' => $value));
}
} catch (PDOException $e) {
throw new ConfigException('Could not change config due to PDOException: '.$e->getMessage(), 1);
}
// And finally for the new values
try {
$stmnt = $this->dbh->prepare('INSERT INTO '.$prefix.'config (`file`,`key`,`value`) VALUES (:file,:key,:value)');
foreach ($new_fields as $key => $value) {
$stmnt->execute(array('file' => $this->file, 'key' => $key, 'value' => $value));
}
} catch (PDOException $e) {
throw new ConfigException('Could not change config due to PDOException: '.$e->getMessage(), 1);
}
}
/**
* Write updates of the config file to the database.
*
* @throws ConfigException on fatal error
*/
public function commit()
{
$this->writeDb();
}
}
/**
* ORM class for config files in PHP files.
*
@ -414,7 +480,7 @@ class ConfigDatabaseORM extends ConfigORM
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
*/
class ConfigFileORM extends ConfigORM
class ConfigFileORM extends ConfigORMAbstract implements ConfigORMInterface
{
/**
* The current filename.
@ -423,6 +489,12 @@ class ConfigFileORM extends ConfigORM
*/
private $file;
private $loaded = false;
private $tree = array();
private $directory = 'Application/Config';
/**
* Sets up the class and the connection to the PHP file.
*
@ -430,9 +502,13 @@ class ConfigFileORM extends ConfigORM
*
* @throws ConfigException on fatal error
*/
public function __construct($file)
public function __construct($file = null)
{
if (file_exists($file)) {
if (is_null($file))
{
return;
}
elseif (file_exists($file)) {
$this->file = $file;
$this->openFile($file);
$this->originalCfg = $this->cfg;
@ -441,6 +517,40 @@ class ConfigFileORM extends ConfigORM
}
}
public function isLoaded()
{
return $this->loaded;
}
public function setTree($tree)
{
$this->tree = $tree;
}
public function retrieve()
{
// Retrieve the value from the tree
$file = end($this->tree);
$fileKey = key($this->tree);
// Replace it with the new one
$actualFile = 'config.'.$file.'.php';
unset($this->tree[$fileKey]);
$this->tree[] = $actualFile;
// Create the filestring and open it
$this->file = preg_replace('#/+#', '/', $this->directory . '/' . implode('/', $this->tree));
$this->openFile($this->file);
$this->originalCfg = $this->file;
$this->loaded = true;
}
public function setScope($scope)
{
$this->directory = $scope;
}
/**
* Opens the file and returns the data.
*

View File

@ -154,22 +154,6 @@ class Core
include_once 'Core/System/class.factory.php';
// Load the core classes
new Config();
new Logger();
new Events();
new Models();
new Layout();
new Modules();
new Libraries();
new Helpers();
new Database();
new Language();
new Utf8();
new URI();
new Security();
new Input();
new Router();
new Output();
new Factory();
self::$loaded = true;

View File

@ -46,8 +46,23 @@ class Factory
public function __construct()
{
$this->instances['Layout'] = new Layout();
self::$sharedFactoryInstance = $this;
$this->instances['Config'] = new Config();
$this->instances['Logger'] = new Logger();
$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();
$this->instances['Language'] = new Language();
$this->instances['Utf8'] = new Utf8();
$this->instances['URI'] = new URI();
$this->instances['Security'] = new Security();
$this->instances['Input'] = new Input();
$this->instances['Router'] = new Router();
$this->instances['Output'] = new Output();
}
public function newInstance($className, $namespace = 'FuzeWorks\\')
@ -57,44 +72,49 @@ class Factory
$this->instances[$instanceName] = new $className();
}
public static function getInstance()
{
return clone self::$sharedFactoryInstance;
public static function getInstance($cloneInstance = true)
{
if ($cloneInstance === true)
{
return clone self::$sharedFactoryInstance;
}
return self::$sharedFactoryInstance;
}
public function getConfig()
{
return new Config();
return $this->instances['Config'];
}
public function getCore()
{
return $this->instances['Core'];
}
public function getDatabase()
{
return $this->instances['Database'];
}
public function getEvents()
{
return $this->instances['Events'];
}
public function getHelpers()
{
return $this->instances['Helpers'];
}
public function getInput()
{
return $this->instances['Input'];
}
public function getLanguage()
{
return $this->instances['Language'];
}
public function getLayout()
@ -104,47 +124,47 @@ class Factory
public function getLibraries()
{
return $this->instances['Config'];
}
public function getLogger()
{
return $this->instances['Logger'];
}
public function getModels()
{
return $this->instances['Models'];
}
public function getModules()
{
return $this->instances['Modules'];
}
public function getOutput()
{
return $this->instances['Output'];
}
public function getRouter()
{
return $this->instances['Router'];
}
public function getSecurity()
{
return $this->instances['Security'];
}
public function getUri()
{
return $this->instances['URI'];
}
public function getUtf8()
{
return $this->instances['Utf8'];
}

View File

@ -35,7 +35,18 @@ namespace FuzeWorks;
/**
* Helpers Class.
*
* @todo Add documentation
* Helpers, as the name suggests, help you with tasks.
*
* Each helper file is simply a collection of functions in a particular category.
* There are URL Helpers, that assist in creating links, there are Form Helpers that help you create form elements,
* Text Helpers perform various text formatting routines, Cookie Helpers set and read cookies,
* File Helpers help you deal with files, etc.
*
* Unlike most other systems in FuzeWorks, Helpers are not written in an Object Oriented format.
* They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions.
*
* FuzeWorks does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded,
* it becomes globally available to everything.
*
* @author Abel Hoogeveen <abel@techfuze.net>
* @copyright Copyright (c) 2013 - 2016, Techfuze. (http://techfuze.net)
@ -43,20 +54,42 @@ namespace FuzeWorks;
class Helpers
{
protected static $helpers = array();
/**
* Array of loadedHelpers, so that they won't be reloaded
*
* @var array Array of loaded helperNames
*/
protected $helpers = array();
protected static $helperPaths = array('Application'.DS.'Helpers', 'Core'.DS.'Helpers');
/**
* Paths where Helpers can be found.
*
* Libraries will only be loaded if either a directory is supplied or it is in one of the helperPaths
*
* @var array Array of paths where helpers can be found
*/
protected $helperPaths = array('Application'.DS.'Helpers', 'Core'.DS.'Helpers');
public static function load($helperName, $directory = null)
/**
* Load a helper.
*
* Supply the name and the helper will be loaded from the supplied directory,
* or from one of the helperPaths (which you can add).
*
* @param string $helperName Name of the helper
* @param string|null $directory Directory to load the helper from, will ignore $helperPaths
* @return bool Whether the helper was succesfully loaded (true if yes)
*/
public 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));
$directories = (is_null($directory) ? $this->helperPaths : array($directory));
// Check it is already loaded
if (isset($helpers[$helperName]))
if (isset($this->helpers[$helperName]))
{
Logger::log("Helper '".$helperName."' is already loaded. Skipping");
return false;
@ -94,7 +127,7 @@ class Helpers
include_once($event->extendedHelperFile);
include_once($event->helperFile);
self::$helpers[$event->helperName] = true;
$this->helpers[$event->helperName] = true;
Logger::log("Loading base helper '".$event->helperName."' and extended helper '".$event->extendedHelperName."'");
return true;
}
@ -115,7 +148,7 @@ class Helpers
}
include_once($event->helperFile);
self::$helpers[$event->helperName] = true;
$this->helpers[$event->helperName] = true;
Logger::log("Loading helper '".$event->helperName."'");
return true;
}
@ -124,29 +157,54 @@ class Helpers
throw new HelperException("Could not load helper. Helper not found.", 1);
}
public static function get($helperName, $directory = null)
/**
* Alias for load
* @see load
*
* @param string $helperName Name of the helper
* @param string|null $directory Directory to load the helper from, will ignore $helperPaths
* @return bool Whether the helper was succesfully loaded (true if yes)
*/
public function get($helperName, $directory = null)
{
return self::load($helperName, $directory);
return $this->load($helperName, $directory);
}
public static function addHelperPath($directory)
/**
* Add a path where helpers can be found
*
* @param string $directory The directory
* @return void
*/
public function addHelperPath($directory)
{
if (!in_array($directory, $directories))
if (!in_array($directory, $this->helperPaths))
{
$directories[] = $directory;
$this->helperPaths[] = $directory;
}
}
public static function removeHelperPath($directory)
/**
* Remove a path where helpers can be found
*
* @param string $directory The directory
* @return void
*/
public function removeHelperPath($directory)
{
if (($key = array_search($directory, $directories)) !== false)
if (($key = array_search($directory, $this->helperPaths)) !== false)
{
unset($directories[$key]);
unset($this->helperPaths[$key]);
}
}
public static function getHelperPaths()
/**
* Get a list of all current helperPaths
*
* @return array Array of paths where helpers can be found
*/
public function getHelperPaths()
{
return $directories;
return $this->helperPaths;
}
}

View File

@ -166,8 +166,7 @@ class Utf8 {
*/
public static function safe_ascii_for_xml($str)
{
Helpers::load('common');
return remove_invisible_characters($str, FALSE);
return self::remove_invisible_characters($str, FALSE);
}
// --------------------------------------------------------------------

112
tests/core_helperTest.php Normal file
View File

@ -0,0 +1,112 @@
<?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
*/
use FuzeWorks\Factory;
/**
* Class HelperTest.
*
* Helpers testing suite, will test basic loading of Helpers
*/
class helperTest extends CoreTestAbstract
{
protected $helpers;
public function setUp()
{
$factory = Factory::getInstance();
$this->helpers = $factory->getHelpers();
}
public function testGetHelpersClass()
{
$this->assertInstanceOf('FuzeWorks\Helpers', $this->helpers);
}
public function testLoadHelper()
{
// First test if the function/helper is not loaded yet
$this->assertFalse(function_exists('testHelperFunction'));
// Test if the helper is properly loaded
$this->assertTrue($this->helpers->load('test', 'tests/helpers/testLoadHelper/'));
// Test if the function exists now
$this->assertTrue(function_exists('testHelperFunction'));
}
/**
* @expectedException FuzeWorks\HelperException
*/
public function testAddHelperPathFail()
{
// First test if the function is not loaded yet
$this->assertFalse(function_exists('testAddHelperPathFunction'));
// Now test if the helper can be loaded (hint: it can not)
$this->helpers->load('testAddHelperPath');
}
/**
* @depends testAddHelperPathFail
*/
public function testAddHelperPath()
{
// Add the helperPath
$this->helpers->addHelperPath('tests/helpers/testAddHelperPath');
// And try to load it again
$this->assertTrue($this->helpers->load('testAddHelperPath'));
// And test if the function is loaded
$this->assertTrue(function_exists('testAddHelperPathFunction'));
}
public function testRemoveHelperPath()
{
// Test if the path does NOT exist
$this->assertFalse(in_array('tests/helpers/testRemoveHelperPath', $this->helpers->getHelperPaths()));
// Add it
$this->helpers->addHelperPath('tests/helpers/testRemoveHelperPath');
// Assert if it's there
$this->assertTrue(in_array('tests/helpers/testRemoveHelperPath', $this->helpers->getHelperPaths()));
// Remove it
$this->helpers->removeHelperPath('tests/helpers/testRemoveHelperPath');
// And test if it's gone again
$this->assertFalse(in_array('tests/helpers/testRemoveHelperPath', $this->helpers->getHelperPaths()));
}
}

View File

@ -0,0 +1,10 @@
<?php
if ( ! function_exists('testAddHelperPathFunction'))
{
function testAddHelperPathFunction($someParameter)
{
return 'SomeResult';
}
}

View File

@ -0,0 +1,10 @@
<?php
if ( ! function_exists('testHelperFunction'))
{
function testHelperFunction($someParameter)
{
return 'SomeResult';
}
}