rss-bridge/lib/RssBridge.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

85 lines
2.6 KiB
PHP
Raw Normal View History

<?php
final class RssBridge
{
private static CacheInterface $cache;
2023-09-21 23:05:55 +03:00
private static Logger $logger;
private static HttpClient $httpClient;
public function __construct()
{
2023-09-21 23:05:55 +03:00
self::$logger = new SimpleLogger('rssbridge');
if (Debug::isEnabled()) {
self::$logger->addHandler(new StreamHandler(Logger::DEBUG));
} else {
self::$logger->addHandler(new StreamHandler(Logger::INFO));
}
self::$httpClient = new CurlHttpClient();
2023-09-21 23:05:55 +03:00
$cacheFactory = new CacheFactory(self::$logger);
if (Debug::isEnabled()) {
self::$cache = $cacheFactory->create('array');
} else {
self::$cache = $cacheFactory->create();
}
}
public function main(array $argv = []): void
{
if ($argv) {
parse_str(implode('&', array_slice($argv, 1)), $cliArgs);
$request = $cliArgs;
} else {
2023-09-20 03:45:48 +03:00
if (Configuration::getConfig('authentication', 'enable')) {
$authenticationMiddleware = new AuthenticationMiddleware();
$authenticationMiddleware();
}
$request = array_merge($_GET, $_POST);
}
try {
foreach ($request as $key => $value) {
if (!is_string($value)) {
throw new \Exception("Query parameter \"$key\" is not a string.");
}
}
$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('Invalid action', 400);
}
$className = '\\' . $actionName;
$action = new $className();
$response = $action->execute($request);
if (is_string($response)) {
print $response;
} elseif ($response instanceof Response) {
$response->send();
}
} catch (\Throwable $e) {
2023-09-21 23:05:55 +03:00
self::$logger->error('Exception in RssBridge::main()', ['e' => $e]);
http_response_code(500);
print render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]);
}
}
2023-09-21 23:05:55 +03:00
public static function getCache(): CacheInterface
{
2023-09-21 23:05:55 +03:00
return self::$cache;
}
2023-09-21 23:05:55 +03:00
public static function getLogger(): Logger
{
2023-09-21 23:05:55 +03:00
return self::$logger;
}
public static function getHttpClient(): HttpClient
{
return self::$httpClient;
}
}