Console Commands

Create CLI commands for maintenance, data import, or automation tasks.

Basic Command

<?php
namespace Plugins\MyPlugin\Command;

use App\Core\Command\AbstractPluginCommand;
use Symfony\Component\Console\Attribute\AsCommand;
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;

#[AsCommand(
    name: 'my-plugin:greet',
    description: 'Greet a user by name',
)]
class GreetCommand extends AbstractPluginCommand
{
    protected function configure(): void
    {
        $this
            ->addArgument('name', InputArgument::REQUIRED, 'The name to greet')
            ->addOption('uppercase', 'u', InputOption::VALUE_NONE, 'Display in uppercase');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $name = $input->getArgument('name');
        $uppercase = $input->getOption('uppercase');

        $message = sprintf('Hello, %s!', $name);

        if ($uppercase) {
            $message = strtoupper($message);
        }

        $this->io->success($message);
        $this->logInfo('Greeted user', ['name' => $name]);

        return Command::SUCCESS;
    }
}

Command with Services

Interactive Commands

Progress Bars

Input Arguments

Usage:

Input Options

Usage:

Output Formatting

AbstractPluginCommand Features

The AbstractPluginCommand provides:

  • $this->io: SymfonyStyle for formatted output

  • $this->logInfo(): Log info messages

  • $this->logError(): Log error messages

  • $this->logWarning(): Log warning messages

Command Naming Convention

Commands must use {plugin-name}:{command} format:

  • my-plugin:greet

  • my-plugin:sync

  • paypal-payment:verify-webhook

Use kebab-case for both plugin name and command name.

Running Commands

Service Registration

Commands are auto-discovered by Symfony:

Last updated