My Symfony command template

Published on 2019-11-24 • Modified on 2019-11-24

You can generate a command with the maker bundle (php bin/console make:command). I use a slightly different template where the help text is dynamically generated with constants. These are also used as parameters to set the various options, so you don't have duplicated code. Remove the constructor parameter as it is specific to this blog (It's a parameter bound in services.yaml). To run the command, I will use a CommandTester this time instead of using the Process component.



// src/Command/ShowVersionCommand.php

namespace App\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

 * Display the current application version. This is also the template I copy/paste
 * when havinf to write a new command.
final class ShowVersionCommand extends Command
    public const NAMESPACE = 'strangebuzz';
    public const CMD = 'version';
    public const DESC = 'Shows the current application version number.';

    private string $appVersion;

    public function __construct(string $appVersion)
        $this->appVersion = $appVersion;

    protected function configure(): void
        [$namespace, $cmd, $desc, $class] = [self::NAMESPACE, self::CMD, self::DESC, self::class];
            ->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
            ->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')


<info>%command.full_name% -vv</info>

<info>%command.full_name% --env=prod --no-debug</info>

    protected function execute(InputInterface $input, OutputInterface $output): int
        $io = new SymfonyStyle($input, $output);
        $io->note(self::NAMESPACE.' '.$this->appVersion);
        $io->success(' -> DONE!');

        return self::SUCCESS;
Bonus, the snippet to run this code: πŸŽ‰


namespace App\Controller\Snippet;

use App\Command\ShowVersionCommand;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Process;

 * I am using a PHP trait to isolate each snippet in a file.
 * This code should be called from a Symfony controller extending AbstractController (as of Symfony 4.2)
 * or Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
 * Services are injected in the main controller constructor.
 * @property KernelInterface    $kernel
 * @property ShowVersionCommand $showVersionCommand
trait Snippet58Trait
    public function snippet58(): void
        $process = new Process([

        echo (new AnsiToHtmlConverter())->convert($process->getOutput()); // That's it! 😁

 Run this snippet  β‰ͺ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫  More on Stackoverflow   Read the doc  Random snippet



namespace App\Tests\Controller\Snippets;

use App\Command\SendSlackNotificationCommand;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Tester\CommandTester;

 * @covers Snippet58Trait
final class Snippet58Test extends KernelTestCase
    private ?SendSlackNotificationCommand $sendSlackNotificationCommand;

    protected function setUp(): void
        $this->sendSlackNotificationCommand = self::$kernel->getContainer()->get('console.command.public_alias.'.SendSlackNotificationCommand::class);

     * @covers Snippet50Trait::snippet50
    public function testSnippet50(): void
        $app = new Application(self::$kernel);
        if (!$this->sendSlackNotificationCommand instanceof SendSlackNotificationCommand) {
            throw new \RuntimeException('Commande non trouvΓ©e.');
        $command = $app->find(SendSlackNotificationCommand::NAMESPACE.':'.SendSlackNotificationCommand::CMD);
        $commandTester = new CommandTester($command);
        $message = 'Hello World!';
            'command' => $command->getName(),
            'message' => $message,
        self::assertStringContainsStringIgnoringCase($message, $commandTester->getDisplay());