diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ActionFactory.php | 38 | ||||
-rw-r--r-- | lib/ActionInterface.php | 2 | ||||
-rw-r--r-- | lib/RssBridge.php | 22 | ||||
-rw-r--r-- | lib/contents.php | 32 |
4 files changed, 51 insertions, 43 deletions
diff --git a/lib/ActionFactory.php b/lib/ActionFactory.php deleted file mode 100644 index c97891b7..00000000 --- a/lib/ActionFactory.php +++ /dev/null @@ -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(); - } -} diff --git a/lib/ActionInterface.php b/lib/ActionInterface.php index ea5020a3..4eb9cc65 100644 --- a/lib/ActionInterface.php +++ b/lib/ActionInterface.php @@ -22,7 +22,7 @@ interface ActionInterface * * Note: This function directly outputs data to the user. * - * @return void + * @return ?string */ public function execute(array $request); } diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 3ff118f7..904a1cd4 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -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(); + } } } diff --git a/lib/contents.php b/lib/contents.php index 33f20cc2..c339d3ca 100644 --- a/lib/contents.php +++ b/lib/contents.php @@ -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; + } } /** |