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 CanDirDirect |
18
|
|
|
* @package kalanis\kw_files\Processing\Storage\Dirs |
19
|
|
|
* Process dirs via predefined api |
20
|
|
|
*/ |
21
|
|
|
class CanDirFlat extends ADirs |
22
|
|
|
{ |
23
|
|
|
protected IPassDirs $storage; |
24
|
|
|
|
25
|
23 |
|
public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null) |
26
|
|
|
{ |
27
|
23 |
|
$this->storage = $storage; |
28
|
23 |
|
$this->setFlLang($lang); |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
public function createDir(array $entry, bool $deep = false): bool |
32
|
|
|
{ |
33
|
4 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
34
|
|
|
try { |
35
|
4 |
|
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
|
|
|
* @throws FilesException |
46
|
|
|
* @throws PathsException |
47
|
|
|
* @return Node[] |
48
|
|
|
*/ |
49
|
6 |
|
public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false): array |
50
|
|
|
{ |
51
|
6 |
|
$entryPath = $this->removeSeparator($this->compactName(array_filter($entry), $this->getStorageSeparator())); |
52
|
6 |
|
$entryPath = empty($entryPath) ? '' : $this->getStorageSeparator() . $entryPath; |
53
|
|
|
try { |
54
|
6 |
|
if (!$this->isNode($entryPath)) { |
55
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotReadDir($entryPath)); |
56
|
|
|
} |
57
|
|
|
/** @var array<string, Node> */ |
58
|
4 |
|
$files = []; |
59
|
4 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
60
|
4 |
|
foreach ($this->storage->lookup($entryPath) as $item) { |
61
|
4 |
|
$usePath = mb_substr($item, $sepLen); |
62
|
4 |
|
if (!$loadRecursive && (false !== mb_strpos($usePath, $this->getStorageSeparator()))) { |
63
|
|
|
// pass sub when not need |
64
|
2 |
|
continue; |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$sub = new Node(); |
68
|
4 |
|
$currentPath = $this->removeSeparator($this->compactName(array_merge( |
69
|
4 |
|
$entry, |
70
|
4 |
|
$this->expandName($item, $this->getStorageSeparator()) |
71
|
4 |
|
), $this->getStorageSeparator())); |
72
|
4 |
|
if (empty($item)) { |
73
|
4 |
|
$sub->setData( |
74
|
4 |
|
[], |
75
|
4 |
|
0, |
76
|
4 |
|
ITypes::TYPE_DIR |
77
|
4 |
|
); |
78
|
4 |
|
} elseif ($this->isNode($this->getStorageSeparator(). $currentPath)) { |
79
|
3 |
|
$sub->setData( |
80
|
3 |
|
$this->expandName($usePath), |
81
|
3 |
|
0, |
82
|
3 |
|
ITypes::TYPE_DIR |
83
|
3 |
|
); |
84
|
|
|
} else { |
85
|
|
|
// normal node - file |
86
|
4 |
|
$sub->setData( |
87
|
4 |
|
$this->expandName($usePath), |
88
|
4 |
|
$wantSize ? $this->getSize($this->getStorageSeparator() . $currentPath) : 0, |
89
|
4 |
|
ITypes::TYPE_FILE |
90
|
4 |
|
); |
91
|
|
|
} |
92
|
4 |
|
$files[] = $sub; |
93
|
|
|
} |
94
|
4 |
|
return $files; |
95
|
2 |
|
} catch (StorageException $ex) { |
96
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
public function copyDir(array $source, array $dest): bool |
101
|
|
|
{ |
102
|
6 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
103
|
6 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
104
|
|
|
try { |
105
|
6 |
|
if ($this->isSubPart($dest, $source)) { |
106
|
1 |
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
if (!$this->isNode($src)) { |
110
|
1 |
|
return false; |
111
|
|
|
} |
112
|
3 |
|
if ($this->storage->exists($dst)) { |
113
|
1 |
|
return false; |
114
|
|
|
} |
115
|
2 |
|
$dstArr = new ArrayPath(); |
116
|
2 |
|
$dstArr->setArray($dest); |
117
|
2 |
|
if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) { |
118
|
1 |
|
return false; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $this->storage->copy($src, $dst); |
122
|
1 |
|
} catch (StorageException $ex) { |
123
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
6 |
|
public function moveDir(array $source, array $dest): bool |
128
|
|
|
{ |
129
|
6 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
130
|
6 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
131
|
|
|
try { |
132
|
6 |
|
if ($this->isSubPart($dest, $source)) { |
133
|
1 |
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
if (!$this->isNode($src)) { |
137
|
1 |
|
return false; |
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
|
5 |
|
public function deleteDir(array $entry, bool $deep = false): bool |
155
|
|
|
{ |
156
|
5 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
157
|
|
|
try { |
158
|
5 |
|
if ($this->storage->isDir($path)) { |
159
|
3 |
|
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
|
6 |
|
protected function removeSeparator(string $path): string |
169
|
|
|
{ |
170
|
6 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
171
|
6 |
|
$first = mb_substr($path, 0, $sepLen); |
172
|
6 |
|
return $this->getStorageSeparator() == $first ? mb_substr($path, $sepLen) : $path; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param string $entry |
177
|
|
|
* @throws StorageException |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
15 |
|
protected function isNode(string $entry): bool |
181
|
|
|
{ |
182
|
15 |
|
return $this->storage->exists($entry) && $this->storage->isDir($entry); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param string $file |
187
|
|
|
* @throws StorageException |
188
|
|
|
* @return int |
189
|
|
|
*/ |
190
|
1 |
|
protected function getSize(string $file): int |
191
|
|
|
{ |
192
|
1 |
|
return strlen(strval($this->storage->read($file))); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
* @codeCoverageIgnore only when path fails |
198
|
|
|
*/ |
199
|
|
|
protected function noDirectoryDelimiterSet(): string |
200
|
|
|
{ |
201
|
|
|
return $this->getFlLang()->flNoDirectoryDelimiterSet(); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|