PHP
Joomla! 6
Joomla! 5
CLI
Advanced
Create a Custom Joomla CLI Command Extending AbstractCommand
Adds custom subcommands to php cli/joomla.php (e.g. example:sync) with SymfonyStyle output, arguments and options.
plugins/console/example/src/CliCommand/SyncCommand.php
<?php
declare(strict_types=1);
namespace Vendor\Plugin\Console\Example\CliCommand;
\defined('_JEXEC') or die;
use Joomla\Console\Command\AbstractCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
class SyncCommand extends AbstractCommand
{
protected static $defaultName = 'example:sync';
protected function configure(): void
{
$this->setDescription('Sincroniza registros externos com o Joomla.');
$this->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'Limite de itens', 50);
}
protected function doExecute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$limit = (int) $input->getOption('limit');
$io->title('Sincronização iniciada');
$io->success(sprintf('Processados até %d registros com sucesso.', $limit));
return Command::SUCCESS;
}
}
Always return Command::SUCCESS (0) or Command::FAILURE (1) from doExecute().
Technical explanation and best practices
Joomla console group plugins register custom commands into cli/joomla.php during ApplicationEvents::BEFORE_EXECUTE, perfect for ERP importers and batch workers.