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