diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ActionAbstract.php | 33 | ||||
-rw-r--r-- | lib/ActionFactory.php | 65 | ||||
-rw-r--r-- | lib/ActionInterface.php | 34 | ||||
-rw-r--r-- | lib/FactoryAbstract.php | 70 | ||||
-rw-r--r-- | lib/rssbridge.php | 7 |
5 files changed, 209 insertions, 0 deletions
diff --git a/lib/ActionAbstract.php b/lib/ActionAbstract.php new file mode 100644 index 00000000..b925d609 --- /dev/null +++ b/lib/ActionAbstract.php @@ -0,0 +1,33 @@ +<?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 + */ + +/** + * An abstract class for action objects + */ +abstract class ActionAbstract implements ActionInterface { + /** + * Holds the user data. + * + * @var array + */ + protected $userData = null; + + /** + * {@inheritdoc} + * + * @param array $userData {@inheritdoc} + */ + public function setUserData($userData) { + $this->userData = $userData; + } +} diff --git a/lib/ActionFactory.php b/lib/ActionFactory.php new file mode 100644 index 00000000..8146e542 --- /dev/null +++ b/lib/ActionFactory.php @@ -0,0 +1,65 @@ +<?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 for action objects. + */ +class ActionFactory extends FactoryAbstract { + /** + * {@inheritdoc} + * + * @param string $name {@inheritdoc} + */ + public function create($name) { + $filePath = $this->buildFilePath($name); + + if(!file_exists($filePath)) { + throw new \Exception('File ' . $filePath . ' does not exist!'); + } + + require_once $filePath; + + $class = $this->buildClassName($name); + + if((new \ReflectionClass($class))->isInstantiable()) { + return new $class(); + } + + return false; + } + + /** + * Build class name from action name + * + * The class name consists of the action name with prefix "Action". The first + * character of the class name must be uppercase. + * + * Example: 'display' => 'DisplayAction' + * + * @param string $name The action name. + * @return string The class name. + */ + protected function buildClassName($name) { + return ucfirst(strtolower($name)) . 'Action'; + } + + /** + * Build file path to the action class. + * + * @param string $name The action name. + * @return string Path to the action class. + */ + protected function buildFilePath($name) { + return $this->getWorkingDir() . $this->buildClassName($name) . '.php'; + } +} diff --git a/lib/ActionInterface.php b/lib/ActionInterface.php new file mode 100644 index 00000000..c38d057a --- /dev/null +++ b/lib/ActionInterface.php @@ -0,0 +1,34 @@ +<?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 + */ + +/** + * Interface for action objects. + */ +interface ActionInterface { + /** + * Set user data for the action to consume. + * + * @param array $userData An associative array of user data. + * @return void + */ + function setUserData($userData); + + /** + * Execute the action. + * + * Note: This function directly outputs data to the user. + * + * @return void + */ + function execute(); +} diff --git a/lib/FactoryAbstract.php b/lib/FactoryAbstract.php new file mode 100644 index 00000000..c91ae2e0 --- /dev/null +++ b/lib/FactoryAbstract.php @@ -0,0 +1,70 @@ +<?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 + */ + +/** + * Abstract class for factories. + */ +abstract class FactoryAbstract { + + /** + * Holds the working directory + * + * @var string + */ + private $workingDir = null; + + /** + * Set the working directory. + * + * @param string $dir The working directory. + * @return void + */ + public function setWorkingDir($dir) { + $this->workingDir = null; + + if(!is_string($dir)) { + throw new \InvalidArgumentException('Working directory must be a string!'); + } + + if(!file_exists($dir)) { + throw new \Exception('Working directory does not exist!'); + } + + if(!is_dir($dir)) { + throw new \InvalidArgumentException($dir . ' is not a directory!'); + } + + $this->workingDir = realpath($dir) . '/'; + } + + /** + * Get the working directory + * + * @return string The working directory. + */ + public function getWorkingDir() { + if(is_null($this->workingDir)) { + throw new \LogicException('Working directory is not set!'); + } + + return $this->workingDir; + } + + /** + * Creates a new instance for the object specified by name. + * + * @param string $name The name of the object to create. + * @return object The object instance + */ + abstract public function create($name); +} diff --git a/lib/rssbridge.php b/lib/rssbridge.php index bc8c8d04..5a523588 100644 --- a/lib/rssbridge.php +++ b/lib/rssbridge.php @@ -29,6 +29,9 @@ define('PATH_LIB_FORMATS', __DIR__ . '/../formats/'); /** Path to the caches library */ define('PATH_LIB_CACHES', __DIR__ . '/../caches/'); +/** Path to the actions library */ +define('PATH_LIB_ACTIONS', __DIR__ . '/../actions/'); + /** Path to the cache folder */ define('PATH_CACHE', __DIR__ . '/../cache/'); @@ -39,11 +42,13 @@ define('WHITELIST', __DIR__ . '/../whitelist.txt'); define('REPOSITORY', 'https://github.com/RSS-Bridge/rss-bridge/'); // Interfaces +require_once PATH_LIB . 'ActionInterface.php'; require_once PATH_LIB . 'BridgeInterface.php'; require_once PATH_LIB . 'CacheInterface.php'; require_once PATH_LIB . 'FormatInterface.php'; // Classes +require_once PATH_LIB . 'FactoryAbstract.php'; require_once PATH_LIB . 'FeedItem.php'; require_once PATH_LIB . 'Debug.php'; require_once PATH_LIB . 'Exceptions.php'; @@ -58,6 +63,8 @@ require_once PATH_LIB . 'Configuration.php'; require_once PATH_LIB . 'BridgeCard.php'; require_once PATH_LIB . 'BridgeList.php'; require_once PATH_LIB . 'ParameterValidator.php'; +require_once PATH_LIB . 'ActionFactory.php'; +require_once PATH_LIB . 'ActionAbstract.php'; // Functions require_once PATH_LIB . 'html.php'; |