1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_files_mapper\Processing\Mapper; |
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\Mapper |
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
|
19 |
|
public function __construct(ARecord $record, ?Process\Translate $translate = null, ?IFLTranslations $lang = null) |
34
|
|
|
{ |
35
|
19 |
|
$this->setFlLang($lang); |
36
|
19 |
|
$this->record = $record; |
37
|
19 |
|
$this->setTranslation($translate); |
38
|
19 |
|
} |
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($path)); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
$tgtArr = new ArrayPath(); |
65
|
6 |
|
$tgtArr->setArray($entry); |
66
|
|
|
|
67
|
6 |
|
$current = $this->getEntry($entry); |
68
|
5 |
|
$parent = $this->getEntry($tgtArr->getArrayDirectory()); |
69
|
|
|
|
70
|
5 |
|
if (!empty($tgtArr->getArrayDirectory()) && empty($parent)) { |
71
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
$prepend = ''; |
75
|
4 |
|
if (is_null($current)) { |
76
|
4 |
|
$current = $this->getLookupRecord(); |
77
|
4 |
|
$current->__set($this->getTranslation()->getParentKey(), $parent ? strval($parent->__get($this->getTranslation()->getPrimaryKey())) : null); |
78
|
4 |
|
$current->__set($this->getTranslation()->getCurrentKey(), $tgtArr->getFileName()); |
79
|
|
|
} else { |
80
|
1 |
|
if (FILE_APPEND == $mode) { |
81
|
1 |
|
$prepend = strval($current->__get($this->getTranslation()->getContentKey())); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
4 |
|
$current->__set($this->getTranslation()->getContentKey(), $prepend . $this->toString($path, $content)); |
86
|
|
|
|
87
|
4 |
|
if (false === $current->save()) { |
88
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path)); |
89
|
|
|
} |
90
|
3 |
|
return true; |
91
|
4 |
|
} catch (MapperException $ex) { |
92
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotSaveFile($path), $ex->getCode(), $ex); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
3 |
|
public function copyFileStream(array $source, array $dest): bool |
97
|
|
|
{ |
98
|
|
|
// simplified run - no moving nodes, just use existing ones |
99
|
3 |
|
$src = Stuff::arrayToPath($source); |
100
|
3 |
|
$dst = Stuff::arrayToPath($dest); |
101
|
|
|
try { |
102
|
3 |
|
$dstRec = $this->getEntry($dest); |
103
|
2 |
|
if ($dstRec) { |
104
|
1 |
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$sourceStream = $this->readFileStream($source); |
108
|
1 |
|
rewind($sourceStream); |
109
|
1 |
|
return $this->saveFileStream($dest, $sourceStream); |
110
|
1 |
|
} catch (MapperException $ex) { |
111
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotCopyFile($src, $dst), $ex->getCode(), $ex); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
5 |
|
public function moveFileStream(array $source, array $dest): bool |
116
|
|
|
{ |
117
|
|
|
try { |
118
|
5 |
|
$ptDst = new ArrayPath(); |
119
|
5 |
|
$ptDst->setArray($dest); |
120
|
|
|
|
121
|
5 |
|
$src = $this->getEntry($source); |
122
|
4 |
|
if (!$src) { |
123
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotProcessNode(Stuff::arrayToPath($source))); |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
$dst = $this->getEntry($ptDst->getArrayDirectory()); |
127
|
3 |
|
if (!$dst) { |
128
|
1 |
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
2 |
|
$tgt = $this->getEntry([$ptDst->getFileName()], $dst); |
132
|
2 |
|
if ($tgt) { |
|
|
|
|
133
|
1 |
|
return false; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
$src->__set($this->getTranslation()->getCurrentKey(), $ptDst->getFileName()); |
137
|
1 |
|
$src->__set($this->getTranslation()->getParentKey(), $dst->__get($this->getTranslation()->getPrimaryKey())); |
138
|
1 |
|
return $src->save(); |
139
|
|
|
|
140
|
2 |
|
} catch (MapperException $ex) { |
141
|
1 |
|
throw new FilesException($this->getFlLang()->flCannotMoveFile( |
142
|
1 |
|
Stuff::arrayToPath($source), |
143
|
1 |
|
Stuff::arrayToPath($dest) |
144
|
1 |
|
), $ex->getCode(), $ex); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
12 |
|
protected function getLookupRecord(): ARecord |
149
|
|
|
{ |
150
|
12 |
|
return clone $this->record; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|