diff --git a/src/Handlers/ArrayHandler.php b/src/Handlers/ArrayHandler.php index b1a2320..d63b184 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,48 @@ 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..639834a 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 = []; + + // Add handlers + foreach ($this->handlers as $handler) { + $properties[$handler::class] = $handler->getAll($class, $context); + } + + return $properties; + } + /** * Save a value to the writable handler for later retrieval. *