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('Dir format must be a 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 Format::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 format name. * * A valid format 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 format name. * @return bool true if the name is a valid format name, false otherwise. */ public static function isFormatName($name){ return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1; } /** * Read format dir and catch informations about each format depending annotation * @return array Informations about each format */ public static function searchInformation(){ $pathDirFormat = self::getWorkingDir(); $listFormat = array(); $searchCommonPattern = array('name'); $dirFiles = scandir($pathDirFormat); if($dirFiles !== false) { foreach($dirFiles as $fileName) { if(preg_match('@^([^.]+)Format\.php$@U', $fileName, $out)) { // Is PHP file ? $listFormat[] = $out[1]; } } } return $listFormat; } }