Creating your Own Makers in Symfony 7
Tags: php, symfony, webdev, tutorial
Published: 2024-04-07 14:40:40

Hello My friends! 👋

Today I gonna show you how to create your own make command inside Symfony.

I got this short description from the docs:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/clcfjlvjq3zf93x7m2pn.png)

Now you are about to learn how to create you own boilerplate code with just a command like when we do "symfony console make:entity".

This is amazing because with that tool you can generate the code with your own standards at an easy way, let's see how to craft it.

Firstly, you will need to create your new symfony application.

`symfony new /project --api`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nz3t08spgsziyga3z8g5.png)

After that, you will need to install the following package

`composer require symfony/maker-bundle --save-dev`

then run

`symfony console make list`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l8t7s6xmk3p11d1ee09p.png)

After running the command you will be able to see the list of commands available. When we create our class the command will appear within this list.

Now create a class extending the AbstractMaker

```
namespace App\Maker;

use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;

final class MakeNewClass extends AbstractMaker
{
    public static function getCommandName(): string
    {
        return 'make:newclass';
    }

    public static function getCommandDescription(): string
    {
        return 'Create a new class';
    }

    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
    {
        $command->setHelp(file_get_contents(__DIR__ . '/NewClass.txt'));
    }

    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
    {
        $generator->generateClass(
            'App\Maker\NewClass',
            __DIR__ .  '/NewClass.tpl.php'
        );

        $generator->writeChanges();
        $this->writeSuccessMessage($io);
    }

    public function configureDependencies(DependencyBuilder $dependencies): void
    {
    }
}
```

Now create a NewClass.txt file and put your text there, you can pass variables with this notation %command.name% and use proper html.

`The <info>%command.name%</info> command generates a Class!`

Next create a template file NewClass.tpl.php and put the following content (You are able to pass variables)

```
<?= "<?php\n" ?>

namespace src\Maker;

class NewClass {

public function hello() {
   echo 'Hello World!';
}

}
```

At the final your directory should looks like:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nqxqgpq2a7brs0d0gpq.png)

Run

`symfony console make list`

and you will see your command at the list

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dr0g5rwh5c22beai2jwq.png)

Then run

`symfony console make:newclass`

Fantastic! the code has been generated based on the template file!

Now you can proceed with your ideas and see other options and ways to do boilerplate code files. I expect this article helped you of dealing with this nice feature from Symfony, which can help on personal project or at work.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6xi4igm1bi0848ox9k7z.png)

The repositorie with the full example:
https://github.com/RazielRodrigues/30-tips-of-php/tree/main/tip5

I suggest to take a look at the MakerBundle, then you can have more examples:
https://github.com/symfony/maker-bundle