From 2460b678869415fff08238afdd61e05eba501dc5 Mon Sep 17 00:00:00 2001 From: logmanoriginal Date: Tue, 18 Jun 2019 19:04:19 +0200 Subject: [PATCH] cache: Refactor cache factory to non-static class The cache factory can be based on the abstract factory class if it wasn't static. This allows for higher abstraction and makes future extensions possible. Also, not all parts of RSS-Bridge need to work on the same instance of the factory. References #1001 --- actions/DisplayAction.php | 4 +- bridges/ElloBridge.php | 4 +- lib/{Cache.php => CacheFactory.php} | 88 ++++------------------------- lib/contents.php | 8 ++- lib/rssbridge.php | 3 +- 5 files changed, 25 insertions(+), 82 deletions(-) rename lib/{Cache.php => CacheFactory.php} (61%) diff --git a/actions/DisplayAction.php b/actions/DisplayAction.php index 1dec5cbffa0..17bc28b7225 100644 --- a/actions/DisplayAction.php +++ b/actions/DisplayAction.php @@ -82,7 +82,9 @@ public function execute() { ); // Initialize cache - $cache = Cache::create(Configuration::getConfig('cache', 'type')); + $cacheFac = new CacheFactory(); + $cacheFac->setWorkingDir(PATH_LIB_CACHES); + $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache->setScope(''); $cache->purgeCache(86400); // 24 hours $cache->setKey($cache_params); diff --git a/bridges/ElloBridge.php b/bridges/ElloBridge.php index 1f66edc375b..3de167efe23 100644 --- a/bridges/ElloBridge.php +++ b/bridges/ElloBridge.php @@ -120,7 +120,9 @@ private function getUsername($post, $postData) { } private function getAPIKey() { - $cache = Cache::create(Configuration::getConfig('cache', 'type')); + $cacheFac = new CacheFactory(); + $cacheFac->setWorkingDir(PATH_LIB_CACHES); + $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache->setScope(get_called_class()); $cache->setKey(['key']); $key = $cache->loadData(); diff --git a/lib/Cache.php b/lib/CacheFactory.php similarity index 61% rename from lib/Cache.php rename to lib/CacheFactory.php index 6c2943ad54e..9ce5c19bddf 100644 --- a/lib/Cache.php +++ b/lib/CacheFactory.php @@ -31,29 +31,7 @@ * $cache = Cache::create('FileCache'); * ``` */ -class Cache { - - /** - * Holds a path to the working directory. - * - * Do not access this property directly! - * Use {@see Cache::setWorkingDir()} and {@see Cache::getWorkingDir()} instead. - * - * @var string|null - */ - protected static $workingDir = null; - - /** - * Throws an exception when trying to create a new instance of this class. - * Use {@see Cache::create()} to create a new cache object from the working - * directory. - * - * @throws \LogicException if called. - */ - public function __construct(){ - throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!'); - } - +class CacheFactory extends FactoryAbstract { /** * Creates a new cache object from the working directory. * @@ -63,14 +41,14 @@ public function __construct(){ * @param string $name Name of the cache object. * @return object|bool The cache object or false if the class is not instantiable. */ - public static function create($name){ - $name = self::sanitizeCacheName($name) . 'Cache'; + public function create($name){ + $name = $this->sanitizeCacheName($name) . 'Cache'; - if(!self::isCacheName($name)) { + if(!$this->isCacheName($name)) { throw new \InvalidArgumentException('Cache name invalid!'); } - $filePath = self::getWorkingDir() . $name . '.php'; + $filePath = $this->getWorkingDir() . $name . '.php'; if(!file_exists($filePath)) { throw new \Exception('Cache file ' . $filePath . ' does not exist!'); @@ -85,48 +63,6 @@ public static function create($name){ return false; } - /** - * Sets the working directory. - * - * @param string $dir Path to a directory containing cache classes - * @throws \InvalidArgumentException if $dir is not a string. - * @throws \Exception if the working directory doesn't exist. - * @throws \InvalidArgumentException if $dir is not a directory. - * @return void - */ - public static function setWorkingDir($dir){ - self::$workingDir = null; - - if(!is_string($dir)) { - throw new \InvalidArgumentException('Working directory is not a valid string!'); - } - - if(!file_exists($dir)) { - throw new \Exception('Working directory does not exist!'); - } - - if(!is_dir($dir)) { - throw new \InvalidArgumentException('Working directory is not a directory!'); - } - - self::$workingDir = realpath($dir) . '/'; - } - - /** - * Returns the working directory. - * The working directory must be set with {@see Cache::setWorkingDir()}! - * - * @throws \LogicException if the working directory is not set. - * @return string The current working directory. - */ - public static function getWorkingDir(){ - if(is_null(self::$workingDir)) { - throw new \LogicException('Working directory is not set!'); - } - - return self::$workingDir; - } - /** * Returns true if the provided name is a valid cache name. * @@ -136,7 +72,7 @@ public static function getWorkingDir(){ * @param string $name The cache name. * @return bool true if the name is a valid cache name, false otherwise. */ - public static function isCacheName($name){ + public function isCacheName($name){ return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1; } @@ -147,12 +83,12 @@ public static function isCacheName($name){ * * @return array List of cache names */ - public static function getCacheNames(){ + public function getCacheNames(){ static $cacheNames = array(); // Initialized on first call if(empty($cacheNames)) { - $files = scandir(self::getWorkingDir()); + $files = scandir($this->getWorkingDir()); if($files !== false) { foreach($files as $file) { @@ -183,7 +119,7 @@ public static function getCacheNames(){ * @return string|null The sanitized cache name if the provided name is * valid, null otherwise. */ - protected static function sanitizeCacheName($name) { + protected function sanitizeCacheName($name) { if(is_string($name)) { @@ -198,9 +134,9 @@ protected static function sanitizeCacheName($name) { } // The name is valid if a corresponding file is found on disk - if(in_array(strtolower($name), array_map('strtolower', self::getCacheNames()))) { - $index = array_search(strtolower($name), array_map('strtolower', self::getCacheNames())); - return self::getCacheNames()[$index]; + if(in_array(strtolower($name), array_map('strtolower', $this->getCacheNames()))) { + $index = array_search(strtolower($name), array_map('strtolower', $this->getCacheNames())); + return $this->getCacheNames()[$index]; } Debug::log('Invalid cache name specified: "' . $name . '"!'); diff --git a/lib/contents.php b/lib/contents.php index 958feb1b885..d59b0d01818 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -45,7 +45,9 @@ function getContents($url, $header = array(), $opts = array()){ Debug::log('Reading contents from "' . $url . '"'); // Initialize cache - $cache = Cache::create(Configuration::getConfig('cache', 'type')); + $cacheFac = new CacheFactory(); + $cacheFac->setWorkingDir(PATH_LIB_CACHES); + $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache->setScope('server'); $cache->purgeCache(86400); // 24 hours (forced) @@ -270,7 +272,9 @@ function getSimpleHTMLDOMCached($url, Debug::log('Caching url ' . $url . ', duration ' . $duration); // Initialize cache - $cache = Cache::create(Configuration::getConfig('cache', 'type')); + $cacheFac = new CacheFactory(); + $cacheFac->setWorkingDir(PATH_LIB_CACHES); + $cache = $cacheFac->create(Configuration::getConfig('cache', 'type')); $cache->setScope('pages'); $cache->purgeCache(86400); // 24 hours (forced) diff --git a/lib/rssbridge.php b/lib/rssbridge.php index a10c117da83..eda4e58361e 100644 --- a/lib/rssbridge.php +++ b/lib/rssbridge.php @@ -66,7 +66,7 @@ require_once PATH_LIB . 'BridgeFactory.php'; require_once PATH_LIB . 'BridgeAbstract.php'; require_once PATH_LIB . 'FeedExpander.php'; -require_once PATH_LIB . 'Cache.php'; +require_once PATH_LIB . 'CacheFactory.php'; require_once PATH_LIB . 'Authentication.php'; require_once PATH_LIB . 'Configuration.php'; require_once PATH_LIB . 'BridgeCard.php'; @@ -88,7 +88,6 @@ // Initialize static members try { Format::setWorkingDir(PATH_LIB_FORMATS); - Cache::setWorkingDir(PATH_LIB_CACHES); } catch(Exception $e) { error_log($e); header('Content-type: text/plain', true, 500);