[Cache] Refactor class
general - Use self:: instead of Cache:: or static:: - Rename $dirCache to $workingDir - Initialize $workingDir with null function setDir - Clear previous working directory before checking input parameters - Change wording for the exception messages - Store realpath instead of raw parameter - Add path separator This ensures the path always ends with the path separator, as assumed by Cache::create - Add check if the provided working directory is a valid directory function getDir - Use static parameter instead of function variable - Change wording for the exception message function create - Rename parameter $nameCache to $name - Rename $pathCache to $filePath - Change wording for the exception messages function isValidNameCache - Rename function to isCacheName - Rename parameter $nameCache to $name - Explain in the function documentation the meaning of a 'valid name' - Ensure Boolean return value (preg_match returns integer) - Check if $name is a string - Use slashes to enclose the regex
This commit is contained in:
parent
427688fd67
commit
e8442a3bf8
1 changed files with 48 additions and 41 deletions
|
@ -17,7 +17,7 @@
|
||||||
* directory.
|
* directory.
|
||||||
*
|
*
|
||||||
* This class is capable of:
|
* This class is capable of:
|
||||||
* - Locating cache classes in the specified working directory (see {@see Cache::$dirCache})
|
* - Locating cache classes in the specified working directory (see {@see Cache::$workingDir})
|
||||||
* - Creating new cache instances based on the cache's name (see {@see Cache::create()})
|
* - Creating new cache instances based on the cache's name (see {@see Cache::create()})
|
||||||
*
|
*
|
||||||
* The following example illustrates the intended use for this class.
|
* The following example illustrates the intended use for this class.
|
||||||
|
@ -35,96 +35,103 @@
|
||||||
class Cache {
|
class Cache {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the working directory.
|
* Holds a path to the working directory.
|
||||||
*
|
*
|
||||||
* Do not access this property directly!
|
* Do not access this property directly!
|
||||||
* Use {@see Cache::setDir()} and {@see Cache::getDir()} instead.
|
* Use {@see Cache::setDir()} and {@see Cache::getDir()} instead.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string|null
|
||||||
*/
|
*/
|
||||||
static protected $dirCache;
|
static protected $workingDir = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws an exception when trying to create a new instance of this class.
|
* 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
|
* Use {@see Cache::create()} to create a new cache object from the working
|
||||||
* directory.
|
* directory.
|
||||||
*
|
*
|
||||||
* @throws LogicException if called.
|
* @throws \LogicException if called.
|
||||||
*/
|
*/
|
||||||
public function __construct(){
|
public function __construct(){
|
||||||
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
|
throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new cache object from the working directory.
|
* Creates a new cache object from the working directory.
|
||||||
*
|
*
|
||||||
* @throws InvalidArgumentException if the provided name is invalid.
|
* @throws \InvalidArgumentException if the requested cache name is invalid.
|
||||||
* @throws Exception if no cache with the given name exist in the working
|
* @throws \Exception if the requested cache file doesn't exist in the
|
||||||
* directory.
|
* working directory.
|
||||||
* @param string $nameCache Name of the cache object.
|
* @param string $name Name of the cache object.
|
||||||
* @return object Instance of the cache.
|
* @return object The cache object.
|
||||||
*/
|
*/
|
||||||
public static function create($nameCache){
|
public static function create($name){
|
||||||
if(!static::isValidNameCache($nameCache)) {
|
if(!self::isCacheName($name)) {
|
||||||
throw new \InvalidArgumentException('Name cache must be at least one
|
throw new \InvalidArgumentException('Cache name invalid!');
|
||||||
uppercase follow or not by alphanumeric or dash characters.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$pathCache = self::getDir() . $nameCache . '.php';
|
$filePath = self::getDir() . $name . '.php';
|
||||||
|
|
||||||
if(!file_exists($pathCache)) {
|
if(!file_exists($filePath)) {
|
||||||
throw new \Exception('The cache you are looking for does not exist.');
|
throw new \Exception('Cache file ' . $filePath . ' does not exist!');
|
||||||
}
|
}
|
||||||
|
|
||||||
require_once $pathCache;
|
require_once $filePath;
|
||||||
|
|
||||||
return new $nameCache();
|
return new $name();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the working directory.
|
* Sets the working directory.
|
||||||
*
|
*
|
||||||
* @param string $dirCache Path to a directory containing cache classes
|
* @param string $workingDir Path to a directory containing cache classes
|
||||||
* @throws InvalidArgumentException if the provided path is not a valid string.
|
* @throws \InvalidArgumentException if $workingDir is not a string.
|
||||||
* @throws Exception if the provided path does not exist.
|
* @throws \Exception if the working directory doesn't exist.
|
||||||
|
* @throws \InvalidArgumentException if $workingDir is not a directory.
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function setDir($dirCache){
|
public static function setDir($workingDir){
|
||||||
if(!is_string($dirCache)) {
|
self::$workingDir = null;
|
||||||
throw new \InvalidArgumentException('Dir cache must be a string.');
|
|
||||||
|
if(!is_string($workingDir)) {
|
||||||
|
throw new \InvalidArgumentException('Working directory is not a valid string!');
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!file_exists($dirCache)) {
|
if(!file_exists($workingDir)) {
|
||||||
throw new \Exception('Dir cache does not exist.');
|
throw new \Exception('Working directory does not exist!');
|
||||||
}
|
}
|
||||||
|
|
||||||
self::$dirCache = $dirCache;
|
if(!is_dir($workingDir)) {
|
||||||
|
throw new \InvalidArgumentException('Working directory is not a directory!');
|
||||||
|
}
|
||||||
|
|
||||||
|
self::$workingDir = realpath($workingDir) . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current working directory.
|
* Returns the current working directory.
|
||||||
* The working directory must be specified with {@see Cache::setDir()}!
|
* The working directory must be set with {@see Cache::setDir()}!
|
||||||
*
|
*
|
||||||
* @throws LogicException if the working directory was not specified.
|
* @throws \LogicException if the working directory is not set.
|
||||||
* @return string The current working directory.
|
* @return string The current working directory.
|
||||||
*/
|
*/
|
||||||
public static function getDir(){
|
public static function getDir(){
|
||||||
$dirCache = self::$dirCache;
|
if(is_null(self::$workingDir)) {
|
||||||
|
throw new \LogicException('Working directory is not set!');
|
||||||
if(is_null($dirCache)) {
|
|
||||||
throw new \LogicException(__CLASS__ . ' class need to know cache path !');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $dirCache;
|
return self::$workingDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns true if the provided name is a valid cache name.
|
* Returns true if the provided name is a valid cache name.
|
||||||
*
|
*
|
||||||
* @param string $nameCache The cache name.
|
* A valid cache name starts with a capital letter ([A-Z]), followed by
|
||||||
* @return int 1 if the name is valid, 0 if not, false if an error occurred.
|
* 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.
|
||||||
*/
|
*/
|
||||||
public static function isValidNameCache($nameCache){
|
public static function isCacheName($name){
|
||||||
return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
|
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue