Skip to content

Instantly share code, notes, and snippets.

@ejntaylor
Created March 4, 2020 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejntaylor/83a9589a40fcc8e2d9200f42461eaec8 to your computer and use it in GitHub Desktop.
Save ejntaylor/83a9589a40fcc8e2d9200f42461eaec8 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use VictorYoalli\LaravelCodeGenerator\Facades\CodeGenerator;
use VictorYoalli\LaravelCodeGenerator\Facades\CodeHelper;
use VictorYoalli\LaravelCodeGenerator\Facades\ModelLoader;
function printif($type, $filename, $msg = '✖ Not generated ')
{
echo ($filename === '' ? $msg . ' : ' . $type : "✔ {$filename}") . "\n";
}
class CodeGeneratorCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'code:generator {model : Model with namespace} ' .
'{--w|views : View files} ' .
'{--i|inertiaviews : Inertia View files} ' .
'{--c|controller : Controller} ' .
'{--a|api : Creates API Controller} ' .
'{--r|routes : Display Routes} ' .
'{--f|factory : Factory} ' .
'{--t|tests : Feacture Test} ' .
'{--A|all : All Files}' .
'{--F|force : Overwrite files if exists} ' .
'{--auth : Auth (not included in all)} ' .
'{--event= : Event (not included in all)} ' .
'{--notification= : Notification (not included in all)} ' .
'{--theme=basic : Theme}';
protected $description = 'Multiple files generation';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$model = $this->argument('model');
if (!$model) {
return;
}
$m = ModelLoader::load($model);
$folder = CodeHelper::plural(CodeHelper::snake($m->name));
$folderCap = ucfirst($folder);
$force = $this->option('force');
//Options
$factory = $this->option('factory');
$controller = $this->option('controller');
$routes = $this->option('routes');
$views = $this->option('views');
$inertiaViews = $this->option('inertiaviews');
$api = $this->option('api');
$tests = $this->option('tests');
$auth = $this->option('auth');
$event = $this->option('event');
$notification = $this->option('notification');
$all = $this->option('all');
$theme = $this->option('theme');
if ($all) {
$factory = $controller = $routes = $views = $api = $tests = $all;
}
$request = ($controller || $api);
$options = compact(['factory', 'controller', 'routes', 'views', 'inertiaviews', 'api', 'tests', 'auth', 'request', 'notification', 'event']);
if ($controller) {
printif('Web Controller', CodeGenerator::generate($m, $theme . '/Http/Controllers/ModelController', "app/Http/Controllers/{$m->name}Controller.php", $force, $options));
}
if ($api) {
printif('API Controller', CodeGenerator::generate($m, $theme . '/Http/Controllers/API/ModelController', "app/Http/Controllers/API/{$m->name}Controller.php", $force, $options));
}
if ($request) {
printif('Form Request', CodeGenerator::generate($m, $theme . '/Http/Requests/ModelPostRequest', "app/Http/Requests/{$m->name}PostRequest.php", $force, $options));
}
if ($views) {
printif('Create View', CodeGenerator::generate($m, $theme . '/create', "resources/views/{$folder}/create.blade.php", $force, $options));
printif('Edit View', CodeGenerator::generate($m, $theme . '/edit', "resources/views/{$folder}/edit.blade.php", $force, $options));
printif('Index View', CodeGenerator::generate($m, $theme . '/index', "resources/views/{$folder}/index.blade.php", $force, $options));
printif('Show View', CodeGenerator::generate($m, $theme . '/show', "resources/views/{$folder}/show.blade.php", $force, $options));
}
if ($inertiaViews) {
printif('Create View', CodeGenerator::generate($m, $theme . '/create', "resources/js/Pages/{$folderCap}/create.vue", $force, $options));
printif('Edit View', CodeGenerator::generate($m, $theme . '/edit', "resources/js/Pages/{$folderCap}/edit.vue", $force, $options));
printif('Index View', CodeGenerator::generate($m, $theme . '/index', "resources/js/Pages/{$folderCap}/index.vue", $force, $options));
printif('Show View', CodeGenerator::generate($m, $theme . '/show', "resources/js/Pages/{$folderCap}/show.vue", $force, $options));
}
if ($factory) {
printif('Factory ', CodeGenerator::generate($m, $theme . '/database/factories/ModelFactory', "database/factories/{$m->name}Factory.php", $force, $options));
}
if ($tests) {
printif('Feature Test Controller', CodeGenerator::generate($m, $theme . '/tests/Feature/Http/Controllers/ModelControllerTest', "tests/Feature/Http/Controllers/{$m->name}ControllerTest.php", $force, $options));
printif('Feature Test API Controller', CodeGenerator::generate($m, $theme . '/tests/Feature/Http/Controllers/API/ModelControllerTest', "tests/Feature/Http/Controllers/API/{$m->name}ControllerTest.php", $force, $options));
}
if ($notification) {
printif('Notification', CodeGenerator::generate($m, $theme . '/Notifications/ModelNotification', "app/Notifications/{$notification}.php", $force, $options));
}
if ($event) {
printif('Event', CodeGenerator::generate($m, $theme . '/Events/ModelEvent', "app/Events/{$event}.php", $force, $options));
}
if ($routes) {
echo CodeGenerator::generate($m, $theme . '/routes') . "\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment