Skip to content

Commit

Permalink
Fix for issues with closure-based scheduled commands in schedule:test (
Browse files Browse the repository at this point in the history
…#47862)

* Fix for Laravel schedule:test command issues

* fixed formatting issues

* fixed formatting issues

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
mobidev86 and taylorotwell authored Jul 27, 2023
1 parent df0563a commit 03564b1
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/Illuminate/Console/Scheduling/ScheduleTestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function handle(Schedule $schedule)

$index = key($matches);
} else {
$index = array_search($this->components->choice('Which command would you like to run?', $commandNames), $commandNames);
$index = $this->getSelectedCommandByIndex($commandNames);
}

$event = $commands[$index];
Expand All @@ -85,4 +85,31 @@ public function handle(Schedule $schedule)

$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 = $this->components->choice('Which command would you like to run?', $uniqueCommandNames);

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

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

0 comments on commit 03564b1

Please sign in to comment.