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:
|
2018-11-14 19:07:53 +01:00
|
|
|
* - Locating cache classes in the specified working directory (see {@see Cache::$workingDir})
|
2018-11-14 17:06:07 +01:00
|
|
|
* - 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
|
2018-11-14 20:39:39 +01:00
|
|
|
* Cache::setWorkingDir(__DIR__ . '/../caches/');
|
2018-11-14 17:06:07 +01:00
|
|
|
*
|
|
|
|
* // 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
|
|
|
/**
|
2018-11-14 19:07:53 +01:00
|
|
|
* Holds a path to the working directory.
|
2018-11-14 17:06:07 +01:00
|
|
|
*
|
|
|
|
* Do not access this property directly!
|
2018-11-14 20:39:39 +01:00
|
|
|
* Use {@see Cache::setWorkingDir()} and {@see Cache::getWorkingDir()} instead.
|
2018-11-14 17:06:07 +01:00
|
|
|
*
|
2018-11-14 19:07:53 +01:00
|
|
|
* @var string|null
|
2018-11-14 17:06:07 +01:00
|
|
|
*/
|
2018-11-15 19:00:48 +01:00
|
|
|
protected static $workingDir = null;
|
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.
|
2018-11-14 19:07:53 +01:00
|
|
|
* Use {@see Cache::create()} to create a new cache object from the working
|
2018-11-14 17:06:07 +01:00
|
|
|
* directory.
|
|
|
|
*
|
2018-11-14 19:07:53 +01:00
|
|
|
* @throws \LogicException if called.
|
2018-11-14 17:06:07 +01:00
|
|
|
*/
|
2016-09-10 20:41:11 +02:00
|
|
|
public function __construct(){
|
2018-11-14 19:07:53 +01:00
|
|
|
throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
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.
|
|
|
|
*
|
2018-11-14 19:07:53 +01:00
|
|
|
* @throws \InvalidArgumentException if the requested cache name is invalid.
|
|
|
|
* @throws \Exception if the requested cache file doesn't exist in the
|
|
|
|
* working directory.
|
|
|
|
* @param string $name Name of the cache object.
|
2018-11-15 19:36:01 +01:00
|
|
|
* @return object|bool The cache object or false if the class is not instantiable.
|
2018-11-14 17:06:07 +01:00
|
|
|
*/
|
2018-11-14 19:07:53 +01:00
|
|
|
public static function create($name){
|
2019-02-06 18:52:44 +01:00
|
|
|
$name = self::sanitizeCacheName($name) . 'Cache';
|
|
|
|
|
2018-11-14 19:07:53 +01:00
|
|
|
if(!self::isCacheName($name)) {
|
|
|
|
throw new \InvalidArgumentException('Cache name invalid!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 20:39:39 +01:00
|
|
|
$filePath = self::getWorkingDir() . $name . '.php';
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 19:07:53 +01:00
|
|
|
if(!file_exists($filePath)) {
|
|
|
|
throw new \Exception('Cache file ' . $filePath . ' does not exist!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 19:07:53 +01:00
|
|
|
require_once $filePath;
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-15 19:36:01 +01:00
|
|
|
if((new \ReflectionClass($name))->isInstantiable()) {
|
|
|
|
return new $name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
|
|
|
* Sets the working directory.
|
|
|
|
*
|
2018-11-14 20:39:39 +01:00
|
|
|
* @param string $dir Path to a directory containing cache classes
|
|
|
|
* @throws \InvalidArgumentException if $dir is not a string.
|
2018-11-14 19:07:53 +01:00
|
|
|
* @throws \Exception if the working directory doesn't exist.
|
2018-11-14 20:39:39 +01:00
|
|
|
* @throws \InvalidArgumentException if $dir is not a directory.
|
2018-11-14 17:06:07 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
2018-11-14 20:39:39 +01:00
|
|
|
public static function setWorkingDir($dir){
|
2018-11-14 19:07:53 +01:00
|
|
|
self::$workingDir = null;
|
|
|
|
|
2018-11-14 20:39:39 +01:00
|
|
|
if(!is_string($dir)) {
|
2018-11-14 19:07:53 +01:00
|
|
|
throw new \InvalidArgumentException('Working directory is not a valid string!');
|
|
|
|
}
|
|
|
|
|
2018-11-14 20:39:39 +01:00
|
|
|
if(!file_exists($dir)) {
|
2018-11-14 19:07:53 +01:00
|
|
|
throw new \Exception('Working directory does not exist!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 20:39:39 +01:00
|
|
|
if(!is_dir($dir)) {
|
2018-11-14 19:07:53 +01:00
|
|
|
throw new \InvalidArgumentException('Working directory is not a directory!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 20:39:39 +01:00
|
|
|
self::$workingDir = realpath($dir) . '/';
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2013-08-11 13:30:41 +02:00
|
|
|
|
2018-11-14 17:06:07 +01:00
|
|
|
/**
|
2018-11-15 19:00:48 +01:00
|
|
|
* Returns the working directory.
|
2018-11-14 20:39:39 +01:00
|
|
|
* The working directory must be set with {@see Cache::setWorkingDir()}!
|
2018-11-14 17:06:07 +01:00
|
|
|
*
|
2018-11-14 19:07:53 +01:00
|
|
|
* @throws \LogicException if the working directory is not set.
|
2018-11-14 17:06:07 +01:00
|
|
|
* @return string The current working directory.
|
|
|
|
*/
|
2018-11-14 20:39:39 +01:00
|
|
|
public static function getWorkingDir(){
|
2018-11-14 19:07:53 +01:00
|
|
|
if(is_null(self::$workingDir)) {
|
|
|
|
throw new \LogicException('Working directory is not set!');
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 19:07:53 +01:00
|
|
|
return self::$workingDir;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
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.
|
|
|
|
*
|
2018-11-14 19:07:53 +01:00
|
|
|
* A valid cache name starts with a capital letter ([A-Z]), followed by
|
|
|
|
* zero or more alphanumeric characters or hyphen ([A-Za-z0-9-]).
|
|
|
|
*
|
|
|
|
* @param string $name The cache name.
|
|
|
|
* @return bool true if the name is a valid cache name, false otherwise.
|
2018-11-14 17:06:07 +01:00
|
|
|
*/
|
2018-11-14 19:07:53 +01:00
|
|
|
public static function isCacheName($name){
|
|
|
|
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
|
2016-09-10 20:41:11 +02:00
|
|
|
}
|
2019-02-06 18:52:44 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of cache names from the working directory.
|
|
|
|
*
|
|
|
|
* The list is cached internally to allow for successive calls.
|
|
|
|
*
|
|
|
|
* @return array List of cache names
|
|
|
|
*/
|
|
|
|
public static function getCacheNames(){
|
|
|
|
|
|
|
|
static $cacheNames = array(); // Initialized on first call
|
|
|
|
|
|
|
|
if(empty($cacheNames)) {
|
|
|
|
$files = scandir(self::getWorkingDir());
|
|
|
|
|
|
|
|
if($files !== false) {
|
|
|
|
foreach($files as $file) {
|
|
|
|
if(preg_match('/^([^.]+)Cache\.php$/U', $file, $out)) {
|
|
|
|
$cacheNames[] = $out[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cacheNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the sanitized cache name.
|
|
|
|
*
|
|
|
|
* The cache name can be specified in various ways:
|
|
|
|
* * The PHP file name (i.e. `FileCache.php`)
|
|
|
|
* * The PHP file name without file extension (i.e. `FileCache`)
|
|
|
|
* * The cache name (i.e. `file`)
|
|
|
|
*
|
|
|
|
* Casing is ignored (i.e. `FILE` and `fIlE` are the same).
|
|
|
|
*
|
|
|
|
* A cache file matching the given cache name must exist in the working
|
|
|
|
* directory!
|
|
|
|
*
|
|
|
|
* @param string $name The cache name
|
|
|
|
* @return string|null The sanitized cache name if the provided name is
|
|
|
|
* valid, null otherwise.
|
|
|
|
*/
|
|
|
|
protected static function sanitizeCacheName($name) {
|
|
|
|
|
|
|
|
if(is_string($name)) {
|
|
|
|
|
|
|
|
// Trim trailing '.php' if exists
|
|
|
|
if(preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing 'Cache' if exists
|
|
|
|
if(preg_match('/(.+)(?:Cache)/i', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log('Invalid cache name specified: "' . $name . '"!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null; // Bad parameter
|
|
|
|
|
|
|
|
}
|
2015-12-04 10:19:05 +01:00
|
|
|
}
|