Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
directory.php
1<?php
2
3namespace Bitrix\Translate\IO;
4
7
8
10 extends Main\IO\Directory
11 implements Translate\IErrorable
12{
13 // trait implements interface Translate\IErrorable
15
21 public function __construct(string $path, ?string $siteId = null)
22 {
23 parent::__construct($path, $siteId);
24 }
25
34 public static function generateTemporalDirectory(string $prefix, int $timeToLive = 3): self
35 {
36 $tempDirPath = \CTempFile::getDirectoryName($timeToLive, array($prefix, \uniqid($prefix, true)));
37 $tempDir = new static($tempDirPath);
38 if (!$tempDir->isExists())
39 {
40 $tempDir->create();
41 }
42
43 return $tempDir;
44 }
45
46
59 public function copy(
60 Main\IO\Directory $target,
61 bool $reWrite = true,
62 bool $recursive = false,
63 bool $convertEncoding = false,
64 string $sourceEncoding = '',
65 string $targetEncoding = ''
66 ): bool
67 {
68 if (\mb_strpos($target->getPhysicalPath(), $this->getPhysicalPath()) === 0)
69 {
70 $this->addError(new Main\Error('Destination is inside in the source folder.'));
71
72 return false;
73 }
74 if (!$this->isExists())
75 {
76 $this->addError(new Main\Error('Source is not exists.'));
77
78 return false;
79 }
80 if (!$target->isExists())
81 {
82 $target->create();
83 }
84
85 $retFlag = true;
86
87 $children = $this->getChildren();
88
90 foreach ($children as $entry)
91 {
92 if (in_array($entry->getName(), Translate\IGNORE_FS_NAMES, true))
93 {
94 continue;
95 }
96
97 if (
98 ($entry instanceof Main\IO\Directory) &&
99 $entry->isDirectory() &&
100 $recursive
101 )
102 {
103 $source = new self($entry->getPhysicalPath());
104 $res = $source->copy(
105 (new Main\IO\Directory($target->getPhysicalPath(). '/'. $entry->getName())),
106 $reWrite,
107 $recursive,
108 $convertEncoding,
109 $sourceEncoding,
110 $targetEncoding
111 );
112 if (!$res)
113 {
114 $retFlag = false;
115 $this->addErrors($source->getErrors());
116 }
117
118 }
119 elseif (
120 ($entry instanceof Main\IO\File) &&
121 $entry->isFile()
122 )
123 {
124 $file = new Main\IO\File($target->getPhysicalPath(). '/'. $entry->getName());
125 if ($file->isExists() && !$reWrite)
126 {
127 continue;
128 }
129
130 try
131 {
132 $content = $entry->getContents();
133 $content = \str_replace(array("\r\n", "\r"), array("\n", "\n"), $content);
134
135 if ($convertEncoding)
136 {
137 $content = \Bitrix\Main\Text\Encoding::convertEncoding($content, $sourceEncoding, $targetEncoding);
138 }
139
140 $file->putContents($content);
141 }
142 catch (Main\IO\IoException $exception)
143 {
144 $retFlag = false;
145 $this->addError(new Main\Error($exception->getMessage()));
146 }
147 }
148 }
149
150 return $retFlag;
151 }
152
153
165 public function copyLangOnly(
166 Main\IO\Directory $target,
167 string $languageId,
168 bool $convertEncoding = false,
169 string $sourceEncoding = '',
170 string $targetEncoding = ''
171 ): bool
172 {
173 if (mb_strpos($target->getPhysicalPath(), $this->getPhysicalPath()) === 0)
174 {
175 $this->addError(new Main\Error('Destination is inside in the source folder.'));
176
177 return false;
178 }
179 if (!$this->isExists())
180 {
181 $this->addError(new Main\Error('Source is not exists.'));
182
183 return false;
184 }
185
186 $children = $this->getChildren();
187
188 $retFlag = true;
189
191 foreach ($children as $dir)
192 {
193 $dirName = $dir->getName();
194 if (
195 !$dir instanceof Main\IO\Directory ||
196 !$dir->isDirectory() ||
197 \in_array($dirName, Translate\IGNORE_FS_NAMES, true)
198 )
199 {
200 continue;
201 }
202
203 if ($dirName === 'lang' || $dirName === 'payment')
204 {
205 $source = new self($dir->getPhysicalPath(). '/'. $languageId);
206 if ($source->isExists())
207 {
208 if (!$target->isExists())
209 {
210 $target->create();
211 }
212 $targetDir = $target->createSubdirectory($dirName)->createSubdirectory($languageId);
213
214 $res = $source->copy(
215 $targetDir,
216 true,
217 true,
218 $convertEncoding,
219 $sourceEncoding,
220 $targetEncoding
221 );
222 if (!$res)
223 {
224 $retFlag = false;
225 $this->addErrors($source->getErrors());
226 }
227 }
228 }
229 else
230 {
231 $source = new self($dir->getPhysicalPath());
232 $res = $source->copyLangOnly(
233 (new Main\IO\Directory($target->getPhysicalPath(). '/'. $dirName)),
234 $languageId,
235 $convertEncoding,
236 $sourceEncoding,
237 $targetEncoding
238 );
239 if (!$res)
240 {
241 $retFlag = false;
242 $this->addErrors($source->getErrors());
243 }
244 }
245 }
246
247 return $retFlag;
248 }
249
258 public function wipe(?\Closure $filter = null): bool
259 {
260 if (!$this->isExists())
261 {
262 throw new Main\IO\FileNotFoundException($this->originalPath);
263 }
264
265 if($this->getPath() === '/')
266 {
267 throw new Main\IO\InvalidPathException($this->originalPath);
268 }
269
270 $children = $this->getChildren();
271 $result = true;
272 foreach ($children as $entry)
273 {
274 if ($filter instanceof \Closure)
275 {
276 if ($filter($entry) !== true)
277 {
278 continue;
279 }
280 }
281 $result = $entry->delete();
282
283 if (!$result)
284 {
285 break;
286 }
287 }
288 if ($result)
289 {
290 \clearstatcache();
291 }
292
293 return $result;
294 }
295}
$path
getPath()
$siteId
static generateTemporalDirectory(string $prefix, int $timeToLive=3)
Definition directory.php:34
wipe(?\Closure $filter=null)
__construct(string $path, ?string $siteId=null)
Definition directory.php:21
addError(Main\Error $error)