Skip to content

Omega MVC — Console Package Manual ​

The Omega\Console package provides the CLI entry point for Omega applications. It bridges the Omega container with Symfony Console, offering attribute-based command declaration, lazy container resolution, a branded ASCII header, and a custom Style helper. Commands are discovered from framework and application directories, cached to a commands.php file, and resolved through the container on demand.

Installation ​

The package ships with the framework and is autoloaded via Composer:

bash
composer require omega-mvc/framework

Symfony Console components are required (symfony/console).

ConsoleApplication ​

ConsoleApplication is the top-level entry point. Call handle() with raw CLI arguments or a pre-built InputInterface:

php
use Omega\Application\ApplicationInterface;
use Omega\Console\ConsoleApplication;

$app->bootstrapWith([
    ConfigBootstrapper::class,
    FacadeBootstrapper::class,
    RegisterProviders::class,
    BootProviders::class,
]);

$console = new ConsoleApplication($app);
$exitCode = $console->handle();           // uses ArgvInput + ConsoleOutput
$exitCode = $console->handle($argv);     // explicit input array

Lifecycle ​

  1. bootstrap() runs configured bootstrappers (config, facades, providers).
  2. configureCommandLoader() scans src/Omega/Console/Commands/ and path.command (the application command directory) for *Command.php classes carrying the #[AsCommand] attribute. Results are cached to <app-cache-path>/commands.php when available.
  3. A CommandLoader is assigned to the Symfony Console Application for lazy resolution.
  4. The ConsoleBranding (extended Symfony Application) renders the ASCII logo and runtime info (environment, debug, PHP version, memory, command cache status) before delegating to Symfony's run loop.

Silent commands (list, help, completion, --version, --quiet) skip the header rendering.

The AsCommand attribute ​

Commands declare metadata via #[AsCommand] instead of overriding configure():

php
use Omega\Console\AbstractCommand;
use Omega\Console\Attribute\AsCommand;

#[AsCommand(
    name: 'app:clean',
    description: 'Clean stale cache entries',
    aliases: ['clean', 'app:flush'],
    hidden: false,
    arguments: [
        'age' => [Argument::REQUIRED, 'Max age in hours', 24],
    ],
    options: [
        'dry-run' => ['--dry-run', ValueOption::NONE, 'Preview without executing'],
    ],
)]
final class CleanCommand extends AbstractCommand
{
    public function __invoke(): int
    {
        $age = $this->getArgument('age');
        $dryRun = $this->getOption('dry-run');
        // ...
        return self::SUCCESS;
    }
}

AsCommand properties ​

PropertyTypeDescription
namestringCommand name (e.g. 'app:clean').
description?stringShort description shown in command list.
aliasesarray<int, string>Alternative invocation names.
hiddenboolHide from the command list.
argumentsarray<string, array>[mode, description, default?] per argument.
optionsarray<string, array>[shortcut, mode, description, default?, suggestedValues?] per option.

Argument/option modes use Symfony constants (Argument::REQUIRED, Argument::OPTIONAL, ValueOption::NONE, ValueOption::REQUIRED, etc.).

AbstractCommand ​

Base class for all Omega commands. Extends Symfony's Command and implements the single-method __invoke() pattern:

php
use Omega\Console\AbstractCommand;
use Omega\Console\Attribute\AsCommand;

#[AsCommand(name: 'hello', description: 'Say hello')]
final class HelloCommand extends AbstractCommand
{
    public function __invoke(): int
    {
        $name = $this->getArgument('name') ?? 'World';
        $this->io->info("Hello, {$name}!");
        return self::SUCCESS;
    }
}

Properties and methods ​

MemberDescription
$inputCurrent InputInterface instance.
$outputCurrent OutputInterface instance.
$ioStyle instance (styled console output).
$terminalSymfony Terminal instance.
$appThe Omega ApplicationInterface, set by the CommandLoader.
getArgument(string $key): mixedRead a command argument.
getOption(string $key): mixedRead a command option.
call(string $commandName, array $parameters = []): intInvoke another command programmatically.
__invoke()Abstract; implement command logic here.

The call() method ​

Runs another registered command internally, forwarding the current output:

php
$this->call('cache:clear');
$this->call('migrate:fresh', ['--force' => true]);

Errors are caught and displayed via $this->io->error(); returns self::FAILURE on failure.

Style ​

