isInstantiable()) { return new $name(); } return false; } /** * 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. * @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!'); } if(!is_dir($dir)) { throw new \InvalidArgumentException('Working directory is not a directory!'); } self::$workingDir = realpath($dir) . '/'; } /** * Returns the working directory. * The working directory must be set with {@see Cache::setWorkingDir()}! * * @throws \LogicException if the working directory is not set. * @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; } /** * 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. */ public static function isCacheName($name){ return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1; } /** * Returns a list of cache names from the working directory. * * The list is cached internally to allow for successive calls. * * @return array List of cache names */ public static function getCacheNames(){ static $cacheNames = array(); // Initialized on first call if(empty($cacheNames)) { $files = scandir(self::getWorkingDir()); if($files !== false) { foreach($files as $file) { if(preg_match('/^([^.]+)Cache\.php$/U', $file, $out)) { $cacheNames[] = $out[1]; } } } } return $cacheNames; } /** * Returns the sanitized cache name. * * The cache name can be specified in various ways: * * The PHP file name (i.e. `FileCache.php`) * * The PHP file name without file extension (i.e. `FileCache`) * * The cache name (i.e. `file`) * * Casing is ignored (i.e. `FILE` and `fIlE` are the same). * * A cache file matching the given cache name must exist in the working * directory! * * @param string $name The cache name * @return string|null The sanitized cache name if the provided name is * valid, null otherwise. */ protected static function sanitizeCacheName($name) { if(is_string($name)) { // Trim trailing '.php' if exists if(preg_match('/(.+)(?:\.php)/', $name, $matches)) { $name = $matches[1]; } // Trim trailing 'Cache' if exists if(preg_match('/(.+)(?:Cache)$/i', $name, $matches)) { $name = $matches[1]; } // The name is valid if a corresponding file is found on disk if(in_array(strtolower($name), array_map('strtolower', self::getCacheNames()))) { $index = array_search(strtolower($name), array_map('strtolower', self::getCacheNames())); return self::getCacheNames()[$index]; } Debug::log('Invalid cache name specified: "' . $name . '"!'); } return null; // Bad parameter } }