Skip to content

Commit fec8b4e

Browse files
CasEbbtaylorotwell
andauthored
Add --database option (#320)
* Add `--database` option * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8612fb7 commit fec8b4e

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/NewCommand.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ protected function configure()
4646
->addOption('branch', null, InputOption::VALUE_REQUIRED, 'The branch that should be created for a new repository', $this->defaultBranch())
4747
->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false)
4848
->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for')
49+
->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver your application will use')
4950
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Breeze / Jetstream stack that should be installed')
5051
->addOption('breeze', null, InputOption::VALUE_NONE, 'Installs the Laravel Breeze scaffolding')
5152
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
@@ -137,6 +138,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
137138
*/
138139
protected function execute(InputInterface $input, OutputInterface $output): int
139140
{
141+
$this->validateDatabaseOption($input);
140142
$this->validateStackOption($input);
141143

142144
$name = $input->getArgument('name');
@@ -448,8 +450,8 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
448450
{
449451
$defaultDatabase = 'sqlite';
450452

451-
if ($input->isInteractive()) {
452-
$database = select(
453+
if (! $input->getOption('database') && $input->isInteractive()) {
454+
$input->setOption('database', select(
453455
label: 'Which database will your application use?',
454456
options: [
455457
'mysql' => 'MySQL',
@@ -459,20 +461,19 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
459461
'sqlsrv' => 'SQL Server',
460462
],
461463
default: $defaultDatabase
462-
);
464+
));
463465

464-
if ($database !== $defaultDatabase) {
466+
if ($input->getOption('database') !== $defaultDatabase) {
465467
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
466468
}
467469
}
468470

469-
return [$database ?? $defaultDatabase, $migrate ?? false];
471+
return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? false];
470472
}
471473

472474
/**
473475
* Determine the stack for Breeze.
474476
*
475-
* @param \Symfony\Component\Console\Input\InputInterface $input
476477
* @return void
477478
*/
478479
protected function promptForBreezeOptions(InputInterface $input)
@@ -554,6 +555,18 @@ protected function promptForJetstreamOptions(InputInterface $input)
554555
))->each(fn ($option) => $input->setOption($option, true));
555556
}
556557

558+
/**
559+
* Validate the database driver input.
560+
*
561+
* @param \Symfony\Components\Console\Input\InputInterface
562+
*/
563+
protected function validateDatabaseOption(InputInterface $input)
564+
{
565+
if ($input->getOption('database') && ! in_array($input->getOption('database'), $drivers = ['mysql', 'mariadb', 'pgsql', 'sqlite', 'sqlsrv'])) {
566+
throw new \InvalidArgumentException("Invalid database driver [{$input->getOption('database')}]. Valid options are: ".implode(', ', $drivers).'.');
567+
}
568+
}
569+
557570
/**
558571
* Validate the starter kit stack input.
559572
*

0 commit comments

Comments
 (0)