Cache.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. require_once(__DIR__ . '/CacheInterface.php');
  3. class Cache {
  4. static protected $dirCache;
  5. public function __construct(){
  6. throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
  7. }
  8. static public function create($nameCache){
  9. if(!static::isValidNameCache($nameCache)) {
  10. throw new \InvalidArgumentException('Name cache must be at least one
  11. uppercase follow or not by alphanumeric or dash characters.');
  12. }
  13. $pathCache = self::getDir() . $nameCache . '.php';
  14. if(!file_exists($pathCache)) {
  15. throw new \Exception('The cache you looking for does not exist.');
  16. }
  17. require_once $pathCache;
  18. return new $nameCache();
  19. }
  20. static public function setDir($dirCache){
  21. if(!is_string($dirCache)) {
  22. throw new \InvalidArgumentException('Dir cache must be a string.');
  23. }
  24. if(!file_exists($dirCache)) {
  25. throw new \Exception('Dir cache does not exist.');
  26. }
  27. self::$dirCache = $dirCache;
  28. }
  29. static public function getDir(){
  30. $dirCache = self::$dirCache;
  31. if(is_null($dirCache)) {
  32. throw new \LogicException(__CLASS__ . ' class need to know cache path !');
  33. }
  34. return $dirCache;
  35. }
  36. static public function isValidNameCache($nameCache){
  37. return preg_match('@^[A-Z][a-zA-Z0-9-]*$@', $nameCache);
  38. }
  39. }