Si vous avez besoin de gérer des processus métier (tel qu’une gestion de commande, gestion de demande de congés ou encore des articles de publication) dans votre application Symfony, le composant Workflow pourrait bien vous être utile.
L’utilisation de ce composant est relativement simple, nous allons procéder par étapes :
- Création de votre entité (commande, congés, article, …)
- Configuration du Workflow
- Utilisation du Workflow
Pour utiliser notre workflow, on va déjà avoir besoin du composant Workflow :
composer require symfony/workflow
Création de l’entité
Pour la création de l’entité, modélisons une commande (d’une boutique en ligne). Elle aura deux propriétés : un numéro et un état, ce dernier étant utilisé pour le workflow.
class Commande
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?int $numeroCommande = null;
#[ORM\Column(length: 255)]
private ?string $etatCommande = null;
}
Configuration du Workflow
Voici la configuration de notre workflow dans le fichier workflow.yaml :
# config/packages/workflow.yaml
framework:
workflows:
commande:
type: state_machine
marking_store:
type: 'method'
property: 'etatCommande'
supports:
- App\Entity\Commande
initial_marking: brouillon
places:
- brouillon
- finalisee
- rejetee
- payee
transitions:
creation:
from: brouillon
to: finalisee
rejet:
from: finalisee
to: rejetee
payee:
from: finalisee
to: payee
Explications :
commande: Nom du workflow, utilisé pour l’injection de dépendance.marking_store: Référence à la propriété de l’entité stockant l’état (etatCommande).supports: L’entité concernée (Commandedans notre exemple).initial_marking: État initial de l’entité (brouillon).places: Liste des états possibles de l’entité (brouillon,finalisee, etc.).transitions: Transitions possibles entre les états (ex.creationdebrouillonàfinalisee).
Utilisation du Workflow
Créons une commande Symfony pour tester notre workflow :
<?php
namespace App\Command;
use App\Entity\Commande;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Workflow\WorkflowInterface;
#[AsCommand(
name: 'app:commande',
description: 'Test du workflow commande',
)]
class CommandeCommand extends Command
{
public function __construct(readonly WorkflowInterface $commandeStateMachine)
{
parent::__construct();
}
protected function configure(): void
{
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
// Création d'une commande
$comm = new Commande();
$comm->setNumeroCommande(1);
$comm->setEtatCommande('brouillon');
// Affichage de l'état initial
$io->text($comm->getEtatCommande());
// Test de la transition "creation"
$res = $this->commandeStateMachine->can($comm, 'creation');
$io->text($res);
// Application de la transition
$this->commandeStateMachine->apply($comm, 'creation');
$io->text($comm->getEtatCommande());
// Nouvelle tentative de transition "creation" (échoue)
$this->commandeStateMachine->apply($comm, 'creation');
return Command::SUCCESS;
}
}
Exportation du Workflow
Vous pouvez également exporter votre workflow sous forme d’image pour une meilleure visualisation :
bin/console workflow:dump commande | dot -Tpng -o workflow.png
Note : Pour exécuter
dot, installez Graphviz.
Voici un exemple d’image générée :

