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
|
|
|
/** @var IPassDirs */ |
24
|
|
|
protected $storage = null; |
25
|
|
|
|
26
|
27 |
|
public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null) |
27
|
|
|
{ |
28
|
27 |
|
$this->storage = $storage; |
29
|
27 |
|
$this->setLang($lang); |
30
|
27 |
|
} |
31
|
|
|
|
32
|
8 |
|
public function createDir(array $entry, bool $deep = false): bool |
33
|
|
|
{ |
34
|
8 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
35
|
|
|
try { |
36
|
8 |
|
return $this->storage->mkDir($path, $deep); |
37
|
1 |
|
} catch (StorageException $ex) { |
38
|
1 |
|
throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string[] $entry |
44
|
|
|
* @param bool $loadRecursive |
45
|
|
|
* @param bool $wantSize |
46
|
|
|
* @param string[] $previousPaths |
47
|
|
|
* @throws FilesException |
48
|
|
|
* @throws PathsException |
49
|
|
|
* @return Node[] |
50
|
|
|
*/ |
51
|
6 |
|
public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false, array $previousPaths = []): array |
52
|
|
|
{ |
53
|
6 |
|
$entryPath = $this->compactName($entry, $this->getStorageSeparator()); |
54
|
|
|
try { |
55
|
6 |
|
if (!$this->storage->isDir($this->getStorageSeparator() . $entryPath)) { |
56
|
1 |
|
throw new FilesException($this->getLang()->flCannotReadDir($entryPath)); |
57
|
|
|
} |
58
|
4 |
|
$files = []; |
59
|
4 |
|
$sepLen = mb_strlen($this->getStorageSeparator()); |
60
|
4 |
|
foreach ($this->storage->lookup($this->getStorageSeparator() . $entryPath) as $item) { |
61
|
4 |
|
$usePath = mb_substr($item, $sepLen); |
62
|
4 |
|
$currentPath = $this->compactName(array_merge($entry, [$usePath]), $this->getStorageSeparator()); |
63
|
4 |
|
$sub = new Node(); |
64
|
4 |
|
if ('' == $item) { |
65
|
4 |
|
if (!empty($previousPaths)) { |
66
|
2 |
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$sub->setData( |
70
|
4 |
|
[], |
71
|
4 |
|
0, |
72
|
4 |
|
ITypes::TYPE_DIR |
73
|
|
|
); |
74
|
4 |
|
} elseif ($this->storage->isDir($this->getStorageSeparator() . $currentPath)) { |
75
|
3 |
|
$sub->setData( |
76
|
3 |
|
array_merge($previousPaths, $this->expandName($usePath)), |
77
|
3 |
|
0, |
78
|
3 |
|
ITypes::TYPE_DIR |
79
|
|
|
); |
80
|
|
|
} else { |
81
|
|
|
// normal node - file |
82
|
4 |
|
$sub->setData( |
83
|
4 |
|
array_merge($previousPaths, $this->expandName($usePath)), |
84
|
4 |
|
$wantSize ? intval($this->storage->size($this->getStorageSeparator() . $currentPath)) : 0, |
85
|
4 |
|
ITypes::TYPE_FILE |
86
|
|
|
); |
87
|
|
|
} |
88
|
4 |
|
$files[] = $sub; |
89
|
4 |
|
if ($loadRecursive && $sub->isDir() && ('' !== $item)) { |
90
|
2 |
|
$files = array_merge($files, $this->readDir(array_merge($entry, $sub->getPath()), $loadRecursive, $wantSize, $sub->getPath())); |
91
|
|
|
} |
92
|
|
|
} |
93
|
4 |
|
return $files; |
94
|
2 |
|
} catch (StorageException $ex) { |
95
|
1 |
|
throw new FilesException($this->getLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
6 |
|
public function copyDir(array $source, array $dest): bool |
100
|
|
|
{ |
101
|
6 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
102
|
6 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
103
|
|
|
try { |
104
|
6 |
|
if ($this->isSubPart($dest, $source)) { |
105
|
1 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
if (!$this->isNode($src)) { |
109
|
1 |
|
return false; |
110
|
|
|
} |
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->getLang()->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
|
|
|
|
140
|
3 |
|
if ($this->storage->exists($dst)) { |
141
|
1 |
|
return false; |
142
|
|
|
} |
143
|
2 |
|
$dstArr = new ArrayPath(); |
144
|
2 |
|
$dstArr->setArray($dest); |
145
|
2 |
|
if (!$this->storage->exists($this->getStorageSeparator() . $this->compactName($dstArr->getArrayDirectory(), $this->getStorageSeparator()))) { |
146
|
1 |
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return $this->storage->move($src, $dst); |
150
|
1 |
|
} catch (StorageException $ex) { |
151
|
1 |
|
throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
9 |
|
public function deleteDir(array $entry, bool $deep = false): bool |
156
|
|
|
{ |
157
|
9 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
158
|
|
|
try { |
159
|
9 |
|
if ($this->storage->isDir($path)) { |
160
|
7 |
|
return $this->storage->rmDir($path, $deep); |
161
|
|
|
} else { |
162
|
2 |
|
return false; |
163
|
|
|
} |
164
|
1 |
|
} catch (StorageException $ex) { |
165
|
1 |
|
throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string $entry |
171
|
|
|
* @throws StorageException |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
9 |
|
protected function isNode(string $entry): bool |
175
|
|
|
{ |
176
|
9 |
|
return $this->storage->isDir($entry); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return string |
181
|
|
|
* @codeCoverageIgnore only when path fails |
182
|
|
|
*/ |
183
|
|
|
protected function noDirectoryDelimiterSet(): string |
184
|
|
|
{ |
185
|
|
|
return $this->getLang()->flNoDirectoryDelimiterSet(); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|