Merge pull request #723 from Zankaria/improve-context-init

Improve Context initialization
This commit is contained in:
Lorenzo Yario 2024-04-18 14:10:59 -07:00 committed by GitHub
commit 004481b920
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -52,22 +52,15 @@ class Context {
private ?HttpDriver $http; 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) { public function __construct(DependencyFactory $factory) {
$this->factory = $factory; $this->factory = $factory;
} }
public function getLog(): Log { public function getLog(): Log {
return $this->lazyGet($this->log, 'logDriver'); return $this->log ??= $this->factory->buildLogDriver();
} }
public function getHttpDriver(): HttpDriver { public function getHttpDriver(): HttpDriver {
return $this->lazyGet($this->http, 'httpDriver'); return $this->http ??= $this->factory->buildHttpDriver();
} }
} }