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');
|
|
|
|
* ```
|
|
|
|
*/
|
2019-06-18 20:15:20 +03:00
|
|
|
class FormatFactory extends FactoryAbstract {
|
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.
|
|
|
|
*/
|
2019-06-18 20:15:20 +03:00
|
|
|
public function create($name){
|
|
|
|
if(!$this->isFormatName($name)) {
|
2018-11-15 22:01:56 +03:00
|
|
|
throw new \InvalidArgumentException('Format name invalid!');
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2019-06-18 20:15:20 +03:00
|
|
|
$name = $this->sanitizeFormatName($name) . 'Format';
|
|
|
|
$pathFormat = $this->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:08:33 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2019-06-18 20:15:20 +03:00
|
|
|
public function isFormatName($name){
|
2018-11-15 22:08:33 +03:00
|
|
|
return is_string($name) && preg_match('/^[A-Z][a-zA-Z0-9-]*$/', $name) === 1;
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:41:11 +03:00
|
|
|
/**
|
2018-11-15 22:16:21 +03:00
|
|
|
* Returns the list of format names from the working directory.
|
|
|
|
*
|
|
|
|
* The list is cached internally to allow for successive calls.
|
|
|
|
*
|
|
|
|
* @return array List of format names
|
|
|
|
*/
|
2019-06-18 20:15:20 +03:00
|
|
|
public function getFormatNames(){
|
2018-11-15 22:16:21 +03:00
|
|
|
static $formatNames = array(); // Initialized on first call
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:16:21 +03:00
|
|
|
if(empty($formatNames)) {
|
2019-06-18 20:15:20 +03:00
|
|
|
$files = scandir($this->getWorkingDir());
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:16:21 +03:00
|
|
|
if($files !== false) {
|
|
|
|
foreach($files as $file) {
|
|
|
|
if(preg_match('/^([^.]+)Format\.php$/U', $file, $out)) {
|
|
|
|
$formatNames[] = $out[1];
|
|
|
|
}
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-11 15:30:41 +04:00
|
|
|
|
2018-11-15 22:16:21 +03:00
|
|
|
return $formatNames;
|
2016-09-10 21:41:11 +03:00
|
|
|
}
|
2019-06-18 19:32:56 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the sanitized format name.
|
|
|
|
*
|
|
|
|
* The format name can be specified in various ways:
|
|
|
|
* * The PHP file name (i.e. `AtomFormat.php`)
|
|
|
|
* * The PHP file name without file extension (i.e. `AtomFormat`)
|
|
|
|
* * The format name (i.e. `Atom`)
|
|
|
|
*
|
|
|
|
* Casing is ignored (i.e. `ATOM` and `atom` are the same).
|
|
|
|
*
|
|
|
|
* A format file matching the given format name must exist in the working
|
|
|
|
* directory!
|
|
|
|
*
|
|
|
|
* @param string $name The format name
|
|
|
|
* @return string|null The sanitized format name if the provided name is
|
|
|
|
* valid, null otherwise.
|
|
|
|
*/
|
2019-06-18 20:15:20 +03:00
|
|
|
protected function sanitizeFormatName($name) {
|
2019-06-18 19:32:56 +03:00
|
|
|
|
|
|
|
if(is_string($name)) {
|
|
|
|
|
|
|
|
// Trim trailing '.php' if exists
|
|
|
|
if(preg_match('/(.+)(?:\.php)/', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trim trailing 'Format' if exists
|
|
|
|
if(preg_match('/(.+)(?:Format)/i', $name, $matches)) {
|
|
|
|
$name = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Improve performance for correctly written format names
|
2019-06-18 20:15:20 +03:00
|
|
|
if(in_array($name, $this->getFormatNames())) {
|
|
|
|
$index = array_search($name, $this->getFormatNames());
|
|
|
|
return $this->getFormatNames()[$index];
|
2019-06-18 19:32:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The name is valid if a corresponding format file is found on disk
|
2019-06-18 20:15:20 +03:00
|
|
|
if(in_array(strtolower($name), array_map('strtolower', $this->getFormatNames()))) {
|
|
|
|
$index = array_search(strtolower($name), array_map('strtolower', $this->getFormatNames()));
|
|
|
|
return $this->getFormatNames()[$index];
|
2019-06-18 19:32:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Debug::log('Invalid format name: "' . $name . '"!');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return null; // Bad parameter
|
|
|
|
|
|
|
|
}
|
2016-08-23 15:29:53 +03:00
|
|
|
}
|