Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
ModuleInfo | |
100.00% |
23 / 23 |
|
100.00% |
15 / 15 |
22 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
load | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
4 | |||
update | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
set | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getId | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getInternalName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExternalName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDependencies | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getProviding | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getDirectory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getCategory | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getLoad | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package phpOMS\Module |
8 | * @copyright Dennis Eichhorn |
9 | * @license OMS License 2.0 |
10 | * @version 1.0.0 |
11 | * @link https://jingga.app |
12 | */ |
13 | declare(strict_types=1); |
14 | |
15 | namespace phpOMS\Module; |
16 | |
17 | use phpOMS\System\File\PathException; |
18 | use phpOMS\Utils\ArrayUtils; |
19 | |
20 | /** |
21 | * ModuleInfo class. |
22 | * |
23 | * Handling the info files for modules |
24 | * |
25 | * @package phpOMS\Module |
26 | * @license OMS License 2.0 |
27 | * @link https://jingga.app |
28 | * @since 1.0.0 |
29 | */ |
30 | final class ModuleInfo |
31 | { |
32 | /** |
33 | * File path. |
34 | * |
35 | * @var string |
36 | * @since 1.0.0 |
37 | */ |
38 | private string $path = ''; |
39 | |
40 | /** |
41 | * Info data. |
42 | * |
43 | * @var array{name:array{id:int, internal:string, external:string}, category:string, vision:string, requirements:array, creator:array{name:string, website:string}, description:string, directory:string, dependencies:array<string, string>, providing:array<string, string>, load:array<int, array{pid:string[], type:int, for:string, file:string, from:string}>}|array |
44 | * @since 1.0.0 |
45 | */ |
46 | private array $info = []; |
47 | |
48 | /** |
49 | * Object constructor. |
50 | * |
51 | * @param string $path Info file path |
52 | * |
53 | * @since 1.0.0 |
54 | */ |
55 | public function __construct(string $path) |
56 | { |
57 | $this->path = $path; |
58 | } |
59 | |
60 | /** |
61 | * Get info path |
62 | * |
63 | * @return string |
64 | * |
65 | * @since 1.0.0 |
66 | */ |
67 | public function getPath() : string |
68 | { |
69 | return $this->path; |
70 | } |
71 | |
72 | /** |
73 | * Load info data from path. |
74 | * |
75 | * @return void |
76 | * |
77 | * @throws PathException this exception is thrown in case the info file path doesn't exist |
78 | * |
79 | * @since 1.0.0 |
80 | */ |
81 | public function load() : void |
82 | { |
83 | if (!\is_file($this->path)) { |
84 | throw new PathException($this->path); |
85 | } |
86 | |
87 | $contents = \file_get_contents($this->path); |
88 | |
89 | /** @var array{name:array{id:int, internal:string, external:string}, category:string, vision:string, requirements:array, creator:array{name:string, website:string}, description:string, directory:string, dependencies:array<string, string>, providing:array<string, string>, load:array<int, array{pid:string[], type:int, for:string, file:string, from:string}>} $info */ |
90 | $info = \json_decode($contents === false ? '[]' : $contents, true); |
91 | $this->info = $info === false ? [] : $info; |
92 | } |
93 | |
94 | /** |
95 | * Update info file |
96 | * |
97 | * @return void |
98 | * |
99 | * @throws PathException |
100 | * |
101 | * @since 1.0.0 |
102 | */ |
103 | public function update() : void |
104 | { |
105 | if (!\is_file($this->path)) { |
106 | throw new PathException($this->path); |
107 | } |
108 | |
109 | \file_put_contents($this->path, \json_encode($this->info, \JSON_PRETTY_PRINT)); |
110 | } |
111 | |
112 | /** |
113 | * Set data |
114 | * |
115 | * @param string $path Value path |
116 | * @param mixed $data Scalar or array of data to set |
117 | * @param string $delim Delimiter of path |
118 | * |
119 | * @return void |
120 | * |
121 | * @throws \InvalidArgumentException |
122 | * |
123 | * @since 1.0.0 |
124 | */ |
125 | public function set(string $path, $data, string $delim = '/') : void |
126 | { |
127 | if (!\is_scalar($data) && !\is_array($data) && !($data instanceof \JsonSerializable)) { |
128 | throw new \InvalidArgumentException('Type of $data "' . \gettype($data) . '" is not supported.'); |
129 | } |
130 | |
131 | ArrayUtils::setArray($path, $this->info, $data, $delim, true); |
132 | } |
133 | |
134 | /** |
135 | * Get info data. |
136 | * |
137 | * @return array{name:array{id:int, internal:string, external:string}, category:string, vision:string, requirements:array, creator:array{name:string, website:string}, description:string, directory:string, dependencies:array<string, string>, providing:array<string, string>, load:array<int, array{pid:string[], type:int, for:string, file:string, from:string}>}|array |
138 | * |
139 | * @since 1.0.0 |
140 | */ |
141 | public function get() : array |
142 | { |
143 | return $this->info; |
144 | } |
145 | |
146 | /** |
147 | * Get info data. |
148 | * |
149 | * @return int |
150 | * |
151 | * @since 1.0.0 |
152 | */ |
153 | public function getId() : int |
154 | { |
155 | return $this->info['name']['id'] ?? 0; |
156 | } |
157 | |
158 | /** |
159 | * Get info data. |
160 | * |
161 | * @return string |
162 | * |
163 | * @since 1.0.0 |
164 | */ |
165 | public function getInternalName() : string |
166 | { |
167 | return $this->info['name']['internal'] ?? ''; |
168 | } |
169 | |
170 | /** |
171 | * Get info data. |
172 | * |
173 | * @return string |
174 | * |
175 | * @since 1.0.0 |
176 | */ |
177 | public function getExternalName() : string |
178 | { |
179 | return $this->info['name']['external'] ?? ''; |
180 | } |
181 | |
182 | /** |
183 | * Get info data. |
184 | * |
185 | * @return array<string, string> |
186 | * |
187 | * @since 1.0.0 |
188 | */ |
189 | public function getDependencies() : array |
190 | { |
191 | return $this->info['dependencies'] ?? []; |
192 | } |
193 | |
194 | /** |
195 | * Get info data. |
196 | * |
197 | * @return array<string, string> |
198 | * |
199 | * @since 1.0.0 |
200 | */ |
201 | public function getProviding() : array |
202 | { |
203 | return $this->info['providing'] ?? []; |
204 | } |
205 | |
206 | /** |
207 | * Get info data. |
208 | * |
209 | * @return string |
210 | * |
211 | * @since 1.0.0 |
212 | */ |
213 | public function getDirectory() : string |
214 | { |
215 | return $this->info['directory'] ?? ''; |
216 | } |
217 | |
218 | /** |
219 | * Get info category. |
220 | * |
221 | * @return string |
222 | * |
223 | * @since 1.0.0 |
224 | */ |
225 | public function getCategory() : string |
226 | { |
227 | return $this->info['category'] ?? ''; |
228 | } |
229 | |
230 | /** |
231 | * Get info data. |
232 | * |
233 | * @return string |
234 | * |
235 | * @since 1.0.0 |
236 | */ |
237 | public function getVersion() : string |
238 | { |
239 | return $this->info['version'] ?? ''; |
240 | } |
241 | |
242 | /** |
243 | * Get info data. |
244 | * |
245 | * @return array<array{pid:string[], type:int, for:string, file:string, from:string}> |
246 | * |
247 | * @since 1.0.0 |
248 | */ |
249 | public function getLoad() : array |
250 | { |
251 | return $this->info['load'] ?? []; |
252 | } |
253 | } |