Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
40.91% |
9 / 22 |
|
25.00% |
1 / 4 |
CRAP | |
0.00% |
0 / 1 |
InstallerAbstract | |
40.91% |
9 / 22 |
|
25.00% |
1 / 4 |
41.71 | |
0.00% |
0 / 1 |
install | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
createTables | |
33.33% |
3 / 9 |
|
0.00% |
0 / 1 |
12.41 | |||
activate | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
reInit | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 |
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\Application\ApplicationAbstract; |
18 | use phpOMS\Application\ApplicationInfo; |
19 | use phpOMS\Autoloader; |
20 | use phpOMS\Config\SettingsInterface; |
21 | use phpOMS\DataStorage\Database\DatabasePool; |
22 | use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; |
23 | |
24 | /** |
25 | * Installer abstract class. |
26 | * |
27 | * @package phpOMS\Module |
28 | * @license OMS License 2.0 |
29 | * @link https://jingga.app |
30 | * @since 1.0.0 |
31 | */ |
32 | abstract class InstallerAbstract |
33 | { |
34 | /** |
35 | * Path of the file |
36 | * |
37 | * @var string |
38 | * @since 1.0.0 |
39 | */ |
40 | public const PATH = ''; |
41 | |
42 | /** |
43 | * Install module. |
44 | * |
45 | * @param ApplicationAbstract $app Application |
46 | * @param ModuleInfo $info Module info |
47 | * @param SettingsInterface $cfgHandler Settings/Configuration handler |
48 | * |
49 | * @return void |
50 | * |
51 | * @since 1.0.0 |
52 | */ |
53 | public static function install(ApplicationAbstract $app, ModuleInfo $info, SettingsInterface $cfgHandler) : void |
54 | { |
55 | self::createTables($app->dbPool, $info); |
56 | self::activate($app, $info); |
57 | } |
58 | |
59 | /** |
60 | * Create tables for module. |
61 | * |
62 | * @param DatabasePool $dbPool Database instance |
63 | * @param ModuleInfo $info Module info |
64 | * |
65 | * @return void |
66 | * |
67 | * @since 1.0.0 |
68 | */ |
69 | protected static function createTables(DatabasePool $dbPool, ModuleInfo $info) : void |
70 | { |
71 | $path = static::PATH . '/Install/db.json'; |
72 | if (!\is_file($path)) { |
73 | return; |
74 | } |
75 | |
76 | $content = \file_get_contents($path); |
77 | if ($content === false) { |
78 | return; // @codeCoverageIgnore |
79 | } |
80 | |
81 | /** @var array[] $definitions */ |
82 | $definitions = \json_decode($content, true); |
83 | |
84 | if (!\is_array($definitions)) { |
85 | return; // @codeCoverageIgnore |
86 | } |
87 | |
88 | foreach ($definitions as $definition) { |
89 | SchemaBuilder::createFromSchema($definition, $dbPool->get('schema'))->execute(); |
90 | } |
91 | } |
92 | |
93 | /** |
94 | * Activate after install. |
95 | * |
96 | * @param ApplicationAbstract $app Application |
97 | * @param ModuleInfo $info Module info |
98 | * |
99 | * @return void |
100 | * |
101 | * @throws \UnexpectedValueException |
102 | * |
103 | * @since 1.0.0 |
104 | */ |
105 | protected static function activate(ApplicationAbstract $app, ModuleInfo $info) : void |
106 | { |
107 | if (($path = \realpath(static::PATH)) === false) { |
108 | return; // @codeCoverageIgnore |
109 | } |
110 | |
111 | $classPath = \substr($path . '/Status', (int) \strlen((string) \realpath(__DIR__ . '/../../'))); |
112 | |
113 | /** @var class-string<StatusAbstract> $class */ |
114 | $class = \strtr($classPath, '/', '\\'); |
115 | |
116 | if (!Autoloader::exists($class)) { |
117 | throw new \UnexpectedValueException($class); // @codeCoverageIgnore |
118 | } |
119 | |
120 | $class::activate($app, $info); |
121 | } |
122 | |
123 | /** |
124 | * Re-init module. |
125 | * |
126 | * @param ModuleInfo $info Module info |
127 | * @param null|ApplicationInfo $appInfo Application info |
128 | * |
129 | * @return void |
130 | * |
131 | * @throws \UnexpectedValueException |
132 | * |
133 | * @since 1.0.0 |
134 | */ |
135 | public static function reInit(ModuleInfo $info, ApplicationInfo $appInfo = null) : void |
136 | { |
137 | if (($path = \realpath(static::PATH)) === false) { |
138 | return; // @codeCoverageIgnore |
139 | } |
140 | |
141 | $classPath = \substr($path . '/Status', \strlen((string) \realpath(__DIR__ . '/../../'))); |
142 | |
143 | /** @var class-string<StatusAbstract> $class */ |
144 | $class = \strtr($classPath, '/', '\\'); |
145 | |
146 | if (!Autoloader::exists($class)) { |
147 | throw new \UnexpectedValueException($class); // @codeCoverageIgnore |
148 | } |
149 | |
150 | $class::activateRoutes($info, $appInfo); |
151 | $class::activateHooks($info, $appInfo); |
152 | } |
153 | } |