|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_modules\ModulesLists; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_modules\Interfaces\Lists\IModulesList; |
|
7
|
|
|
use kalanis\kw_modules\Interfaces\Lists\File\IParamFormat; |
|
8
|
|
|
use kalanis\kw_modules\Interfaces\Lists\IFile; |
|
9
|
|
|
use kalanis\kw_modules\ModuleException; |
|
10
|
|
|
use kalanis\kw_paths\Stuff; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class File |
|
15
|
|
|
* @package kalanis\kw_modules\ModulesLists |
|
16
|
|
|
*/ |
|
17
|
|
|
class File implements IModulesList |
|
18
|
|
|
{ |
|
19
|
|
|
protected const PARAM_SEPARATOR = '|'; |
|
20
|
|
|
protected const LINE_SEPARATOR = "\r\n"; |
|
21
|
|
|
|
|
22
|
|
|
/** @var IFile */ |
|
23
|
|
|
protected $storage = null; |
|
24
|
|
|
/** @var IParamFormat */ |
|
25
|
|
|
protected $format = null; |
|
26
|
|
|
|
|
27
|
14 |
|
public function __construct(IFile $storage, IParamFormat $format) |
|
28
|
|
|
{ |
|
29
|
14 |
|
$this->storage = $storage; |
|
30
|
14 |
|
$this->format = $format; |
|
31
|
14 |
|
} |
|
32
|
|
|
|
|
33
|
3 |
|
public function setModuleLevel(int $level): void |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->storage->setModuleLevel($level); |
|
36
|
3 |
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
public function add(string $moduleName, bool $enabled = false, array $params = []): bool |
|
39
|
|
|
{ |
|
40
|
2 |
|
$all = $this->listing(); |
|
41
|
2 |
|
if (isset($all[$moduleName])) { |
|
42
|
1 |
|
return false; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
$record = new Record(); |
|
46
|
1 |
|
$record->setModuleName($moduleName); |
|
47
|
1 |
|
$record->setEnabled($enabled); |
|
48
|
1 |
|
$record->setParams($params); |
|
49
|
|
|
|
|
50
|
1 |
|
$all[$moduleName] = $record; |
|
51
|
1 |
|
return $this->saveData($all); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
public function get(string $moduleName): ?Record |
|
55
|
|
|
{ |
|
56
|
1 |
|
$all = $this->listing(); |
|
57
|
1 |
|
return isset($all[$moduleName]) ? $all[$moduleName] : null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
public function listing(): array |
|
61
|
|
|
{ |
|
62
|
3 |
|
$records = $this->unpack($this->storage->load()); |
|
63
|
3 |
|
return array_combine(array_map([$this, 'getRecordName'], $records), $records); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
3 |
|
public function getRecordName(Record $record): string |
|
67
|
|
|
{ |
|
68
|
3 |
|
return $record->getModuleName(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
2 |
|
public function updateBasic(string $moduleName, ?bool $enabled, ?array $params): bool |
|
72
|
|
|
{ |
|
73
|
2 |
|
if (is_null($enabled) && is_null($params)) { |
|
|
|
|
|
|
74
|
1 |
|
return false; |
|
75
|
|
|
} |
|
76
|
2 |
|
$all = $this->listing(); |
|
77
|
2 |
|
if (!isset($all[$moduleName])) { |
|
78
|
1 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
/** @var Record $rec */ |
|
81
|
1 |
|
$rec = & $all[$moduleName]; |
|
82
|
1 |
|
if (!is_null($params)) { |
|
|
|
|
|
|
83
|
1 |
|
$rec->setParams($params); |
|
84
|
|
|
} |
|
85
|
1 |
|
if (!is_null($enabled)) { |
|
86
|
1 |
|
$rec->setEnabled($enabled); |
|
87
|
|
|
} |
|
88
|
1 |
|
return $this->saveData($all); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
public function updateObject(Record $record): bool |
|
92
|
|
|
{ |
|
93
|
2 |
|
$all = $this->listing(); |
|
94
|
2 |
|
if (!isset($all[$record->getModuleName()])) { |
|
95
|
1 |
|
return false; |
|
96
|
|
|
} |
|
97
|
|
|
// intentionally separated |
|
98
|
|
|
/** @var Record $rec */ |
|
99
|
1 |
|
$rec = & $all[$record->getModuleName()]; |
|
100
|
1 |
|
$rec->setParams($record->getParams()); |
|
101
|
1 |
|
$rec->setEnabled($record->isEnabled()); |
|
102
|
1 |
|
return $this->saveData($all); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
public function remove(string $moduleName): bool |
|
106
|
|
|
{ |
|
107
|
1 |
|
$all = $this->listing(); |
|
108
|
1 |
|
if (isset($all[$moduleName])) { |
|
109
|
1 |
|
unset($all[$moduleName]); |
|
110
|
|
|
} |
|
111
|
1 |
|
return $this->saveData($all); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param Record[] $records |
|
116
|
|
|
* @throws ModuleException |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
1 |
|
protected function saveData(array $records): bool |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->storage->save($this->pack($records)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param Record[] $records |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
1 |
|
public function pack(array $records): string |
|
129
|
|
|
{ |
|
130
|
1 |
|
return implode(self::LINE_SEPARATOR, array_map([$this, 'toLine'], $records)) . self::LINE_SEPARATOR; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
1 |
|
public function toLine(Record $record): string |
|
134
|
|
|
{ |
|
135
|
1 |
|
return implode(static::PARAM_SEPARATOR, [ |
|
136
|
1 |
|
$record->getModuleName(), |
|
137
|
1 |
|
strval(intval($record->isEnabled())), |
|
138
|
1 |
|
Stuff::arrayIntoHttpString($record->getParams()), |
|
139
|
1 |
|
'' |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param mixed $content |
|
145
|
|
|
* @return Record[] |
|
146
|
|
|
*/ |
|
147
|
3 |
|
public function unpack($content): array |
|
148
|
|
|
{ |
|
149
|
3 |
|
return array_map([$this, 'fillRecord'], |
|
150
|
3 |
|
array_filter(explode(static::LINE_SEPARATOR, strval($content)), [$this, 'useLine']) |
|
151
|
|
|
); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
3 |
|
public function useLine(string $line): bool |
|
155
|
|
|
{ |
|
156
|
3 |
|
return !empty($line) && ('#' != $line[0]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
3 |
|
public function fillRecord(string $line): Record |
|
160
|
|
|
{ |
|
161
|
3 |
|
list($name, $enabled, $params, ) = explode(static::PARAM_SEPARATOR, $line, 4); |
|
162
|
3 |
|
$record = new Record(); |
|
163
|
3 |
|
$record->setModuleName($name); |
|
164
|
3 |
|
$record->setEnabled(boolval(intval(strval($enabled)))); |
|
165
|
3 |
|
$record->setParams(Stuff::httpStringIntoArray($params)); |
|
166
|
3 |
|
return $record; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|