GitHub

Spark CLI Tool

The TinyMVC framework includes Spark CLI, a command-line interface, providing helpful commands for development and maintenance tasks.

Basic Usage

Run Spark CLI commands using the following syntax:

php spark [command] [options] [arguments]

To see all available commands:

php spark help

Available Commands

Command Description Usage
help Show command help php spark help [command]
key:generate Generate a new encryption key php spark key:generate
make:controller Create a new controller class php spark make:controller UserController
make:middleware Create a new middleware class php spark make:middleware AuthMiddleware
make:migration Create a new database migration file php spark make:migration users
make:model Create a new model class php spark make:model User
make:provider Create a new service provider class php spark make:provider CustomServiceProvider
make:view Create a new view template php spark make:view users/profile
migrate:fresh Rollback all migrations and re-run them php spark migrate:fresh
migrate:run Run the database migrations php spark migrate:run
migrate:rollback Rollback to last migration (optional steps) php spark migrate:rollback [steps]
queue:clear Clear all queued jobs php spark queue:clear
queue:run Run the queued jobs php spark queue:run
route:list Show all available routes php spark route:list
serve Run PHP built-in development server php spark serve

Creating Custom Commands

You can easily add custom commands by defining them in router/commands.php:

<?php
use Spark\Console\Prompt;

command('greet', function (Prompt $prompt) {
    $name = $prompt->ask('What is your name?');
    $prompt->message("Hello, {$name}!", 'success');
})->description('Show a Greeting Message');

Command structure:

  1. command('name', callable) - Defines the command name and handler
  2. The handler will be injected with any dependencies automatically.
  3. ->description() - Sets the command description shown in help

Prompt Methods

The Prompt class provides these interaction methods:

$prompt->ask('Question'); // Basic input
$prompt->confirm('Proceed?'); // Yes/No confirmation
$prompt->message('Text', 'success'); // Colored output (success/error/info)

Command Structure

For more complex commands, you can create command classes:

command('complex:task', ComplexTaskCommand::class)
    ->description('Perform a complex task');

Where ComplexTaskCommand implements __invoke(array $args):

class ComplexTaskCommand {
    public function __construct(private Prompt $prompt) {
        
    }

    public function __invoke(array $args) {
        // Command logic here
    }
}

Best Practices

  • Keep command names consistent (verb:noun format)
  • Provide clear descriptions for all commands
  • Use the Prompt class for all user interaction
  • Group related commands in command classes when they become complex
  • Document your custom commands in your project documentation
Tip: You can chain additional methods to commands like ->help('Additional help text') to provide more detailed documentation for your custom commands.