2013-08-11 15:30:41 +04:00
|
|
|
<?php
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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');
|
|
|
|
* ```
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
class Format {
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2016-09-10 21:41:11 +03:00
|
|
|
public function __construct(){
|
2018-11-15 22:01:56 +03:00
|
|
|
throw new \LogicException('Use ' . __CLASS__ . '::create($name) to create cache objects!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
$name = $name . 'Format';
|
|
|
|
$pathFormat = self::getWorkingDir() . $name . '.php';
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2017-07-29 20:28:00 +03:00
|
|
|
if(!file_exists($pathFormat)) {
|
2018-11-15 22:01:56 +03:00
|
|
|
throw new \Exception('Format file ' . $filePath . ' does not exist!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
require_once $pathFormat;
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
if((new \ReflectionClass($name))->isInstantiable()) {
|
|
|
|
return new $name();
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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)) {
|
2016-09-10 21:41:11 +03:00
|
|
|
throw new \InvalidArgumentException('Dir format must be a string.');
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
if(!file_exists($dir)) {
|
|
|
|
throw new \Exception('Working directory does not exist!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
if(!is_dir($dir)) {
|
|
|
|
throw new \InvalidArgumentException('Working directory is not a directory!');
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:06:45 +03:00
|
|
|
self::$workingDir = realpath($dir) . '/';
|
2018-11-15 22:01:56 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
/**
|
|
|
|
* 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!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:01:56 +03:00
|
|
|
return self::$workingDir;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
|
|
|
* Read format dir and catch informations about each format depending annotation
|
|
|
|
* @return array Informations about each format
|
|
|
|
*/
|
2018-11-13 20:27:05 +03:00
|
|
|
public static function searchInformation(){
|
2018-11-15 22:01:56 +03:00
|
|
|
$pathDirFormat = self::getWorkingDir();
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$listFormat = array();
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$searchCommonPattern = array('name');
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
$dirFiles = scandir($pathDirFormat);
|
2017-07-29 20:28:00 +03:00
|
|
|
if($dirFiles !== false) {
|
|
|
|
foreach($dirFiles as $fileName) {
|
|
|
|
if(preg_match('@^([^.]+)Format\.php$@U', $fileName, $out)) { // Is PHP file ?
|
2016-09-10 21:41:11 +03:00
|
|
|
$listFormat[] = $out[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
return $listFormat;
|
|
|
|
}
|
2016-08-23 15:29:53 +03:00
|
|
|
}
|