Guide

CLI

Commands, YAML registration, and handing longer work off to the queue.

Create a command

A command implements CommandInterface with name(), description(), and run(array $args): int. Keep the command class focused on orchestrating a concrete task.

example.php
<?php

declare(strict_types=1);

namespace App\Console;

use Lemonade\Framework\Cli\CommandInterface;

final class AboutCommand implements CommandInterface
{
    public function name(): string
    {
        return 'about';
    }

    public function description(): string
    {
        return 'Display basic information about the application.';
    }

    public function run(array $args): int
    {
        unset($args);

        fwrite(STDOUT, "Application is running\n");

        return 0;
    }
}

Register and run a command

Register command classes in app/Config/Commands.yaml. In public documentation, use the canonical entry point vendor/bin/lemonade, for example vendor/bin/lemonade about.

example.php
module: commands
config:
  commands:
    - App\Console\AboutCommand

CLI tasks and the queue

Use commands for scheduled or operator-driven work. Hand longer follow-up processing off as a queue message and let a CLI worker finish it.

example.php
use Lemonade\Framework\Queue\QueueBusInterface;

final class PublishService
{
    public function __construct(
        private readonly QueueBusInterface $queue,
    ) {}

    public function publish(int $articleId): void
    {
        $this->queue->dispatch(new PublishArticleMessage($articleId));
    }
}