From 84ba6c8aa673084c93af257c04d6645932f09ef4 Mon Sep 17 00:00:00 2001 From: AmrFekry Date: Mon, 23 Sep 2019 16:39:44 +0200 Subject: [PATCH] Add some modifications --- module/Migrations/mongodb-migration.php | 4 +- module/Migrations/mysql-migration.php | 1 + src/Console/Commands/EngezMigration.php | 14 +++++-- src/Console/Commands/ModuleBuilder.php | 51 +++++++++++++++---------- 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/module/Migrations/mongodb-migration.php b/module/Migrations/mongodb-migration.php index 8445472..7a8605d 100644 --- a/module/Migrations/mongodb-migration.php +++ b/module/Migrations/mongodb-migration.php @@ -14,7 +14,9 @@ class TableName extends Migration public function up() { Schema::create('TableName', function (Blueprint $table) { -// Table-Schema + $table->int('id')->unique(); + // Table-Schema + }); } diff --git a/module/Migrations/mysql-migration.php b/module/Migrations/mysql-migration.php index d564104..2931536 100644 --- a/module/Migrations/mysql-migration.php +++ b/module/Migrations/mysql-migration.php @@ -15,6 +15,7 @@ public function up() { Schema::create('TableName', function (Blueprint $table) { $table->loggers(); + $table->int('id')->unique(); // Table-Schema diff --git a/src/Console/Commands/EngezMigration.php b/src/Console/Commands/EngezMigration.php index 3eaf916..7aef998 100644 --- a/src/Console/Commands/EngezMigration.php +++ b/src/Console/Commands/EngezMigration.php @@ -16,7 +16,7 @@ class EngezMigration extends Command implements EngezInterface * * @var string */ - protected $signature = 'engez:migration {moduleName} {--data=} {--index=} {--unique=}'; + protected $signature = 'engez:migration {moduleName} {--data=} {--uploads=} {--index=} {--unique=}'; /** * The console command description. @@ -64,7 +64,8 @@ public function init() $this->info['moduleName'] = Str::studly($this->argument('moduleName')); $this->info['index'] = []; $this->info['unique'] = []; - + $this->info['uploads'] = []; + if ($this->hasOption('index')) { $this->info['index'] = explode(',', $this->option('index')); } @@ -76,6 +77,10 @@ public function init() if ($this->hasOption('data')) { $this->info['data'] = explode(',', $this->option('data')); } + + if ($this->hasOption('uploads')) { + $this->info['uploads'] = explode(',', $this->option('uploads')); + } } /** @@ -116,11 +121,12 @@ public function create() unset($this->info['index'][array_search($singleIndexData, $this->info['index'])]); } } + $allData = array_filter(array_merge($this->info['data'], $this->info['uploads'])); - if (isset($this->info['data'])) { + if (!empty($allData)) { $schema = ''; $tabs = "\n" . str_repeat("\t", 3); - foreach ($this->info['data'] as $data) { + foreach ($allData as $data) { $dataSchema = "{$tabs}\$table->string('$data');"; if (in_array($data, $this->info['index'])) { diff --git a/src/Console/Commands/ModuleBuilder.php b/src/Console/Commands/ModuleBuilder.php index 8eacde3..0641813 100644 --- a/src/Console/Commands/ModuleBuilder.php +++ b/src/Console/Commands/ModuleBuilder.php @@ -152,17 +152,17 @@ protected function create() $this->info('Creating database files'); $this->createDatabase(); - $this->info('Generating routes files'); - $this->createRoutes(); + // $this->info('Generating routes files'); + // $this->createRoutes(); - $this->info('Generating Module Postman File'); + // $this->info('Generating Module Postman File'); $this->generatePostmanModule(); - $this->info('Generating Module Docs'); + // $this->info('Generating Module Docs'); $this->generateModuleDocs(); - $this->info('Updating configurations.'); - $this->updateConfig(); + // $this->info('Updating configurations.'); + // $this->updateConfig(); } /** @@ -351,14 +351,12 @@ protected function createResource() protected function createDatabase() { $databaseFileName = strtolower(str::plural($this->moduleName)); - $path = $this->modulePath("database/migrations"); - $this->checkDirectory($path); - + $databaseDriver = config('database.default'); - if ($databaseDriver == 'mongodb') { - $this->createSchema($databaseFileName, $path); - } + // if ($databaseDriver == 'mongodb') { + $this->createSchema($databaseFileName); + // } $this->createMigration(); } @@ -394,6 +392,11 @@ protected function createMigration() $migrationsOptions['--data'] = $data; } + if ($this->hasOption('uploads')) { + $uploads = $this->option('uploads'); + $migrationsOptions['--uploads'] = $uploads; + } + Artisan::call('engez:migration', $migrationsOptions); } @@ -403,8 +406,11 @@ protected function createMigration() * @param string $dataFileName * @return void */ - protected function createSchema($databaseFileName, $path) + protected function createSchema($databaseFileName) { + $path = $this->modulePath("database/migrations/schema"); + $this->checkDirectory($path); + $defaultContent = [ '_id' => "objectId", 'id'=>'int', @@ -412,13 +418,16 @@ protected function createSchema($databaseFileName, $path) $customData = $this->info['data'] ?? []; + $uploadsData = $this->info['uploads'] ?? []; + unset($customData['id'], $customData['_id']); $customData = array_fill_keys($customData, 'string'); - $content = array_merge($defaultContent, $customData); - + $uploadsData = array_fill_keys($uploadsData, 'string'); + $content = array_merge($defaultContent, $customData, $uploadsData); + $this->createFile("$path/{$databaseFileName}.json", json_encode($content, JSON_PRETTY_PRINT), 'Schema'); } @@ -538,10 +547,13 @@ protected function generatePostmanModule() 'data' => $data ]); - $path = $this->modulePath($this->info['moduleName']); + $path = $this->modulePath("docs"); + $this->checkDirectory($path); + $fileName = strtolower($this->info['moduleName']).'.postman.json'; $content = $postman->getContent(); - $this->createFile("$path.json", $content, 'PostmanFile'); + + $this->createFile("{$path}/{$fileName}", $content, 'PostmanFile'); } /** @@ -559,9 +571,8 @@ protected function generateModuleDocs() 'data' => $data ]); - $path = $this->modulePath($this->info['moduleName']); - + $path = $this->modulePath("docs"); $content = $postman->getContent(); - $this->createFile("$path.md", $content, 'Docs'); + $this->createFile("{$path}/README.md", $content, 'Docs'); } } \ No newline at end of file