diff options
author | 2024-08-29 22:48:59 +0200 | |
---|---|---|
committer | 2024-08-29 22:48:59 +0200 | |
commit | 58544cd61a465102bebc315b4467e4e45d587723 (patch) | |
tree | b4cba82d80591078ec7564d076ccf49c13e6cc0f /lib | |
parent | e010fd4d52e3273d6ba2d2d6f45e5a58880ba043 (diff) | |
download | rss-bridge-58544cd61a465102bebc315b4467e4e45d587723.tar.gz rss-bridge-58544cd61a465102bebc315b4467e4e45d587723.tar.zst rss-bridge-58544cd61a465102bebc315b4467e4e45d587723.zip |
refactor: introduce DI container (#4238)
* refactor: introduce DI container
* add bin/test
Diffstat (limited to 'lib')
-rw-r--r-- | lib/BridgeCard.php | 9 | ||||
-rw-r--r-- | lib/BridgeFactory.php | 10 | ||||
-rw-r--r-- | lib/CacheFactory.php | 4 | ||||
-rw-r--r-- | lib/Container.php | 33 | ||||
-rw-r--r-- | lib/RssBridge.php | 23 | ||||
-rw-r--r-- | lib/dependencies.php | 78 | ||||
-rw-r--r-- | lib/logger.php | 3 |
7 files changed, 132 insertions, 28 deletions
diff --git a/lib/BridgeCard.php b/lib/BridgeCard.php index f270c1a3..855ddb93 100644 --- a/lib/BridgeCard.php +++ b/lib/BridgeCard.php @@ -2,10 +2,11 @@ final class BridgeCard { - public static function render(string $bridgeClassName, ?string $token): string - { - $bridgeFactory = new BridgeFactory(); - + public static function render( + BridgeFactory $bridgeFactory, + string $bridgeClassName, + ?string $token + ): string { $bridge = $bridgeFactory->create($bridgeClassName); $uri = $bridge->getURI(); diff --git a/lib/BridgeFactory.php b/lib/BridgeFactory.php index ad433287..c214e44b 100644 --- a/lib/BridgeFactory.php +++ b/lib/BridgeFactory.php @@ -8,10 +8,12 @@ final class BridgeFactory private array $enabledBridges = []; private array $missingEnabledBridges = []; - public function __construct() - { - $this->cache = RssBridge::getCache(); - $this->logger = RssBridge::getLogger(); + public function __construct( + CacheInterface $cache, + Logger $logger + ) { + $this->cache = $cache; + $this->logger = $logger; // Create all possible bridge class names from fs foreach (scandir(__DIR__ . '/../bridges/') as $file) { diff --git a/lib/CacheFactory.php b/lib/CacheFactory.php index 90aa21ba..47bbbf72 100644 --- a/lib/CacheFactory.php +++ b/lib/CacheFactory.php @@ -14,10 +14,6 @@ class CacheFactory public function create(string $name = null): CacheInterface { - $name ??= Configuration::getConfig('cache', 'type'); - if (!$name) { - throw new \Exception('No cache type configured'); - } $cacheNames = []; foreach (scandir(PATH_LIB_CACHES) as $file) { if (preg_match('/^([^.]+)Cache\.php$/U', $file, $m)) { diff --git a/lib/Container.php b/lib/Container.php new file mode 100644 index 00000000..6dd0b6d3 --- /dev/null +++ b/lib/Container.php @@ -0,0 +1,33 @@ +<?php + +declare(strict_types=1); + +class Container implements \ArrayAccess +{ + private array $values = []; + private array $resolved = []; + + public function offsetSet($offset, $value): void + { + $this->values[$offset] = $value; + } + + #[ReturnTypeWillChange] public function offsetGet($offset) + { + if (!isset($this->values[$offset])) { + throw new \Exception(sprintf('Unknown container key: "%s"', $offset)); + } + if (!isset($this->resolved[$offset])) { + $this->resolved[$offset] = $this->values[$offset]($this); + } + return $this->resolved[$offset]; + } + + #[ReturnTypeWillChange] public function offsetExists($offset) + { + } + + public function offsetUnset($offset): void + { + } +} diff --git a/lib/RssBridge.php b/lib/RssBridge.php index 35318c5b..9c8f5767 100644 --- a/lib/RssBridge.php +++ b/lib/RssBridge.php @@ -2,18 +2,12 @@ final class RssBridge { - private static Logger $logger; - private static CacheInterface $cache; - private static HttpClient $httpClient; + private static Container $container; public function __construct( - Logger $logger, - CacheInterface $cache, - HttpClient $httpClient + Container $container ) { - self::$logger = $logger; - self::$cache = $cache; - self::$httpClient = $httpClient; + self::$container = $container; } public function main(Request $request): Response @@ -83,10 +77,9 @@ final class RssBridge return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Invalid action']), 400); } - $className = '\\' . $actionName; - $actionObject = new $className(); + $controller = self::$container[$actionName]; - $response = $actionObject($request); + $response = $controller($request); return $response; } @@ -94,16 +87,16 @@ final class RssBridge public static function getLogger(): Logger { // null logger is only for the tests not to fail - return self::$logger ?? new NullLogger(); + return self::$container['logger'] ?? new NullLogger(); } public static function getCache(): CacheInterface { - return self::$cache; + return self::$container['cache']; } public static function getHttpClient(): HttpClient { - return self::$httpClient; + return self::$container['http_client']; } } diff --git a/lib/dependencies.php b/lib/dependencies.php new file mode 100644 index 00000000..227a66f1 --- /dev/null +++ b/lib/dependencies.php @@ -0,0 +1,78 @@ +<?php + +declare(strict_types=1); + +$container = new Container(); + +$container[ConnectivityAction::class] = function ($c) { + return new ConnectivityAction($c['bridge_factory']); +}; + +$container[DetectAction::class] = function ($c) { + return new DetectAction($c['bridge_factory']); +}; + +$container[DisplayAction::class] = function ($c) { + return new DisplayAction($c['cache'], $c['logger'], $c['bridge_factory']); +}; + +$container[FindfeedAction::class] = function ($c) { + return new FindfeedAction($c['bridge_factory']); +}; + +$container[FrontpageAction::class] = function ($c) { + return new FrontpageAction($c['bridge_factory']); +}; + +$container[HealthAction::class] = function () { + return new HealthAction(); +}; + +$container[ListAction::class] = function ($c) { + return new ListAction($c['bridge_factory']); +}; + +$container['bridge_factory'] = function ($c) { + return new BridgeFactory($c['cache'], $c['logger']); +}; + + +$container['http_client'] = function () { + return new CurlHttpClient(); +}; + +$container['cache_factory'] = function ($c) { + return new CacheFactory($c['logger']); +}; + +$container['logger'] = function () { + $logger = new SimpleLogger('rssbridge'); + if (Debug::isEnabled()) { + $logger->addHandler(new ErrorLogHandler(Logger::DEBUG)); + } else { + $logger->addHandler(new ErrorLogHandler(Logger::INFO)); + } + // Uncomment this for info logging to fs + // $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::INFO)); + + // Uncomment this for debug logging to fs + //$logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.txt', Logger::DEBUG)); + return $logger; +}; + +$container['cache'] = function ($c) { + /** @var CacheFactory $cacheFactory */ + $cacheFactory = $c['cache_factory']; + $type = Configuration::getConfig('cache', 'type'); + if (!$type) { + throw new \Exception('No cache type configured'); + } + if (Debug::isEnabled()) { + $cache = $cacheFactory->create('array'); + } else { + $cache = $cacheFactory->create($type); + } + return $cache; +}; + +return $container; diff --git a/lib/logger.php b/lib/logger.php index 3ebe3b0a..74a0e713 100644 --- a/lib/logger.php +++ b/lib/logger.php @@ -175,8 +175,9 @@ final class ErrorLogHandler $context = Json::encode($record['context']); } } + // Intentionally omitting newline $text = sprintf( - "[%s] %s.%s %s %s\n", + '[%s] %s.%s %s %s', $record['created_at']->format('Y-m-d H:i:s'), $record['name'], $record['level_name'], |