From 0de1694371eaaff8bda7f8fca8580c18d5c10e3a Mon Sep 17 00:00:00 2001 From: Dag Date: Thu, 18 Aug 2022 22:52:01 +0200 Subject: [PATCH] refactor: extract index.php to RssBridge.php (#2961) * refactor: extract entry point into class * refactor: js * refactor: extract frontpage action --- .../FrontpageAction.php | 157 ++++++------------ index.php | 53 +----- lib/RssBridge.php | 58 +++++++ static/{search.js => rss-bridge.js} | 9 + static/select.js | 10 -- 5 files changed, 116 insertions(+), 171 deletions(-) rename lib/BridgeList.php => actions/FrontpageAction.php (58%) create mode 100644 lib/RssBridge.php rename static/{search.js => rss-bridge.js} (86%) delete mode 100644 static/select.js diff --git a/lib/BridgeList.php b/actions/FrontpageAction.php similarity index 58% rename from lib/BridgeList.php rename to actions/FrontpageAction.php index 077b39ac..2cafe688 100644 --- a/lib/BridgeList.php +++ b/actions/FrontpageAction.php @@ -1,57 +1,27 @@ ' - . BridgeList::getHead() - . '' - . BridgeList::getHeader() - . BridgeList::getSearchbar() - . BridgeList::getBridges($showInactive, $totalBridges, $totalActiveBridges) - . BridgeList::getFooter($totalBridges, $totalActiveBridges, $showInactive) - . ''; + $html = self::getHead() + . self::getHeader() + . self::getSearchbar() + . self::getBridges($showInactive, $totalBridges, $totalActiveBridges) + . self::getFooter($totalBridges, $totalActiveBridges, $showInactive); + + print $html; } - /** - * Get the document head - * - * @return string The document head - */ private static function getHead() { return << @@ -59,62 +29,15 @@ final class BridgeList RSS-Bridge - - - + + + EOD; } - /** - * Get the document body for all bridge cards - * - * @param bool $showInactive Inactive bridges are visible on the home page if - * enabled. - * @param int $totalBridges (ref) Returns the total number of bridges. - * @param int $totalActiveBridges (ref) Returns the number of active bridges. - * @return string The document body for all bridge cards. - */ - private static function getBridges($showInactive, &$totalBridges, &$totalActiveBridges) - { - $body = ''; - $totalActiveBridges = 0; - $inactiveBridges = ''; - - $bridgeFactory = new BridgeFactory(); - $bridgeClassNames = $bridgeFactory->getBridgeClassNames(); - - $formatFactory = new FormatFactory(); - $formats = $formatFactory->getFormatNames(); - - $totalBridges = count($bridgeClassNames); - - foreach ($bridgeClassNames as $bridgeClassName) { - if ($bridgeFactory->isWhitelisted($bridgeClassName)) { - $body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats); - $totalActiveBridges++; - } elseif ($showInactive) { - // inactive bridges - $inactiveBridges .= BridgeCard::displayBridgeCard($bridgeClassName, $formats, false) . PHP_EOL; - } - } - - $body .= $inactiveBridges; - - return $body; - } - - /** - * Get the document header - * - * @return string The document header - */ private static function getHeader() { $warning = ''; @@ -141,11 +64,6 @@ EOD; EOD; } - /** - * Get the searchbar - * - * @return string The searchbar - */ private static function getSearchbar() { $query = filter_input(INPUT_GET, 'q', \FILTER_SANITIZE_SPECIAL_CHARS); @@ -166,23 +84,41 @@ EOD; EOD; } - /** - * Get the document footer - * - * @param int $totalBridges The total number of bridges, shown in the footer - * @param int $totalActiveBridges The total number of active bridges, shown - * in the footer. - * @param bool $showInactive Sets the 'Show active'/'Show inactive' text in - * the footer. - * @return string The document footer - */ + private static function getBridges($showInactive, &$totalBridges, &$totalActiveBridges) + { + $body = ''; + $totalActiveBridges = 0; + $inactiveBridges = ''; + + $bridgeFactory = new BridgeFactory(); + $bridgeClassNames = $bridgeFactory->getBridgeClassNames(); + + $formatFactory = new FormatFactory(); + $formats = $formatFactory->getFormatNames(); + + $totalBridges = count($bridgeClassNames); + + foreach ($bridgeClassNames as $bridgeClassName) { + if ($bridgeFactory->isWhitelisted($bridgeClassName)) { + $body .= BridgeCard::displayBridgeCard($bridgeClassName, $formats); + $totalActiveBridges++; + } elseif ($showInactive) { + $inactiveBridges .= BridgeCard::displayBridgeCard($bridgeClassName, $formats, false) . PHP_EOL; + } + } + + $body .= $inactiveBridges; + + return $body; + } + private static function getFooter($totalBridges, $totalActiveBridges, $showInactive) { $version = Configuration::getVersion(); $email = Configuration::getConfig('admin', 'email'); $admininfo = ''; - if (!empty($email)) { + if ($email) { $admininfo = << @@ -210,6 +146,7 @@ EOD; {$inactive} {$admininfo} + EOD; } } diff --git a/index.php b/index.php index c17d74ad..1835f963 100644 --- a/index.php +++ b/index.php @@ -2,55 +2,6 @@ require_once __DIR__ . '/lib/rssbridge.php'; -try { - Configuration::verifyInstallation(); - Configuration::loadConfiguration(); +$rssBridge = new RssBridge(); - date_default_timezone_set(Configuration::getConfig('system', 'timezone')); - - define('CUSTOM_CACHE_TIMEOUT', Configuration::getConfig('cache', 'custom_timeout')); - - $authenticationMiddleware = new AuthenticationMiddleware(); - if (Configuration::getConfig('authentication', 'enable')) { - $authenticationMiddleware(); - } - - if (isset($argv)) { - parse_str(implode('&', array_slice($argv, 1)), $cliArgs); - $request = $cliArgs; - } else { - $request = $_GET; - } - foreach ($request as $key => $value) { - if (! is_string($value)) { - throw new \Exception("Query parameter \"$key\" is not a string."); - } - } - - $actionFactory = new ActionFactory(); - - 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); - } -} catch (\Throwable $e) { - error_log($e); - - $message = sprintf( - 'Uncaught Exception %s: %s at %s line %s', - get_class($e), - $e->getMessage(), - trim_path_prefix($e->getFile()), - $e->getLine() - ); - - http_response_code(500); - print render('error.html.php', [ - 'message' => $message, - 'stacktrace' => create_sane_stacktrace($e), - ]); -} +$rssBridge->main($argv ?? []); diff --git a/lib/RssBridge.php b/lib/RssBridge.php new file mode 100644 index 00000000..76873025 --- /dev/null +++ b/lib/RssBridge.php @@ -0,0 +1,58 @@ +run($request); + } catch (\Throwable $e) { + error_log($e); + $message = sprintf( + 'Uncaught Exception %s: %s at %s line %s', + get_class($e), + $e->getMessage(), + trim_path_prefix($e->getFile()), + $e->getLine() + ); + http_response_code(500); + print render('error.html.php', [ + 'message' => $message, + 'stacktrace' => create_sane_stacktrace($e), + ]); + } + } + + private function run($request): void + { + Configuration::verifyInstallation(); + Configuration::loadConfiguration(); + + date_default_timezone_set(Configuration::getConfig('system', 'timezone')); + + define('CUSTOM_CACHE_TIMEOUT', Configuration::getConfig('cache', 'custom_timeout')); + + $authenticationMiddleware = new AuthenticationMiddleware(); + if (Configuration::getConfig('authentication', 'enable')) { + $authenticationMiddleware(); + } + + foreach ($request as $key => $value) { + if (!is_string($value)) { + throw new \Exception("Query parameter \"$key\" is not a string."); + } + } + + $actionFactory = new ActionFactory(); + $action = $request['action'] ?? 'Frontpage'; + $action = $actionFactory->create($action); + $action->execute($request); + } +} diff --git a/static/search.js b/static/rss-bridge.js similarity index 86% rename from static/search.js rename to static/rss-bridge.js index 93bede3d..498acd37 100644 --- a/static/search.js +++ b/static/rss-bridge.js @@ -38,3 +38,12 @@ function rssbridge_list_search() { } } } + +function rssbridge_toggle_bridge(){ + var fragment = window.location.hash.substr(1); + var bridge = document.getElementById(fragment); + + if(bridge !== null) { + bridge.getElementsByClassName('showmore-box')[0].checked = true; + } +} diff --git a/static/select.js b/static/select.js deleted file mode 100644 index 792b92de..00000000 --- a/static/select.js +++ /dev/null @@ -1,10 +0,0 @@ -function select(){ - var fragment = window.location.hash.substr(1); - var bridge = document.getElementById(fragment); - - if(bridge !== null) { - bridge.getElementsByClassName('showmore-box')[0].checked = true; - } -} - -document.addEventListener('DOMContentLoaded', select); \ No newline at end of file