Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
settings.php
1<?php
2
3namespace Bitrix\Translate;
4
7
8
10 extends Translate\IO\File
11 implements \Iterator, \Countable, \ArrayAccess
12{
13 public const FILE_NAME = '.settings.php';
14
15 public const OPTION_LANGUAGES = 'languages';
16
18 protected $options;
19
21 protected $optionsCount;
22
24 protected $optionCodes = [];
25
27 protected $dataPosition = 0;
28
29
38 public static function instantiateByPath(string $fullPath): ?self
39 {
40 if (empty($fullPath))
41 {
42 throw new Main\ArgumentException();
43 }
44
45 $file = null;
46 if (\mb_substr($fullPath, -5) === '/lang' || \mb_substr($fullPath, -6) === '/lang/')
47 {
48 $file = new static($fullPath. '/'. self::FILE_NAME);
49 }
50 elseif (preg_match("#^(.*?/lang/)([^/]+)/*(.+)#".(Translate\Config::isUtfMode() ? 'u' : ''), $fullPath, $parts))
51 {
52 $file = new static($parts[1]. '/'. self::FILE_NAME);
53 }
54
55 return $file;
56 }
57
58
59 //region Load & Save
60
69 public function getOption(string $langPath, string $optionType): array
70 {
71 $options = $this->getOptions($langPath);
72 if (!empty($options[$optionType]))
73 {
74 return $options[$optionType];
75 }
76
77 return [];
78 }
79
87 public function getOptions(string $langPath = ''): array
88 {
89 // lazy load
90 if ($this->options === null && !$this->load())
91 {
92 return [];
93 }
94
95 if (empty($langPath))
96 {
97 return $this->options;
98 }
99 // for all in lang/
100 if ($langPath === '*' && isset($this->options['*']))
101 {
102 return $this->options['*'];
103 }
104
105 $options = [];
106 if (isset($this->options['*']))
107 {
108 $options = $this->options['*'];
109 }
110
111 if (\preg_match("#^(.*?/lang/)([^/]+)/+(.+)#".(Translate\Config::isUtfMode() ? 'u' : ''), $langPath, $parts))
112 {
113 $langPath = $parts[3];
114 }
115
116 if (isset($this->options[$langPath]))
117 {
118 $options = $this->options[$langPath];
119 }
120 else
121 {
122 if (\mb_strpos($langPath, '/') !== false)
123 {
124 $parts = \explode('/', $langPath);
125 $path = '';
126 foreach ($parts as $part)
127 {
128 $path .= ($path != '' ? '/' : ''). $part;
129 if (isset($this->options[$path]))
130 {
131 $options = $this->options[$path];
132 }
133 }
134 }
135 }
136
137 return $options;
138 }
139
140 //endregion
141
142
143 //region Load & Save
144
150 public function load(): bool
151 {
152 if (!$this->isExists() || !$this->isFile() || $this->getName() !== self::FILE_NAME)
153 {
154 return false;
155 }
156
157 $this->options = [];
158 $this->optionCodes = [];
159 $this->optionsCount = 0;
160
161 $options = include $this->getPhysicalPath();
162
163 if (\is_array($options) && \count($options) > 0)
164 {
165 $this->options = $options;
166 $this->optionCodes = \array_keys($options);
167 $this->optionsCount = \count($options);
168 }
169
170 return true;
171 }
172
173
180 public function save(): bool
181 {
182 $content = '';
183 if ($this->count() > 0)
184 {
185 $content = \var_export($this->options, true);
186 $content = \preg_replace("/^[ ]{6}(.*)/m", "\t\t\t$1", $content);
187 $content = \preg_replace("/^[ ]{4}(.*)/m", "\t\t$1", $content);
188 $content = \preg_replace("/^[ ]{2}(.*)/m", "\t$1", $content);
189 $content = \str_replace(["\r\n", "\r"], ["\n", ''], $content);
190 }
191
192 \set_error_handler(
193 function ($severity, $message, $file, $line)
194 {
195 throw new \ErrorException($message, $severity, $severity, $file, $line);
196 }
197 );
198
199 try
200 {
201 if ($content <> '')
202 {
203 if (parent::putContents("<". "?php\nreturn ". $content. "\n?". '>') === false)
204 {
205 $filePath = $this->getPath();
206 throw new Main\IO\IoException("Couldn't write option file '{$filePath}'");
207 }
208 }
209 elseif ($this->isExists())
210 {
211 $this->markWritable();
212 $this->delete();
213 }
214 }
215 catch (\ErrorException $exception)
216 {
217 \restore_error_handler();
218 throw new Main\IO\IoException($exception->getMessage());
219 }
220
221 \restore_error_handler();
222
223 return true;
224 }
225
226 //endregion
227
228 //region ArrayAccess
229
235 public function offsetExists($code): bool
236 {
237 return isset($this->options[$code]);
238 }
239
245 #[\ReturnTypeWillChange]
246 public function offsetGet($code)
247 {
248 if (isset($this->options[$code]))
249 {
250 return $this->options[$code];
251 }
252
253 return null;
254 }
255
264 public function offsetSet($code, $phrase): void
265 {
266 $this->options[$code] = $phrase;
267 }
268
276 public function offsetUnset($code): void
277 {
278 unset($this->options[$code]);
279 }
280
281 //endregion
282
283 //region Iterator
284
290 #[\ReturnTypeWillChange]
291 public function current()
292 {
293 $code = $this->optionCodes[$this->dataPosition];
294
295 return $this->options[$code] ?: null;
296 }
297
303 public function next(): void
304 {
306 }
307
313 public function key(): ?string
314 {
315 return $this->optionCodes[$this->dataPosition] ?: null;
316 }
317
323 public function valid(): bool
324 {
325 $code = $this->optionCodes[$this->dataPosition];
326 return isset($this->options[$code]);
327 }
328
334 public function rewind(): void
335 {
336 $this->dataPosition = 0;
337 $this->optionCodes = \array_keys($this->options);
338 }
339
340 //endregion
341
342 //region Countable
343
351 public function count($allowDirectFileAccess = false): int
352 {
353 if ($this->optionsCount === null)
354 {
355 if ($this->options !== null && \count($this->options) > 0)
356 {
357 $this->optionsCount = \count($this->options);
358 }
359 elseif ($allowDirectFileAccess)
360 {
361 $options = include $this->getPhysicalPath();
362
363 if (\is_array($options) && \count($options) > 0)
364 {
365 $this->optionsCount = \count($options);
366 }
367 }
368 }
369
370 return $this->optionsCount ?: 0;
371 }
372
373 //endregion
374}
isFile()
Definition fileentry.php:38
$path
getPath()
getName()
getPhysicalPath()
count($allowDirectFileAccess=false)
Definition settings.php:351
getOptions(string $langPath='')
Definition settings.php:87
getOption(string $langPath, string $optionType)
Definition settings.php:69
static instantiateByPath(string $fullPath)
Definition settings.php:38
offsetSet($code, $phrase)
Definition settings.php:264