Create helper command using stub and command

 First of all create stub folder inside the root director like below image


After that create a helper.stub file inside the stub folder

helper.stub

<?php
namespace App\Helpers;

class {{class}}
{

}

Now create a HelperCommand  using below command

php artisan make:command HelperCommand

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Pluralizer;
use Symfony\Component\CssSelector\Node\FunctionNode;

class HelperCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:helper {className}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Make a helper class';

    /**
     * Execute the console command.
     *
     * @return int
     */

    protected $files;

    public function __construct(Filesystem $files)
    {
        parent::__construct();
        $this->files = $files;
    }


    protected function makeDirectory($path)
    {

        if (!$this->files->isDirectory($path)) {
            $this->files->makeDirectory($path, 0777, true, true);
        }

        return $path;
    }

    public function  getSingularClassName($className)
    {
        return ucwords(Pluralizer::singular($className));
    }

    public function getStubPath()
    {
        return __DIR__ . '/../../../stubs/helper.stub';
    }

    public function getStubVariables()
    {
        return [
            'class' => $this->getSingularClassName($this->argument('className'))
        ];
    }

    public function getSourceFile()
    {
        return $this->getStubContents($this->getStubPath(), $this->getStubVariables());
    }

    public function getStubContents($stub, $stubVariables = [])
    {
        $contents = file_get_contents($stub);

        foreach ($stubVariables as $search => $replace) {
            $contents = str_replace('{{' . $search . '}}', $replace, $contents);
        }

        return $contents;
    }

    public function getSourcefilePath()
    {
        return base_path('App\\Helpers') . '\\' . $this->getSingularClassName($this->argument('className')) . 'Helper.php';
    }


    public function handle()
    {
        $path = $this->getSourceFilePath();

        $this->makeDirectory(dirname($path));

        $contents = $this->getSourceFile();
        $file = $this->argument('className')."Helper";

        if (!$this->files->exists($path)) {
            $this->files->put($path, $contents);
            $this->info(" {$path} created successfully ");
            // echo "\033[31m : {$path} created 033[0m \n";
        } else {
            // $this->info("File : {$path} already exits");
            echo "\033[31m $file is already exist \033[0m \n";

        }
    }
}


Creating for new helper use below command 
php artisan make:heler My
Ouput of above command


Comments