Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 82 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
Installer | |
0.00% |
0 / 82 |
|
0.00% |
0 / 5 |
650 | |
0.00% |
0 / 1 |
installExternal | |
0.00% |
0 / 34 |
|
0.00% |
0 / 1 |
110 | |||
installApplication | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
installPage | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
56 | |||
installRoutesHooks | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
30 | |||
installNavigation | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Jingga |
4 | * |
5 | * PHP Version 8.1 |
6 | * |
7 | * @package Modules\CMS\Admin |
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 Modules\CMS\Admin; |
16 | |
17 | use phpOMS\Application\ApplicationAbstract; |
18 | use phpOMS\Message\Http\HttpRequest; |
19 | use phpOMS\Message\Http\HttpResponse; |
20 | use phpOMS\Module\InstallerAbstract; |
21 | use phpOMS\System\File\PathException; |
22 | use phpOMS\System\File\PermissionException; |
23 | use phpOMS\Uri\HttpUri; |
24 | use phpOMS\Utils\ArrayUtils; |
25 | use phpOMS\Utils\Parser\Php\ArrayParser; |
26 | |
27 | /** |
28 | * Installer class. |
29 | * |
30 | * @package Modules\CMS\Admin |
31 | * @license OMS License 2.0 |
32 | * @link https://jingga.app |
33 | * @since 1.0.0 |
34 | */ |
35 | final class Installer extends InstallerAbstract |
36 | { |
37 | /** |
38 | * Path of the file |
39 | * |
40 | * @var string |
41 | * @since 1.0.0 |
42 | */ |
43 | public const PATH = __DIR__; |
44 | |
45 | /** |
46 | * Install data from providing modules. |
47 | * |
48 | * @param ApplicationAbstract $app Application |
49 | * @param array $data Additional data |
50 | * |
51 | * @return array |
52 | * |
53 | * @throws PathException |
54 | * @throws \Exception |
55 | * |
56 | * @since 1.0.0 |
57 | */ |
58 | public static function installExternal(ApplicationAbstract $app, array $data) : array |
59 | { |
60 | if (!\is_file($data['path'] ?? '')) { |
61 | throw new PathException($data['path'] ?? ''); |
62 | } |
63 | |
64 | $cmsFile = \file_get_contents($data['path'] ?? ''); |
65 | if ($cmsFile === false) { |
66 | throw new PathException($data['path'] ?? ''); // @codeCoverageIgnore |
67 | } |
68 | |
69 | $cmsData = \json_decode($cmsFile, true) ?? []; |
70 | if ($cmsData === false) { |
71 | throw new \Exception(); // @codeCoverageIgnore |
72 | } |
73 | |
74 | $apiApp = new class() extends ApplicationAbstract |
75 | { |
76 | protected string $appName = 'Api'; |
77 | }; |
78 | |
79 | $apiApp->dbPool = $app->dbPool; |
80 | $apiApp->unitId = $app->unitId; |
81 | $apiApp->accountManager = $app->accountManager; |
82 | $apiApp->appSettings = $app->appSettings; |
83 | $apiApp->moduleManager = $app->moduleManager; |
84 | $apiApp->eventManager = $app->eventManager; |
85 | |
86 | /** @var array $cmsData */ |
87 | foreach ($cmsData as $cms) { |
88 | switch ($cms['type']) { |
89 | case 'application': |
90 | self::installApplication($apiApp, $cms); |
91 | break; |
92 | case 'page': |
93 | $cms['path'] = $data['path']; |
94 | |
95 | self::installPage($apiApp, $cms); |
96 | break; |
97 | case 'route': |
98 | self::installRoutesHooks( |
99 | \dirname($data['path']) . '/' . $cms['src'] . '/Routes.php', |
100 | __DIR__ . '/../../../Web/' . $cms['dest'] . '/Routes.php' |
101 | ); |
102 | break; |
103 | case 'nav': |
104 | self::installNavigation($apiApp, $cms); |
105 | |
106 | break; |
107 | default: |
108 | } |
109 | } |
110 | |
111 | return []; |
112 | } |
113 | |
114 | /** |
115 | * Install application. |
116 | * |
117 | * @param ApplicationAbstract $app Application |
118 | * @param array $data Additional data |
119 | * |
120 | * @return void |
121 | * |
122 | * @since 1.0.0 |
123 | */ |
124 | private static function installApplication(ApplicationAbstract $app, array $data) : void |
125 | { |
126 | if (!isset($data['name'], $data['src'], $data['dest'])) { |
127 | return; |
128 | } |
129 | |
130 | $response = new HttpResponse(); |
131 | $request = new HttpRequest(new HttpUri('')); |
132 | |
133 | $request->header->account = 1; |
134 | $request->setData('name', $data['name']); |
135 | $request->setData('theme', $data['theme'] ?? 'Default', true); |
136 | |
137 | $app->moduleManager->get('CMS')->apiApplicationInstall($request, $response); |
138 | } |
139 | |
140 | /** |
141 | * Install application page. |
142 | * |
143 | * @param ApplicationAbstract $app Application |
144 | * @param array $data Additional data |
145 | * |
146 | * @return void |
147 | * |
148 | * @since 1.0.0 |
149 | */ |
150 | private static function installPage(ApplicationAbstract $app, array $data) : void |
151 | { |
152 | if (!isset($data['id'], $data['src'])) { |
153 | return; |
154 | } |
155 | |
156 | $response = new HttpResponse(); |
157 | $request = new HttpRequest(new HttpUri('')); |
158 | |
159 | $request->header->account = 1; |
160 | $request->setData('name', $data['id']); |
161 | $request->setData('app', $data['app'] ?? 2); |
162 | $app->moduleManager->get('CMS')->apiPageCreate($request, $response); |
163 | |
164 | $responseData = $response->get(''); |
165 | if (!\is_array($responseData)) { |
166 | return; |
167 | } |
168 | |
169 | $id = $responseData['response']->id; |
170 | |
171 | $l11ns = \scandir(\dirname($data['path']) . '/' . $data['src']); |
172 | |
173 | if ($l11ns === false) { |
174 | return; // @codeCoverageIgnore |
175 | } |
176 | |
177 | foreach ($l11ns as $l11n) { |
178 | $langCode = \explode('.', $l11n)[0]; |
179 | |
180 | if ($l11n === '.' || $l11n === '..') { |
181 | continue; |
182 | } |
183 | |
184 | $response = new HttpResponse(); |
185 | $request = new HttpRequest(new HttpUri('')); |
186 | |
187 | $request->header->account = 1; |
188 | |
189 | $request->setData('page', $id); |
190 | $request->setData('name', $data['id']); |
191 | $request->setData('language', $langCode); |
192 | $request->setDatA('content', \file_get_contents(\dirname($data['path']) . '/' . $data['src'] . '/' . $l11n)); |
193 | |
194 | $app->moduleManager->get('CMS')->apiPageL11nCreate($request, $response); |
195 | } |
196 | } |
197 | |
198 | /** |
199 | * Install routes. |
200 | * |
201 | * @param string $destRoutePath Destination route path |
202 | * @param string $srcRoutePath Source route path |
203 | * |
204 | * @return void |
205 | * |
206 | * @throws PathException |
207 | * @throws PermissionException |
208 | * |
209 | * @since 1.0.0 |
210 | */ |
211 | private static function installRoutesHooks(string $destRoutePath, string $srcRoutePath) : void |
212 | { |
213 | if (!\is_file($destRoutePath)) { |
214 | \file_put_contents($destRoutePath, '<?php return [];'); |
215 | } |
216 | |
217 | if (!\is_file($srcRoutePath)) { |
218 | return; |
219 | } |
220 | |
221 | if (!\is_file($destRoutePath)) { |
222 | throw new PathException($destRoutePath); |
223 | } |
224 | |
225 | if (!\is_writable($destRoutePath)) { |
226 | throw new PermissionException($destRoutePath); |
227 | } |
228 | |
229 | /** @noinspection PhpIncludeInspection */ |
230 | $appRoutes = include $destRoutePath; |
231 | /** @noinspection PhpIncludeInspection */ |
232 | $moduleRoutes = include $srcRoutePath; |
233 | |
234 | $appRoutes = \array_merge_recursive($appRoutes, $moduleRoutes); |
235 | |
236 | \file_put_contents($destRoutePath, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', \LOCK_EX); |
237 | } |
238 | |
239 | /** |
240 | * Uninstall routes. |
241 | * |
242 | * @param string $destRoutePath Destination route path |
243 | * @param string $srcRoutePath Source route path |
244 | * |
245 | * @return void |
246 | * |
247 | * @throws PermissionException |
248 | * |
249 | * @since 1.0.0 |
250 | */ |
251 | /* |
252 | private static function uninstallRoutesHooks(string $destRoutePath, string $srcRoutePath) : void |
253 | { |
254 | if (!\is_file($destRoutePath) |
255 | || !\is_file($srcRoutePath) |
256 | ) { |
257 | return; |
258 | } |
259 | |
260 | if (!\is_writable($destRoutePath)) { |
261 | throw new PermissionException($destRoutePath); |
262 | } |
263 | |
264 | // @noinspection PhpIncludeInspection |
265 | $appRoutes = include $destRoutePath; |
266 | // @noinspection PhpIncludeInspection |
267 | $moduleRoutes = include $srcRoutePath; |
268 | |
269 | $appRoutes = ArrayUtils::array_diff_assoc_recursive($appRoutes, $moduleRoutes); |
270 | |
271 | \file_put_contents($destRoutePath, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', \LOCK_EX); |
272 | } |
273 | */ |
274 | |
275 | /** |
276 | * Install navigation. |
277 | * |
278 | * @param ApplicationAbstract $app Application |
279 | * @param array $data Additional data |
280 | * |
281 | * @return void |
282 | * |
283 | * @since 1.0.0 |
284 | */ |
285 | private static function installNavigation(ApplicationAbstract $app, array $data) : void |
286 | { |
287 | $class = '\Web\\' . $data['dest'] . '\Admin\Install\Application\Navigation'; |
288 | $class::install($app, ''); |
289 | } |
290 | } |