Skip to content

Commit 1dfc9be

Browse files
committed
Allow returning null when no database item and no config file item.
1 parent afc61ed commit 1dfc9be

File tree

1 file changed

+113
-101
lines changed

1 file changed

+113
-101
lines changed

src/Settings.php

Lines changed: 113 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -7,107 +7,119 @@
77
* retrieve settings that were original set in config files
88
* in the core application or any third-party module.
99
*/
10+
1011
class Settings
1112
{
12-
/**
13-
* An array of Setting Stores that handle
14-
* the actual act of getting/setting the values.
15-
*
16-
* @var array
17-
*/
18-
private $handlers = [];
19-
20-
/**
21-
* The name of the handler that handles writes.
22-
* @var string
23-
*/
24-
private $writeHandler;
25-
26-
/**
27-
* Grabs instances of our handlers.
28-
*/
29-
public function __construct()
30-
{
31-
foreach (config('Settings')->handlers as $handler) {
32-
$class = config('Settings')->{$handler}['class'] ?? null;
33-
34-
if ($class === null) {
35-
continue;
36-
}
37-
38-
$this->handlers[$handler] = new $class();
39-
40-
$writeable = config('Settings')->{$handler}['writeable'] ?? null;
41-
42-
if ($writeable) {
43-
$this->writeHandler = $handler;
44-
}
45-
}
46-
}
47-
48-
/**
49-
* Retrieve a value from either the database
50-
* or from a config file matching the name
51-
* file.arg.optionalArg
52-
*
53-
* @param string $class
54-
* @param string $key
55-
*/
56-
public function get(string $class, string $key)
57-
{
58-
$config = config($class);
59-
60-
if ($config !== null) {
61-
$class = get_class($config);
62-
}
63-
64-
// Try grabbing the values from any of our handlers
65-
foreach ($this->handlers as $name => $handler) {
66-
$value = $handler->get($class, $key);
67-
68-
if ($value !== null) {
69-
return $value;
70-
}
71-
}
72-
73-
return $config->{$key};
74-
}
75-
76-
/**
77-
* Save a value to the writable handler for later retrieval.
78-
*
79-
* @param string $class
80-
* @param string $key
81-
* @param null $value
82-
*
83-
* @return void|null
84-
*/
85-
public function set(string $class, string $key, $value = null)
86-
{
87-
$config = config($class);
88-
89-
// Use a fully qualified class name if the
90-
// config file was found.
91-
if ($config !== null) {
92-
$class = get_class($config);
93-
}
94-
95-
$handler = $this->getWriteHandler();
96-
97-
return $handler->set($class, $key, $value);
98-
}
99-
100-
/**
101-
* Returns the handler that is set to store values.
102-
*
103-
* @return mixed
104-
*/
105-
private function getWriteHandler()
106-
{
107-
if (empty($this->writeHandler) || ! isset($this->handlers[$this->writeHandler])) {
108-
throw new \RuntimeException('Unable to find a Settings handler that can store values.');
109-
}
110-
111-
return $this->handlers[$this->writeHandler];
112-
}
13+
/**
14+
* An array of Setting Stores that handle
15+
* the actual act of getting/setting the values.
16+
*
17+
* @var array
18+
*/
19+
private $handlers = [];
20+
21+
/**
22+
* The name of the handler that handles writes.
23+
*
24+
* @var string
25+
*/
26+
private $writeHandler;
27+
28+
/**
29+
* Grabs instances of our handlers.
30+
*/
31+
public function __construct()
32+
{
33+
foreach (config('Settings')->handlers as $handler)
34+
{
35+
$class = config('Settings')->{$handler}['class'] ?? null;
36+
37+
if ($class === null)
38+
{
39+
continue;
40+
}
41+
42+
$this->handlers[$handler] = new $class();
43+
44+
$writeable = config('Settings')->{$handler}['writeable'] ?? null;
45+
46+
if ($writeable)
47+
{
48+
$this->writeHandler = $handler;
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Retrieve a value from either the database
55+
* or from a config file matching the name
56+
* file.arg.optionalArg
57+
*
58+
* @param string $class
59+
* @param string $key
60+
*
61+
* @return mixed|null
62+
*/
63+
public function get(string $class, string $key)
64+
{
65+
$config = config($class);
66+
67+
if ($config !== null)
68+
{
69+
$class = get_class($config);
70+
}
71+
72+
// Try grabbing the values from any of our handlers
73+
foreach ($this->handlers as $name => $handler)
74+
{
75+
$value = $handler->get($class, $key);
76+
77+
if ($value !== null)
78+
{
79+
return $value;
80+
}
81+
}
82+
83+
return $config->{$key} ?? null;
84+
}
85+
86+
/**
87+
* Save a value to the writable handler for later retrieval.
88+
*
89+
* @param string $class
90+
* @param string $key
91+
* @param null $value
92+
*
93+
* @return void|null
94+
*/
95+
public function set(string $class, string $key, $value = null)
96+
{
97+
$config = config($class);
98+
99+
// Use a fully qualified class name if the
100+
// config file was found.
101+
if ($config !== null)
102+
{
103+
$class = get_class($config);
104+
}
105+
106+
$handler = $this->getWriteHandler();
107+
108+
return $handler->set($class, $key, $value);
109+
}
110+
111+
/**
112+
* Returns the handler that is set to store values.
113+
*
114+
* @return mixed
115+
*/
116+
private function getWriteHandler()
117+
{
118+
if (empty($this->writeHandler) || ! isset($this->handlers[$this->writeHandler]))
119+
{
120+
throw new \RuntimeException('Unable to find a Settings handler that can store values.');
121+
}
122+
123+
return $this->handlers[$this->writeHandler];
124+
}
113125
}

0 commit comments

Comments
 (0)