Skip to content

Commit

Permalink
commands moved to package from the project
Browse files Browse the repository at this point in the history
  • Loading branch information
nasirkhan committed May 4, 2024
1 parent 3b4f79b commit 30864a8
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/Commands/AuthPermissionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Nasirkhan\ModuleManager\Commands;

use App\Models\Permission;
use Illuminate\Console\Command;
use Illuminate\Support\Str;

class AuthPermissionCommand extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'auth:permission {name} {--R|remove}';

/**
* The console command description.
*/
protected $description = 'Create Permissions for default mothods. The Names shoule be plural.';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$permissions = $this->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')));
}
}
122 changes: 122 additions & 0 deletions src/Commands/InsertDemoDataCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Nasirkhan\ModuleManager\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\Category\Models\Category;
use Modules\Comment\Models\Comment;
use Modules\Post\Models\Post;
use Modules\Tag\Models\Tag;

use function Laravel\Prompts\confirm;

class InsertDemoDataCommand extends Command
{
/**
* The name and signature of the console command.
*/
protected $signature = 'starter:insert-demo-data {--fresh}';

/**
* The console command description.
*/
protected $description = 'Insert demo data for posts, categories, tags, and comments. --fresh option will truncate the tables.';

/**
* Execute the console command.
*/
public function handle()
{
Auth::loginUsingId(1);

$fresh = $this->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();
}
}
12 changes: 11 additions & 1 deletion src/ModuleManagerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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,

]);
}
}
Expand Down

0 comments on commit 30864a8

Please sign in to comment.