DEV Community

Cover image for Tutorial | Creating your Own Makers in Symfony 7 🛠️
Raziel Rodrigues
Raziel Rodrigues

Posted on • Edited on

10 2 1

Tutorial | Creating your Own Makers in Symfony 7 🛠️

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

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

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

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
    {
    }
}
Enter fullscreen mode Exit fullscreen mode

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!';
}

}
Enter fullscreen mode Exit fullscreen mode

At the final your directory should looks like:

Image description

Run

symfony console make list

and you will see your command at the list

Image description

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

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

Tiugo image

Fast, Lean, and Fully Extensible

CKEditor 5 is built for developers who value flexibility and speed. Pick the features that matter, drop the ones that don’t and enjoy a high-performance WYSIWYG that fits into your workflow

Start now

Top comments (0)

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay