1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Processing\Volume; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use FilesystemIterator; |
7
|
|
|
use kalanis\kw_files\FilesException; |
8
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
9
|
|
|
use kalanis\kw_files\Interfaces\IProcessDirs; |
10
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
11
|
|
|
use kalanis\kw_files\Node; |
12
|
|
|
use kalanis\kw_files\Processing\TPath; |
13
|
|
|
use kalanis\kw_files\Traits\TLang; |
14
|
|
|
use kalanis\kw_files\Traits\TSubPart; |
15
|
|
|
use kalanis\kw_paths\ArrayPath; |
16
|
|
|
use kalanis\kw_paths\Extras\TPathTransform; |
17
|
|
|
use kalanis\kw_paths\PathsException; |
18
|
|
|
use kalanis\kw_storage\Extras\TRemoveCycle; |
19
|
|
|
use kalanis\kw_storage\Extras\TVolumeCopy; |
20
|
|
|
use RecursiveDirectoryIterator; |
21
|
|
|
use RecursiveIteratorIterator; |
22
|
|
|
use SplFileInfo; |
23
|
|
|
use Throwable; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class ProcessDir |
28
|
|
|
* @package kalanis\kw_files\Processing\Volume |
29
|
|
|
* Process dirs in basic ways |
30
|
|
|
*/ |
31
|
|
|
class ProcessDir implements IProcessDirs |
32
|
|
|
{ |
33
|
|
|
use TLang; |
34
|
|
|
use TPath; |
35
|
|
|
use TPathTransform; |
36
|
|
|
use TRemoveCycle; |
37
|
|
|
use TSubPart; |
38
|
|
|
use TVolumeCopy; |
39
|
|
|
|
40
|
|
|
/** @var int */ |
41
|
|
|
protected $sliceStartParts = 0; |
42
|
|
|
/** @var int */ |
43
|
|
|
protected $sliceCustomParts = 0; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $path |
47
|
|
|
* @param IFLTranslations|null $lang |
48
|
|
|
* @throws PathsException |
49
|
|
|
*/ |
50
|
23 |
|
public function __construct(string $path = '', ?IFLTranslations $lang = null) |
51
|
|
|
{ |
52
|
23 |
|
$this->setLang($lang); |
53
|
23 |
|
$this->setPath($path); |
54
|
23 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $path |
58
|
|
|
* @throws PathsException |
59
|
|
|
*/ |
60
|
23 |
|
public function setPath(string $path = ''): void |
61
|
|
|
{ |
62
|
23 |
|
$used = realpath($path); |
63
|
23 |
|
if (false !== $used) { |
64
|
22 |
|
$this->path = $used; |
65
|
22 |
|
$this->sliceStartParts = count($this->expandName($used)); |
66
|
|
|
} |
67
|
23 |
|
} |
68
|
|
|
|
69
|
4 |
|
public function createDir(array $entry, bool $deep = false): bool |
70
|
|
|
{ |
71
|
4 |
|
$path = $this->fullPath($entry); |
72
|
|
|
try { |
73
|
4 |
|
return @mkdir($path, 0777, $deep); |
74
|
|
|
// @codeCoverageIgnoreStart |
75
|
|
|
} catch (Throwable $ex) { |
76
|
|
|
throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex); |
77
|
|
|
} |
78
|
|
|
// @codeCoverageIgnoreEnd |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false): array |
82
|
|
|
{ |
83
|
4 |
|
$path = $this->fullPath($entry); |
84
|
4 |
|
$this->sliceCustomParts = count($entry); |
85
|
4 |
|
if (!is_dir($path)) { |
86
|
1 |
|
throw new FilesException($this->getLang()->flCannotReadDir($path)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
try { |
90
|
3 |
|
$iter = $loadRecursive |
91
|
1 |
|
? new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)) |
92
|
3 |
|
: new FilesystemIterator($path, 0) |
93
|
|
|
; |
94
|
3 |
|
return array_values( |
95
|
3 |
|
array_map( |
96
|
3 |
|
[$this, 'intoNode'], |
97
|
3 |
|
array_filter( |
98
|
3 |
|
array_filter( |
99
|
3 |
|
array_filter( |
100
|
3 |
|
($loadRecursive ? [] : [$this->addRootNode($path)]) + iterator_to_array($iter) |
101
|
|
|
), |
102
|
3 |
|
[$this, 'filterFilesAndDirsOnly'] |
103
|
|
|
), |
104
|
3 |
|
[$this, 'filterDots'] |
105
|
|
|
) |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
// @codeCoverageIgnoreStart |
109
|
|
|
} catch (Throwable $ex) { |
110
|
|
|
throw new FilesException($this->getLang()->flCannotReadDir($path), $ex->getCode(), $ex); |
111
|
|
|
} |
112
|
|
|
// @codeCoverageIgnoreEnd |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
protected function addRootNode(string $path): SplFileInfo |
116
|
|
|
{ |
117
|
2 |
|
return new SplFileInfo($path); |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
public function filterFilesAndDirsOnly(SplFileInfo $file): bool |
121
|
|
|
{ |
122
|
3 |
|
return in_array($file->getType(), [ITypes::TYPE_DIR, ITypes::TYPE_FILE]); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
public function filterDots(SplFileInfo $file): bool |
126
|
|
|
{ |
127
|
3 |
|
return '..' !== $file->getFilename(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param SplFileInfo $file |
132
|
|
|
* @throws PathsException |
133
|
|
|
* @return Node |
134
|
|
|
*/ |
135
|
3 |
|
public function intoNode(SplFileInfo $file): Node |
136
|
|
|
{ |
137
|
3 |
|
$node = new Node(); |
138
|
3 |
|
return $node->setData( |
139
|
3 |
|
array_slice($this->expandName($file->getRealPath()), $this->sliceStartParts + $this->sliceCustomParts), |
140
|
3 |
|
$file->getSize(), |
141
|
3 |
|
$file->getType() |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
5 |
|
public function copyDir(array $source, array $dest): bool |
146
|
|
|
{ |
147
|
5 |
|
$src = $this->fullPath($source); |
148
|
5 |
|
$dst = $this->fullPath($dest); |
149
|
5 |
|
if ($this->isSubPart($dest, $source)) { |
150
|
1 |
|
return false; |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
if (!file_exists($src)) { |
154
|
1 |
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
$dstArr = new ArrayPath(); |
158
|
3 |
|
$dstArr->setArray($dest); |
159
|
|
|
|
160
|
3 |
|
if (!file_exists($this->fullPath($dstArr->getArrayDirectory()))) { |
161
|
1 |
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
2 |
|
if (file_exists($dst)) { |
165
|
1 |
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
try { |
169
|
1 |
|
return $this->xcopy($src, $dst); |
170
|
|
|
// @codeCoverageIgnoreStart |
171
|
|
|
} catch (Throwable $ex) { |
172
|
|
|
throw new FilesException($this->getLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex); |
173
|
|
|
} |
174
|
|
|
// @codeCoverageIgnoreEnd |
175
|
|
|
} |
176
|
|
|
|
177
|
5 |
|
public function moveDir(array $source, array $dest): bool |
178
|
|
|
{ |
179
|
5 |
|
$src = $this->fullPath($source); |
180
|
5 |
|
$dst = $this->fullPath($dest); |
181
|
5 |
|
if ($this->isSubPart($dest, $source)) { |
182
|
1 |
|
return false; |
183
|
|
|
} |
184
|
|
|
|
185
|
4 |
|
if (!file_exists($src)) { |
186
|
1 |
|
return false; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
$dstArr = new ArrayPath(); |
190
|
3 |
|
$dstArr->setArray($dest); |
191
|
|
|
|
192
|
3 |
|
if (!file_exists($this->fullPath($dstArr->getArrayDirectory()))) { |
193
|
1 |
|
return false; |
194
|
|
|
} |
195
|
|
|
|
196
|
2 |
|
if (file_exists($dst)) { |
197
|
1 |
|
return false; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
try { |
201
|
|
|
// original call with doomed sub-calls chmod and chown - see |
202
|
|
|
// @link https://www.php.net/manual/en/function.rename.php#117590 |
203
|
|
|
// return @rename($src, $dst); |
204
|
|
|
|
205
|
|
|
// to avoid that internal bug... |
206
|
1 |
|
$v1 = $this->copyDir($source, $dest); |
207
|
1 |
|
$v2 = $this->deleteDir($source, true); |
208
|
1 |
|
return $v1 && $v2; |
209
|
|
|
// @codeCoverageIgnoreStart |
210
|
|
|
} catch (Throwable $ex) { |
211
|
|
|
throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex); |
212
|
|
|
} |
213
|
|
|
// @codeCoverageIgnoreEnd |
214
|
|
|
} |
215
|
|
|
|
216
|
6 |
|
public function deleteDir(array $entry, bool $deep = false): bool |
217
|
|
|
{ |
218
|
6 |
|
$path = $this->fullPath($entry); |
219
|
|
|
try { |
220
|
6 |
|
if ($deep) { |
221
|
3 |
|
return $this->removeCycle($path); |
222
|
|
|
} else { |
223
|
6 |
|
return @rmdir($path); |
224
|
|
|
} |
225
|
|
|
// @codeCoverageIgnoreStart |
226
|
|
|
} catch (Throwable $ex) { |
227
|
|
|
throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex); |
228
|
|
|
} |
229
|
|
|
// @codeCoverageIgnoreEnd |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param array<string> $path |
234
|
|
|
* @throws PathsException |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
19 |
|
protected function fullPath(array $path): string |
238
|
|
|
{ |
239
|
19 |
|
return $this->getPath() . DIRECTORY_SEPARATOR . $this->compactName($path); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
* @codeCoverageIgnore only when path fails |
245
|
|
|
*/ |
246
|
|
|
protected function noDirectoryDelimiterSet(): string |
247
|
|
|
{ |
248
|
|
|
return $this->getLang()->flNoDirectoryDelimiterSet(); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|