Installation:
composer require toolkit/cli-utils
Requires PHP 8.0+.
First Use Case:
use Toolkit\Cli\Color;
// Basic colored output
Color::println('Hello, Laravel!', 'success');
Where to Look First:
Workflow:
use Toolkit\Cli\CliApp;
// Define a command
$cmd = CliApp::new('deploy', 'Deploy Laravel assets');
$cmd->addOpt('dry-run', 'd', 'Simulate deployment');
$cmd->addOpt('force', 'f', 'Force overwrite');
$cmd->addArg('env', 'Target environment', true); // Required
$cmd->setHandler(function (CliApp $cmd) {
$env = $cmd->getArg('env');
$dryRun = $cmd->hasOpt('dry-run');
// Logic here (e.g., Artisan::call('deploy', ['env' => $env, '--dry-run' => $dryRun]))
Color::println("Deploying to $env", $dryRun ? 'warning' : 'success');
});
$cmd->run();
Integration Tips:
CliApp::help() to auto-generate --help output.Artisan for hybrid CLI tools:
$cmd->setHandler(fn($cmd) => Artisan::call('migrate', $cmd->getOpts()));
Patterns:
use Toolkit\Cli\Download;
$downloader = Download::file('https://example.com/large-file.zip', 'downloads/');
$downloader->setShowType('bar')->start(); // Visual feedback
use Toolkit\Cli\Util\Terminal;
Terminal::hideCursor();
// Render spinner...
Terminal::showCursor();
Laravel-Specific Use Cases:
Symfony/Console helpers (e.g., OutputFormatter) with Color for consistent styling.Highlighter in tinker or make:command scaffolding to display code snippets.Workflow:
use Toolkit\Cli\Util\Clog;
// Log levels: debug, info, warning, error, success
Clog::log('info', 'Database connection established');
Clog::log('error', 'Failed to migrate: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]);
Integration:
Log::channel('stack')->debug() with Clog for CLI-specific logs.Clog output to Laravel’s Log for unified logging:
Clog::setHandler(function ($level, $message, $context) {
Log::channel('cli')->$level($message, $context);
});
Use Case:
use Toolkit\Cli\Util\Highlighter;
// Highlight a Blade template or config file
$highlighted = Highlighter::create()->highlight(file_get_contents(resource_path('views/welcome.blade.php')));
echo $highlighted;
Laravel Integration:
Artisan::command() output for better debugging:
Artisan::command('inspect:view {view}', function ($view) {
echo Highlighter::create()->highlight(view()->file($view));
});
Color Compatibility:
Color uses ANSI escape codes. Test in Windows Terminal (may require enableVirtualTerminalProcessing for CMD).Color::isSupported() to check before rendering:
if (!Color::isSupported()) {
echo "Fallback: " . strip_tags($message);
}
CliApp Argument Parsing:
-f) override long options (--force) if values conflict.addOpt() with required: true and validate early:
$cmd->addOpt('port', 'p', 'Server port', 8000, true); // Default + required
if (!$cmd->hasOpt('port') || !is_numeric($cmd->getOpt('port'))) {
throw new \RuntimeException('Port must be numeric');
}
Terminal Control Quirks:
Terminal::clearScreen() may not work in SSH multiplexing (e.g., tmux).Terminal::savePosition()/restorePosition() for localized clearing.CliApp:
$cmd->setHandler(function (CliApp $cmd) {
var_dump($cmd->getOpts(), $cmd->getArgs());
});
Download:
$downloader->setDebug(true); // Shows raw HTTP responses
Highlighter:
Highlighter:
$highlighter = Highlighter::create()
->addRule('/\b(Artisan|Route)\b/', 'keyword');
Custom Color Themes:
Color::setTheme([
'success' => ['fg' => 'green', 'bold' => true],
'warning' => ['fg' => 'yellow', 'bg' => 'black'],
]);
CliApp Middleware:
$cmd->addMiddleware(function (CliApp $cmd) {
if ($cmd->hasOpt('verbose')) {
Clog::log('debug', 'Verbose mode enabled');
}
});
Download Hooks:
Download for custom progress logic:
$downloader->onProgress(function ($progress) {
if ($progress > 0.9) {
Color::println('Almost done!', 'warning');
}
});
Artisan Integration:
CliApp and Artisan input parsing (use one or the other).CliApp first, then pass to Artisan:
$args = $cmd->getRemainArgs();
Artisan::call('migrate', array_merge($cmd->getOpts(), ['--force' => $args]));
Service Provider:
CliApp to Laravel’s container for dependency injection:
$this->app->singleton(CliApp::class, fn() => CliApp::new('laravel:cli-utils'));
How can I help you explore Laravel packages today?