mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-03-14 20:21:14 +03:00
refactor: inject the action params via its execute method (#2907)
This commit is contained in:
parent
22c10941dc
commit
a966213cd7
7 changed files with 32 additions and 35 deletions
|
@ -24,8 +24,6 @@
|
|||
*/
|
||||
class ConnectivityAction implements ActionInterface
|
||||
{
|
||||
public $userData = [];
|
||||
|
||||
private BridgeFactory $bridgeFactory;
|
||||
|
||||
public function __construct()
|
||||
|
@ -33,18 +31,18 @@ class ConnectivityAction implements ActionInterface
|
|||
$this->bridgeFactory = new \BridgeFactory();
|
||||
}
|
||||
|
||||
public function execute()
|
||||
public function execute(array $request)
|
||||
{
|
||||
if (!Debug::isEnabled()) {
|
||||
returnError('This action is only available in debug mode!', 400);
|
||||
}
|
||||
|
||||
if (!isset($this->userData['bridge'])) {
|
||||
if (!isset($request['bridge'])) {
|
||||
$this->returnEntryPage();
|
||||
return;
|
||||
}
|
||||
|
||||
$bridgeName = $this->userData['bridge'];
|
||||
$bridgeName = $request['bridge'];
|
||||
|
||||
$bridgeClassName = $this->bridgeFactory->sanitizeBridgeName($bridgeName);
|
||||
|
||||
|
|
|
@ -14,14 +14,12 @@
|
|||
|
||||
class DetectAction implements ActionInterface
|
||||
{
|
||||
public $userData = [];
|
||||
|
||||
public function execute()
|
||||
public function execute(array $request)
|
||||
{
|
||||
$targetURL = $this->userData['url']
|
||||
$targetURL = $request['url']
|
||||
or returnClientError('You must specify a url!');
|
||||
|
||||
$format = $this->userData['format']
|
||||
$format = $request['format']
|
||||
or returnClientError('You must specify a format!');
|
||||
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
|
|
@ -14,19 +14,20 @@
|
|||
|
||||
class DisplayAction implements ActionInterface
|
||||
{
|
||||
public $userData = [];
|
||||
|
||||
public function execute()
|
||||
public function execute(array $request)
|
||||
{
|
||||
$bridgeFactory = new \BridgeFactory();
|
||||
|
||||
$bridgeClassName = isset($this->userData['bridge']) ? $bridgeFactory->sanitizeBridgeName($this->userData['bridge']) : null;
|
||||
$bridgeClassName = null;
|
||||
if (isset($request['bridge'])) {
|
||||
$bridgeClassName = $bridgeFactory->sanitizeBridgeName($request['bridge']);
|
||||
}
|
||||
|
||||
if ($bridgeClassName === null) {
|
||||
throw new \InvalidArgumentException('Bridge name invalid!');
|
||||
}
|
||||
|
||||
$format = $this->userData['format']
|
||||
$format = $request['format']
|
||||
or returnClientError('You must specify a format!');
|
||||
|
||||
// whitelist control
|
||||
|
@ -39,8 +40,8 @@ class DisplayAction implements ActionInterface
|
|||
$bridge = $bridgeFactory->create($bridgeClassName);
|
||||
$bridge->loadConfiguration();
|
||||
|
||||
$noproxy = array_key_exists('_noproxy', $this->userData)
|
||||
&& filter_var($this->userData['_noproxy'], FILTER_VALIDATE_BOOLEAN);
|
||||
$noproxy = array_key_exists('_noproxy', $request)
|
||||
&& filter_var($request['_noproxy'], FILTER_VALIDATE_BOOLEAN);
|
||||
|
||||
if (defined('PROXY_URL') && PROXY_BYBRIDGE && $noproxy) {
|
||||
define('NOPROXY', true);
|
||||
|
@ -48,22 +49,22 @@ class DisplayAction implements ActionInterface
|
|||
|
||||
// Cache timeout
|
||||
$cache_timeout = -1;
|
||||
if (array_key_exists('_cache_timeout', $this->userData)) {
|
||||
if (array_key_exists('_cache_timeout', $request)) {
|
||||
if (!CUSTOM_CACHE_TIMEOUT) {
|
||||
unset($this->userData['_cache_timeout']);
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . '?' . http_build_query($this->userData);
|
||||
unset($request['_cache_timeout']);
|
||||
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . '?' . http_build_query($request);
|
||||
header('Location: ' . $uri, true, 301);
|
||||
exit;
|
||||
}
|
||||
|
||||
$cache_timeout = filter_var($this->userData['_cache_timeout'], FILTER_VALIDATE_INT);
|
||||
$cache_timeout = filter_var($request['_cache_timeout'], FILTER_VALIDATE_INT);
|
||||
} else {
|
||||
$cache_timeout = $bridge->getCacheTimeout();
|
||||
}
|
||||
|
||||
// Remove parameters that don't concern bridges
|
||||
$bridge_params = array_diff_key(
|
||||
$this->userData,
|
||||
$request,
|
||||
array_fill_keys(
|
||||
[
|
||||
'action',
|
||||
|
@ -79,7 +80,7 @@ class DisplayAction implements ActionInterface
|
|||
|
||||
// Remove parameters that don't concern caches
|
||||
$cache_params = array_diff_key(
|
||||
$this->userData,
|
||||
$request,
|
||||
array_fill_keys(
|
||||
[
|
||||
'action',
|
||||
|
@ -162,19 +163,19 @@ class DisplayAction implements ActionInterface
|
|||
$item = new \FeedItem();
|
||||
|
||||
// Create "new" error message every 24 hours
|
||||
$this->userData['_error_time'] = urlencode((int)(time() / 86400));
|
||||
$request['_error_time'] = urlencode((int)(time() / 86400));
|
||||
|
||||
$message = sprintf(
|
||||
'Bridge returned error %s! (%s)',
|
||||
$e->getCode(),
|
||||
$this->userData['_error_time']
|
||||
$request['_error_time']
|
||||
);
|
||||
$item->setTitle($message);
|
||||
|
||||
$item->setURI(
|
||||
(isset($_SERVER['REQUEST_URI']) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : '')
|
||||
. '?'
|
||||
. http_build_query($this->userData)
|
||||
. http_build_query($request)
|
||||
);
|
||||
|
||||
$item->setTimestamp(time());
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
class ListAction implements ActionInterface
|
||||
{
|
||||
public function execute()
|
||||
public function execute(array $request)
|
||||
{
|
||||
$list = new StdClass();
|
||||
$list->bridges = [];
|
||||
|
|
12
index.php
12
index.php
|
@ -8,18 +8,18 @@ rss-bridge from the command line
|
|||
*/
|
||||
if (isset($argv)) {
|
||||
parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
|
||||
$params = array_merge($_GET, $cliArgs);
|
||||
$request = array_merge($_GET, $cliArgs);
|
||||
} else {
|
||||
$params = $_GET;
|
||||
$request = $_GET;
|
||||
}
|
||||
|
||||
try {
|
||||
$actionFactory = new ActionFactory();
|
||||
|
||||
if (array_key_exists('action', $params)) {
|
||||
$action = $actionFactory->create($params['action']);
|
||||
$action->userData = $params;
|
||||
$action->execute();
|
||||
if (array_key_exists('action', $request)) {
|
||||
$action = $actionFactory->create($request['action']);
|
||||
|
||||
$action->execute($request);
|
||||
} else {
|
||||
$showInactive = filter_input(INPUT_GET, 'show_inactive', FILTER_VALIDATE_BOOLEAN);
|
||||
echo BridgeList::create($showInactive);
|
||||
|
|
|
@ -24,5 +24,5 @@ interface ActionInterface
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function execute();
|
||||
public function execute(array $request);
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ class ListActionTest extends TestCase
|
|||
$action = $actionFactory->create('list');
|
||||
|
||||
ob_start();
|
||||
$action->execute();
|
||||
$action->execute([]);
|
||||
$this->data = ob_get_contents();
|
||||
ob_clean();
|
||||
ob_end_flush();
|
||||
|
|
Loading…
Add table
Reference in a new issue