|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_files\Processing\Storage\Dirs; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
|
7
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
|
8
|
|
|
use kalanis\kw_files\Interfaces\ITypes; |
|
9
|
|
|
use kalanis\kw_files\Node; |
|
10
|
|
|
use kalanis\kw_paths\ArrayPath; |
|
11
|
|
|
use kalanis\kw_paths\PathsException; |
|
12
|
|
|
use kalanis\kw_storage\Interfaces\IPassDirs; |
|
13
|
|
|
use kalanis\kw_storage\StorageException; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class CanDirRecursive |
|
18
|
|
|
* @package kalanis\kw_files\Processing\Storage\Dirs |
|
19
|
|
|
* Process dirs via predefined api |
|
20
|
|
|
*/ |
|
21
|
|
|
class CanDirRecursive extends ADirs |
|
22
|
|
|
{ |
|
23
|
|
|
protected IPassDirs $storage; |
|
24
|
|
|
|
|
25
|
27 |
|
public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null) |
|
26
|
|
|
{ |
|
27
|
27 |
|
$this->storage = $storage; |
|
28
|
27 |
|
$this->setFlLang($lang); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
8 |
|
public function createDir(array $entry, bool $deep = false): bool |
|
32
|
|
|
{ |
|
33
|
8 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
|
34
|
|
|
try { |
|
35
|
8 |
|
return $this->storage->mkDir($path, $deep); |
|
36
|
1 |
|
} catch (StorageException $ex) { |
|
37
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotCreateDir($path), $ex->getCode(), $ex); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string[] $entry |
|
43
|
|
|
* @param bool $loadRecursive |
|
44
|
|
|
* @param bool $wantSize |
|
45
|
|
|
* @param string[] $previousPaths |
|
46
|
|
|
* @throws FilesException |
|
47
|
|
|
* @throws PathsException |
|
48
|
|
|
* @return Node[] |
|
49
|
|
|
*/ |
|
50
|
6 |
|
public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false, array $previousPaths = []): array |
|
51
|
|
|
{ |
|
52
|
6 |
|
$entryPath = $this->compactName($entry, $this->getStorageSeparator()); |
|
53
|
|
|
try { |
|
54
|
6 |
|
if (!$this->storage->isDir($this->getStorageSeparator() . $entryPath)) { |
|
55
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotReadDir($entryPath)); |
|
56
|
|
|
} |
|
57
|
4 |
|
$files = []; |
|
58
|
4 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
|
59
|
4 |
|
foreach ($this->storage->lookup($this->getStorageSeparator() . $entryPath) as $item) { |
|
60
|
4 |
|
$usePath = mb_substr($item, $sepLen); |
|
61
|
4 |
|
$currentPath = $this->compactName(array_merge($entry, [$usePath]), $this->getStorageSeparator()); |
|
62
|
4 |
|
$sub = new Node(); |
|
63
|
4 |
|
if ('' == $item) { |
|
64
|
4 |
|
if (!empty($previousPaths)) { |
|
65
|
2 |
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
$sub->setData( |
|
69
|
4 |
|
[], |
|
70
|
4 |
|
0, |
|
71
|
4 |
|
ITypes::TYPE_DIR |
|
72
|
4 |
|
); |
|
73
|
4 |
|
} elseif ($this->storage->isDir($this->getStorageSeparator() . $currentPath)) { |
|
74
|
3 |
|
$sub->setData( |
|
75
|
3 |
|
array_merge($previousPaths, $this->expandName($usePath)), |
|
76
|
3 |
|
0, |
|
77
|
3 |
|
ITypes::TYPE_DIR |
|
78
|
3 |
|
); |
|
79
|
|
|
} else { |
|
80
|
|
|
// normal node - file |
|
81
|
4 |
|
$sub->setData( |
|
82
|
4 |
|
array_merge($previousPaths, $this->expandName($usePath)), |
|
83
|
4 |
|
$wantSize ? intval($this->storage->size($this->getStorageSeparator() . $currentPath)) : 0, |
|
84
|
4 |
|
ITypes::TYPE_FILE |
|
85
|
4 |
|
); |
|
86
|
|
|
} |
|
87
|
4 |
|
$files[] = $sub; |
|
88
|
4 |
|
if ($loadRecursive && $sub->isDir() && ('' !== $item)) { |
|
89
|
2 |
|
$files = array_merge($files, $this->readDir(array_merge($entry, $sub->getPath()), $loadRecursive, $wantSize, $sub->getPath())); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
4 |
|
return $files; |
|
93
|
2 |
|
} catch (StorageException $ex) { |
|
94
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
6 |
|
public function copyDir(array $source, array $dest): bool |
|
99
|
|
|
{ |
|
100
|
6 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
|
101
|
6 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
|
102
|
|
|
try { |
|
103
|
6 |
|
if ($this->isSubPart($dest, $source)) { |
|
104
|
1 |
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
if (!$this->isNode($src)) { |
|
108
|
1 |
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
if ($this->storage->exists($dst)) { |
|
112
|
1 |
|
return false; |
|
113
|
|
|
} |
|
114
|
2 |
|
$dstArr = new ArrayPath(); |
|
115
|
2 |
|
$dstArr->setArray($dest); |
|
116
|
2 |
|
if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) { |
|
117
|
1 |
|
return false; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
return $this->storage->copy($src, $dst); |
|
121
|
1 |
|
} catch (StorageException $ex) { |
|
122
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
6 |
|
public function moveDir(array $source, array $dest): bool |
|
127
|
|
|
{ |
|
128
|
6 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
|
129
|
6 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
|
130
|
|
|
try { |
|
131
|
6 |
|
if ($this->isSubPart($dest, $source)) { |
|
132
|
1 |
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
5 |
|
if (!$this->isNode($src)) { |
|
136
|
1 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
3 |
|
if ($this->storage->exists($dst)) { |
|
140
|
1 |
|
return false; |
|
141
|
|
|
} |
|
142
|
2 |
|
$dstArr = new ArrayPath(); |
|
143
|
2 |
|
$dstArr->setArray($dest); |
|
144
|
2 |
|
if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) { |
|
145
|
1 |
|
return false; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
return $this->storage->move($src, $dst); |
|
149
|
1 |
|
} catch (StorageException $ex) { |
|
150
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
9 |
|
public function deleteDir(array $entry, bool $deep = false): bool |
|
155
|
|
|
{ |
|
156
|
9 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
|
157
|
|
|
try { |
|
158
|
9 |
|
if ($this->storage->isDir($path)) { |
|
159
|
7 |
|
return $this->storage->rmDir($path, $deep); |
|
160
|
|
|
} else { |
|
161
|
2 |
|
return false; |
|
162
|
|
|
} |
|
163
|
1 |
|
} catch (StorageException $ex) { |
|
164
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotRemoveDir($path), $ex->getCode(), $ex); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string $entry |
|
170
|
|
|
* @throws StorageException |
|
171
|
|
|
* @return bool |
|
172
|
|
|
*/ |
|
173
|
9 |
|
protected function isNode(string $entry): bool |
|
174
|
|
|
{ |
|
175
|
9 |
|
return $this->storage->isDir($entry); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @return string |
|
180
|
|
|
* @codeCoverageIgnore only when path fails |
|
181
|
|
|
*/ |
|
182
|
|
|
protected function noDirectoryDelimiterSet(): string |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->getFlLang()->flNoDirectoryDelimiterSet(); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|