Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create an artisan command for generating custom classes or files

What's the best way ( or maybe the way it's actually done ) of creating an artisan command for generating custom classes or files? Like php artisan make:console itself that creates a php class for our new artisan command.

From what I can think of, we have two options:

  • Add the template for that new file using php heredoc(or any string inside the new command's class file for that matter), which is really messy.

  • Put a template file somewhere, read it, replace what's necessary, and then create the new file. But I don't know where would be best to put the template file.

So is there a best-practice for handling this situation in Laravel? I googled it, but there was only articles and documentation for simple artisan command creation.

like image 302
Milad.Nozari Avatar asked Jan 21 '16 11:01

Milad.Nozari


People also ask

How do I make a custom artisan command?

To create a new artisan command, we can use the make:command artisan command. This command will make a new command class within the app/Console/Commands catalog. In case the directory does not exist in our laravel project, it'll be automatically made the primary time we run the artisan make:command command.

What are artisan commands?

Artisan is a command-line interface that Laravel provides which helps in making the production process fast and easy. Laravel has its own Command Line interface called Artisan. Its like a Linux command line but the commands are helpful for building a Laravel application.


2 Answers

Update 04/2020: Laravel 7 comes with a way to edit the default stubs to make changes to them and have Laravel pick up those changes. If you want to make a completely different stub to publish a totally different file the process below is appropriate otherwise look at the docs at the link below. https://laravel.com/docs/7.x/artisan#stub-customization


I know this question is a bit old but this is pretty easy if you just want to create a similar file that Laravel already does. (I wanted to create a job with some custom traits attached on creation)

So first look at the stubs Laravel comes with here on github.

Next, pick the stub of the type of class you want (I copied the job-queued stub) and paste it somewhere you can access in your app. I put mine inside App\Console\Stubs since that makes sense that commands will use the stubs.

After that, create your artisan command with php artisan make:command commandName.

Inside the command created use this file Illuminate\Console\GeneratorCommand. Now make your command extend this class instead of Command; This class is the class Laravel uses to create classes and it extends Command itself.

Inside your command create a few properties and methods as follows:

protected $name = 'make:custom-file'; The name of your command. This replaces $signature

protected $description = 'Command description.';

protected $type = 'Job'; Type of class to make

//location of your custom stub
protected function getStub()
{
    return  app_path().'/Console/Stubs/custom-job.stub';
}

//The root location the file should be written to
protected function getDefaultNamespace($rootNamespace)
{
    return $rootNamespace.'\Jobs';
}

//option flags if any see this for how it works
protected function getOptions()
{
    return [];
}

A full example of how the class should look is like this:

<?php

namespace App\Console\Commands;

use Illuminate\Console\GeneratorCommand;

class CustomJob extends GeneratorCommand
{

    /**
    * The name and signature of the console command.
    *
    * @var string
    */
    protected $name = 'make:custom';

    /**
    * The console command description.
    *
    * @var string
    */
    protected $description = 'Create a custom job.';

    /**
    * The type of class being generated.
    *
    * @var string
    */
    protected $type = 'Job';

    /**
    * Get the stub file for the generator.
    *
    * @return string
    */
    protected function getStub()
    {
        return  app_path().'/Console/Stubs/custom-job.stub';
    }

    /**
    * Get the default namespace for the class.
    *
    * @param  string  $rootNamespace
    * @return string
    */
    protected function getDefaultNamespace($rootNamespace)
    {
        return $rootNamespace.'\Jobs';
    }

    /**
    * Get the console command options.
    *
    * @return array
    */
    protected function getOptions()
    {
        return [];
    }
}

Once you run your custom artisan command it will write your custom stub to where you specify.

like image 171
abetwothree Avatar answered Nov 15 '22 15:11

abetwothree


Laravel uses .stub files as templates, and replaces the tokens inside the template.

Since you mentioned the make:console command, for reference you can take a look at the following files:

  • vendor/laravel/framework/src/Illuminate/Foundation/Console/stubs/console.stub
    (on github)
    This the template for making new console commands.
  • vendor/laravel/framework/src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
    (on github)
    This is the code that is executed when you run the php artisan make:console command.

If you want to take a look at packages that have done this, as well, a good example is the generators package by Jeffrey Way at Laracasts.

like image 45
patricus Avatar answered Nov 15 '22 17:11

patricus