|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_files_mapper\Processing\MapperNoRoot; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_files\FilesException; |
|
7
|
|
|
use kalanis\kw_files\Interfaces\IFLTranslations; |
|
8
|
|
|
use kalanis\kw_files\Interfaces\IProcessFiles; |
|
9
|
|
|
use kalanis\kw_files\Traits\TLang; |
|
10
|
|
|
use kalanis\kw_files_mapper\Support\Process; |
|
11
|
|
|
use kalanis\kw_mapper\MapperException; |
|
12
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
13
|
|
|
use kalanis\kw_paths\ArrayPath; |
|
14
|
|
|
use kalanis\kw_paths\Stuff; |
|
15
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ProcessFile |
|
19
|
|
|
* @package kalanis\kw_files_mapper\Processing\MapperNoRoot |
|
20
|
|
|
* Process files in many ways |
|
21
|
|
|
*/ |
|
22
|
|
|
class ProcessFile implements IProcessFiles |
|
23
|
|
|
{ |
|
24
|
|
|
use TEntryLookup; |
|
25
|
|
|
use TLang; |
|
26
|
|
|
|
|
27
|
|
|
protected ARecord $record; |
|
28
|
|
|
|
|
29
|
20 |
|
public function __construct(ARecord $record, ?Process\Translate $translate = null, ?IFLTranslations $lang = null) |
|
30
|
|
|
{ |
|
31
|
20 |
|
$this->setFlLang($lang); |
|
32
|
20 |
|
$this->record = $record; |
|
33
|
20 |
|
$this->setTranslation($translate); |
|
34
|
20 |
|
} |
|
35
|
|
|
|
|
36
|
7 |
|
public function readFile(array $entry, ?int $offset = null, ?int $length = null): string |
|
37
|
|
|
{ |
|
38
|
|
|
try { |
|
39
|
7 |
|
$record = $this->getEntry($entry); |
|
40
|
6 |
|
if (is_null($record)) { |
|
41
|
2 |
|
throw new FilesException($this->getFlLang()->flCannotLoadFile(Stuff::arrayToPath($entry))); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
4 |
|
$content = $record->__get($this->getTranslation()->getContentKey()); |
|
45
|
|
|
// shit with substr... that needed undefined params was from some java dude?! |
|
46
|
4 |
|
if (!is_null($length)) { |
|
47
|
1 |
|
return strval(substr(strval($content), intval($offset), $length)); |
|
48
|
|
|
} |
|
49
|
4 |
|
if (!is_null($offset)) { |
|
50
|
1 |
|
return strval(substr(strval($content), $offset)); |
|
51
|
|
|
} |
|
52
|
4 |
|
return strval($content); |
|
53
|
3 |
|
} catch (MapperException $ex) { |
|
54
|
1 |
|
throw new FilesException($ex->getMessage(), $ex->getCode(), $ex); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
8 |
|
public function saveFile(array $entry, string $content, ?int $offset = null, int $mode = 0): bool |
|
59
|
|
|
{ |
|
60
|
8 |
|
$path = Stuff::arrayToPath($entry); |
|
61
|
|
|
try { |
|
62
|
|
|
|
|
63
|
8 |
|
if (1 > count($entry)) { |
|
64
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile('')); |
|
65
|
7 |
|
} elseif (2 > count($entry)) { |
|
66
|
5 |
|
$name = strval(end($entry)); |
|
67
|
5 |
|
$current = $this->getEntry($entry); |
|
68
|
4 |
|
$parent = null; |
|
69
|
|
|
} else { |
|
70
|
3 |
|
$parentPath = array_slice($entry, 0, -1); |
|
71
|
|
|
|
|
72
|
3 |
|
$name = strval(end($entry)); |
|
73
|
3 |
|
$parent = $this->getEntry($parentPath); |
|
74
|
3 |
|
if (is_null($parent)) { |
|
75
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
2 |
|
$current = $this->getEntry([$name], $parent); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
$prepend = ''; |
|
82
|
5 |
|
if (is_null($current)) { |
|
83
|
5 |
|
$current = $this->getLookupRecord(); |
|
84
|
5 |
|
$current->__set($this->getTranslation()->getParentKey(), $parent ? strval($parent->__get($this->getTranslation()->getPrimaryKey())) : null); |
|
85
|
5 |
|
$current->__set($this->getTranslation()->getCurrentKey(), $name); |
|
86
|
|
|
} else { |
|
87
|
1 |
|
if (FILE_APPEND == $mode) { |
|
88
|
1 |
|
$prepend = strval($current->__get($this->getTranslation()->getContentKey())); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
5 |
|
if (!is_null($offset)) { |
|
93
|
1 |
|
$prepend = str_pad(strval(substr($prepend, 0, $offset)), $offset, chr(0)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
5 |
|
$current->__set($this->getTranslation()->getContentKey(), $prepend . $content); |
|
97
|
|
|
|
|
98
|
5 |
|
if (false === $current->save()) { |
|
99
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); |
|
100
|
|
|
} |
|
101
|
4 |
|
return true; |
|
102
|
4 |
|
} catch (MapperException $ex) { |
|
103
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
public function copyFile(array $source, array $dest): bool |
|
108
|
|
|
{ |
|
109
|
|
|
// simplified run - no moving nodes, just use existing ones |
|
110
|
3 |
|
$src = Stuff::arrayToPath($source); |
|
111
|
3 |
|
$dst = Stuff::arrayToPath($dest); |
|
112
|
|
|
try { |
|
113
|
3 |
|
$dstRec = $this->getEntry($dest); |
|
114
|
2 |
|
if ($dstRec) { |
|
115
|
1 |
|
return false; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
1 |
|
return $this->saveFile($dest, $this->readFile($source)); |
|
119
|
1 |
|
} catch (MapperException $ex) { |
|
120
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotCopyFile($src, $dst), $ex->getCode(), $ex); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
5 |
|
public function moveFile(array $source, array $dest): bool |
|
125
|
|
|
{ |
|
126
|
|
|
try { |
|
127
|
5 |
|
$ptDst = new ArrayPath(); |
|
128
|
5 |
|
$ptDst->setArray($dest); |
|
129
|
|
|
|
|
130
|
5 |
|
$src = $this->getEntry($source); |
|
131
|
4 |
|
if (!$src) { |
|
132
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotProcessNode(Stuff::arrayToPath($source))); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
3 |
|
$dst = $this->getEntry($ptDst->getArrayDirectory()); |
|
136
|
|
|
|
|
137
|
3 |
|
$tgt = $this->getEntry([$ptDst->getFileName()], $dst); |
|
138
|
3 |
|
if ($tgt) { |
|
|
|
|
|
|
139
|
2 |
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
1 |
|
$src->__set($this->getTranslation()->getCurrentKey(), $ptDst->getFileName()); |
|
143
|
1 |
|
$src->__set( |
|
144
|
1 |
|
$this->getTranslation()->getParentKey(), |
|
145
|
1 |
|
$dst ? $dst->__get($this->getTranslation()->getPrimaryKey()) : null |
|
146
|
|
|
); |
|
147
|
1 |
|
return $src->save(); |
|
148
|
|
|
|
|
149
|
2 |
|
} catch (MapperException $ex) { |
|
150
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotMoveFile( |
|
151
|
1 |
|
Stuff::arrayToPath($source), |
|
152
|
1 |
|
Stuff::arrayToPath($dest) |
|
153
|
1 |
|
), $ex->getCode(), $ex); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
3 |
|
public function deleteFile(array $entry): bool |
|
158
|
|
|
{ |
|
159
|
|
|
try { |
|
160
|
3 |
|
$record = $this->getEntry($entry); |
|
161
|
2 |
|
if (is_null($record)) { |
|
162
|
2 |
|
return true; |
|
163
|
|
|
} |
|
164
|
2 |
|
return $record->delete(); |
|
165
|
1 |
|
} catch (MapperException $ex) { |
|
166
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotRemoveFile(Stuff::arrayToPath($entry)), $ex->getCode(), $ex); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
14 |
|
protected function getLookupRecord(): ARecord |
|
171
|
|
|
{ |
|
172
|
14 |
|
return clone $this->record; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|