Repository: symfony/symfony
File: src/Symfony/Bundle/SecurityBundle/Command/SecurityRoleHierarchyDumpCommand.php (PHP)

Code before:
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Core\Dumper\MermaidDumper;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;

/**
 * Command to dump the role hierarchy as a Mermaid flowchart.
 *
 * @author Damien Fernandes <damien.fernandes24@gmail.com>
 */
#[AsCommand(name: 'debug:security:role-hierarchy', description: 'Dump the role hierarchy as a Mermaid flowchart')]
class SecurityRoleHierarchyDumpCommand extends Command
{
    public function __construct(
        private readonly ?RoleHierarchyInterface $roleHierarchy = null,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setDefinition([
                new InputOption('direction', 'd', InputOption::VALUE_REQUIRED, 'The direction of the flowchart ['.implode('|', MermaidDumper::VALID_DIRECTIONS).']', MermaidDumper::DIRECTION_TOP_TO_BOTTOM),
                new InputOption('format', 'f', InputOption::VALUE_REQUIRED, 'The output format', 'mermaid'),
            ])
            ->setHelp(<<<'USAGE'
The <info>%command.name%</info> command dumps the role hierarchy in different formats.

<info>Mermaid</info>: %command.full_name% > roles.mmd
<info>Mermaid with direction</info>: %command.full_name% --direction=BT > roles.mmd
USAGE
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        if (null === $this->roleHierarchy) {
            $output->writeln('<comment>No role hierarchy is configured.</comment>');
            return Command::SUCCESS;
        }

        $direction = $input->getOption('direction');
        $format = $input->getOption('format');

        if ('mermaid' !== $format) {
            $output->writeln('<error>Only "mermaid" format is currently supported.</error>');
            return Command::FAILURE;
        }

Review comment (suggestion):
The command implementation being strongly tight to mermaid at the moment, I suggest not to add this format option until we consider supporting another format.

Code after:
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Security\Core\Dumper\MermaidDirectionEnum;
use Symfony\Component\Security\Core\Dumper\MermaidDumper;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;

/**
 * Command to dump the role hierarchy as a Mermaid flowchart.
 *
 * @author Damien Fernandes <damien.fernandes24@gmail.com>
 */
#[AsCommand(name: 'debug:security:role-hierarchy', description: 'Dump the role hierarchy as a Mermaid flowchart')]
class SecurityRoleHierarchyDumpCommand extends Command
{
    public function __construct(
        private readonly RoleHierarchyInterface $roleHierarchy,
    ) {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setDefinition([
                new InputOption(
                    'direction',
                    'd',
                    InputOption::VALUE_REQUIRED,
                    'The direction of the flowchart ['.implode('|', $this->getAvailableDirections()).']',
                    MermaidDirectionEnum::TOP_TO_BOTTOM->value,
                    $this->getAvailableDirections()
                ),
            ])
            ->setHelp(<<<'USAGE'
                The <info>%command.name%</info> command dumps the role hierarchy in Mermaid format.

                <info>Mermaid</info>: %command.full_name% > roles.mmd
                <info>Mermaid with direction</info>: %command.full_name% --direction=BT > roles.mmd
                USAGE
            )
        ;
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle($input, $output);

        if (null === $this->roleHierarchy) {
            $io->getErrorStyle()->writeln('<comment>No role hierarchy is configured.</comment>');

            return Command::SUCCESS;