forked from GithubBackups/vichan
Context: simplify lazy initialization
This commit is contained in:
parent
f93dd1fae5
commit
a83bcf14a4
@ -52,21 +52,22 @@ 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 {
|
||||||
if (is_null($this->log)) {
|
return $this->lazyGet($this->log, 'logDriver');
|
||||||
$this->log = $this->factory->buildLogDriver();
|
|
||||||
}
|
|
||||||
return $this->log;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getHttpDriver(): HttpDriver {
|
public function getHttpDriver(): HttpDriver {
|
||||||
if (is_null($this->http)) {
|
return $this->lazyGet($this->http, 'httpDriver');
|
||||||
$this->http = $this->factory->buildHttpDriver();
|
|
||||||
}
|
|
||||||
return $this->http;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user