|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules_mapper\ModulesLists; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_mapper\MapperException; |
|
7
|
|
|
use kalanis\kw_mapper\Records\ARecord; |
|
8
|
|
|
use kalanis\kw_modules\Interfaces\Lists; |
|
9
|
|
|
use kalanis\kw_modules\ModuleException; |
|
10
|
|
|
use kalanis\kw_modules\ModulesLists\Record; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Mapper |
|
15
|
|
|
* @package kalanis\kw_modules\ModulesLists |
|
16
|
|
|
* |
|
17
|
|
|
* Because kw_mapper does not know json as storage format, it needs to set the way to pack params |
|
18
|
|
|
*/ |
|
19
|
|
|
class Mapper implements Lists\IModulesList |
|
20
|
|
|
{ |
|
21
|
|
|
protected int $level = Lists\ISitePart::SITE_NOWHERE; |
|
22
|
|
|
protected Mapper\Translate $translate; |
|
23
|
|
|
protected Lists\File\IParamFormat $format; |
|
24
|
|
|
protected ARecord $record; |
|
25
|
|
|
|
|
26
|
9 |
|
public function __construct(ARecord $record, Mapper\Translate $tr, Lists\File\IParamFormat $format) |
|
27
|
|
|
{ |
|
28
|
9 |
|
$this->record = $record; |
|
29
|
9 |
|
$this->translate = $tr; |
|
30
|
9 |
|
$this->format = $format; |
|
31
|
9 |
|
} |
|
32
|
|
|
|
|
33
|
9 |
|
public function setModuleLevel(int $level): void |
|
34
|
|
|
{ |
|
35
|
9 |
|
$this->level = $level; |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function add(string $moduleName, bool $enabled = false, array $params = []): bool |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
3 |
|
if ($this->get($moduleName)) { |
|
42
|
1 |
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
2 |
|
$record = new Record(); |
|
46
|
2 |
|
$record->setModuleName($moduleName); |
|
47
|
2 |
|
$record->setEnabled($enabled); |
|
48
|
2 |
|
$record->setParams($params); |
|
49
|
|
|
|
|
50
|
2 |
|
$rec = $this->fillDbRecord($record); |
|
51
|
2 |
|
return $rec->save(); |
|
52
|
|
|
|
|
53
|
1 |
|
} catch (MapperException $ex) { |
|
54
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
public function get(string $moduleName): ?Record |
|
59
|
|
|
{ |
|
60
|
|
|
try { |
|
61
|
4 |
|
$rec = clone $this->record; |
|
62
|
4 |
|
$rec->offsetSet($this->translate->getName(), $moduleName); |
|
63
|
4 |
|
$rec->offsetSet($this->translate->getLevel(), $this->level); |
|
64
|
|
|
|
|
65
|
4 |
|
if (!$rec->load()) { |
|
66
|
2 |
|
return null; |
|
67
|
|
|
} |
|
68
|
2 |
|
return $this->fillModuleRecord($rec); |
|
69
|
|
|
|
|
70
|
1 |
|
} catch (MapperException $ex) { |
|
71
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
2 |
|
public function listing(): array |
|
76
|
|
|
{ |
|
77
|
|
|
try { |
|
78
|
2 |
|
$rec = clone $this->record; |
|
79
|
2 |
|
$rec->offsetSet($this->translate->getLevel(), $this->level); |
|
80
|
2 |
|
$records = array_map([$this, 'fillModuleRecord'], $rec->loadMultiple()); |
|
81
|
1 |
|
return array_combine(array_map([$this, 'getRecordName'], $records), $records); |
|
82
|
|
|
|
|
83
|
1 |
|
} catch (MapperException $ex) { |
|
84
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
3 |
|
public function updateBasic(string $moduleName, ?bool $enabled, ?array $params): bool |
|
89
|
|
|
{ |
|
90
|
|
|
try { |
|
91
|
3 |
|
if (!is_null($enabled) || !is_null($params)) { |
|
|
|
|
|
|
92
|
3 |
|
$rec = clone $this->record; |
|
93
|
|
|
// known things |
|
94
|
3 |
|
$rec->getEntry($this->translate->getName())->setData($moduleName, true); |
|
95
|
3 |
|
$rec->getEntry($this->translate->getLevel())->setData($this->level, true); |
|
96
|
|
|
|
|
97
|
|
|
// changed things |
|
98
|
3 |
|
$rec->offsetSet($this->translate->getParams(), is_null($params) ? false : $this->format->pack($params)); |
|
|
|
|
|
|
99
|
3 |
|
$rec->offsetSet($this->translate->getEnabled(), $enabled); |
|
100
|
3 |
|
return $rec->save(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// not exists or nothing to change |
|
104
|
1 |
|
return false; |
|
105
|
|
|
|
|
106
|
1 |
|
} catch (MapperException $ex) { |
|
107
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
3 |
|
public function updateObject(Record $record): bool |
|
112
|
|
|
{ |
|
113
|
|
|
try { |
|
114
|
3 |
|
$rec = clone $this->record; |
|
115
|
|
|
// known things |
|
116
|
3 |
|
$rec->getEntry($this->translate->getName())->setData($record->getModuleName(), true); |
|
117
|
3 |
|
$rec->getEntry($this->translate->getLevel())->setData($this->level, true); |
|
118
|
|
|
|
|
119
|
|
|
// changed things |
|
120
|
3 |
|
$rec->offsetSet($this->translate->getParams(), $this->format->pack($record->getParams())); |
|
121
|
3 |
|
$rec->offsetSet($this->translate->getEnabled(), $record->isEnabled()); |
|
122
|
3 |
|
return $rec->save(); |
|
123
|
|
|
|
|
124
|
1 |
|
} catch (MapperException $ex) { |
|
125
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
2 |
|
public function remove(string $moduleName): bool |
|
130
|
|
|
{ |
|
131
|
|
|
try { |
|
132
|
2 |
|
$rec = clone $this->record; |
|
133
|
2 |
|
$rec->offsetSet($this->translate->getName(), $moduleName); |
|
134
|
2 |
|
$rec->offsetSet($this->translate->getLevel(), $this->level); |
|
135
|
2 |
|
return $rec->delete(); |
|
136
|
|
|
|
|
137
|
1 |
|
} catch (MapperException $ex) { |
|
138
|
1 |
|
throw new ModuleException($ex->getMessage(), $ex->getCode(), $ex); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param ARecord $rec |
|
144
|
|
|
* @throws MapperException |
|
145
|
|
|
* @return Record |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function fillModuleRecord(ARecord $rec): Record |
|
148
|
|
|
{ |
|
149
|
3 |
|
$record = new Record(); |
|
150
|
3 |
|
$record->setModuleName(strval($rec->offsetGet($this->translate->getName()))); |
|
151
|
3 |
|
$record->setEnabled(boolval($rec->offsetGet($this->translate->getEnabled()))); |
|
152
|
3 |
|
$record->setParams($this->format->unpack(strval($rec->offsetGet($this->translate->getParams())))); |
|
153
|
3 |
|
return $record; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param Record $record |
|
158
|
|
|
* @return ARecord |
|
159
|
|
|
*/ |
|
160
|
2 |
|
public function fillDbRecord(Record $record): ARecord |
|
161
|
|
|
{ |
|
162
|
2 |
|
$rec = clone $this->record; |
|
163
|
2 |
|
$rec->offsetSet($this->translate->getName(), $record->getModuleName()); |
|
164
|
2 |
|
$rec->offsetSet($this->translate->getLevel(), $this->level); |
|
165
|
2 |
|
$rec->offsetSet($this->translate->getParams(), $this->format->pack($record->getParams())); |
|
166
|
2 |
|
$rec->offsetSet($this->translate->getEnabled(), $record->isEnabled()); |
|
167
|
2 |
|
return $rec; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
public function getRecordName(Record $record): string |
|
171
|
|
|
{ |
|
172
|
1 |
|
return $record->getModuleName(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|