spatie/laravel-random-command
Adds a playful php artisan random command that picks and runs a random Artisan command for you. Also auto-schedules itself to run at random times. Mostly for fun—probably not something you want in production (or anywhere).
Installation:
composer require spatie/laravel-random-command
Publish the config file (optional):
php artisan vendor:publish --provider="Spatie\RandomCommand\RandomCommandServiceProvider"
First Use Case:
Run the random command to execute a random registered Artisan command:
php artisan random
By default, it picks from all registered commands in app/Console/Kernel.php under the $commands array.
config/random-command.php (if published) – Customize excluded commands or command weights.app/Console/Kernel.php – Verify which commands are registered for random selection.--help flag for the random command to see available options:
php artisan random --help
Basic Execution:
php artisan random
Randomly picks and runs a command from the registered list.
Excluding Commands:
Exclude specific commands in config/random-command.php:
'exclude' => [
'migrate',
'migrate:fresh',
],
Weighted Selection:
Assign weights to commands in config/random-command.php to influence selection probability:
'weights' => [
'migrate' => 10,
'queue:work' => 1,
],
Custom Command Groups:
Register commands in Kernel.php under a specific group (e.g., group:testing) and filter them via config:
'groups' => [
'testing',
],
Integration with CI/CD: Use in scripts to randomly execute commands during testing or deployment checks:
php artisan random --group=testing
Daily Development: Use as a quick way to test multiple commands without manual invocation:
php artisan random --group=testing --limit=3
(Runs 3 random commands from the testing group.)
Debugging:
Combine with --verbose to debug command execution:
php artisan random --verbose
Automated Testing: Integrate into PHPUnit tests to randomly validate command behavior:
public function testRandomCommandExecution()
{
$this->artisan('random')
->expectsQuestion('Confirm?', 'yes')
->assertExitCode(0);
}
Extend the Command:
Override the Spatie\RandomCommand\Commands\RandomCommand class to add custom logic (e.g., logging, pre/post hooks):
namespace App\Console\Commands;
use Spatie\RandomCommand\Commands\RandomCommand as BaseRandomCommand;
class CustomRandomCommand extends BaseRandomCommand
{
protected function executeRandomCommand(string $commandName)
{
// Custom logic before/after execution
parent::executeRandomCommand($commandName);
}
}
Register the custom command in app/Console/Kernel.php.
Dynamic Command Lists:
Fetch commands dynamically (e.g., from a database) by extending the RandomCommand class and overriding getCommands().
Environment-Specific Config: Use environment variables to toggle random command execution:
'enabled' => env('RANDOM_COMMAND_ENABLED', true),
Command Dependencies:
Some commands (e.g., migrate) may fail if prerequisites (e.g., database connections) aren’t met. Exclude them in config or handle errors gracefully:
'handle-failures' => true, // Silently skip failed commands
Infinite Loops:
Avoid commands that trigger other random commands (e.g., a command that calls php artisan random). Exclude such commands explicitly.
Command Registration Timing:
Commands registered via service providers after boot() may not appear in the random pool. Ensure all commands are registered early in the lifecycle.
Output Clutter:
Running php artisan random in a terminal may flood output. Use --quiet or pipe to a file:
php artisan random --quiet > random_output.log
List Available Commands:
Use the --list flag to see the pool of selectable commands:
php artisan random --list
Log Selection:
Enable debug mode in config/random-command.php:
'debug' => true,
This logs the selected command and its weight.
Manual Override: Force a specific command for testing:
php artisan random --command=queue:work
Case Sensitivity:
Command names in exclude or weights are case-sensitive. Use the exact name from php artisan list.
Wildcards:
The package doesn’t support wildcards (e.g., migrate:*). Exclude/include commands individually.
Hidden Commands:
Commands marked as hidden in Kernel.php ($hidden) are excluded by default. Override in config if needed:
'include-hidden' => true,
Custom Command Resolver:
Override Spatie\RandomCommand\RandomCommand::getCommands() to fetch commands from an external source (e.g., API).
Post-Execution Hooks:
Extend the executeRandomCommand method to run logic after command execution (e.g., notifications, analytics).
Command Metadata: Add metadata (e.g., tags, categories) to commands and filter by metadata in the random selection logic.
Rate Limiting: Implement rate limiting to avoid running the same command repeatedly:
'rate-limit' => [
'commands' => ['queue:work', 'schedule:run'],
'max-per-hour' => 1,
],
How can I help you explore Laravel packages today?