wp-starter/console
Laravel console utilities for WordPress starter projects. Provides commands and helpers to scaffold, manage, and automate common WP setup tasks from the CLI, streamlining development workflows when integrating WP into a Laravel-based setup.
Installation:
composer require wp-starter/console
Ensure wp-starter/support, wp-starter/collections, and wp-starter/macroable are also installed (they are dependencies).
First Command:
Register a basic command in app/Console/Kernel.php:
protected $commands = [
\WpStarter\Console\Commands\ExampleCommand::class,
];
(Note: The package lacks built-in commands; you’ll extend it.)
Create a Custom Command:
php artisan make:command MyCustomCommand
Extend \WpStarter\Console\Command in your new command:
namespace App\Console\Commands;
use WpStarter\Console\Command;
class MyCustomCommand extends Command
{
protected $signature = 'my:command {argument?}';
protected $description = 'A custom command example.';
public function handle()
{
$this->info('Command executed!');
}
}
Run It:
php artisan my:command
\WpStarter\Console\Command (extends Symfony’s Command).wp-starter/macroable to extend commands dynamically.wp-starter/collections for structured output (e.g., tables, lists).$this->ask(), $this->confirm(), $this->table()) for user interaction.public function handle()
{
$users = collect([['id' => 1, 'name' => 'John'], ['id' => 2, 'name' => 'Jane']]);
$this->table(['ID', 'Name'], $users->toArray());
}
app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('my:command')->daily();
}
Artisan::call() in controllers).\WpStarter\Console\Command::macro('logError', function ($message) {
$this->error("[ERROR] $message");
});
Use in commands:
$this->logError('Failed to process!');
use Symfony\Component\Process\Process;
$process = new Process(['ls', '-la']);
$process->run();
$this->info($process->getOutput());
(Dependency: symfony/process is included.)config('wpstarter.console') (if the package defines defaults).config/wpstarter.php:
return [
'timeout' => 30, // Example custom setting
];
Artisan facade in tests:
public function test_command()
{
$this->artisan('my:command')
->expectsQuestion('Confirm?', 'yes')
->assertExitCode(0);
}
No Built-in Commands:
Dependency Conflicts:
wp-starter/collections and wp-starter/support are required but may introduce version constraints. Test thoroughly.Macro Scope:
\WpStarter\Console\Command are not automatically available in child classes unless explicitly extended. Re-register macros in your custom commands if needed.Process Timeouts:
max_execution_time. Use symfony/process with timeouts:
$process = new Process(['php', 'artisan', 'queue:work']);
$process->setTimeout(60); // 60 seconds
Symfony Console Quirks:
Command class is strict about method signatures. Override configure() or execute() carefully:
// ❌ Avoid this (will fail):
public function execute(InputInterface $input, OutputInterface $output) { ... }
// ✅ Correct:
public function handle() { ... }
php artisan my:command --verbose
$process = new Process(['some', 'command']);
$process->run(function ($type, $buffer) {
$this->line("<info>$buffer</info>");
});
Custom Command Helpers: Create a trait for shared logic:
trait LogsCommands
{
protected function logStart()
{
$this->info("Starting at " . now()->toDateTimeString());
}
}
Use in commands:
use LogsCommands;
class MyCommand extends Command
{
use LogsCommands;
public function handle() { $this->logStart(); }
}
Event-Driven Commands: Listen for events and trigger commands:
// In an Event Service Provider
public function boot()
{
event(new \App\Events\OrderProcessed());
}
// In a listener
public function handle(OrderProcessed $event)
{
Artisan::call('orders:process', ['id' => $event->order->id]);
}
Dynamic Command Registration: Register commands programmatically (e.g., from a service provider):
$this->app->singleton(\WpStarter\Console\Command::class, function () {
return new class extends \WpStarter\Console\Command {
protected $signature = 'dynamic:command';
public function handle() { $this->info('Dynamic command!'); }
};
});
$this->table(['ID'], Model::query()->cursor()->map(fn ($item) => [$item->id]));
$cacheKey = 'command:results';
if (!$this->cache->has($cacheKey)) {
$results = $this->fetchData();
$this->cache->put($cacheKey, $results, now()->addHours(1));
}
How can I help you explore Laravel packages today?