master

laravel/framework

Last updated at: 29/12/2023 09:20

ScheduleTestCommand.php

TLDR

This file contains the ScheduleTestCommand class, which is a command that allows running scheduled commands. It extends the Command class and is used for testing scheduled commands. The command can be executed with the schedule:test command name.

Methods

handle(Schedule $schedule)

This method is responsible for handling the execution of the console command. It takes an instance of the Schedule class as a parameter and performs the following actions:

  1. Retrieves the PHP binary path.
  2. Gets the list of scheduled commands.
  3. If there are no scheduled commands, displays a message indicating that no commands have been defined.
  4. If the --name option is provided, filters the scheduled commands based on the provided name.
    • If there is no exact match or multiple matches are found, displays a message informing that no matching scheduled command was found.
    • If a single match is found, sets the index to the match.
  5. If the --name option is not provided, prompts the user to select a command from a list.
  6. Gets the scheduled event based on the selected index.
  7. Determines the command to be executed based on the event type.
  8. Displays a summary of the command and whether it will run in the background.
  9. Executes the command in the Laravel container.
  10. If the event is not a callback event, displays the summary of the event.
  11. Inserts a new line.

getSelectedCommandByIndex(array $commandNames)

This method is used by the handle method to get the selected command name by index. It takes an array of command names as a parameter and returns the index of the selected command.

  1. Checks if there are any duplicate command names.
    • If there are duplicates, adds unique indexes to each command name in the form of "[index]".
    • Prompts the user to select a command from the list.
    • Extracts the selected command index from the chosen option and returns it.
  2. If there are no duplicates, prompts the user to select a command from the list.
    • Returns the index of the selected command in the original array.

Class

ScheduleTestCommand

This class extends the Command class and represents a console command used for testing scheduled commands. The command name is set to schedule:test. It includes the handle method for executing the command and the getSelectedCommandByIndex method for retrieving the selected command index.

<?php

namespace Illuminate\Console\Scheduling;

use Illuminate\Console\Application;
use Illuminate\Console\Command;
use Symfony\Component\Console\Attribute\AsCommand;

use function Laravel\Prompts\select;

#[AsCommand(name: 'schedule:test')]
class ScheduleTestCommand extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $signature = 'schedule:test {--name= : The name of the scheduled command to run}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run a scheduled command';

    /**
     * Execute the console command.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    public function handle(Schedule $schedule)
    {
        $phpBinary = Application::phpBinary();

        $commands = $schedule->events();

        $commandNames = [];

        foreach ($commands as $command) {
            $commandNames[] = $command->command ?? $command->getSummaryForDisplay();
        }

        if (empty($commandNames)) {
            return $this->components->info('No scheduled commands have been defined.');
        }

        if (! empty($name = $this->option('name'))) {
            $commandBinary = $phpBinary.' '.Application::artisanBinary();

            $matches = array_filter($commandNames, function ($commandName) use ($commandBinary, $name) {
                return trim(str_replace($commandBinary, '', $commandName)) === $name;
            });

            if (count($matches) !== 1) {
                $this->components->info('No matching scheduled command found.');

                return;
            }

            $index = key($matches);
        } else {
            $index = $this->getSelectedCommandByIndex($commandNames);
        }

        $event = $commands[$index];

        $summary = $event->getSummaryForDisplay();

        $command = $event instanceof CallbackEvent
            ? $summary
            : trim(str_replace($phpBinary, '', $event->command));

        $description = sprintf(
            'Running [%s]%s',
            $command,
            $event->runInBackground ? ' in background' : '',
        );

        $this->components->task($description, fn () => $event->run($this->laravel));

        if (! $event instanceof CallbackEvent) {
            $this->components->bulletList([$event->getSummaryForDisplay()]);
        }

        $this->newLine();
    }

    /**
     * Get the selected command name by index.
     *
     * @param  array  $commandNames
     * @return int
     */
    protected function getSelectedCommandByIndex(array $commandNames)
    {
        if (count($commandNames) !== count(array_unique($commandNames))) {
            // Some commands (likely closures) have the same name, append unique indexes to each one...
            $uniqueCommandNames = array_map(function ($index, $value) {
                return "$value [$index]";
            }, array_keys($commandNames), $commandNames);

            $selectedCommand = select('Which command would you like to run?', $uniqueCommandNames);

            preg_match('/\[(\d+)\]/', $selectedCommand, $choice);

            return (int) $choice[1];
        } else {
            return array_search(
                select('Which command would you like to run?', $commandNames),
                $commandNames
            );
        }
    }
}