Artisan command: make
Add additional command to “php artisan make:…” console command list
I’m making custom global scope in this example, but it can be easily adjusted for any other template.
Stubs
directory (unless you have it already)Location of this directory is up to you.
I located it in app\Console\, since this seemed logical to me.
Stubs
dir create your stub fileThis is a file, that will be used as auto-generated Scopes file.
Here is what I use:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php /** * IMPORTANT!!! no space between DummyNamespace and Scopes * Scopes can be replaced with other Dir name * used in step: 3 */ namespace App\ScopesDummyNamespace; use Illuminate\Database\Eloquent\Scope; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; class DummyClass implements Scope { /** * Apply the scope to a given Eloquent query builder. * * @param \Illuminate\Database\Eloquent\Builder $builder * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function apply(Builder $builder, Model $model) { // $builder->where('age', '>', 200); } } |
Please note:
- DummyNamespace, and
- DummyClass
… they will be replaced by names for class and namespace.
Both of Names will be deducted from your path, you put in your php artisan make command.
Besides pasting this stub file inside of Stubs dir, nothing needs to be done.
Now we actually need to make command, that will take care of doing all the magic, after you open console window and type: php artisan make: MyScope
First, lest use existing Artisan make command and create command template file:
1 2 3 |
php artisan make:command MakeScope |
This will place out command template file in app\Console\Commands dir:
Now, you can either compare your newly created template with file I will paste right below and apply changes, or replace piecemeal all, that you have in scope template.
Actually, we did not have to use console command making scope template, but just create file by hand, name it MakeScope (or any other name, if you do not like it) and paste this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; class MakeScope extends Command { /** * The name and signature of the console command. * note: * this is where you make your command structure * one, you will use in console, e.g.: * php artisan make:scope MyDir\Myscope * * @var string */ protected $signature = 'make:scope {path}'; /** * The console command description. * note: * this info will appear next to your command in, * when you list all commands available: * >> php artisan list * kind of quick help what is it and how to use it * content is up to you * * @var string */ protected $description = 'Create new global scope. Add scope classname with deep path, if desired'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { /*get passed argument*/ $path = $this->argument('path'); /*split path*/ $path_array = preg_split( "/[\\\\\/]/", $path); /*get classname*/ $className = studly_case(array_pop($path_array)); /*get namespace*/ $path_array = array_map(function ($a) { return studly_case($a); }, $path_array); $slash = empty($path_array) ? '' : '\\'; $nameSpace = $slash . studly_case(implode('\\', $path_array)); /*get stub file into a string*/ $stubString = file_get_contents(app_path('Console/Stubs/scope.stub')); /*replace dummies*/ $readyStub = str_replace(['DummyNamespace', 'DummyClass'], [$nameSpace, $className], $stubString); /*write scope file to location*/ Storage::disk('app')->put('Scopes/' . $nameSpace . '/' . $className . '.php', $readyStub); } |
Code is pretty much self-explanatory – just read in-code comments.
Except for one thing –
disc creation.app
Just put this code:
1 2 3 4 5 6 |
'app' => [ 'driver' => 'local', 'root' => app_path(), ], |
in Filesystem Disks
in file: config/filesystems.php
We do it in Kernel – see below:
Inside of Kernel, just add line of code in method: protected $commands = [], see below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ Commands\MakeScope::class, //<<<-- this is where registering of your command happens ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { //your cron schedules here } /** * Register the Closure based commands for the application. * * @return void */ protected function commands() { require base_path('routes/console.php'); } } |
How to use this is put on very top, but just to remind you:
1 2 3 4 5 6 7 |
> php artisan make:SomeScope //or, if you want to place scope in some deeper dir: > php artisan make:MyDir\MyOtherDir\MyScope |
app\Scopes\
dir.In case it does not (and you get some errors, or nothing happens), add it by hand here:
Unless I missed something, above step-by-step should get you working make shortcut accessible from Artisan console.