Omega\Console\Style extends Symfony's SymfonyStyle with Omega-specific formatting:

  • Automatic two-space indentation on all output lines.
  • Labelled message blocks: success(), error(), warning(), comment(), note(), info(), title(), section(), text().
  • ask() and confirm() with cyan ? prefix.
  • progressBar(int $max, string $message) — custom-styled progress bar with green bar characters and grey empty fill.

Spacing is managed via ensureTopSpacing() to avoid duplicate blank lines between output blocks.

ConsoleBranding ​

Extends Symfony's Application to render the Omega ASCII logo and a runtime info bar (environment, debug status, PHP version, memory usage, command cache status) before non-silent commands. The logo and info are skipped for list, help, completion, and --version/--quiet flags.

CommandLoader ​

Implements Symfony's CommandLoaderInterface. Commands are resolved lazily through the Omega container:

php
$loader = new CommandLoader($app, [
    'cache:clear'  => CacheClearCommand::class,
    'route:list'   => RouteListCommand::class,
]);
$command = $loader->get('cache:clear');  // container-resolved

The $app property is injected into AbstractCommand instances on resolution.

Attributes ​

AsCommand ​

Declares command metadata (see above). Processed via reflection at discovery and configuration time.

Make ​

Metadata attribute for "make" style commands that generate files from templates:

PropertyDescription
templatePath to the stub/template file.
pathContainer binding or key for the base save path.
patternPlaceholder in the template to replace.
suffixSuffix appended to the generated file name.
targetLogical target path for display/resolution.
infoSuccess message.
warningMessage when file already exists.
varsAdditional template variables [placeholder => transformation].

Traits ​

InteractsWithConsoleOutputTrait ​

Provides width-aware helpers for aligned console output:

MethodDescription
getTerminalWidth(): intCached terminal width in columns.
getVisibleWidth(string): intVisible width of an ANSI-decorated string.
getVisibleMaxWidth(array): intMaximum visible width from an array of strings.
writeRight(string, int $margin)Right-align a message against the terminal edge.
componentsTwoColumns(string $left, string $right)Dotted-fill two-column layout.

InteractWithFilesystemTrait ​

Recursively finds files using fnmatch() glob patterns:

php
$files = $this->findFiles('/path', '*.php', ['Test*.php']);

Hidden files and symlinks are skipped. Results are sorted by path.

Shipped commands ​

The framework ships with commands under Omega\Console\Commands\:

CommandDescription
cache:clearClear the cache
command:mapList all registered commands
command:clearClear the command cache
config:cacheCache the configuration
config:clearClear the configuration cache
cron:runRun all scheduled cron jobs
cron:listList all scheduled cron jobs
cron:workSimulate the cron scheduler in the terminal
database:createCreate the database
database:showShow database tables
database:wipeDrop all tables
maintenance:downPut application in maintenance mode
maintenance:upBring application out of maintenance mode
make:commandCreate a new console command
make:controllerCreate a new controller
make:exceptionCreate a new exception class
make:middlewareCreate a new middleware class
make:migrationCreate a new database migration
make:modelCreate a new model class
make:providerCreate a new service provider
make:seedCreate a new database seeder
make:viewCreate a new view template
migrate:freshDrop all tables and re-run migrations
migrate:initInitialize the migration system
migrate:refreshReset and re-run migrations
migrate:resetRollback all migrations
migrate:rollbackRollback the last migration
migrate:runRun pending migrations
migrate:statusShow migration status
package:discoverRebuild the cached package manifest
route:cacheCache the routes
route:clearClear the route cache
route:listList all registered routes
seedRun database seeders
serveStart the development server
vendor:publishPublish vendor assets
view:cacheCompile all view templates
view:clearClear compiled view templates
view:watchWatch view templates for changes
queue:workProcess queued jobs

Application commands placed in path.command (typically app/Console/Commands/) are discovered and merged with framework commands.

Exceptions ​

ExceptionWhen thrown
InvalidArgumentExceptionInvalid argument/option configuration in #[AsCommand]

The exception extends LogicException.

Reference ​

  • ConsoleApplication.php, AbstractCommand.php, CommandLoader.php
  • Style.php, ConsoleBranding.php
  • Attribute/AsCommand.php, Attribute/Make.php
  • Traits/InteractsWithConsoleOutputTrait.php, Traits/InteractWithFilesystemTrait.php
  • Commands/ — all shipped commands
  • Exceptions/InvalidArgumentException.php
  • Tests: tests/Unit/Console/
  • Dependencies: Symfony Console, Omega Container
  • License: GPL-3.0+