Skip to content

Commit e972c16

Browse files
author
Julian Atkins
committed
Implement a getAll function
1 parent 7fb10c2 commit e972c16

4 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/Handlers/ArrayHandler.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ public function get(string $class, string $property, ?string $context = null)
5454
return $this->getStored($class, $property, $context);
5555
}
5656

57+
public function getAll(?string $class, ?string $context = null)
58+
{
59+
return $this->getAllStored($class, $context);
60+
}
61+
5762
public function set(string $class, string $property, $value = null, ?string $context = null)
5863
{
5964
$this->setStored($class, $property, $value, $context);
@@ -98,6 +103,47 @@ protected function getStored(string $class, string $property, ?string $context)
98103
: $this->parseValue(...$this->contexts[$context][$class][$property]);
99104
}
100105

106+
/**
107+
* Retrieves all values from storage.
108+
*
109+
* @return mixed|null
110+
*/
111+
protected function getAllStored(?string $class, ?string $context)
112+
{
113+
$properties = null;
114+
115+
if ($context === null) {
116+
if ($class === null) {
117+
$properties = $this->general;
118+
} elseif (isset($this->general[$class])) {
119+
$properties = $this->general[$class];
120+
}
121+
} elseif (isset($this->contexts[$context])) {
122+
if ($class === null) {
123+
$properties = $this->contexts[$context];
124+
} elseif (isset($this->contexts[$context][$class])) {
125+
$properties = $this->contexts[$context][$class];
126+
}
127+
}
128+
129+
if($properties === null)
130+
return null;
131+
132+
if($class === null) {
133+
foreach($properties as &$c) {
134+
foreach($c as &$p) {
135+
$p = $this->parseValue(...$p);
136+
}
137+
}
138+
} else {
139+
foreach($properties as &$p) {
140+
$p = $this->parseValue(...$p);
141+
}
142+
}
143+
144+
return $properties;
145+
}
146+
101147
/**
102148
* Adds values to storage.
103149
*

src/Handlers/BaseHandler.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ abstract public function has(string $class, string $property, ?string $context =
1818
*/
1919
abstract public function get(string $class, string $property, ?string $context = null);
2020

21+
/**
22+
* Returns all values from the handler, if stored.
23+
*
24+
* @return mixed
25+
*/
26+
abstract public function getAll(?string $class, ?string $context = null);
27+
2128
/**
2229
* If the Handler supports saving values, it
2330
* MUST override this method to provide that functionality.

src/Handlers/DatabaseHandler.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ public function get(string $class, string $property, ?string $context = null)
6969
return $this->getStored($class, $property, $context);
7070
}
7171

72+
/**
73+
* Retrieve all values from the database.
74+
*
75+
* @return mixed|null
76+
*/
77+
public function getAll(?string $class, ?string $context = null)
78+
{
79+
return $this->getAllStored($class, $context);
80+
}
81+
7282
/**
7383
* Stores values into the database for later retrieval.
7484
*

src/Settings.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ public function get(string $key, ?string $context = null)
7070
return $config->{$property} ?? null;
7171
}
7272

73+
/**
74+
* Retrieve all values from any handler
75+
* file.arg.optionalArg
76+
*/
77+
public function getAll(?string $key = null, ?string $context = null): array
78+
{
79+
if($key !== null) {
80+
[$class,,] = $this->prepareClassAndProperty($key . ".");
81+
} else {
82+
$class = null;
83+
}
84+
85+
$properties = array();
86+
87+
// Add handlers
88+
foreach ($this->handlers as $handler) {
89+
$properties[get_class($handler)] = $handler->getAll($class, $context);
90+
}
91+
92+
return $properties;
93+
}
94+
7395
/**
7496
* Save a value to the writable handler for later retrieval.
7597
*

0 commit comments

Comments
 (0)