Skip to content

Commit d340db3

Browse files
crynoboneStyleCIBottaylorotwell
authored
Add ability to select default database connection (#275)
* Add ability to select default database connection Signed-off-by: Mior Muhammad Zaki <[email protected]> * Apply fixes from StyleCI * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * wip Signed-off-by: Mior Muhammad Zaki <[email protected]> * formatting --------- Signed-off-by: Mior Muhammad Zaki <[email protected]> Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1029781 commit d340db3

File tree

1 file changed

+106
-13
lines changed

1 file changed

+106
-13
lines changed

src/NewCommand.php

Lines changed: 106 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -166,17 +166,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
166166
$directory.'/.env'
167167
);
168168

169-
$this->replaceInFile(
170-
'DB_DATABASE=laravel',
171-
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
172-
$directory.'/.env'
173-
);
169+
$database = $this->promptForDatabaseOptions($input);
174170

175-
$this->replaceInFile(
176-
'DB_DATABASE=laravel',
177-
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
178-
$directory.'/.env.example'
179-
);
171+
$this->configureDefaultDatabaseConnection($directory, $database, $name);
180172
}
181173

182174
if ($input->getOption('git') || $input->getOption('github') !== false) {
@@ -218,6 +210,81 @@ protected function defaultBranch()
218210
return $process->isSuccessful() && $output ? $output : 'main';
219211
}
220212

213+
/**
214+
* Configure the default database connection.
215+
*
216+
* @param string $directory
217+
* @param string $database
218+
* @param string $name
219+
* @return void
220+
*/
221+
protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name)
222+
{
223+
$this->replaceInFile(
224+
'DB_CONNECTION=mysql',
225+
'DB_CONNECTION='.$database,
226+
$directory.'/.env'
227+
);
228+
229+
if (! in_array($database, ['sqlite'])) {
230+
$this->replaceInFile(
231+
'DB_CONNECTION=mysql',
232+
'DB_CONNECTION='.$database,
233+
$directory.'/.env.example'
234+
);
235+
}
236+
237+
$defaults = [
238+
'DB_DATABASE=laravel',
239+
'DB_HOST=127.0.0.1',
240+
'DB_PORT=3306',
241+
'DB_DATABASE=laravel',
242+
'DB_USERNAME=root',
243+
'DB_PASSWORD=',
244+
];
245+
246+
if ($database === 'sqlite') {
247+
$this->replaceInFile(
248+
$defaults,
249+
collect($defaults)->map(fn ($default) => "# {$default}")->all(),
250+
$directory.'/.env'
251+
);
252+
253+
return;
254+
}
255+
256+
$defaultPorts = [
257+
'pgsql' => '5432',
258+
'sqlsrv' => '1433',
259+
];
260+
261+
if (isset($defaultPorts[$database])) {
262+
$this->replaceInFile(
263+
'DB_PORT=3306',
264+
'DB_PORT='.$defaultPorts[$database],
265+
$directory.'/.env'
266+
);
267+
268+
$this->replaceInFile(
269+
'DB_PORT=3306',
270+
'DB_PORT='.$defaultPorts[$database],
271+
$directory.'/.env.example'
272+
);
273+
}
274+
275+
$this->replaceInFile(
276+
'DB_DATABASE=laravel',
277+
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
278+
$directory.'/.env'
279+
);
280+
281+
$this->replaceInFile(
282+
'DB_DATABASE=laravel',
283+
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
284+
$directory.'/.env.example'
285+
);
286+
}
287+
221288
/**
222289
* Install Laravel Breeze into the application.
223290
*
@@ -277,6 +344,32 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
277344
$this->commitChanges('Install Jetstream', $directory, $input, $output);
278345
}
279346

347+
/**
348+
* Determine the default database connection.
349+
*
350+
* @param \Symfony\Component\Console\Input\InputInterface $input
351+
* @return string
352+
*/
353+
protected function promptForDatabaseOptions(InputInterface $input)
354+
{
355+
$database = 'mysql';
356+
357+
if ($input->isInteractive()) {
358+
$database = select(
359+
label: 'Which database will your application use?',
360+
options: [
361+
'mysql' => 'MySQL',
362+
'pgsql' => 'PostgreSQL',
363+
'sqlite' => 'SQLite',
364+
'sqlsrv' => 'SQL Server',
365+
],
366+
default: $database
367+
);
368+
}
369+
370+
return $database;
371+
}
372+
280373
/**
281374
* Determine the stack for Breeze.
282375
*
@@ -636,12 +729,12 @@ protected function replaceFile(string $replace, string $file)
636729
/**
637730
* Replace the given string in the given file.
638731
*
639-
* @param string $search
640-
* @param string $replace
732+
* @param string|array $search
733+
* @param string|array $replace
641734
* @param string $file
642735
* @return void
643736
*/
644-
protected function replaceInFile(string $search, string $replace, string $file)
737+
protected function replaceInFile(string|array $search, string|array $replace, string $file)
645738
{
646739
file_put_contents(
647740
$file,

0 commit comments

Comments
 (0)