2013-08-11 13:30:41 +02:00
|
|
|
<?php
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
|
|
|
|
* Atom feeds for websites that don't have one.
|
|
|
|
*
|
|
|
|
* For the full license information, please view the UNLICENSE file distributed
|
|
|
|
* with this source code.
|
|
|
|
*
|
|
|
|
* @package Core
|
|
|
|
* @license http://unlicense.org/ UNLICENSE
|
|
|
|
* @link https://github.com/rss-bridge/rss-bridge
|
|
|
|
*/
|
2018-11-06 19:23:32 +01:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory class responsible for creating cache objects from a given working
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* This class is capable of:
|
|
|
|
* - Locating cache classes in the specified working directory (see {@see Cache::$dirCache})
|
|
|
|
* - Creating new cache instances based on the cache's name (see {@see Cache::create()})
|
|
|
|
*
|
|
|
|
* The following example illustrates the intended use for this class.
|
|
|
|
*
|
|
|
|
* ```PHP
|
|
|
|
* require_once __DIR__ . '/rssbridge.php';
|
|
|
|
*
|
|
|
|
* // Step 1: Set the working directory
|
|
|
|
* Cache::setDir(__DIR__ . '/../caches/');
|
|
|
|
*
|
|
|
|
* // Step 2: Create a new instance of a cache object (based on the name)
|
|
|
|
* $cache = Cache::create('FileCache');
|
|
|
|
* ```
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
class Cache {
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Holds the working directory.
|
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
|
|
|
* Use {@see Cache::setDir()} and {@see Cache::getDir()} instead.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
static protected $dirCache;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Throws an exception when trying to create a new instance of this class.
|
|
|
|
* Use {@see Cache::create()} to instanciate a new cache from the working
|
|
|
|
* directory.
|
|
|
|
*
|
|
|
|
* @throws LogicException if called.
|
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct(){
|
|
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Creates a new cache object from the working directory.
|
|
|
|
*
|
|
|
|
* @throws InvalidArgumentException if the provided name is invalid.
|
|
|
|
* @throws Exception if no cache with the given name exist in the working
|
|
|
|
* directory.
|
|
|
|
* @param string $nameCache Name of the cache object.
|
|
|
|
* @return object Instance of the cache.
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function create($nameCache){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!static::isValidNameCache($nameCache)) {
|
2016-09-10 21:01:02 +02:00
|
|
|
throw new \InvalidArgumentException('Name cache must be at least one
|
|
|
|
uppercase follow or not by alphanumeric or dash characters.');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
$pathCache = self::getDir() . $nameCache . '.php';
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($pathCache)) {
|
2018-11-13 17:24:36 +01:00
|
|
|
throw new \Exception('The cache you are looking for does not exist.');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
require_once $pathCache;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
return new $nameCache();
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Sets the working directory.
|
|
|
|
*
|
|
|
|
* @param string $dirCache Path to a directory containing cache classes
|
|
|
|
* @throws InvalidArgumentException if the provided path is not a valid string.
|
|
|
|
* @throws Exception if the provided path does not exist.
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function setDir($dirCache){
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!is_string($dirCache)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \InvalidArgumentException('Dir cache must be a string.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(!file_exists($dirCache)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \Exception('Dir cache does not exist.');
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2016-09-10 20:41:11 +02:00
|
|
|
self::$dirCache = $dirCache;
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Returns the current working directory.
|
|
|
|
* The working directory must be specified with {@see Cache::setDir()}!
|
|
|
|
*
|
|
|
|
* @throws LogicException if the working directory was not specified.
|
|
|
|
* @return string The current working directory.
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function getDir(){
|
2016-09-10 20:41:11 +02:00
|
|
|
$dirCache = self::$dirCache;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2017-07-29 19:28:00 +02:00
|
|
|
if(is_null($dirCache)) {
|
2016-09-10 20:41:11 +02:00
|
|
|
throw new \LogicException(__CLASS__ . ' class need to know cache path !');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dirCache;
|
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Returns true if the provided name is a valid cache name.
|
|
|
|
*
|
|
|
|
* @param string $nameCache The cache name.
|
|
|
|
* @return int 1 if the name is valid, 0 if not, false if an error occurred.
|
|
|
|
*/
|
2018-11-13 18:27:05 +01:00
|
|
|
public static function isValidNameCache($nameCache){
|
2016-09-10 20:41:11 +02:00
|
|
|
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
|
|
|
|
}
|
2015-12-04 10:19:05 +01:00
|
|
|
}
|