mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-07-31 13:50:23 +02:00
refactor: extract frontpage to template (#3130)
Also introduce usage of Response object
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
<?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
|
||||
*/
|
||||
|
||||
class ActionFactory
|
||||
{
|
||||
private $folder;
|
||||
|
||||
public function __construct(string $folder = PATH_LIB_ACTIONS)
|
||||
{
|
||||
$this->folder = $folder;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name The name of the action e.g. "Display", "List", or "Connectivity"
|
||||
*/
|
||||
public function create(string $name): ActionInterface
|
||||
{
|
||||
$name = strtolower($name) . 'Action';
|
||||
$name = implode(array_map('ucfirst', explode('-', $name)));
|
||||
$filePath = $this->folder . $name . '.php';
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \Exception('Invalid action');
|
||||
}
|
||||
$className = '\\' . $name;
|
||||
return new $className();
|
||||
}
|
||||
}
|
@@ -22,7 +22,7 @@ interface ActionInterface
|
||||
*
|
||||
* Note: This function directly outputs data to the user.
|
||||
*
|
||||
* @return void
|
||||
* @return ?string
|
||||
*/
|
||||
public function execute(array $request);
|
||||
}
|
||||
|
@@ -60,6 +60,7 @@ final class RssBridge
|
||||
}
|
||||
});
|
||||
|
||||
// Consider: ini_set('error_reporting', E_ALL & ~E_DEPRECATED);
|
||||
date_default_timezone_set(Configuration::getConfig('system', 'timezone'));
|
||||
|
||||
$authenticationMiddleware = new AuthenticationMiddleware();
|
||||
@@ -73,9 +74,22 @@ final class RssBridge
|
||||
}
|
||||
}
|
||||
|
||||
$actionFactory = new ActionFactory();
|
||||
$action = $request['action'] ?? 'Frontpage';
|
||||
$action = $actionFactory->create($action);
|
||||
$action->execute($request);
|
||||
$actionName = $request['action'] ?? 'Frontpage';
|
||||
$actionName = strtolower($actionName) . 'Action';
|
||||
$actionName = implode(array_map('ucfirst', explode('-', $actionName)));
|
||||
|
||||
$filePath = __DIR__ . '/../actions/' . $actionName . '.php';
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \Exception(sprintf('Invalid action: %s', $actionName));
|
||||
}
|
||||
$className = '\\' . $actionName;
|
||||
$action = new $className();
|
||||
|
||||
$response = $action->execute($request);
|
||||
if (is_string($response)) {
|
||||
print $response;
|
||||
} elseif ($response instanceof Response) {
|
||||
$response->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -44,6 +44,38 @@ final class Response
|
||||
'504' => 'Gateway Timeout',
|
||||
'505' => 'HTTP Version Not Supported'
|
||||
];
|
||||
private string $body;
|
||||
private int $code;
|
||||
private array $headers;
|
||||
|
||||
public function __construct(
|
||||
string $body = '',
|
||||
int $code = 200,
|
||||
array $headers = []
|
||||
) {
|
||||
$this->body = $body;
|
||||
$this->code = $code;
|
||||
$this->headers = $headers;
|
||||
}
|
||||
|
||||
public function getBody()
|
||||
{
|
||||
return $this->body;
|
||||
}
|
||||
|
||||
public function getHeaders()
|
||||
{
|
||||
return $this->headers;
|
||||
}
|
||||
|
||||
public function send(): void
|
||||
{
|
||||
http_response_code($this->code);
|
||||
foreach ($this->headers as $name => $value) {
|
||||
header(sprintf('%s: %s', $name, $value));
|
||||
}
|
||||
print $this->body;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user