splash/console
Splash Console is a lightweight PHP/Laravel package for building and running console commands with clean output, structured arguments/options, and simple command registration—ideal for automation tasks, maintenance scripts, and developer tooling.
Installation Add the package via Composer:
composer require splash/console
Publish the configuration (if needed):
php artisan vendor:publish --provider="Splash\Console\ConsoleServiceProvider"
First Use Case Register a basic console command:
use Splash\Console\ConsoleCommand;
class ExampleCommand extends ConsoleCommand
{
protected $signature = 'example:greet {--name=World}';
protected $description = 'A simple greeting command';
public function handle()
{
$this->info("Hello, {$this->option('name')}!");
}
}
Register the command in app/Console/Kernel.php:
protected $commands = [
\App\Console\Commands\ExampleCommand::class,
];
Run it:
php artisan example:greet --name=Laravel
Key Files to Explore
config/console.php (if published)app/Console/Kernel.php (command registration)app/Console/Commands/ (custom command directory)Splash\Console\ConsoleCommand for reusable base functionality (e.g., logging, input validation).$signature and $description for CLI help generation (php artisan help).$arguments and $options for dynamic input handling:
protected $signature = 'user:create {name} {--email=} {--role=admin}';
ask(), confirm(), or choice() for user input:
$name = $this->ask('What is your name?');
if ($this->confirm('Proceed?')) {
$this->info("Confirmed: {$name}");
}
$bar = $this->output->createProgressBar(100);
for ($i = 0; $i < 100; $i++) {
$bar->advance();
}
$bar->finish();
$this->info(), $this->error(), or $this->table() for formatted output:
$this->table(['Name', 'Email'], [['John', '[email protected]']]);
ConsoleServiceProvider:
$this->app->bind('custom.service', function () {
return new CustomService();
});
Inject into commands:
public function __construct(CustomService $service) {
$this->service = $service;
}
// In command
event(new CommandExecuted($this->getName()));
// In EventServiceProvider
protected $listen = [
CommandExecuted::class => [CommandListener::class],
];
Illuminate\Console\Application:
use Splash\Console\ConsoleApplication;
class CustomArtisan extends ConsoleApplication { ... }
Output Buffering
$this->line() or $this->output->flush() to force output.$this->output->setDecorated(false); // Disable ANSI colors if needed
Signature Conflicts
vendor:package:command).Error Handling
try/catch or throw new \Exception() for graceful failures:
try {
// Risky operation
} catch (\Exception $e) {
$this->error($e->getMessage());
}
Configuration Overrides
config/console.php) may not auto-load. Ensure it’s merged in config/app.php under console:
'console' => [
'timeout' => env('CONSOLE_TIMEOUT', 30),
],
php artisan command --verbose
\Log::debug('Debug message', ['context' => 'command']);
$this->comment("Step 1: Starting...");
Custom Helpers
Extend ConsoleCommand to add reusable methods:
class BaseCommand extends ConsoleCommand
{
protected function logAction(string $action): void
{
$this->info("[{$this->getName()}] {$action}");
}
}
Pre/Post Hooks
Use booting() and booted() in ConsoleServiceProvider for lifecycle events:
public function booting()
{
$this->app->singleton('command.hook', function () {
return new CommandHook();
});
}
Testing Commands
Use Laravel’s Artisan facade in tests:
public function test_example_command()
{
$this->artisan('example:greet --name=Test')
->expectsOutput('Hello, Test!')
->assertExitCode(0);
}
Environment-Specific Commands
Restrict commands to specific environments in Kernel.php:
protected $shouldRunInConsole = [
'local' => ['example:local-only'],
'production' => ['example:prod-only'],
];
How can I help you explore Laravel packages today?