Generating File (Class) Using Template (Stub) In Laravel

Pramod Khatiwada
4 min readJul 15, 2021
php artisan make:command MyCommand

Laravel has a beautiful console command system that helps in many tasks, today I am going to talk about generator command but before dive into that, we need to understand commands in Laravel.

php artisan make:controller HomeController

Just by typing the above command, Laravel will create a file having a class in the right path where you want, and you can change the skeleton of the class by sending different flags to that command. How Laravel Do that, WANNA LEARN? And create your OWN. Keep Reading !!!

In one of my project, I had to create similar class again and again so I look into Laravel how they did it and Now I am to share with you.

I have divided this article into three parts, let's look into it.

  1. STUBS
  2. COMMAND
  3. GENERATOR COMMAND

STUBS

Stubs are template file for the class which we are going to create. There are different stubs used to generate, classes in Laravel. For example, to generate a controller there is the controller. stub.

A stub is a skeleton of the class. Few strings are unique for each controller (or class) like controller name, namespace, etc. In that case, we use DummyClass or {{ class }} in the stub. These strings will be replaced according to the input provided during the command execution. This replacing task will be taken care of by the generator command abstract class.

Stub Demo:

We have used DummyClass and DummyNamespace in our stub ‘makeprocedure. stub’

<?phpnamespace DummyNamespace;use App\Procedure\AbstractDataLayer;use Doctrine\ORM\Query\ResultSetMapping;use Doctrine\DBAL\DBALException;class DummyClass extends AbstractDataLayer{/*** Full definition of procedure*/protected $proc = '';/*** Input parameters in procedure which can be null*/protected $canBeNull = [];/***  Result Set Mapping*/protected $resultSetMapping = [];public function defineFunctionName(array $inputParameter = [], array $resultSetMapping = []){$rsm = $this->setResultSetMapping($resultSetMapping);$nativeQuery = $this->entityManager->createNativeQuery($this->    genProQuery($this->proc), $rsm);// set input parameter and execute query
}
}

COMMAND

Laravel console command is itself a huge topic. So we will try to understand just what we need in this article. It's awesome, that Laravel has provided the command to create a command. We can create it and use it according to our desire. To generate a command class in Laravel we can use

php artisan make:command MakeProcedure

The above command will create MakeProcedure class inside App/Command, it will extend the Command class. The class created in this way will have two attributes and two methods and they are:

> Attributes

protected $signature = 'make:procedure {name}';protected $description = 'This command will generate procedure class.';

The signature variable will hold the command name which we have to enter when we want to execute it. We can pass a different value at last, in this case, the last value will be store in the name variable.

php artisan make:procedure Testing

Here, Testing will be store in the name.

> Methods

The first one is a constructor and the other one is a handle. The handle method will be executed when we run our command. Our desired action needs to be inside the handle method.

This is How normal command works in Laravel. BUT we want to generate a file (class) inside a particular path and replace few things from stubs like the name of a class. So we need to understand about GeneratorCommand.

GENERATOR COMMAND

There is an abstract class called GeneratorCommand in the Laravel, which will handle most of the things like creating a file, executing the handle method (of command), and converting provided namespace into a file path where to create class, Loading stubs, Replacing DummyClass, and DummyNamespace from stubs everything is handled inside the generator command class.

So in our command class let's replace extends Command by extends GeneratorCommand and use a path to GeneratorCommand.

use Illuminate\Console\GeneratorCommand;class MakeProcedure extends GeneratorCommand {

> Attributes

Two attributes are the same as a normal commands (signature and description) we need to add another attribute.

protected $type = 'Procedure';

Value of type is used after the command is executed and giving info to command executer like (Procedure is created).

> Methods

Two methods are used, but they are different from the normal command class.

protected function getDefaultNamespace($rootNamespace){
return $rootNamespace.'\Procedure';
}
protected function getStub(){
return __DIR__ . '/stubs/makeprocedure.stub');
}

GetDefaultNamespace is providing the namespace where we want to create our class. (This namespace will be converted to the path, we do not have to worry about it is handled inside GeneratorCommand).

GetStub is providing a path where we have stored our template (stub).

Just by providing these few things we are done, I think this is the power of design pattern, so nice and clean.

Done, we can run our command,

php artisan make:procedure Testing

It will create a class using a stub, in App/Procedure/Testing.php.

Thank You.

--

--

Pramod Khatiwada

Pondering thoughts, questioning them, and trying to solve the mystery of life.