Rss-Bridge/lib/Cache.php

54 lines
1.2 KiB
PHP
Raw Normal View History

2013-08-11 13:30:41 +02:00
<?php
2018-11-06 19:23:32 +01:00
class Cache {
2013-08-11 13:30:41 +02:00
static protected $dirCache;
2013-08-11 13:30:41 +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-13 18:27:05 +01:00
public static function create($nameCache){
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.');
}
2013-08-11 13:30:41 +02:00
$pathCache = self::getDir() . $nameCache . '.php';
2013-08-11 13:30:41 +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.');
}
2013-08-11 13:30:41 +02:00
require_once $pathCache;
2013-08-11 13:30:41 +02:00
return new $nameCache();
}
2013-08-11 13:30:41 +02:00
2018-11-13 18:27:05 +01:00
public static function setDir($dirCache){
if(!is_string($dirCache)) {
throw new \InvalidArgumentException('Dir cache must be a string.');
}
2013-08-11 13:30:41 +02:00
if(!file_exists($dirCache)) {
throw new \Exception('Dir cache does not exist.');
}
2013-08-11 13:30:41 +02:00
self::$dirCache = $dirCache;
}
2013-08-11 13:30:41 +02:00
2018-11-13 18:27:05 +01:00
public static function getDir(){
$dirCache = self::$dirCache;
2013-08-11 13:30:41 +02:00
if(is_null($dirCache)) {
throw new \LogicException(__CLASS__ . ' class need to know cache path !');
}
return $dirCache;
}
2013-08-11 13:30:41 +02:00
2018-11-13 18:27:05 +01:00
public static function isValidNameCache($nameCache){
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
}
2015-12-04 10:19:05 +01:00
}