mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-04-22 06:34:31 +00:00
81 lines
1.8 KiB
PHP
81 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* All cache logic
|
|
* Note : adapter are store in other place
|
|
*/
|
|
|
|
interface CacheInterface
|
|
{
|
|
public function loadData();
|
|
public function saveData($datas);
|
|
public function getTime();
|
|
}
|
|
|
|
abstract class CacheAbstract implements CacheInterface
|
|
{
|
|
protected $param;
|
|
|
|
public function prepare(array $param)
|
|
{
|
|
$this->param = $param;
|
|
|
|
return $this;
|
|
}
|
|
}
|
|
|
|
class Cache
|
|
{
|
|
protected static $dirCache;
|
|
|
|
public function __construct()
|
|
{
|
|
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
|
}
|
|
|
|
public static function create($nameCache)
|
|
{
|
|
if ( !static::isValidNameCache($nameCache) ) {
|
|
throw new \InvalidArgumentException('Name cache must be at least one uppercase follow or not by alphanumeric or dash characters.');
|
|
}
|
|
|
|
$pathCache = self::getDir() . $nameCache . '.php';
|
|
|
|
if ( !file_exists($pathCache) ) {
|
|
throw new \Exception('The cache you looking for does not exist.');
|
|
}
|
|
|
|
require_once $pathCache;
|
|
|
|
return new $nameCache();
|
|
}
|
|
|
|
public static function setDir($dirCache)
|
|
{
|
|
if ( !is_string($dirCache) ) {
|
|
throw new \InvalidArgumentException('Dir cache must be a string.');
|
|
}
|
|
|
|
if ( !file_exists($dirCache) ) {
|
|
throw new \Exception('Dir cache does not exist.');
|
|
}
|
|
|
|
self::$dirCache = $dirCache;
|
|
}
|
|
|
|
public static function getDir()
|
|
{
|
|
$dirCache = self::$dirCache;
|
|
|
|
if ( is_null($dirCache) ) {
|
|
throw new \LogicException(__CLASS__ . ' class need to know cache path !');
|
|
}
|
|
|
|
return $dirCache;
|
|
}
|
|
|
|
public static function isValidNameCache($nameCache)
|
|
{
|
|
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
|
|
}
|
|
}
|