8final class Configuration implements \ArrayAccess, \Iterator, \Countable
18 private static array $instances = [];
19 private ?
string $moduleId =
null;
20 private ?
array $storedData =
null;
21 private array $data = [];
22 private bool $isLoaded =
false;
27 return $configuration->get(
$name);
33 $configuration->add(
$name, $value);
34 $configuration->saveConfiguration();
37 private function __construct(
$moduleId =
null)
41 $this->moduleId = preg_replace(
"/[^a-zA-Z0-9_.]+/i",
"", trim(
$moduleId));
47 if (!isset(self::$instances[$moduleId]))
49 self::$instances[$moduleId] =
new self($moduleId);
52 return self::$instances[$moduleId];
55 private static function getPathConfigForModule(
$moduleId): ?string
63 if ($moduleConfigPath ===
false)
68 return $moduleConfigPath;
71 private function loadConfiguration(): void
73 $this->isLoaded =
false;
77 $path = self::getPathConfigForModule($this->moduleId);
80 $dataTmp = include
$path;
81 if (is_array($dataTmp))
83 $this->data = $dataTmp;
91 $dataTmp = include
$path;
92 if (is_array($dataTmp))
94 $this->data = $dataTmp;
98 if (($pathExtra =
Loader::getLocal(self::CONFIGURATION_FILE_EXTRA)) !==
false)
100 $dataTmp = include $pathExtra;
101 if (is_array($dataTmp) && !empty($dataTmp))
104 foreach ($dataTmp as
$k => $v)
106 $this->data[
$k] = $v;
112 $this->isLoaded =
true;
117 if (!$this->isLoaded)
119 $this->loadConfiguration();
131 $data = ($this->storedData !==
null) ? $this->storedData :
$this->data;
132 $data = var_export($data,
true);
134 if (!is_writable(
$path))
138 file_put_contents(
$path,
"<" .
"?php\nreturn " . $data .
";\n");
143 if (!$this->isLoaded)
145 $this->loadConfiguration();
148 if (!isset($this->data[
$name]) || !$this->data[
$name][
"readonly"])
150 $this->data[
$name] = [
"value" => $value,
"readonly" =>
false];
152 if (($this->storedData !==
null) && (!isset($this->storedData[
$name]) || !$this->storedData[
$name][
"readonly"]))
154 $this->storedData[
$name] = [
"value" => $value,
"readonly" =>
false];
168 if (!$this->isLoaded)
170 $this->loadConfiguration();
173 $this->data[
$name] = [
"value" => $value,
"readonly" =>
true];
174 if ($this->storedData !==
null)
176 $this->storedData[
$name] = [
"value" => $value,
"readonly" =>
true];
182 if (!$this->isLoaded)
184 $this->loadConfiguration();
187 if (isset($this->data[
$name]) && !$this->data[
$name][
"readonly"])
189 unset($this->data[
$name]);
191 if (($this->storedData !==
null) && isset($this->storedData[
$name]) && !$this->storedData[
$name][
"readonly"])
193 unset($this->storedData[
$name]);
199 if (!$this->isLoaded)
201 $this->loadConfiguration();
204 if (isset($this->data[
$name][
'value']))
206 return $this->data[
$name][
'value'];
214 if (!$this->isLoaded)
216 $this->loadConfiguration();
219 return isset($this->data[$offset]);
224 return $this->
get($offset);
229 $this->
add($offset, $value);
234 $this->
delete($offset);
239 if (!$this->isLoaded)
241 $this->loadConfiguration();
246 return $c ===
false ?
false : $c[
"value"];
251 if (!$this->isLoaded)
253 $this->loadConfiguration();
259 public function key(): mixed
261 if (!$this->isLoaded)
263 $this->loadConfiguration();
266 return key($this->data);
271 if (!$this->isLoaded)
273 $this->loadConfiguration();
277 return isset($this->data[
$key]);
282 if (!$this->isLoaded)
284 $this->loadConfiguration();
292 if (!$this->isLoaded)
294 $this->loadConfiguration();
297 return count($this->data);
300 public static function wnc(): void
static getInstance($moduleId=null)
static setValue($name, $value)
const CONFIGURATION_FILE_PATH
const CONFIGURATION_FILE_EXTRA
addReadonly($name, $value)
offsetSet($offset, $value)
static getLocal($path, $root=null)
static isModuleInstalled($moduleName)