Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
10.34% |
3 / 29 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| UninstallerAbstract | |
10.34% |
3 / 29 |
|
0.00% |
0 / 4 |
67.37 | |
0.00% |
0 / 1 |
| uninstall | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| deactivate | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| dropTables | |
25.00% |
3 / 12 |
|
0.00% |
0 / 1 |
15.55 | |||
| unregisterFromDatabase | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
| 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\DataStorage\Database\DatabasePool; |
| 19 | use phpOMS\DataStorage\Database\Query\Builder; |
| 20 | use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; |
| 21 | |
| 22 | /** |
| 23 | * Uninstaller abstract class. |
| 24 | * |
| 25 | * @package phpOMS\Module |
| 26 | * @license OMS License 2.0 |
| 27 | * @link https://jingga.app |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | abstract class UninstallerAbstract |
| 31 | { |
| 32 | /** |
| 33 | * Path of the file |
| 34 | * |
| 35 | * @var string |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | public const PATH = ''; |
| 39 | |
| 40 | /** |
| 41 | * Install module. |
| 42 | * |
| 43 | * @param ApplicationAbstract $app Application |
| 44 | * @param ModuleInfo $info Module info |
| 45 | * |
| 46 | * @return void |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public static function uninstall(ApplicationAbstract $app, ModuleInfo $info) : void |
| 51 | { |
| 52 | self::deactivate($app, $info); |
| 53 | self::dropTables($app->dbPool, $info); |
| 54 | self::unregisterFromDatabase($app->dbPool, $info); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Activate after install. |
| 59 | * |
| 60 | * @param ApplicationAbstract $app Application |
| 61 | * @param ModuleInfo $info Module info |
| 62 | * |
| 63 | * @return void |
| 64 | * |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | protected static function deactivate(ApplicationAbstract $app, ModuleInfo $info) : void |
| 68 | { |
| 69 | if (($path = \realpath(static::PATH)) === false) { |
| 70 | return; // @codeCoverageIgnore |
| 71 | } |
| 72 | |
| 73 | /** @var string $classPath */ |
| 74 | $classPath = \substr($path . '/Status', \strlen((string) \realpath(__DIR__ . '/../../'))); |
| 75 | |
| 76 | /** @var StatusAbstract $class */ |
| 77 | $class = \strtr($classPath, '/', '\\'); |
| 78 | $class::deactivate($app, $info); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Drop tables of module. |
| 83 | * |
| 84 | * @param DatabasePool $dbPool Database instance |
| 85 | * @param ModuleInfo $info Module info |
| 86 | * |
| 87 | * @return void |
| 88 | * |
| 89 | * @since 1.0.0 |
| 90 | */ |
| 91 | public static function dropTables(DatabasePool $dbPool, ModuleInfo $info) : void |
| 92 | { |
| 93 | $path = static::PATH . '/Install/db.json'; |
| 94 | if (!\is_file($path)) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $content = \file_get_contents($path); |
| 99 | if ($content === false) { |
| 100 | return; // @codeCoverageIgnore |
| 101 | } |
| 102 | |
| 103 | $definitions = \json_decode($content, true); |
| 104 | $builder = new SchemaBuilder($dbPool->get('schema')); |
| 105 | |
| 106 | if (!\is_array($definitions)) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | foreach ($definitions as $name => $_) { |
| 111 | $builder->dropTable($name); |
| 112 | } |
| 113 | |
| 114 | $builder->execute(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Unregister module from database. |
| 119 | * |
| 120 | * @param DatabasePool $dbPool Database instance |
| 121 | * @param ModuleInfo $info Module info |
| 122 | * |
| 123 | * @return void |
| 124 | * |
| 125 | * @since 1.0.0 |
| 126 | */ |
| 127 | public static function unregisterFromDatabase(DatabasePool $dbPool, ModuleInfo $info) : void |
| 128 | { |
| 129 | $queryLoad = new Builder($dbPool->get('delete')); |
| 130 | $queryLoad->delete() |
| 131 | ->from('module_load') |
| 132 | ->where('module_load_from', '=', $info->getInternalName()) |
| 133 | ->execute(); |
| 134 | |
| 135 | $queryModule = new Builder($dbPool->get('delete')); |
| 136 | $queryModule->delete() |
| 137 | ->from('module') |
| 138 | ->where('module_id', '=', $info->getInternalName()) |
| 139 | ->execute(); |
| 140 | } |
| 141 | } |