From f93dd1fae5c31a6dc793a2a2e540222dae2369f2 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 9 Apr 2024 11:20:04 +0200 Subject: [PATCH 1/2] Context: extract dependency building from the container --- inc/context.php | 37 ++++++++++++++++++++++++++----------- post.php | 7 +++---- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/inc/context.php b/inc/context.php index 3e52b569..46dd5061 100644 --- a/inc/context.php +++ b/inc/context.php @@ -6,18 +6,20 @@ use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers}; defined('TINYBOARD') or exit; -interface Context { - public function getLog(): Log; - public function getHttpDriver(): HttpDriver; +interface DependencyFactory { + public function buildLogDriver(): Log; + public function buildHttpDriver(): HttpDriver; } -class AppContext implements Context { +class WebDependencyFactory implements DependencyFactory { private array $config; - private ?Log $log; - private ?HttpDriver $http; - private function initLogDriver(): Log { + public function __construct(array $config) { + $this->config = $config; + } + + public function buildLogDriver(): Log { $name = $this->config['log_system']['name']; $level = $this->config['debug'] ? Log::DEBUG : Log::NOTICE; $backend = $this->config['log_system']['type']; @@ -36,21 +38,34 @@ class AppContext implements Context { } } + public function buildHttpDriver(): HttpDriver { + return HttpDrivers::getHttpDriver( + $this->config['upload_by_url_timeout'], + $this->config['max_filesize'] + ); + } +} - public function __construct(array $config) { - $this->config = $config; +class Context { + private DependencyFactory $factory; + private ?Log $log; + private ?HttpDriver $http; + + + public function __construct(DependencyFactory $factory) { + $this->factory = $factory; } public function getLog(): Log { if (is_null($this->log)) { - $this->log = $this->initLogDriver(); + $this->log = $this->factory->buildLogDriver(); } return $this->log; } public function getHttpDriver(): HttpDriver { if (is_null($this->http)) { - $this->http = HttpDrivers::getHttpDriver($this->config['upload_by_url_timeout'], $this->config['max_filesize']); + $this->http = $this->factory->buildHttpDriver(); } return $this->http; } diff --git a/post.php b/post.php index 923828c0..4b28a908 100644 --- a/post.php +++ b/post.php @@ -5,9 +5,8 @@ require_once 'inc/bootstrap.php'; -use Vichan\AppContext; -use Vichan\Driver\HttpDriver; -use Vichan\Driver\Log; +use Vichan\{Context, WebDependencyFactory}; +use Vichan\Driver\{HttpDriver, Log}; use Vichan\Service\{RemoteCaptchaQuery, NativeCaptchaQuery}; /** @@ -172,7 +171,7 @@ function strip_image_metadata(string $img_path): int { */ $dropped_post = false; -$context = new AppContext($config); +$context = new Context(new WebDependencyFactory($config)); // Is it a post coming from NNTP? Let's extract it and pretend it's a normal post. if (isset($_GET['Newsgroups']) && $config['nntpchan']['enabled']) { From a83bcf14a4704566960f64e600014b7e869aa3a1 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Tue, 9 Apr 2024 11:43:46 +0200 Subject: [PATCH 2/2] Context: simplify lazy initialization --- inc/context.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/inc/context.php b/inc/context.php index 46dd5061..98e30d6d 100644 --- a/inc/context.php +++ b/inc/context.php @@ -52,21 +52,22 @@ class Context { private ?HttpDriver $http; + private function lazyGet(mixed &$field_ref, string $dependency_name): mixed { + if (is_null($field_ref)) { + $field_ref = [$this->factory, "build{$dependency_name}"](); + } + return $field_ref; + } + public function __construct(DependencyFactory $factory) { $this->factory = $factory; } public function getLog(): Log { - if (is_null($this->log)) { - $this->log = $this->factory->buildLogDriver(); - } - return $this->log; + return $this->lazyGet($this->log, 'logDriver'); } public function getHttpDriver(): HttpDriver { - if (is_null($this->http)) { - $this->http = $this->factory->buildHttpDriver(); - } - return $this->http; + return $this->lazyGet($this->http, 'httpDriver'); } }