Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@bayareawebpro
Last active October 13, 2019 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayareawebpro/150c43cdc2607547436fcd0e4e0b8839 to your computer and use it in GitHub Desktop.
Save bayareawebpro/150c43cdc2607547436fcd0e4e0b8839 to your computer and use it in GitHub Desktop.
Laravel Artisan Macros for Development

Artisan Macros for Development

Place in the routes/console.php file.

Install Application

The install macro will migrate / seed the database and link the storage directory.
Be sure to enter your database credentials in the .env file first.

php artisan install

Reset Application

The reset macro will flush the application caches and re-migrate & re-seed all the tables.

php artisan reset

Persist Application State

The persist macro will create seeders from the configured tables with "Laravel Iseed".

php artisan persist

Flush Application

The clear:all macro will flush all the application caches.

php artisan clear:all

Cache Application

The cache:all macro will prime & optimize all the application caches.

php artisan cache:all

Clear Storage Directories

The storage:clear macro will clear & reset the application storage directories.

php artisan storage:clear

Make Many Application Files

The make:many macro will prompt you to create common files for an entity: Tests (Http, Unit, Browser), Model, Migration, FormRequest, Controller, JsonResource, Policy, Observer...

php artisan make:many {name}
<?php
/*
|--------------------------------------------------------------------------
| Console Routes by Dan Alvidrez
|--------------------------------------------------------------------------
*/
use Illuminate\Support\Str;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Artisan;


$confirmed = 'yes';
$options = ['no', 'yes'];
$persistTables = [
    'links',
    'articles',
    'favorites',
    'ratings',
    'widgets',
    'interests',
    'interests_links',
    'interests_articles',
    'media',
];

/**
 * Make Many Application Files.
 */
Artisan::command('make:many {name}', function () use ($confirmed, $options){

    $this->alert("Making Application Files...");
    $allCommands = Artisan::all();

    $singular   = Str::ucfirst(Str::camel($this->argument('name')));
    $plural     = Str::ucfirst(Str::camel(Str::plural($this->argument('name'))));

    if($this->askWithCompletion("Tests", $options, $confirmed) === $confirmed){
        if($this->askWithCompletion("UnitTest", $options, $confirmed) === $confirmed){
            $this->call('make:test',array(
                'name' => "{$singular}Test",
                '--unit' => false
            ));
        }
        if($this->askWithCompletion("HttpTest", $options, $confirmed) === $confirmed){
            $this->call('make:test',array(
                'name' => "{$singular}Test",
                '--unit' => false
            ));
        }
        if(Arr::has($allCommands, 'dusk:make')){
            if($this->askWithCompletion("BrowserTest", $options, $confirmed) === $confirmed){
                $this->call('dusk:make',array(
                    'name' => "{$singular}Test"
                ));
            }
        }
    }
    if($this->askWithCompletion("Request", $options, $confirmed) === $confirmed) {
        $this->call('make:request', array(
            'name' => "{$plural}Request"
        ));
    }
    if($this->askWithCompletion("Controller", $options, $confirmed) === $confirmed) {
        $this->call('make:controller', array(
            "-r" => true,
            "name" => "{$plural}Controller"
        ));
    }
    if($this->askWithCompletion("JsonResource", $options, $confirmed) === $confirmed) {
        $this->call('make:resource', array(
            'name' => "{$plural}Resource"
        ));
    }
    if($this->askWithCompletion("Model", $options, $confirmed) === $confirmed) {
        $this->call('make:model', array(
            'name' => "{$singular}"
        ));
    }
    if($this->askWithCompletion("Migration", $options, $confirmed) === $confirmed){
        $this->call('make:migration',array(
            'name' => "Create{$plural}Table"
        ));
    }
    if($this->askWithCompletion("Policy", $options, $confirmed) === $confirmed){
        $this->call('make:policy',array(
            'name' => "{$singular}Policy"
        ));
    }
    if($this->askWithCompletion("Observer", $options, $confirmed) === $confirmed){
        $this->call('make:observer',array(
            'name' => "{$singular}Observer"
        ));
    }
    $this->info("Done.  Don't forget to add a route if needed.");

})->describe('MakeMany: Tests (Http, Unit, Browser), Model, Migration, FormRequest, Controller, JsonResource, Policy, Observer...');

/**
 * Clear the Application Caches & Logs
 */
Artisan::command('clear:all', function () {

    $this->alert("Flushing Application Caches & Storage...");
    $allCommands = Artisan::all();
    if(Arr::has($allCommands, 'telescope:clear')){
        $this->call('telescope:clear');
    }
    $this->call('config:clear');
    $this->call('cache:clear');
    $this->call('route:clear');
    $this->call('view:clear');
    $this->call('storage:clear');
    $this->call('clear-compiled');
    $this->info("Caches Cleared.");

})->describe('Clear the Application Caches...');

/**
 * Optimize the Application
 */
Artisan::command('cache:all', function (){

    $this->alert("Optimizing Application...");
    $this->call('config:cache');
    $this->call('route:cache');
    $this->call('view:cache');
    $this->call('optimize');
    $this->info("Application Caches Created.");

})->describe('Optimize the Application...');

/**
 * Delete the Application Storage Directories
 */
Artisan::command('storage:clear', function () use ($confirmed, $options) {

    if($this->askWithCompletion("Clear Storage?", $options, $confirmed) === $confirmed) {
        //Make Ignorable Directories.
        $directories = [
            'app/public',
            'logs'
        ];

        app('files')->cleanDirectory(storage_path('app'));

        foreach($directories as $path){
            app('files')->makeDirectory(storage_path($path));
            app('files')->put(storage_path("$path/.gitignore"),'*'.PHP_EOL.'!.gitignore');
        }
        $this->info("Storage Cleared.");
    }

})->describe('Clear the Storage Directories?!');

/**
 * Install the Application
 */
Artisan::command('install', function () {

    $this->alert("Installing Application...");
    $this->call('key:generate');
    $this->call('storage:link');
    $this->call('reset');
    $this->info("Install Done.");

})->describe('Install the Application...');

/**
 * Reset the Application State
 */
Artisan::command('reset', function (){

    $this->alert("Resetting Application...");
    $this->call('clear:all');
    $this->call('migrate:fresh');
    $this->call('db:seed');
    $this->info("Reset Done.");

})->describe('Reset the Application Entirely?!');

/**
 * Persist the Application State
 */
Artisan::command('persist', function () use ($persistTables){

    $this->alert("Persisting Application State with Seeders...");
    $this->call('iseed', [
        'tables' => implode(',', $persistTables),
        "--force" => true,
    ]);
    $this->info("Persistence Completed.");

})->describe('Persist the Application State...');


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