From 4fc518dd4bcbde916b75a2578ace02d4c74ddee9 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 21 Dec 2018 20:21:50 +0100 Subject: [PATCH] Prepare for automatic table create --- Module/InstallerAbstract.php | 60 ++++++++++++++++++++++++++++++++++ Module/UninstallerAbstract.php | 38 ++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index a9690c0a9..3b11ce440 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -16,6 +16,7 @@ namespace phpOMS\Module; use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; use phpOMS\System\File\Local\Directory; use phpOMS\System\File\Local\File; use phpOMS\System\File\PathException; @@ -86,12 +87,71 @@ class InstallerAbstract */ public static function install(DatabasePool $dbPool, InfoManager $info) : void { + self::createTables($dbPool, $info); self::registerInDatabase($dbPool, $info); self::initRoutes($info); self::initHooks($info); self::activate($dbPool, $info); } + /** + * Create tables for module. + * + * @param DatabasePool $dbPool Database instance + * @param InfoManager $info Module info + * + * @return void + * + * @since 1.0.0 + */ + public static function createTables(DatabasePool $dbPool, InfoManager $info) : void + { + $path = \dirname($info->getPath()) . '/Admin/Install/db.json'; + + if (!\file_exists($path)) { + return; + } + + $content = \file_get_contents($path); + if ($content === false) { + return; + } + + $definitions = \json_decode($content, true); + + foreach ($definitions as $definition) { + self::createTable($definition, $dbPool); + } + } + + /** + * Create table module. + * + * @param array $definition Table definition + * @param DatabasePool $dbPool Database instance + * + * @return void + * + * @since 1.0.0 + */ + public static function createTable(array $definition, DatabasePool $dbPool) : void + { + $builder = new SchemaBuilder($dbPool->get('schema')); + $builder->prefix($dbPool->get('schema')->prefix); + $builder->createTable($definition['table'] ?? ''); + + foreach ($definition['fields'] as $name => $def) { + $builder->field( + $name, $def['type'], $def['default'], + $def['null'], $def['primary'], $def['autoincrement'], + $def['foreign']['table'], $def['foreign']['field'] + ); + } + + $builder->execute(); + + } + /** * Activate after install. * diff --git a/Module/UninstallerAbstract.php b/Module/UninstallerAbstract.php index 52fa13e49..a80e3c230 100644 --- a/Module/UninstallerAbstract.php +++ b/Module/UninstallerAbstract.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace phpOMS\Module; use phpOMS\DataStorage\Database\DatabasePool; +use phpOMS\DataStorage\Database\Schema\Builder as SchemaBuilder; /** * Installer Abstract class. @@ -39,6 +40,41 @@ class UninstallerAbstract */ public static function uninstall(DatabasePool $dbPool, InfoManager $info) : void { - + self::dropTables($dbPool, $info); } + + /** + * Drop tables of module. + * + * @param DatabasePool $dbPool Database instance + * @param InfoManager $info Module info + * + * @return void + * + * @since 1.0.0 + */ + public static function dropTables(DatabasePool $dbPool, InfoManager $info) : void + { + $path = \dirname($info->getPath()) . '/Admin/Install/db.json'; + + if (!\file_exists($path)) { + return; + } + + $content = \file_get_contents($path); + if ($content === false) { + return; + } + + $definitions = \json_decode($content, true); + + $builder = new SchemaBuilder($dbPool->get('schema')); + $builder->prefix($dbPool->get('schema')->prefix); + + foreach ($definitions as $definition) { + $builder->dropTable($definition['table'] ?? ''); + } + + $builder->execute(); + } }