From 66c9bb45c54d88043df6a3c5c9aa2d49d63270c7 Mon Sep 17 00:00:00 2001 From: Abel Hoogeveen Date: Mon, 16 Apr 2018 20:17:17 +0200 Subject: [PATCH] Implemented all changes requested by the TechFuze team. --- src/FuzeWorks/Layout.php | 10 + src/FuzeWorks/Libraries.php | 4 +- src/FuzeWorks/URI.php | 2 +- src/Libraries/Cache/Cache.php | 17 +- src/Libraries/Cache/Cache_Interface.php | 109 +++++++++++ src/Libraries/Cache/drivers/Cache_apc.php | 6 +- src/Libraries/Cache/drivers/Cache_array.php | 182 ++++++++++++++++++ src/Libraries/Cache/drivers/Cache_dummy.php | 6 +- src/Libraries/Cache/drivers/Cache_file.php | 19 +- .../Cache/drivers/Cache_memcached.php | 6 +- src/Libraries/Cache/drivers/Cache_redis.php | 6 +- .../Cache/drivers/Cache_wincache.php | 6 +- 12 files changed, 335 insertions(+), 38 deletions(-) create mode 100644 src/Libraries/Cache/Cache_Interface.php create mode 100644 src/Libraries/Cache/drivers/Cache_array.php diff --git a/src/FuzeWorks/Layout.php b/src/FuzeWorks/Layout.php index b8d1aa0..ffb77c1 100644 --- a/src/FuzeWorks/Layout.php +++ b/src/FuzeWorks/Layout.php @@ -408,6 +408,16 @@ class Layout throw new LayoutException('Could not set engine. Engine does not exist', 1); } + /** + * Reset the engine so a new one can be used the next time + * + */ + public function resetEngine() + { + $this->current_engine = null; + Logger::log('Reset the active engine for Layout'); + } + /** * Get a loaded template engine. * diff --git a/src/FuzeWorks/Libraries.php b/src/FuzeWorks/Libraries.php index d2da8c4..4f05b67 100644 --- a/src/FuzeWorks/Libraries.php +++ b/src/FuzeWorks/Libraries.php @@ -348,10 +348,10 @@ class Libraries } // If required to do so, return the existing instance or load a new one - if ($newInstance) + if (!$newInstance) { $this->factory->logger->log("Library '".$className."' already loaded. Returning existing instance"); - return $this->libraries[$prefix.$class]; + return $this->libraries[$className]; } $this->factory->logger->log("Library '".$className."' already loaded. Returning new instance"); diff --git a/src/FuzeWorks/URI.php b/src/FuzeWorks/URI.php index 0f6d690..13328d5 100644 --- a/src/FuzeWorks/URI.php +++ b/src/FuzeWorks/URI.php @@ -154,7 +154,7 @@ class URI { : $this->_parse_request_uri(); break; } - + $this->_set_uri_string($uri, FALSE); } } diff --git a/src/Libraries/Cache/Cache.php b/src/Libraries/Cache/Cache.php index a28e2fc..58460b1 100644 --- a/src/Libraries/Cache/Cache.php +++ b/src/Libraries/Cache/Cache.php @@ -4,7 +4,7 @@ * * The FuzeWorks MVC PHP FrameWork * - * Copyright (C) 2015 TechFuze + * Copyright (C) 2018 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 - 2018, 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.1.5 */ namespace FuzeWorks\Library; @@ -53,6 +53,7 @@ class FW_Cache extends FW_Driver_Library { * @var array */ protected $valid_drivers = array( + 'array', 'apc', 'dummy', 'file', @@ -99,6 +100,8 @@ class FW_Cache extends FW_Driver_Library { */ public function __construct($config = array()) { + require_once('Cache_Interface.php'); + isset($config['adapter']) && $this->_adapter = $config['adapter']; isset($config['backup']) && $this->_backup_driver = $config['backup']; isset($config['key_prefix']) && $this->key_prefix = $config['key_prefix']; @@ -148,9 +151,9 @@ class FW_Cache extends FW_Driver_Library { * @param bool $raw Whether to store the raw value * @return bool TRUE on success, FALSE on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { - return $this->{$this->_adapter}->save($this->key_prefix.$id, $data, $ttl, $raw); + return $this->{$this->_adapter}->set($this->key_prefix.$id, $data, $ttl, $raw); } // ------------------------------------------------------------------------ @@ -161,9 +164,9 @@ class FW_Cache extends FW_Driver_Library { * @param string $id Cache ID * @return bool TRUE on success, FALSE on failure */ - public function delete($id) + public function remove($id) { - return $this->{$this->_adapter}->delete($this->key_prefix.$id); + return $this->{$this->_adapter}->remove($this->key_prefix.$id); } // ------------------------------------------------------------------------ diff --git a/src/Libraries/Cache/Cache_Interface.php b/src/Libraries/Cache/Cache_Interface.php new file mode 100644 index 0000000..d031fa4 --- /dev/null +++ b/src/Libraries/Cache/Cache_Interface.php @@ -0,0 +1,109 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2018, 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.1.5 + * + * @version Version 1.1.5 + */ + +namespace FuzeWorks\Library; + +interface CacheInterface +{ + + /** + * Retrieve an item from the cache, resolves with its value on + * success or null when no item can be found. + * + * @param string $key + * @return mixed + */ + public function get($key); + /** + * Store an item in the cache. + * + * @param string $key + * @param mixed $value + * @return void + */ + public function set($key, $value, $ttl = 60, $raw = FALSE); + /** + * Remove an item from the cache. + * + * @param string $key + * @return void + */ + public function remove($key); + + /** + * Increment a raw value + * + * @param string $id Cache ID + * @param int $offset Step/value to add + * @return mixed New value on success or FALSE on failure + */ + public function increment($id, $offset = 1); + + /** + * Decrement a raw value + * + * @param string $id Cache ID + * @param int $offset Step/value to reduce by + * @return mixed New value on success or FALSE on failure + */ + public function decrement($id, $offset = 1); + + /** + * Clean the cache + * + * @return bool TRUE on success, FALSE on failure + */ + public function clean(); + + /** + * Cache Info + * + * @param string $type = 'user' user/filehits + * @return mixed array containing cache info on success OR FALSE on failure + */ + public function cache_info($type = 'user'); + + /** + * Get Cache Metadata + * + * @param string $id key to get cache metadata on + * @return mixed cache item metadata + */ + public function get_metadata($id); + + /** + * Is the requested driver supported in this environment? + * + * @return array + */ + public function is_supported(); +} diff --git a/src/Libraries/Cache/drivers/Cache_apc.php b/src/Libraries/Cache/drivers/Cache_apc.php index 7c12fa3..afb926d 100644 --- a/src/Libraries/Cache/drivers/Cache_apc.php +++ b/src/Libraries/Cache/drivers/Cache_apc.php @@ -45,7 +45,7 @@ use FuzeWorks\Logger; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_apc extends FW_Driver { +class FW_Cache_apc extends FW_Driver implements CacheInterface { /** * Class constructor @@ -100,7 +100,7 @@ class FW_Cache_apc extends FW_Driver { * @param bool $raw Whether to store the raw value * @return bool TRUE on success, FALSE on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { $ttl = (int) $ttl; @@ -119,7 +119,7 @@ class FW_Cache_apc extends FW_Driver { * @param mixed unique identifier of the item in the cache * @return bool true on success/false on failure */ - public function delete($id) + public function remove($id) { return apc_delete($id); } diff --git a/src/Libraries/Cache/drivers/Cache_array.php b/src/Libraries/Cache/drivers/Cache_array.php new file mode 100644 index 0000000..0383a82 --- /dev/null +++ b/src/Libraries/Cache/drivers/Cache_array.php @@ -0,0 +1,182 @@ +. + * + * @author TechFuze + * @copyright Copyright (c) 2013 - 2018, 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.1.5 + * + * @version Version 1.1.5 + */ + +namespace FuzeWorks\Library; + +/** + * FuzeWorks Array Caching Class + * + * @author TechFuze + */ +class FW_Cache_array extends FW_Driver implements CacheInterface +{ + + private $data = array(); + + /** + * Cache Get + * + * Since this is the dummy class, it's always going to return FALSE. + * + * @param string + * @return bool FALSE + */ + public function get($id) + { + if (isset($this->data[$id])) + { + $expire = $this->data[$id]['expire']; + if (date('U') > $expire ) + { + unset($this->data[$id]); + } + + return $this->data[$id]['data']; + } + + return false; + } + + // ------------------------------------------------------------------------ + + /** + * Cache Save + * + * @param string Unique Key + * @param mixed Data to store + * @param int Length of time (in seconds) to cache the data + * @param bool Whether to store the raw value + * @return bool TRUE, Simulating success + */ + public function set($id, $data, $ttl = 60, $raw = FALSE) + { + $this->data[$id] = array('data' => $data, 'expire' => date('U') + $ttl); + return true; + } + + // ------------------------------------------------------------------------ + + /** + * Delete from Cache + * + * @param mixed unique identifier of the item in the cache + * @return bool TRUE, simulating success + */ + public function remove($id) + { + if (isset($this->data[$id])) + unset($this->data[$id]); + + return false; + } + + // ------------------------------------------------------------------------ + + /** + * Increment a raw value + * + * @param string $id Cache ID + * @param int $offset Step/value to add + * @return mixed New value on success or FALSE on failure + */ + public function increment($id, $offset = 1) + { + return TRUE; + } + + // ------------------------------------------------------------------------ + + /** + * Decrement a raw value + * + * @param string $id Cache ID + * @param int $offset Step/value to reduce by + * @return mixed New value on success or FALSE on failure + */ + public function decrement($id, $offset = 1) + { + return TRUE; + } + + // ------------------------------------------------------------------------ + + /** + * Clean the cache + * + * @return bool TRUE, simulating success + */ + public function clean() + { + $this->data = array(); + return true; + } + + // ------------------------------------------------------------------------ + + /** + * Cache Info + * + * @param string user/filehits + * @return bool FALSE + */ + public function cache_info($type = NULL) + { + return FALSE; + } + + // ------------------------------------------------------------------------ + + /** + * Get Cache Metadata + * + * @param mixed key to get cache metadata on + * @return bool FALSE + */ + public function get_metadata($id) + { + return FALSE; + } + + // ------------------------------------------------------------------------ + + /** + * Is this caching driver supported on the system? + * Of course this one is. + * + * @return bool TRUE + */ + public function is_supported() + { + return TRUE; + } + +} diff --git a/src/Libraries/Cache/drivers/Cache_dummy.php b/src/Libraries/Cache/drivers/Cache_dummy.php index a2067a7..53d67ea 100644 --- a/src/Libraries/Cache/drivers/Cache_dummy.php +++ b/src/Libraries/Cache/drivers/Cache_dummy.php @@ -44,7 +44,7 @@ namespace FuzeWorks\Library; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_dummy extends FW_Driver { +class FW_Cache_dummy extends FW_Driver implements CacheInterface { /** * Get @@ -70,7 +70,7 @@ class FW_Cache_dummy extends FW_Driver { * @param bool Whether to store the raw value * @return bool TRUE, Simulating success */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { return TRUE; } @@ -83,7 +83,7 @@ class FW_Cache_dummy extends FW_Driver { * @param mixed unique identifier of the item in the cache * @return bool TRUE, simulating success */ - public function delete($id) + public function remove($id) { return TRUE; } diff --git a/src/Libraries/Cache/drivers/Cache_file.php b/src/Libraries/Cache/drivers/Cache_file.php index cea40c1..ca7269b 100644 --- a/src/Libraries/Cache/drivers/Cache_file.php +++ b/src/Libraries/Cache/drivers/Cache_file.php @@ -27,7 +27,7 @@ * @link http://techfuze.net/fuzeworks * @since Version 0.0.1 * - * @version Version 1.0.0 + * @version Version 1.1.5 */ namespace FuzeWorks\Library; @@ -46,7 +46,7 @@ use FuzeWorks\Core; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_file extends FW_Driver { +class FW_Cache_file extends FW_Driver implements CacheInterface { /** * Directory in which to save cache files @@ -55,13 +55,6 @@ class FW_Cache_file extends FW_Driver { */ protected $_cache_path; - /** - * The FuzeWorks factory class - * - * @var Fuzeworks\Factory; - */ - private $factory; - /** * Initialize file-based cache * @@ -70,10 +63,10 @@ class FW_Cache_file extends FW_Driver { public function __construct() { // Load the factory - $this->factory = Factory::getInstance(); + $factory = Factory::getInstance(); // Load the required helpers - $this->factory->helpers->load('file'); + $factory->helpers->load('file'); $this->_cache_path = Core::$tempDir . DS . 'CacheLibrary' . DS; } @@ -102,7 +95,7 @@ class FW_Cache_file extends FW_Driver { * @param bool $raw Whether to store the raw value (unused) * @return bool TRUE on success, FALSE on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { $contents = array( 'time' => time(), @@ -127,7 +120,7 @@ class FW_Cache_file extends FW_Driver { * @param mixed unique identifier of item in cache * @return bool true on success/false on failure */ - public function delete($id) + public function remove($id) { return file_exists($this->_cache_path.$id) ? unlink($this->_cache_path.$id) : FALSE; } diff --git a/src/Libraries/Cache/drivers/Cache_memcached.php b/src/Libraries/Cache/drivers/Cache_memcached.php index 8440f67..8a44954 100644 --- a/src/Libraries/Cache/drivers/Cache_memcached.php +++ b/src/Libraries/Cache/drivers/Cache_memcached.php @@ -48,7 +48,7 @@ use Memcache; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_memcached extends FW_Driver { +class FW_Cache_memcached extends FW_Driver implements CacheInterface { /** * Holds the memcached object @@ -153,7 +153,7 @@ class FW_Cache_memcached extends FW_Driver { * @param bool $raw Whether to store the raw value * @return bool TRUE on success, FALSE on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { if ($raw !== TRUE) { @@ -180,7 +180,7 @@ class FW_Cache_memcached extends FW_Driver { * @param mixed $id key to be deleted. * @return bool true on success, false on failure */ - public function delete($id) + public function remove($id) { return $this->_memcached->delete($id); } diff --git a/src/Libraries/Cache/drivers/Cache_redis.php b/src/Libraries/Cache/drivers/Cache_redis.php index 365cc32..a97b9d8 100644 --- a/src/Libraries/Cache/drivers/Cache_redis.php +++ b/src/Libraries/Cache/drivers/Cache_redis.php @@ -48,7 +48,7 @@ use RedisException; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_redis extends FW_Driver +class FW_Cache_redis extends FW_Driver implements CacheInterface { /** * Default config @@ -165,7 +165,7 @@ class FW_Cache_redis extends FW_Driver * @param bool $raw Whether to store the raw value (unused) * @return bool TRUE on success, FALSE on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { if (is_array($data) OR is_object($data)) { @@ -194,7 +194,7 @@ class FW_Cache_redis extends FW_Driver * @param string $key Cache key * @return bool */ - public function delete($key) + public function remove($key) { if ($this->_redis->delete($key) !== 1) { diff --git a/src/Libraries/Cache/drivers/Cache_wincache.php b/src/Libraries/Cache/drivers/Cache_wincache.php index acbc35f..27decb5 100644 --- a/src/Libraries/Cache/drivers/Cache_wincache.php +++ b/src/Libraries/Cache/drivers/Cache_wincache.php @@ -48,7 +48,7 @@ use FuzeWorks\Logger; * @link * @license http://opensource.org/licenses/MIT MIT License */ -class FW_Cache_wincache extends FW_Driver { +class FW_Cache_wincache extends FW_Driver implements CacheInterface { /** * Class constructor @@ -97,7 +97,7 @@ class FW_Cache_wincache extends FW_Driver { * @param bool $raw Whether to store the raw value (unused) * @return bool true on success/false on failure */ - public function save($id, $data, $ttl = 60, $raw = FALSE) + public function set($id, $data, $ttl = 60, $raw = FALSE) { return wincache_ucache_set($id, $data, $ttl); } @@ -110,7 +110,7 @@ class FW_Cache_wincache extends FW_Driver { * @param mixed unique identifier of the item in the cache * @return bool true on success/false on failure */ - public function delete($id) + public function remove($id) { return wincache_ucache_delete($id); }