From 8389c399bd421be954416f9c5bd519cc15b278e1 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Wed, 10 Apr 2024 17:29:25 +0200 Subject: [PATCH] Context: shorter lazy initialization --- inc/context.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/inc/context.php b/inc/context.php index 98e30d6d..bb261047 100644 --- a/inc/context.php +++ b/inc/context.php @@ -52,22 +52,15 @@ 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 { - return $this->lazyGet($this->log, 'logDriver'); + return $this->log ??= $this->factory->buildLogDriver(); } public function getHttpDriver(): HttpDriver { - return $this->lazyGet($this->http, 'httpDriver'); + return $this->http ??= $this->factory->buildHttpDriver(); } }