format: Refactor class Format

This commit is contained in:
logmanoriginal 2018-11-15 20:01:56 +01:00
parent 88b0656954
commit 6c4e239f64
2 changed files with 101 additions and 23 deletions

View file

@ -1,51 +1,129 @@
<?php
/**
* This file is part of RSS-Bridge, a PHP project capable of generating RSS and
* Atom feeds for websites that don't have one.
*
* For the full license information, please view the UNLICENSE file distributed
* with this source code.
*
* @package Core
* @license http://unlicense.org/ UNLICENSE
* @link https://github.com/rss-bridge/rss-bridge
*/
/**
* Factory class responsible for creating format objects from a given working
* directory.
*
* This class is capable of:
* - Locating format classes in the specified working directory (see {@see Format::$workingDir})
* - Creating new format instances based on the format's name (see {@see Format::create()})
*
* The following example illustrates the intended use for this class.
*
* ```PHP
* require_once __DIR__ . '/rssbridge.php';
*
* // Step 1: Set the working directory
* Format::setWorkingDir(__DIR__ . '/../formats/');
*
* // Step 2: Create a new instance of a format object (based on the name)
* $format = Format::create('Atom');
* ```
*/
class Format {
static protected $dirFormat;
/**
* Holds a path to the working directory.
*
* Do not access this property directly!
* Use {@see Format::setWorkingDir()} and {@see Format::getWorkingDir()} instead.
*
* @var string|null
*/
protected static $workingDir = null;
/**
* Throws an exception when trying to create a new instance of this class.
* Use {@see Format::create()} to create a new format object from the working
* directory.
*
* @throws \LogicException if called.
*/
public function __construct(){
throw new \LogicException('Please use ' . __CLASS__ . '::create for new object.');
throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!');
}
public static function create($nameFormat){
if(!preg_match('@^[A-Z][a-zA-Z]*$@', $nameFormat)) {
throw new \InvalidArgumentException('Name format must be at least
one uppercase follow or not by alphabetic characters.');
/**
* Creates a new format object from the working directory.
*
* @throws \InvalidArgumentException if the requested format name is invalid.
* @throws \Exception if the requested format file doesn't exist in the
* working directory.
* @param string $name Name of the format object.
* @return object|bool The format object or false if the class is not instantiable.
*/
public static function create($name){
if(!preg_match('@^[A-Z][a-zA-Z]*$@', $name)) {
throw new \InvalidArgumentException('Format name invalid!');
}
$nameFormat = $nameFormat . 'Format';
$pathFormat = self::getDir() . $nameFormat . '.php';
$name = $name . 'Format';
$pathFormat = self::getWorkingDir() . $name . '.php';
if(!file_exists($pathFormat)) {
throw new \Exception('The format you are looking for does not exist.');
throw new \Exception('Format file ' . $filePath . ' does not exist!');
}
require_once $pathFormat;
return new $nameFormat();
if((new \ReflectionClass($name))->isInstantiable()) {
return new $name();
}
public static function setDir($dirFormat){
if(!is_string($dirFormat)) {
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($dirFormat)) {
throw new \Exception('Dir format does not exist.');
if(!file_exists($dir)) {
throw new \Exception('Working directory does not exist!');
}
self::$dirFormat = $dirFormat;
if(!is_dir($dir)) {
throw new \InvalidArgumentException('Working directory is not a directory!');
}
public static function getDir(){
$dirFormat = self::$dirFormat;
if(is_null($dirFormat)) {
throw new \LogicException(__CLASS__ . ' class need to know format path !');
self::$workingDir = $dir;
}
return $dirFormat;
/**
* 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;
}
/**
@ -53,7 +131,7 @@ class Format {
* @return array Informations about each format
*/
public static function searchInformation(){
$pathDirFormat = self::getDir();
$pathDirFormat = self::getWorkingDir();
$listFormat = array();

View file

@ -42,7 +42,7 @@ require_once PATH_LIB_VENDOR . 'php-urljoin/src/urljoin.php';
// Initialize static members
try {
Bridge::setWorkingDir(PATH_LIB_BRIDGES);
Format::setDir(PATH_LIB_FORMATS);
Format::setWorkingDir(PATH_LIB_FORMATS);
Cache::setWorkingDir(PATH_LIB_CACHES);
} catch(Exception $e) {
error_log($e);