Skip to content

Instantly share code, notes, and snippets.

@JoshuaDoshua
Last active December 4, 2019 18:23
Show Gist options
  • Save JoshuaDoshua/6caa0946f00fd441c7aba0c2872be691 to your computer and use it in GitHub Desktop.
Save JoshuaDoshua/6caa0946f00fd441c7aba0c2872be691 to your computer and use it in GitHub Desktop.
laravel shortcut artisan command to quickly do all entity generation commands
<?php
namespace App\Console\Commands;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
class MakeEntityCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:entity
{name : The singular name of the entity.}
{--i|interactive : Interactively choose which classes to make.}
{--api= : Include a version prefix for API Controllers.}
';
private $singular;
private $plural;
private $interactive;
private $api_version;
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate all classes for a new entity.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->singular = $this->argument('name');
$this->plural = Str::plural($this->singular);
$this->interactive = $this->option('interactive');
$this->api_version = "v" . ($this->option('api') ?? '1');
$classes = [
'model',
'migration',
'seeder',
'factory', 'unit_test',
'policy', 'observer',
'controller', 'admin_controller',
'api_controller', 'api_resource',
'form_request',
];
foreach ($classes as $cls):
if (!$this->interactive)
call_user_func([$this, "handle_{$cls}"]);
elseif ($this->confirm("Generate a [{$cls}]?", true))
call_user_func([$this, "handle_{$cls}"]);
endforeach;
}
private function handle_model()
{
$args = ['name' => $this->singular];
$this->call('make:model', [
'name' => $this->singular
]);
}
private function handle_migration()
{
$table_name = str_replace('-', '_', Str::kebab($this->plural));
$this->call('make:migration', [
'name' => "create_{$table_name}_table"
]);
}
private function handle_factory()
{
$this->call('make:factory', [
'name' => "{$this->singular}Factory",
'--model' => $this->singular
]);
}
private function handle_controller()
{
$this->call('make:controller', [
'name' => "{$this->singular}Controller",
'--model' => $this->singular,
'--resource' => 'default'
]);
}
private function handle_admin_controller()
{
$this->call('make:controller', [
'name' => "Admin/{$this->singular}Controller",
'--model' => $this->singular,
'--resource' => 'default'
]);
}
private function handle_api_controller()
{
$this->call('make:controller', [
'name' => "Api/{$this->api_version}/{$this->singular}Controller",
'--model' => $this->singular,
'--resource' => 'default',
'--api' => 'default'
]);
}
private function handle_api_resource()
{
$this->call('make:resource', [
'name' => "{$this->api_version}/{$this->singular}Resource"
]);
$this->call('make:resource', [
'name' => "{$this->api_version}/{$this->plural}Collection",
'--collection' => 'default'
]);
}
private function handle_seeder()
{
$this->call('make:seeder', [
'name' => "{$this->plural}TableSeeder"
]);
}
private function handle_policy()
{
$this->call('make:policy', [
'name' => "{$this->singular}Policy",
'--model' => $this->singular
]);
}
private function handle_observer()
{
$this->call('make:observer', [
'name' => "{$this->singular}Observer",
'--model' => $this->singular
]);
}
private function handle_form_request()
{
$this->call('make:request', [
'name' => "{$this->singular}FormRequest",
]);
}
private function handle_unit_test()
{
$this->call('make:test', [
'name' => "{$this->singular}Test",
'--unit' => 'default'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment