Rss-Bridge/lib/Cache.php

137 lines
3.9 KiB
PHP
Raw Normal View History

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::$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
* 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');
* ```
*/
class Cache {
2013-08-11 13:30:41 +02:00
2018-11-14 17:06:07 +01:00
/**
* Holds a path to the working directory.
2018-11-14 17:06:07 +01:00
*
* Do not access this property directly!
* Use {@see Cache::setWorkingDir()} and {@see Cache::getWorkingDir()} instead.
2018-11-14 17:06:07 +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.
* Use {@see Cache::create()} to create a new cache object from the working
2018-11-14 17:06:07 +01:00
* directory.
*
* @throws \LogicException if called.
2018-11-14 17:06:07 +01:00
*/
public function __construct(){
throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!');
}
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 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.
* @return object The cache object.
2018-11-14 17:06:07 +01:00
*/
public static function create($name){
if(!self::isCacheName($name)) {
throw new \InvalidArgumentException('Cache name invalid!');
}
2013-08-11 13:30:41 +02:00
$filePath = self::getWorkingDir() . $name . '.php';
2013-08-11 13:30:41 +02:00
if(!file_exists($filePath)) {
throw new \Exception('Cache file ' . $filePath . ' does not exist!');
}
2013-08-11 13:30:41 +02:00
require_once $filePath;
2013-08-11 13:30:41 +02:00
return new $name();
}
2013-08-11 13:30:41 +02:00
2018-11-14 17:06:07 +01:00
/**
* 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.
2018-11-14 17:06:07 +01:00
* @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!');
}
2013-08-11 13:30:41 +02:00
if(!is_dir($dir)) {
throw new \InvalidArgumentException('Working directory is not a directory!');
}
2013-08-11 13:30:41 +02:00
self::$workingDir = realpath($dir) . '/';
}
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.
* The working directory must be set with {@see Cache::setWorkingDir()}!
2018-11-14 17:06:07 +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.
*/
public static function getWorkingDir(){
if(is_null(self::$workingDir)) {
throw new \LogicException('Working directory is not set!');
}
return self::$workingDir;
}
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.
*
* 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
*/
public static function isCacheName($name){
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
}
2015-12-04 10:19:05 +01:00
}