1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_images\Sources; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_files\Access\CompositeAdapter; |
7
|
|
|
use kalanis\kw_files\Extended\Config; |
8
|
|
|
use kalanis\kw_files\FilesException; |
9
|
|
|
use kalanis\kw_images\Interfaces\IIMTranslations; |
10
|
|
|
use kalanis\kw_images\Traits\TLang; |
11
|
|
|
use kalanis\kw_paths\PathsException; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AFiles |
16
|
|
|
* Shared operations over files |
17
|
|
|
* @package kalanis\kw_images\Sources |
18
|
|
|
*/ |
19
|
|
|
abstract class AFiles |
20
|
|
|
{ |
21
|
|
|
use TLang; |
22
|
|
|
|
23
|
|
|
protected CompositeAdapter $lib; |
24
|
|
|
protected Config $config; |
25
|
|
|
|
26
|
56 |
|
public function __construct(CompositeAdapter $lib, Config $config, ?IIMTranslations $lang = null) |
27
|
|
|
{ |
28
|
56 |
|
$this->setImLang($lang); |
29
|
56 |
|
$this->lib = $lib; |
30
|
56 |
|
$this->config = $config; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string[] $path |
35
|
|
|
* @throws FilesException |
36
|
|
|
* @throws PathsException |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
21 |
|
public function isHere(array $path): bool |
40
|
|
|
{ |
41
|
21 |
|
return $this->lib->isFile($this->getPath($path)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string[] $path |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
abstract public function getPath(array $path): array; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string[] $source |
52
|
|
|
* @param string[] $target |
53
|
|
|
* @param bool $overwrite |
54
|
|
|
* @param string $sourceFileNotExistsErr |
55
|
|
|
* @param string $targetFileExistsErr |
56
|
|
|
* @param string $unlinkErr |
57
|
|
|
* @param string $copyErr |
58
|
|
|
* @throws FilesException |
59
|
|
|
* @throws PathsException |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
16 |
|
protected function dataCopy( |
63
|
|
|
array $source, |
64
|
|
|
array $target, |
65
|
|
|
bool $overwrite, |
66
|
|
|
string $sourceFileNotExistsErr, |
67
|
|
|
string $targetFileExistsErr, |
68
|
|
|
string $unlinkErr, |
69
|
|
|
string $copyErr |
70
|
|
|
): bool |
71
|
|
|
{ |
72
|
16 |
|
if (!$this->lib->isFile($source)) { |
73
|
1 |
|
throw new FilesException($sourceFileNotExistsErr); |
74
|
|
|
} |
75
|
|
|
|
76
|
15 |
|
if ($this->lib->isFile($target) && !$overwrite) { |
77
|
1 |
|
throw new FilesException($targetFileExistsErr); |
78
|
|
|
} |
79
|
|
|
|
80
|
15 |
|
return $this->dataOverwriteCopy( $source, $target, $unlinkErr, $copyErr); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string[] $source |
85
|
|
|
* @param string[] $target |
86
|
|
|
* @param string $unlinkErrDesc |
87
|
|
|
* @param string $copyErrDesc |
88
|
|
|
* @throws FilesException |
89
|
|
|
* @throws PathsException |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
15 |
|
protected function dataOverwriteCopy(array $source, array $target, string $unlinkErrDesc, string $copyErrDesc): bool |
93
|
|
|
{ |
94
|
15 |
|
if ($this->lib->isFile($target) && !$this->lib->deleteFile($target)) { |
95
|
1 |
|
throw new FilesException($unlinkErrDesc); |
96
|
|
|
} |
97
|
15 |
|
if ($this->lib->isFile($source) && !$this->lib->copyFile($source, $target)) { |
98
|
1 |
|
throw new FilesException($copyErrDesc); |
99
|
|
|
} |
100
|
15 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string[] $source |
105
|
|
|
* @param string[] $target |
106
|
|
|
* @param bool $overwrite |
107
|
|
|
* @param string $sourceFileNotExistsErr |
108
|
|
|
* @param string $targetFileExistsErr |
109
|
|
|
* @param string $unlinkErr |
110
|
|
|
* @param string $copyErr |
111
|
|
|
* @throws FilesException |
112
|
|
|
* @throws PathsException |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
14 |
|
protected function dataRename( |
116
|
|
|
array $source, |
117
|
|
|
array $target, |
118
|
|
|
bool $overwrite, |
119
|
|
|
string $sourceFileNotExistsErr, |
120
|
|
|
string $targetFileExistsErr, |
121
|
|
|
string $unlinkErr, |
122
|
|
|
string $copyErr |
123
|
|
|
): bool |
124
|
|
|
{ |
125
|
14 |
|
if (!$this->lib->isFile($source)) { |
126
|
1 |
|
throw new FilesException($sourceFileNotExistsErr); |
127
|
|
|
} |
128
|
|
|
|
129
|
13 |
|
if ($this->lib->isFile($target) && !$overwrite) { |
130
|
1 |
|
throw new FilesException($targetFileExistsErr); |
131
|
|
|
} |
132
|
|
|
|
133
|
13 |
|
return $this->dataOverwriteRename( $source, $target, $unlinkErr, $copyErr); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string[] $source |
138
|
|
|
* @param string[] $target |
139
|
|
|
* @param string $unlinkErrDesc |
140
|
|
|
* @param string $copyErrDesc |
141
|
|
|
* @throws FilesException |
142
|
|
|
* @throws PathsException |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
13 |
|
protected function dataOverwriteRename(array $source, array $target, string $unlinkErrDesc, string $copyErrDesc): bool |
146
|
|
|
{ |
147
|
13 |
|
if ($this->lib->isFile($target) && !$this->lib->deleteFile($target)) { |
148
|
1 |
|
throw new FilesException($unlinkErrDesc); |
149
|
|
|
} |
150
|
12 |
|
if ($this->lib->isFile($source) && !$this->lib->moveFile($source, $target)) { |
151
|
1 |
|
throw new FilesException($copyErrDesc); |
152
|
|
|
} |
153
|
11 |
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string[] $source |
158
|
|
|
* @param string $unlinkErrDesc |
159
|
|
|
* @throws FilesException |
160
|
|
|
* @throws PathsException |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
16 |
|
protected function dataRemove(array $source, string $unlinkErrDesc): bool |
164
|
|
|
{ |
165
|
16 |
|
if (!$this->lib->isFile($source)) { |
166
|
2 |
|
return true; |
167
|
|
|
} |
168
|
15 |
|
if (!$this->lib->deleteFile($source)) { |
169
|
1 |
|
throw new FilesException($unlinkErrDesc); |
170
|
|
|
} |
171
|
14 |
|
return true; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|