Centum\Console
The Console component makes it easy to develop command line applications in Centum.
Application endpoints are treated as Centum\Console\Command
objects. These Commands contain all of the code and all of the metadata is stored in a Centum\Console\CommandMetadata
object.
Centum\Console\Application
extracts the command name from $argv
, finds the appropriate Command, and then executes the Command’s code.
Centum\Console\Application
implements Centum\Interfaces\Console\ApplicationInterface
.
Constructor
Centum\Console\Application(
Centum\Interfaces\Container\ContainerInterface $container
);
public function getCommandMetadata(class-string<Centum\Interfaces\Console\CommandInterface> $commandClass): Centum\Console\CommandMetadata
Retrieve metadata for a command.public function addCommand(class-string<Centum\Interfaces\Console\CommandInterface> $commandClass): void
Register a new command.public function getCommands(): array<string, class-string<Centum\Interfaces\Console\CommandInterface>>
List all registered commands.public function addExceptionHandler(class-string<Centum\Interfaces\Console\ExceptionHandlerInterface> $exceptionClass): void
Register an exception handler for console errors.public function handle(Centum\Interfaces\Console\TerminalInterface $terminal): int
Run the console application.
Default Commands
The following code snippets assume that the console application will be stored in cli.php
.
Centum\Console\Command\ListCommand
Will list all registered Centum\Console\Command
objects:
php cli.php list
Centum\Console\Command\QueueConsumeCommand
Will take the next available Task from the Queue and consume it (see Queue docs):
php cli.php queue:consume
Example: Defining a Custom Command
use Centum\Console\CommandMetadata;
use Centum\Interfaces\Console\CommandInterface;
use Centum\Interfaces\Console\TerminalInterface;
#[CommandMetadata("greet")]
class GreetCommand implements CommandInterface
{
public function execute(TerminalInterface $terminal): int
{
$terminal->writeLine("Hello from Centum Console!");
return self::SUCCESS;
}
}