1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-11-28 01:10:51 +01:00

Merge pull request #722 from Zankaria/dep-inj-factory

Splits the dependecy container from the dependency building
This commit is contained in:
Lorenzo Yario 2024-04-09 05:37:21 -05:00 committed by GitHub
commit 73f2e3222b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 21 deletions

View File

@ -6,18 +6,20 @@ use Vichan\Driver\{HttpDriver, HttpDrivers, Log, LogDrivers};
defined('TINYBOARD') or exit; defined('TINYBOARD') or exit;
interface Context { interface DependencyFactory {
public function getLog(): Log; public function buildLogDriver(): Log;
public function getHttpDriver(): HttpDriver; public function buildHttpDriver(): HttpDriver;
} }
class AppContext implements Context { class WebDependencyFactory implements DependencyFactory {
private array $config; 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']; $name = $this->config['log_system']['name'];
$level = $this->config['debug'] ? Log::DEBUG : Log::NOTICE; $level = $this->config['debug'] ? Log::DEBUG : Log::NOTICE;
$backend = $this->config['log_system']['type']; $backend = $this->config['log_system']['type'];
@ -36,22 +38,36 @@ 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) { class Context {
$this->config = $config; private DependencyFactory $factory;
private ?Log $log;
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 { public function getLog(): Log {
if (is_null($this->log)) { return $this->lazyGet($this->log, 'logDriver');
$this->log = $this->initLogDriver();
}
return $this->log;
} }
public function getHttpDriver(): HttpDriver { public function getHttpDriver(): HttpDriver {
if (is_null($this->http)) { return $this->lazyGet($this->http, 'httpDriver');
$this->http = HttpDrivers::getHttpDriver($this->config['upload_by_url_timeout'], $this->config['max_filesize']);
}
return $this->http;
} }
} }

View File

@ -5,9 +5,8 @@
require_once 'inc/bootstrap.php'; require_once 'inc/bootstrap.php';
use Vichan\AppContext; use Vichan\{Context, WebDependencyFactory};
use Vichan\Driver\HttpDriver; use Vichan\Driver\{HttpDriver, Log};
use Vichan\Driver\Log;
use Vichan\Service\{RemoteCaptchaQuery, NativeCaptchaQuery}; use Vichan\Service\{RemoteCaptchaQuery, NativeCaptchaQuery};
/** /**
@ -172,7 +171,7 @@ function strip_image_metadata(string $img_path): int {
*/ */
$dropped_post = false; $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. // 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']) { if (isset($_GET['Newsgroups']) && $config['nntpchan']['enabled']) {