Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
operation.php
1<?php
3
8
14{
19
27 public function __construct($name, Main\Engine\Controller $controller, array $config = [])
28 {
29 self::$enabledLanguagesList = Translate\Config::getEnabledLanguages();
30 self::$documentRoot = rtrim(Translate\IO\Path::tidy(Main\Application::getDocumentRoot()), '/');
31
32 parent::__construct($name, $controller, $config);
33 }
34
42 protected function updateLangFile(Translate\File $langFile): bool
43 {
44 // backup
45 if ($langFile->isExists() && Translate\Config::needToBackUpFiles())
46 {
47 if (!$langFile->backup())
48 {
49 $this->addError(new Main\Error(
50 Loc::getMessage('TR_CREATE_BACKUP_ERROR', ['#FILE#' => $langFile->getPath()])
51 ));
52 }
53 }
54
55 // sort phrases by key
56 if (Translate\Config::needToSortPhrases())
57 {
58 if (\in_array($langFile->getLangId(), Translate\Config::getNonSortPhraseLanguages()) === false)
59 {
60 $langFile->sortPhrases();
61 }
62 }
63
64 try
65 {
66 if (!$langFile->save())
67 {
68 if ($langFile->hasErrors())
69 {
70 $this->addErrors($langFile->getErrors());
71 }
72
73 return false;
74 }
75 }
76 catch (Main\IO\IoException $exception)
77 {
78 if (!$langFile->isExists())
79 {
80 $this->addError(new Main\Error(
81 Loc::getMessage('TR_ERROR_WRITE_CREATE', ['#FILE#' => $langFile->getPath()])
82 ));
83 }
84 else
85 {
86 $this->addError(new Main\Error(
87 Loc::getMessage('TR_ERROR_WRITE_UPDATE', ['#FILE#' => $langFile->getPath()])
88 ));
89 }
90
91 return false;
92 }
93
94 return true;
95 }
96
104 protected function deleteLangFile(Translate\File $langFile): bool
105 {
106 // backup
107 if ($langFile->isExists() && Translate\Config::needToBackUpFiles())
108 {
109 if (!$langFile->backup())
110 {
111 $this->addError(new Main\Error(
112 Loc::getMessage('TR_CREATE_BACKUP_ERROR', ['#FILE#' => $langFile->getPath()])
113 ));
114 }
115 }
116
117 try
118 {
119 if (!$langFile->delete())
120 {
121 if ($langFile->hasErrors())
122 {
123 $this->addErrors($langFile->getErrors());
124 }
125 else
126 {
127 $this->addError(new Main\Error(
128 Loc::getMessage('TR_ERROR_DELETE', ['#FILE#' => $langFile->getPath()])
129 ));
130 }
131
132 return false;
133 }
134 }
135 catch (Main\IO\IoException $exception)
136 {
137 $this->addError(new Main\Error(
138 Loc::getMessage('TR_ERROR_DELETE', ['#FILE#' => $langFile->getPath()])
139 ));
140
141 return false;
142 }
143
144 return true;
145 }
146
147
155 protected function updatePhraseIndex(Translate\File $langFile): bool
156 {
157 $langFile->updatePhraseIndex();
158
159 return true;
160 }
161
169 protected function deletePhraseIndex(Translate\File $langFile): bool
170 {
171 $langFile->deletePhraseIndex();
172
173 return true;
174 }
175
183 protected function lookThroughLangFolder($langPath): iterable
184 {
185 $files = [];
186 $folders = [];
187
188 foreach (self::$enabledLanguagesList as $langId)
189 {
190 $langFolderRelPath = Translate\IO\Path::replaceLangId($langPath, $langId);
191 $langFolderFullPath = Translate\IO\Path::tidy(self::$documentRoot.'/'.$langFolderRelPath);
192 $langFolderFullPath = Main\Localization\Translation::convertLangPath($langFolderFullPath, $langId);
193
194 $childrenList = Translate\IO\FileSystemHelper::getFileList($langFolderFullPath);
195 if (!empty($childrenList))
196 {
197 foreach ($childrenList as $fullPath)
198 {
199 $name = \basename($fullPath);
200 if (\in_array($name, Translate\IGNORE_FS_NAMES))
201 {
202 continue;
203 }
204
205 if (Translate\IO\Path::isPhpFile($fullPath, true))
206 {
207 $files[$langPath.'/'.$name][$langId] = $fullPath;
208 }
209 }
210 }
211
212 // dir only
213 $childrenList = Translate\IO\FileSystemHelper::getFolderList($langFolderFullPath);
214 if (!empty($childrenList))
215 {
216 $ignoreDev = \implode('|', Translate\IGNORE_MODULE_NAMES);
217 foreach ($childrenList as $fullPath)
218 {
219 $name = \basename($fullPath);
220 if (\in_array($name, Translate\IGNORE_FS_NAMES))
221 {
222 continue;
223 }
224
225 $relPath = $langFolderRelPath.'/'.$name;
226
227 if (!\is_dir($fullPath))
228 {
229 continue;
230 }
231
232 if (\in_array($relPath, Translate\IGNORE_BX_NAMES))
233 {
234 continue;
235 }
236
237 // /bitrix/modules/[smth]/dev/
238 if (\preg_match("#^bitrix/modules/[^/]+/({$ignoreDev})$#", \trim($relPath, '/')))
239 {
240 continue;
241 }
242
243 if (\in_array($name, Translate\IGNORE_LANG_NAMES))
244 {
245 continue;
246 }
247
248 $folders[$langPath.'/'.$name] = $langPath.'/'.$name;
249 }
250 }
251 }
252
253 if (\count($files) > 0)
254 {
255 yield $files;
256 }
257
258 if (\count($folders) > 0)
259 {
260 foreach ($folders as $subFolderPath)
261 {
262 foreach ($this->lookThroughLangFolder($subFolderPath) as $subFiles)// go deeper
263 {
264 yield $subFiles;
265 }
266 }
267 }
268 }
269}
addError(Error $error)
Definition action.php:200
addErrors(array $errors)
Definition action.php:213
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
updatePhraseIndex(Translate\File $langFile)
updateLangFile(Translate\File $langFile)
Definition operation.php:42
deleteLangFile(Translate\File $langFile)
deletePhraseIndex(Translate\File $langFile)
__construct($name, Main\Engine\Controller $controller, array $config=[])
Definition operation.php:27