mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-07 09:06:36 +02:00
core: Implement action factory (#1002)
This commit is contained in:
33
lib/ActionAbstract.php
Normal file
33
lib/ActionAbstract.php
Normal file
@@ -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;
|
||||
}
|
||||
}
|
65
lib/ActionFactory.php
Normal file
65
lib/ActionFactory.php
Normal file
@@ -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';
|
||||
}
|
||||
}
|
34
lib/ActionInterface.php
Normal file
34
lib/ActionInterface.php
Normal file
@@ -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();
|
||||
}
|
70
lib/FactoryAbstract.php
Normal file
70
lib/FactoryAbstract.php
Normal file
@@ -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);
|
||||
}
|
@@ -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';
|
||||
|
Reference in New Issue
Block a user