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_storage\Interfaces\IPassDirs; |
11
|
|
|
use kalanis\kw_storage\StorageException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CanDir |
16
|
|
|
* @package kalanis\kw_files\Processing\Storage\Dirs |
17
|
|
|
* Process dirs via predefined api |
18
|
|
|
*/ |
19
|
|
|
class CanDir extends ADirs |
20
|
|
|
{ |
21
|
|
|
/** @var IPassDirs */ |
22
|
|
|
protected $storage = null; |
23
|
|
|
|
24
|
11 |
|
public function __construct(IPassDirs $storage, ?IFLTranslations $lang = null) |
25
|
|
|
{ |
26
|
11 |
|
$this->storage = $storage; |
27
|
11 |
|
$this->setLang($lang); |
28
|
11 |
|
} |
29
|
|
|
|
30
|
2 |
|
public function createDir(array $entry, bool $deep = false): bool |
31
|
|
|
{ |
32
|
2 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
33
|
|
|
try { |
34
|
2 |
|
return $this->storage->mkDir($path, $deep); |
35
|
1 |
|
} catch (StorageException $ex) { |
36
|
1 |
|
throw new FilesException($this->getLang()->flCannotCreateDir($path), $ex->getCode(), $ex); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
5 |
|
public function readDir(array $entry, bool $loadRecursive = false, bool $wantSize = false, bool $wantFirst = true): array |
41
|
|
|
{ |
42
|
5 |
|
$entryPath = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
43
|
|
|
try { |
44
|
5 |
|
if (!$this->storage->isDir($entryPath)) { |
45
|
1 |
|
throw new FilesException($this->getLang()->flCannotReadDir($entryPath)); |
46
|
|
|
} |
47
|
3 |
|
$files = []; |
48
|
3 |
|
foreach ($this->storage->lookup($entryPath) as $item) { |
49
|
3 |
|
if ('..' == $item) { |
50
|
3 |
|
continue; |
51
|
|
|
} |
52
|
3 |
|
$currentPath = $this->compactName(array_merge($entry, [$item]), $this->getStorageSeparator()); |
53
|
3 |
|
$sub = new Node(); |
54
|
3 |
|
if ('.' == $item) { |
55
|
3 |
|
if (!$wantFirst) { |
56
|
1 |
|
continue; |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$sub->setData( |
60
|
3 |
|
$entry, |
61
|
3 |
|
0, |
62
|
3 |
|
ITypes::TYPE_DIR |
63
|
|
|
); |
64
|
3 |
|
} elseif ($this->storage->isDir($this->getStorageSeparator() . $currentPath)) { |
65
|
2 |
|
$sub->setData( |
66
|
2 |
|
$this->expandName($this->getStorageSeparator() . $currentPath), |
67
|
2 |
|
0, |
68
|
2 |
|
ITypes::TYPE_DIR |
69
|
|
|
); |
70
|
|
|
} else { |
71
|
|
|
// normal node - file |
72
|
3 |
|
$sub->setData( |
73
|
3 |
|
$this->expandName($this->getStorageSeparator() . $currentPath), |
74
|
3 |
|
$wantSize ? intval($this->storage->size($currentPath)) : 0, |
75
|
3 |
|
ITypes::TYPE_FILE |
76
|
|
|
); |
77
|
|
|
} |
78
|
3 |
|
$files[] = $sub; |
79
|
3 |
|
if ($loadRecursive && $sub->isDir() && ('.' !== $item)) { |
80
|
1 |
|
$files = array_merge($files, $this->readDir(array_merge($entry, [$item]), $loadRecursive, $wantSize, false)); |
81
|
|
|
} |
82
|
|
|
} |
83
|
3 |
|
return $files; |
84
|
2 |
|
} catch (StorageException $ex) { |
85
|
1 |
|
throw new FilesException($this->getLang()->flCannotReadDir($entryPath), $ex->getCode(), $ex); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
2 |
|
public function copyDir(array $source, array $dest): bool |
90
|
|
|
{ |
91
|
2 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
92
|
2 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
93
|
|
|
try { |
94
|
2 |
|
return $this->storage->copy($src, $dst); |
95
|
1 |
|
} catch (StorageException $ex) { |
96
|
1 |
|
throw new FilesException($this->getLang()->flCannotCopyDir($src, $dst), $ex->getCode(), $ex); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
2 |
|
public function moveDir(array $source, array $dest): bool |
101
|
|
|
{ |
102
|
2 |
|
$src = $this->getStorageSeparator() . $this->compactName($source, $this->getStorageSeparator()); |
103
|
2 |
|
$dst = $this->getStorageSeparator() . $this->compactName($dest, $this->getStorageSeparator()); |
104
|
|
|
try { |
105
|
2 |
|
return $this->storage->move($src, $dst); |
106
|
1 |
|
} catch (StorageException $ex) { |
107
|
1 |
|
throw new FilesException($this->getLang()->flCannotMoveDir($src, $dst), $ex->getCode(), $ex); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
public function deleteDir(array $entry, bool $deep = false): bool |
112
|
|
|
{ |
113
|
2 |
|
$path = $this->getStorageSeparator() . $this->compactName($entry, $this->getStorageSeparator()); |
114
|
|
|
try { |
115
|
2 |
|
if ($this->storage->isDir($path)) { |
116
|
1 |
|
return $this->storage->rmDir($path, $deep); |
117
|
|
|
} else { |
118
|
1 |
|
return false; |
119
|
|
|
} |
120
|
1 |
|
} catch (StorageException $ex) { |
121
|
1 |
|
throw new FilesException($this->getLang()->flCannotRemoveDir($path), $ex->getCode(), $ex); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
* @codeCoverageIgnore only when path fails |
128
|
|
|
*/ |
129
|
|
|
protected function noDirectoryDelimiterSet(): string |
130
|
|
|
{ |
131
|
|
|
return $this->getLang()->flNoDirectoryDelimiterSet(); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|