Mon template de commande Symfony
Publié le 24/11/2019 • Mis à jour le 24/11/2019
On peut générer une commande avec le bundle Maker (php bin/console make:command
). J'utilise un template légérement différent ou le texte d'aide est généré dynamiquement avec des constantes. Celles-ci sont aussi utilisées en tant que paramétres des différentes options afin de ne pas avoir de code dupliqué. Supprimez le paramètre du constructeur, il est spécifique à ce blog (c'est un paramètre "nommé" dans le fichier services.yaml
). Pour lancer ce snippet, j'utiliserai cette fois un testeur de commandes (CommandTester
) au lieu d'utiliser le composant Process
.
<?php
declare(strict_types=1);
// 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)
{
parent::__construct();
$this->appVersion = $appVersion;
}
protected function configure(): void
{
[$namespace, $cmd, $desc, $class] = [self::NAMESPACE, self::CMD, self::DESC, self::class];
$this
->setName($namespace.':'.$cmd)
->setDescription(self::DESC)
->addArgument('arg1', InputArgument::OPTIONAL, 'Argument description')
->addOption('option1', null, InputOption::VALUE_NONE, 'Option description')
->setHelp(
<<<EOT
{$desc}
COMMAND:
<comment>{$class}</comment>
DEV:
<info>%command.full_name% -vv</info>
PROD:
<info>%command.full_name% --env=prod --no-debug</info>
EOT
);
}
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;
}
}
En bonus, le snippet permettant d'utiliser ce code : 🎉<?php
declare(strict_types=1);
namespace App\Controller\Snippet;
use App\Command\ShowVersionCommand;
use SensioLabs\AnsiConverter\AnsiToHtmlConverter;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Process;
/**
* J'utilise un trait PHP afin d'isoler chaque snippet dans un fichier.
* Ce code doit être apellé d'un contrôleur Symfony étendant AbstractController (depuis Symfony 4.2)
* ou Symfony\Bundle\FrameworkBundle\Controller\Controller (Symfony <= 4.1).
* Les services sont injectés dans le constructeur du contrôleur principal..
*
* @property KernelInterface $kernel
* @property ShowVersionCommand $showVersionCommand
*/
trait Snippet58Trait
{
public function snippet58(): void
{
$process = new Process([
'php',
$this->kernel->getProjectDir().'/bin/console',
'strangebuzz:version',
'--ansi',
]);
$process->run();
echo (new AnsiToHtmlConverter())->convert($process->getOutput()); // That's it! 😁
}
}
Exécuter le snippet ≪ this.showUnitTest ? this.trans.hide_unit_test : this.trans.show_unit_test ≫ Plus sur Stackoverflow Lire la doc Snippet aléatoire
<?php
declare(strict_types=1);
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
{
self::bootKernel();
$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.');
}
$app->add($this->sendSlackNotificationCommand);
$command = $app->find(SendSlackNotificationCommand::NAMESPACE.':'.SendSlackNotificationCommand::CMD);
$commandTester = new CommandTester($command);
$message = 'Hello World!';
$commandTester->execute([
'command' => $command->getName(),
'message' => $message,
]);
self::assertStringContainsStringIgnoringCase($message, $commandTester->getDisplay());
}
}