6final class Configuration implements \ArrayAccess, \Iterator, \Countable
11 private static $instances;
13 private $moduleId =
null;
14 private $storedData =
null;
15 private $data = array();
16 private $isLoaded =
false;
24 return $configuration->get($name);
30 $configuration->add($name, $value);
31 $configuration->saveConfiguration();
34 private function __construct($moduleId =
null)
36 if($moduleId !==
null)
38 $this->moduleId = preg_replace(
"/[^a-zA-Z0-9_.]+/i",
"", trim($moduleId));
50 if (!isset(self::$instances[$moduleId]))
52 self::$instances[$moduleId] =
new self($moduleId);
55 return self::$instances[$moduleId];
58 private static function getPath($path)
60 $path = Main\Loader::getDocumentRoot().$path;
61 return preg_replace(
"'[\\\\/]+'",
"/", $path);
64 private static function getPathConfigForModule($moduleId)
71 $moduleConfigPath = getLocalPath(
"modules/{$moduleId}/.settings.php");
72 if ($moduleConfigPath ===
false)
77 return self::getPath($moduleConfigPath);
80 private function loadConfiguration()
82 $this->isLoaded =
false;
86 $path = self::getPathConfigForModule($this->moduleId);
87 if (file_exists($path))
89 $dataTmp = include($path);
90 if(is_array($dataTmp))
92 $this->data = $dataTmp;
98 $path = self::getPath(self::CONFIGURATION_FILE_PATH);
99 if (file_exists($path))
101 $dataTmp = include($path);
102 if(is_array($dataTmp))
104 $this->data = $dataTmp;
108 $pathExtra = self::getPath(self::CONFIGURATION_FILE_PATH_EXTRA);
109 if (file_exists($pathExtra))
111 $dataTmp = include($pathExtra);
112 if (is_array($dataTmp) && !empty($dataTmp))
114 $this->storedData = $this->data;
115 foreach ($dataTmp as $k => $v)
117 $this->data[$k] = $v;
123 $this->isLoaded =
true;
128 if (!$this->isLoaded)
129 $this->loadConfiguration();
137 $path = self::getPath(self::CONFIGURATION_FILE_PATH);
140 $data = ($this->storedData !==
null) ? $this->storedData : $this->data;
141 $data = var_export($data,
true);
143 if (!is_writable($path))
145 file_put_contents($path,
"<".
"?php\nreturn ".$data.
";\n");
148 public function add($name, $value)
150 if (!$this->isLoaded)
151 $this->loadConfiguration();
153 if (!isset($this->data[$name]) || !$this->data[$name][
"readonly"])
154 $this->data[$name] = array(
"value" => $value,
"readonly" =>
false);
155 if (($this->storedData !==
null) && (!isset($this->storedData[$name]) || !$this->storedData[$name][
"readonly"]))
156 $this->storedData[$name] = array(
"value" => $value,
"readonly" =>
false);
169 if (!$this->isLoaded)
170 $this->loadConfiguration();
172 $this->data[$name] = array(
"value" => $value,
"readonly" =>
true);
173 if ($this->storedData !==
null)
174 $this->storedData[$name] = array(
"value" => $value,
"readonly" =>
true);
177 public function delete($name)
179 if (!$this->isLoaded)
180 $this->loadConfiguration();
182 if (isset($this->data[$name]) && !$this->data[$name][
"readonly"])
183 unset($this->data[$name]);
184 if (($this->storedData !==
null) && isset($this->storedData[$name]) && !$this->storedData[$name][
"readonly"])
185 unset($this->storedData[$name]);
188 public function get($name)
190 if (!$this->isLoaded)
191 $this->loadConfiguration();
193 if (isset($this->data[$name][
'value']))
195 return $this->data[$name][
'value'];
203 if (!$this->isLoaded)
204 $this->loadConfiguration();
206 return isset($this->data[$offset]);
209 #[\ReturnTypeWillChange]
212 return $this->
get($offset);
217 $this->
add($offset, $value);
222 $this->
delete($offset);
225 #[\ReturnTypeWillChange]
228 if (!$this->isLoaded)
229 $this->loadConfiguration();
233 return $c ===
false ? false : $c[
"value"];
236 #[\ReturnTypeWillChange]
239 if (!$this->isLoaded)
240 $this->loadConfiguration();
242 $c =
next($this->data);
244 return $c ===
false ? false : $c[
"value"];
247 #[\ReturnTypeWillChange]
250 if (!$this->isLoaded)
251 $this->loadConfiguration();
253 return key($this->data);
258 if (!$this->isLoaded)
259 $this->loadConfiguration();
262 return isset($this->data[$key]);
267 if (!$this->isLoaded)
268 $this->loadConfiguration();
275 if (!$this->isLoaded)
276 $this->loadConfiguration();
278 return count($this->data);
281 public static function wnc()
static getInstance($moduleId=null)
static setValue($name, $value)
const CONFIGURATION_FILE_PATH
addReadonly($name, $value)
const CONFIGURATION_FILE_PATH_EXTRA
offsetSet($offset, $value)
static isModuleInstalled($moduleName)