To leverage Laravel Prompt's default "cancel" behavior when a user selects "Cancel" from your options, you can manually throw an exception that mimics the behavior of a CTRL + C cancellation. Laravel's console commands typically handle CTRL + C by throwing a Symfony\Component\Console\Exception\RuntimeException. You can use this to simulate the cancellation.
Here's how you can modify your selectModule method to achieve this:
use Symfony\Component\Console\Exception\RuntimeException;
protected function selectModule(): string
{
$modulesPath = base_path('modules');
// Get all module directories
$modules = Module::all()->pluck('name')->toArray();
$module = select(
label: 'What role should the user have?',
options: [...$modules, 'Cancel'],
default: 'Owner',
hint: 'The role may be changed at any time.'
);
if ($module === 'Cancel') {
// Simulate a CTRL + C cancellation
throw new RuntimeException('Command cancelled.');
}
return $module;
}
By throwing a RuntimeException, you can simulate the cancellation behavior, and Laravel's console will handle it similarly to a CTRL + C event, including the display of the cancellation message.