-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commands moved to package from the project
- Loading branch information
Showing
3 changed files
with
205 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters