Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Random Command Laravel Package

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).

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require spatie/laravel-random-command
    

    Publish the config file (optional):

    php artisan vendor:publish --provider="Spatie\RandomCommand\RandomCommandServiceProvider"
    
  2. 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.


Where to Look First

  • Config File: config/random-command.php (if published) – Customize excluded commands or command weights.
  • Kernel.php: app/Console/Kernel.php – Verify which commands are registered for random selection.
  • Command Output: Check the --help flag for the random command to see available options:
    php artisan random --help
    

Implementation Patterns

Usage Patterns

  1. Basic Execution:

    php artisan random
    

    Randomly picks and runs a command from the registered list.

  2. Excluding Commands: Exclude specific commands in config/random-command.php:

    'exclude' => [
        'migrate',
        'migrate:fresh',
    ],
    
  3. Weighted Selection: Assign weights to commands in config/random-command.php to influence selection probability:

    'weights' => [
        'migrate' => 10,
        'queue:work' => 1,
    ],
    
  4. Custom Command Groups: Register commands in Kernel.php under a specific group (e.g., group:testing) and filter them via config:

    'groups' => [
        'testing',
    ],
    
  5. Integration with CI/CD: Use in scripts to randomly execute commands during testing or deployment checks:

    php artisan random --group=testing
    

Workflows

  1. 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.)

  2. Debugging: Combine with --verbose to debug command execution:

    php artisan random --verbose
    
  3. Automated Testing: Integrate into PHPUnit tests to randomly validate command behavior:

    public function testRandomCommandExecution()
    {
        $this->artisan('random')
             ->expectsQuestion('Confirm?', 'yes')
             ->assertExitCode(0);
    }
    

Integration Tips

  1. 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.

  2. Dynamic Command Lists: Fetch commands dynamically (e.g., from a database) by extending the RandomCommand class and overriding getCommands().

  3. Environment-Specific Config: Use environment variables to toggle random command execution:

    'enabled' => env('RANDOM_COMMAND_ENABLED', true),
    

Gotchas and Tips

Pitfalls

  1. 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
    
  2. Infinite Loops: Avoid commands that trigger other random commands (e.g., a command that calls php artisan random). Exclude such commands explicitly.

  3. 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.

  4. 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
    

Debugging

  1. List Available Commands: Use the --list flag to see the pool of selectable commands:

    php artisan random --list
    
  2. Log Selection: Enable debug mode in config/random-command.php:

    'debug' => true,
    

    This logs the selected command and its weight.

  3. Manual Override: Force a specific command for testing:

    php artisan random --command=queue:work
    

Config Quirks

  1. Case Sensitivity: Command names in exclude or weights are case-sensitive. Use the exact name from php artisan list.

  2. Wildcards: The package doesn’t support wildcards (e.g., migrate:*). Exclude/include commands individually.

  3. Hidden Commands: Commands marked as hidden in Kernel.php ($hidden) are excluded by default. Override in config if needed:

    'include-hidden' => true,
    

Extension Points

  1. Custom Command Resolver: Override Spatie\RandomCommand\RandomCommand::getCommands() to fetch commands from an external source (e.g., API).

  2. Post-Execution Hooks: Extend the executeRandomCommand method to run logic after command execution (e.g., notifications, analytics).

  3. Command Metadata: Add metadata (e.g., tags, categories) to commands and filter by metadata in the random selection logic.

  4. Rate Limiting: Implement rate limiting to avoid running the same command repeatedly:

    'rate-limit' => [
        'commands' => ['queue:work', 'schedule:run'],
        'max-per-hour' => 1,
    ],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport