Skip to content

Instantly share code, notes, and snippets.

@mhedges1
Last active June 5, 2020 02:50
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mhedges1/2811f1a55018edd0319f to your computer and use it in GitHub Desktop.
Save mhedges1/2811f1a55018edd0319f to your computer and use it in GitHub Desktop.
Quickly build boilerplate for your Laravel 5.1 application's resources. Builds a Model, Migration, Controller, FormRequests, Commands, and Events in one expression. Simply place this file into App/Console/Commands/ and add " \App\Console\Commands\Resource\SeedResource::class" to your $commands array in App\Console\Kernel.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
/**
* Class SeedResource
* @package App\Console\Commands\Resource
*/
class SeedResource extends Command
{
//RESTful Requests?
/**
* @var array
*/
protected $requests = [
'index',
'show',
'create',
'store',
'update',
'destroy'
];
//CRUD actions?
/**
* @var array
*/
protected $commands = [
'create',
'store',
'update',
'destroy'
];
//valid options for override
/**
* @var array
*/
protected $validOptions = [
'controller',
'requests',
'commands',
'events',
'listener',
'model',
'migration'
];
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:resource {name} {--except=null}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Quickly build boilerplate for your Laravel 5.1 application\'s sources. Builds a Model, Migration, Controller, FormRequests, Commands, and Events in one expression.';
/**
* Create a new command instance.
*
* @return \App\Console\Commands\Resource\SeedResource
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$name = studly_case($this->argument('name'));
$this->info('Scaffolding a '. $name . ' resource!');
$classesToBuild = $this->fetchUserOptions();
//make a controller
if(in_array('controller', $classesToBuild)){
$this->info('Generating a '. $name . ' Controller!');
$this->call('make:controller', ['name' => $this->makeClassName($name)]);
}
//make our requests
if(in_array('requests', $classesToBuild)){
$this->comment('Generating '. $name . ' Form Requests!');
$this->makeRequests($name);
}
//make our commands
if(in_array('requests', $classesToBuild)){
$this->comment('Generating '. $name . ' Commands!');
$this->makeCommands($name);
}
//make our events
if(in_array('events', $classesToBuild)) {
$this->comment('Generating ' . $name . ' Events!');
$this->makeEvents($name);
}
//make a listener
if(in_array('listener', $classesToBuild)) {
$this->comment('Generating ' . $name . ' Event Listener!');
$this->makeListener($name);
}
//make a model
if(in_array('model', $classesToBuild)) {
$this->comment('Generating ' . $name . ' Model!');
$this->makeModel($name);
}
//make a migration
if(in_array('migration', $classesToBuild)) {
$this->comment('Generating ' . $name . ' Migration!');
$this->makeMigration();
}
}
/**
* Make A Class Name
* @param $name
* @param string $type
* @param null $action
* @return string
*/
private function makeClassName($name, $type = 'controller', $action = null)
{
return $name.'/'.$name.studly_case($type).studly_case($action);
}
/**
* Get user options and return an array of options
* to allow a user to generate only the classes they wish to generate.
*/
private function fetchUserOptions(){
$except = $this->option('except');
if($except === "null") return $this->validOptions;
$except = explode(',', str_replace(' ', '', $except));
if(count(array_intersect($except, $this->validOptions)) === 0) {
$this->error("Invalid Options! Valid Options are ". implode(", ", $this->validOptions));
die();
}
return array_diff($this->validOptions, $except);
}
/**
* Make a form request
* @param $name
*/
private function makeRequests($name)
{
foreach ($this->requests as $action) {
$this->call('make:request', ['name' => $this->makeClassName($name, 'request', $action)]);
}
}
/**
* make a command
* @param $name
*/
private function makeCommands($name)
{
foreach ($this->commands as $action) {
$this->call('make:command', ['name' => $this->makeClassName($name, 'command', $action)]);
}
}
/**
* Generate an event
* @param $name
*/
private function makeEvents($name)
{
foreach ($this->commands as $action) {
$this->call('make:event', ['name' => $this->makeClassName($name, 'event', $action)]);
}
}
/**
* Generate a Listener
* @param $name
*/
private function makeListener($name)
{
$this->call('make:listener', ['name' => $this->makeClassName($name, null, 'listener')]);
}
/**
* Generate a model
* @param $name
*/
private function makeModel($name)
{
$this->call('make:model', ['name' => $this->makeClassName($name, null)]);
}
/**
* Generate a Migration
*/
private function makeMigration()
{
$schema_name = str_plural($this->argument('name'));
$this->call('make:migration', ['name' => 'create_' . $schema_name . '_table']);
}
}
@mhedges1
Copy link
Author

Note this simply makes the files for you. How you implement them is entirely up to you.

@mhedges1
Copy link
Author

mhedges1 commented Jul 1, 2015

I added an except option. i.e.

php artisan make:resource user --except="requests, listener, events"

this allows for more flexibility in what the tool actually generates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment