refactor: change the way dependencies are wired (#4194)

* refactor: change the way dependencies are setup

* lint
This commit is contained in:
Dag 2024-08-07 03:15:43 +02:00 committed by GitHub
parent 6ec9193546
commit 4faaa79101
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 93 additions and 81 deletions

View file

@ -198,6 +198,9 @@ final class Configuration
public static function getConfig(string $section, string $key, $default = null)
{
if (self::$config === []) {
throw new \Exception('Config has not been loaded');
}
return self::$config[strtolower($section)][strtolower($key)] ?? $default;
}

View file

@ -16,6 +16,9 @@ class Debug
return false;
}
/**
* @deprecated Use $this->logger->debug()
*/
public static function log($message)
{
$e = new \Exception();

View file

@ -2,25 +2,18 @@
final class RssBridge
{
private static CacheInterface $cache;
private static Logger $logger;
private static CacheInterface $cache;
private static HttpClient $httpClient;
public function __construct()
{
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();
$cacheFactory = new CacheFactory(self::$logger);
if (Debug::isEnabled()) {
self::$cache = $cacheFactory->create('array');
} else {
self::$cache = $cacheFactory->create();
}
public function __construct(
Logger $logger,
CacheInterface $cache,
HttpClient $httpClient
) {
self::$logger = $logger;
self::$cache = $cache;
self::$httpClient = $httpClient;
}
public function main(array $argv = []): Response
@ -105,16 +98,16 @@ final class RssBridge
return $response;
}
public static function getCache(): CacheInterface
{
return self::$cache;
}
public static function getLogger(): Logger
{
return self::$logger;
}
public static function getCache(): CacheInterface
{
return self::$cache;
}
public static function getHttpClient(): HttpClient
{
return self::$httpClient;

View file

@ -45,9 +45,3 @@ spl_autoload_register(function ($className) {
}
}
});
$customConfig = [];
if (file_exists(__DIR__ . '/../config.ini.php')) {
$customConfig = parse_ini_file(__DIR__ . '/../config.ini.php', true, INI_SCANNER_TYPED);
}
Configuration::loadConfiguration($customConfig, getenv());

View file

@ -83,10 +83,12 @@ final class SimpleLogger implements Logger
final class StreamHandler
{
private $stream;
private int $level;
public function __construct(int $level = Logger::DEBUG)
public function __construct(string $stream, int $level = Logger::DEBUG)
{
$this->stream = $stream;
$this->level = $level;
}
@ -147,13 +149,8 @@ final class StreamHandler
$record['message'],
$context
);
error_log($text);
if ($record['level'] < Logger::ERROR && Debug::isEnabled()) {
// The record level is INFO or WARNING here
// Not a good idea to print here because http headers might not have been sent
print sprintf("<pre>%s</pre>\n", e($text));
}
//$bytes = file_put_contents('/tmp/rss-bridge.log', $text, FILE_APPEND | LOCK_EX);
$bytes = file_put_contents($this->stream, $text, FILE_APPEND | LOCK_EX);
}
}