From e972c1655c2ac30dafb9a62b90b0ffbf5036efae Mon Sep 17 00:00:00 2001 From: Julian Atkins Date: Mon, 8 Dec 2025 17:13:38 +0200 Subject: [PATCH 1/2] Implement a getAll function --- src/Handlers/ArrayHandler.php | 46 ++++++++++++++++++++++++++++++++ src/Handlers/BaseHandler.php | 7 +++++ src/Handlers/DatabaseHandler.php | 10 +++++++ src/Settings.php | 22 +++++++++++++++ 4 files changed, 85 insertions(+) diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index b1a2320..ea9c688 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -54,6 +54,11 @@ public function get(string $class, string $property, ?string $context = null) return $this->getStored($class, $property, $context); } + public function getAll(?string $class, ?string $context = null) + { + return $this->getAllStored($class, $context); + } + public function set(string $class, string $property, $value = null, ?string $context = null) { $this->setStored($class, $property, $value, $context); @@ -98,6 +103,47 @@ protected function getStored(string $class, string $property, ?string $context) : $this->parseValue(...$this->contexts[$context][$class][$property]); } + /** + * Retrieves all values from storage. + * + * @return mixed|null + */ + protected function getAllStored(?string $class, ?string $context) + { + $properties = null; + + if ($context === null) { + if ($class === null) { + $properties = $this->general; + } elseif (isset($this->general[$class])) { + $properties = $this->general[$class]; + } + } elseif (isset($this->contexts[$context])) { + if ($class === null) { + $properties = $this->contexts[$context]; + } elseif (isset($this->contexts[$context][$class])) { + $properties = $this->contexts[$context][$class]; + } + } + + if($properties === null) + return null; + + if($class === null) { + foreach($properties as &$c) { + foreach($c as &$p) { + $p = $this->parseValue(...$p); + } + } + } else { + foreach($properties as &$p) { + $p = $this->parseValue(...$p); + } + } + + return $properties; + } + /** * Adds values to storage. * diff --git a/src/Handlers/BaseHandler.php b/src/Handlers/BaseHandler.php index 696f854..e276fc9 100644 --- a/src/Handlers/BaseHandler.php +++ b/src/Handlers/BaseHandler.php @@ -18,6 +18,13 @@ abstract public function has(string $class, string $property, ?string $context = */ abstract public function get(string $class, string $property, ?string $context = null); + /** + * Returns all values from the handler, if stored. + * + * @return mixed + */ + abstract public function getAll(?string $class, ?string $context = null); + /** * If the Handler supports saving values, it * MUST override this method to provide that functionality. diff --git a/src/Handlers/DatabaseHandler.php b/src/Handlers/DatabaseHandler.php index 8b97ab7..220dc13 100644 --- a/src/Handlers/DatabaseHandler.php +++ b/src/Handlers/DatabaseHandler.php @@ -69,6 +69,16 @@ public function get(string $class, string $property, ?string $context = null) return $this->getStored($class, $property, $context); } + /** + * Retrieve all values from the database. + * + * @return mixed|null + */ + public function getAll(?string $class, ?string $context = null) + { + return $this->getAllStored($class, $context); + } + /** * Stores values into the database for later retrieval. * diff --git a/src/Settings.php b/src/Settings.php index 0e0d62c..857d96f 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -70,6 +70,28 @@ public function get(string $key, ?string $context = null) return $config->{$property} ?? null; } + /** + * Retrieve all values from any handler + * file.arg.optionalArg + */ + public function getAll(?string $key = null, ?string $context = null): array + { + if($key !== null) { + [$class,,] = $this->prepareClassAndProperty($key . "."); + } else { + $class = null; + } + + $properties = array(); + + // Add handlers + foreach ($this->handlers as $handler) { + $properties[get_class($handler)] = $handler->getAll($class, $context); + } + + return $properties; + } + /** * Save a value to the writable handler for later retrieval. * From d1c7865a9d0546d7af6bf56a98ff24ff2a8cac9e Mon Sep 17 00:00:00 2001 From: Julian Atkins Date: Mon, 8 Dec 2025 17:43:01 +0200 Subject: [PATCH 2/2] Praise the Gods of standards compliance --- src/Handlers/ArrayHandler.php | 11 ++++++----- src/Settings.php | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index ea9c688..d63b184 100644 --- a/src/Handlers/ArrayHandler.php +++ b/src/Handlers/ArrayHandler.php @@ -126,17 +126,18 @@ protected function getAllStored(?string $class, ?string $context) } } - if($properties === null) + if ($properties === null) { return null; + } - if($class === null) { - foreach($properties as &$c) { - foreach($c as &$p) { + if ($class === null) { + foreach ($properties as &$c) { + foreach ($c as &$p) { $p = $this->parseValue(...$p); } } } else { - foreach($properties as &$p) { + foreach ($properties as &$p) { $p = $this->parseValue(...$p); } } diff --git a/src/Settings.php b/src/Settings.php index 857d96f..639834a 100644 --- a/src/Settings.php +++ b/src/Settings.php @@ -76,17 +76,17 @@ public function get(string $key, ?string $context = null) */ public function getAll(?string $key = null, ?string $context = null): array { - if($key !== null) { - [$class,,] = $this->prepareClassAndProperty($key . "."); + if ($key !== null) { + [$class] = $this->prepareClassAndProperty($key . '.'); } else { $class = null; } - $properties = array(); + $properties = []; // Add handlers foreach ($this->handlers as $handler) { - $properties[get_class($handler)] = $handler->getAll($class, $context); + $properties[$handler::class] = $handler->getAll($class, $context); } return $properties;