Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
configuration.php
1<?php
3
5
6final class Configuration implements \ArrayAccess, \Iterator, \Countable
7{
11 private static $instances;
12
13 private $moduleId = null;
14 private $storedData = null;
15 private $data = array();
16 private $isLoaded = false;
17
18 const CONFIGURATION_FILE_PATH = "/bitrix/.settings.php";
19 const CONFIGURATION_FILE_PATH_EXTRA = "/bitrix/.settings_extra.php";
20
21 public static function getValue($name)
22 {
23 $configuration = Configuration::getInstance();
24 return $configuration->get($name);
25 }
26
27 public static function setValue($name, $value)
28 {
29 $configuration = Configuration::getInstance();
30 $configuration->add($name, $value);
31 $configuration->saveConfiguration();
32 }
33
34 private function __construct($moduleId = null)
35 {
36 if($moduleId !== null)
37 {
38 $this->moduleId = preg_replace("/[^a-zA-Z0-9_.]+/i", "", trim($moduleId));
39 }
40 }
41
48 public static function getInstance($moduleId = null)
49 {
50 if (!isset(self::$instances[$moduleId]))
51 {
52 self::$instances[$moduleId] = new self($moduleId);
53 }
54
55 return self::$instances[$moduleId];
56 }
57
58 private static function getPath($path)
59 {
60 $path = Main\Loader::getDocumentRoot().$path;
61 return preg_replace("'[\\\\/]+'", "/", $path);
62 }
63
64 private static function getPathConfigForModule($moduleId)
65 {
66 if (!$moduleId || !Main\ModuleManager::isModuleInstalled($moduleId))
67 {
68 return false;
69 }
70
71 $moduleConfigPath = getLocalPath("modules/{$moduleId}/.settings.php");
72 if ($moduleConfigPath === false)
73 {
74 return false;
75 }
76
77 return self::getPath($moduleConfigPath);
78 }
79
80 private function loadConfiguration()
81 {
82 $this->isLoaded = false;
83
84 if ($this->moduleId)
85 {
86 $path = self::getPathConfigForModule($this->moduleId);
87 if (file_exists($path))
88 {
89 $dataTmp = include($path);
90 if(is_array($dataTmp))
91 {
92 $this->data = $dataTmp;
93 }
94 }
95 }
96 else
97 {
98 $path = self::getPath(self::CONFIGURATION_FILE_PATH);
99 if (file_exists($path))
100 {
101 $dataTmp = include($path);
102 if(is_array($dataTmp))
103 {
104 $this->data = $dataTmp;
105 }
106 }
107
108 $pathExtra = self::getPath(self::CONFIGURATION_FILE_PATH_EXTRA);
109 if (file_exists($pathExtra))
110 {
111 $dataTmp = include($pathExtra);
112 if (is_array($dataTmp) && !empty($dataTmp))
113 {
114 $this->storedData = $this->data;
115 foreach ($dataTmp as $k => $v)
116 {
117 $this->data[$k] = $v;
118 }
119 }
120 }
121 }
122
123 $this->isLoaded = true;
124 }
125
126 public function saveConfiguration()
127 {
128 if (!$this->isLoaded)
129 $this->loadConfiguration();
130
131 if($this->moduleId)
132 {
133 throw new Main\InvalidOperationException('There is no support to rewrite .settings.php in module');
134 }
135 else
136 {
137 $path = self::getPath(self::CONFIGURATION_FILE_PATH);
138 }
139
140 $data = ($this->storedData !== null) ? $this->storedData : $this->data;
141 $data = var_export($data, true);
142
143 if (!is_writable($path))
144 @chmod($path, 0644);
145 file_put_contents($path, "<"."?php\nreturn ".$data.";\n");
146 }
147
148 public function add($name, $value)
149 {
150 if (!$this->isLoaded)
151 $this->loadConfiguration();
152
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);
157 }
158
167 public function addReadonly($name, $value)
168 {
169 if (!$this->isLoaded)
170 $this->loadConfiguration();
171
172 $this->data[$name] = array("value" => $value, "readonly" => true);
173 if ($this->storedData !== null)
174 $this->storedData[$name] = array("value" => $value, "readonly" => true);
175 }
176
177 public function delete($name)
178 {
179 if (!$this->isLoaded)
180 $this->loadConfiguration();
181
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]);
186 }
187
188 public function get($name)
189 {
190 if (!$this->isLoaded)
191 $this->loadConfiguration();
192
193 if (isset($this->data[$name]['value']))
194 {
195 return $this->data[$name]['value'];
196 }
197
198 return null;
199 }
200
201 public function offsetExists($offset): bool
202 {
203 if (!$this->isLoaded)
204 $this->loadConfiguration();
205
206 return isset($this->data[$offset]);
207 }
208
209 #[\ReturnTypeWillChange]
210 public function offsetGet($offset)
211 {
212 return $this->get($offset);
213 }
214
215 public function offsetSet($offset, $value): void
216 {
217 $this->add($offset, $value);
218 }
219
220 public function offsetUnset($offset): void
221 {
222 $this->delete($offset);
223 }
224
225 #[\ReturnTypeWillChange]
226 public function current()
227 {
228 if (!$this->isLoaded)
229 $this->loadConfiguration();
230
231 $c = current($this->data);
232
233 return $c === false ? false : $c["value"];
234 }
235
236 #[\ReturnTypeWillChange]
237 public function next()
238 {
239 if (!$this->isLoaded)
240 $this->loadConfiguration();
241
242 $c = next($this->data);
243
244 return $c === false ? false : $c["value"];
245 }
246
247 #[\ReturnTypeWillChange]
248 public function key()
249 {
250 if (!$this->isLoaded)
251 $this->loadConfiguration();
252
253 return key($this->data);
254 }
255
256 public function valid(): bool
257 {
258 if (!$this->isLoaded)
259 $this->loadConfiguration();
260
261 $key = $this->key();
262 return isset($this->data[$key]);
263 }
264
265 public function rewind(): void
266 {
267 if (!$this->isLoaded)
268 $this->loadConfiguration();
269
270 reset($this->data);
271 }
272
273 public function count(): int
274 {
275 if (!$this->isLoaded)
276 $this->loadConfiguration();
277
278 return count($this->data);
279 }
280
281 public static function wnc()
282 {
284 }
285}
static getInstance($moduleId=null)
static setValue($name, $value)
static isModuleInstalled($moduleName)