diff --git a/src/Commands/AuthPermissionCommand.php b/src/Commands/AuthPermissionCommand.php new file mode 100644 index 0000000..21e3052 --- /dev/null +++ b/src/Commands/AuthPermissionCommand.php @@ -0,0 +1,72 @@ +generatePermissions(); + + // check if its remove + if ($this->option('remove')) { + // remove permission + if (Permission::where('name', 'LIKE', '%'.$this->getNameArgument())->delete()) { + $this->warn('Permissions '.implode(', ', $permissions).' deleted.'); + } else { + $this->warn('No permissions for '.$this->getNameArgument().' found!'); + } + } else { + // create permissions + foreach ($permissions as $permission) { + Permission::firstOrCreate(['name' => $permission]); + } + + $this->info('Permissions '.implode(', ', $permissions).' created.'); + } + } + + private function generatePermissions() + { + $abilities = ['view', 'add', 'edit', 'delete', 'restore']; + $name = $this->getNameArgument(); + + return array_map(function ($val) use ($name) { + return $val.'_'.$name; + }, $abilities); + } + + private function getNameArgument() + { + return strtolower(Str::plural($this->argument('name'))); + } +} diff --git a/src/Commands/InsertDemoDataCommand.php b/src/Commands/InsertDemoDataCommand.php new file mode 100644 index 0000000..175a82f --- /dev/null +++ b/src/Commands/InsertDemoDataCommand.php @@ -0,0 +1,122 @@ +option('fresh'); + + if ($fresh) { + $this->truncate_tables(); + } + + $this->insert_demo_data(); + } + + public function insert_demo_data() + { + $this->info('Inserting Demo Data'); + + /** + * Categories. + */ + $this->components->task('Inserting Categories', function () { + Category::factory()->count(5)->create(); + }); + + /** + * Tags. + */ + $this->components->task('Inserting Tags', function () { + Tag::factory()->count(10)->create(); + }); + + /** + * Posts. + */ + $this->components->task('Inserting Posts', function () { + Post::factory()->count(25)->create()->each(function ($post) { + $post->tags()->attach( + Tag::inRandomOrder()->limit(rand(5, 10))->pluck('id')->toArray() + ); + }); + }); + + // /** + // * Comments. + // */ + // $this->components->task("Inserting Comments", function () { + // Comment::factory()->count(25)->create(); + // }); + + $this->newLine(2); + $this->info('-- Completed --'); + $this->newLine(); + } + + public function truncate_tables() + { + $tables_list = [ + 'posts', + 'categories', + 'tags', + 'taggables', + // 'comments', + 'activity_log', + ]; + + $confirmed = confirm( + label: 'Database tables (posts, categories, tags, comments) will become empty. Confirm truncate tables?', + default: false, + ); + + $this->info('Truncate tables'); + + if ($confirmed) { + // Disable foreign key checks! + Schema::disableForeignKeyConstraints(); + + foreach ($tables_list as $row) { + $table_name = $row; + + $this->components->task("Truncate Table: {$table_name}", function () use ($table_name) { + DB::table($table_name)->truncate(); + }); + } + + // Enable foreign key checks! + Schema::enableForeignKeyConstraints(); + } else { + $this->warn('Skipped database truncate.'); + } + $this->newLine(); + } +} diff --git a/src/ModuleManagerServiceProvider.php b/src/ModuleManagerServiceProvider.php index e2c44f9..1df8a8c 100644 --- a/src/ModuleManagerServiceProvider.php +++ b/src/ModuleManagerServiceProvider.php @@ -4,6 +4,8 @@ use Illuminate\Support\Facades\File; use Illuminate\Support\ServiceProvider; +use Nasirkhan\ModuleManager\Commands\AuthPermissionCommand; +use Nasirkhan\ModuleManager\Commands\InsertDemoDataCommand; use Nasirkhan\ModuleManager\Commands\ModuleBuildCommand; use Nasirkhan\ModuleManager\Commands\TestCommand; @@ -54,8 +56,16 @@ public function boot() */ if ($this->app->runningInConsole()) { $this->commands([ - // TestCommand::class, + + // Insert Demo Data Command + InsertDemoDataCommand::class, + + // Auth Permission Command + AuthPermissionCommand::class, + + // Module Build Command to Create Module ModuleBuildCommand::class, + ]); } }