From 373ae2ce85883bcf24bfed46a9dba0f74d25794d Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Fri, 18 Aug 2023 23:01:26 +0800 Subject: [PATCH 01/10] Add ability to select default database connection Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 58 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 04d96175..db25f2bb 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -164,17 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $directory.'/.env' ); - $this->replaceInFile( - 'DB_DATABASE=laravel', - 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), - $directory.'/.env' - ); - - $this->replaceInFile( - 'DB_DATABASE=laravel', - 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), - $directory.'/.env.example' - ); + $this->configureDefaultDatabaseConnection($directory, $name); } if ($input->getOption('git') || $input->getOption('github') !== false) { @@ -216,6 +206,52 @@ protected function defaultBranch() return $process->isSuccessful() && $output ? $output : 'main'; } + /** + * Configure the default database connection + * + * @param string $directory + * @param string $name + * @return void + */ + protected function configureDefaultDatabaseConnection(string $directory, string $name) + { + $database = select( + label: 'Choose Database Driver', + options: [ + 'mysql' => 'MySQL', + 'sqlite' => 'SQLite', + 'pgsql' => 'PostgreSQL', + ], + default: 'mysql' + ); + + $this->replaceInFile( + 'DB_CONNECTION=mysql', + 'DB_CONNECTION='.$database, + $directory.'/.env' + ); + + if ($database === 'sqlite') { + $this->replaceInFile( + 'DB_DATABASE=laravel', + '# DB_DATABASE=', + $directory.'/.env' + ); + } else { + $this->replaceInFile( + 'DB_DATABASE=laravel', + 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), + $directory.'/.env' + ); + + $this->replaceInFile( + 'DB_DATABASE=laravel', + 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), + $directory.'/.env.example' + ); + } + } + /** * Install Laravel Breeze into the application. * From 1f8e4235135c19bcc21ceda62065b3a2d49e6b5a Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 18 Aug 2023 15:02:08 +0000 Subject: [PATCH 02/10] Apply fixes from StyleCI --- src/NewCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index db25f2bb..a589e79d 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -207,10 +207,10 @@ protected function defaultBranch() } /** - * Configure the default database connection + * Configure the default database connection. * - * @param string $directory - * @param string $name + * @param string $directory + * @param string $name * @return void */ protected function configureDefaultDatabaseConnection(string $directory, string $name) From fb9e74f422cf5e95774e3e9e1988bf97fc51c337 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 19 Aug 2023 10:27:55 +0800 Subject: [PATCH 03/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index db25f2bb..6584c434 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -164,7 +164,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $directory.'/.env' ); - $this->configureDefaultDatabaseConnection($directory, $name); + $database = $this->promptForDatabaseOptions($input); + + $this->configureDefaultDatabaseConnection($directory, $database, $name); } if ($input->getOption('git') || $input->getOption('github') !== false) { @@ -209,22 +211,13 @@ protected function defaultBranch() /** * Configure the default database connection * - * @param string $directory - * @param string $name + * @param string $directory + * @param string $database + * @param string $name * @return void */ - protected function configureDefaultDatabaseConnection(string $directory, string $name) + protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name) { - $database = select( - label: 'Choose Database Driver', - options: [ - 'mysql' => 'MySQL', - 'sqlite' => 'SQLite', - 'pgsql' => 'PostgreSQL', - ], - default: 'mysql' - ); - $this->replaceInFile( 'DB_CONNECTION=mysql', 'DB_CONNECTION='.$database, @@ -309,6 +302,31 @@ protected function installJetstream(string $directory, InputInterface $input, Ou $this->commitChanges('Install Jetstream', $directory, $input, $output); } + /** + * Determine the default database connection. + * + * @param \Symfony\Component\Console\Input\InputInterface $input + * @return void + */ + protected function promptForDatabaseOptions(InputInterface $input) + { + $database = 'mysql'; + + if ($input->isInteractive()) { + $database = select( + label: 'Choose Database Driver', + options: [ + 'mysql' => 'MySQL', + 'sqlite' => 'SQLite', + 'pgsql' => 'PostgreSQL', + ], + default: $database + ); + } + + return $database; + } + /** * Determine the stack for Breeze. * From 3f0bb097a264318680bf85b47d96901fb3f077bc Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 19 Aug 2023 10:57:30 +0800 Subject: [PATCH 04/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 3cbc3554..326d8494 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -306,7 +306,7 @@ protected function installJetstream(string $directory, InputInterface $input, Ou * Determine the default database connection. * * @param \Symfony\Component\Console\Input\InputInterface $input - * @return void + * @return string */ protected function promptForDatabaseOptions(InputInterface $input) { From f7cac59aba111817b6863eea6094e365a309e30d Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 19 Aug 2023 11:05:47 +0800 Subject: [PATCH 05/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 326d8494..0bad7399 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -224,10 +224,19 @@ protected function configureDefaultDatabaseConnection(string $directory, string $directory.'/.env' ); + $defaults = [ + 'DB_DATABASE=laravel', + 'DB_HOST=127.0.0.1', + 'DB_PORT=3306', + 'DB_DATABASE=laravel', + 'DB_USERNAME=root', + 'DB_PASSWORD=', + ]; + if ($database === 'sqlite') { $this->replaceInFile( - 'DB_DATABASE=laravel', - '# DB_DATABASE=', + $defaults, + collect($defaults)->map(fn ($default) => "# {$default}")->all(), $directory.'/.env' ); } else { @@ -682,12 +691,12 @@ protected function replaceFile(string $replace, string $file) /** * Replace the given string in the given file. * - * @param string $search - * @param string $replace + * @param string|array $search + * @param string|array $replace * @param string $file * @return void */ - protected function replaceInFile(string $search, string $replace, string $file) + protected function replaceInFile(string|array $search, string|array $replace, string $file) { file_put_contents( $file, From 367f97f082116c55f368ba42970c781f8459e555 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Sat, 19 Aug 2023 11:10:24 +0800 Subject: [PATCH 06/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 0bad7399..b2578ff1 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -239,19 +239,21 @@ protected function configureDefaultDatabaseConnection(string $directory, string collect($defaults)->map(fn ($default) => "# {$default}")->all(), $directory.'/.env' ); - } else { - $this->replaceInFile( - 'DB_DATABASE=laravel', - 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), - $directory.'/.env' - ); - $this->replaceInFile( - 'DB_DATABASE=laravel', - 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), - $directory.'/.env.example' - ); + return; } + + $this->replaceInFile( + 'DB_DATABASE=laravel', + 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), + $directory.'/.env' + ); + + $this->replaceInFile( + 'DB_DATABASE=laravel', + 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), + $directory.'/.env.example' + ); } /** From e37ea291cb2484dfc104d7177f949f7ae80b5f0b Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 22 Aug 2023 20:49:43 +0800 Subject: [PATCH 07/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/NewCommand.php b/src/NewCommand.php index b2578ff1..9d317cf7 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -224,6 +224,14 @@ protected function configureDefaultDatabaseConnection(string $directory, string $directory.'/.env' ); + if (! in_array($database, 'sqlite')) { + $this->replaceInFile( + 'DB_CONNECTION=mysql', + 'DB_CONNECTION='.$database, + $directory.'/.env.example' + ); + } + $defaults = [ 'DB_DATABASE=laravel', 'DB_HOST=127.0.0.1', @@ -243,6 +251,26 @@ protected function configureDefaultDatabaseConnection(string $directory, string return; } + $defaultPorts = [ + 'mysql' => '3306', + 'pgsql' => '5432', + 'sqlsrv' => '1433', + ]; + + if (isset($defaultPorts[$database])) { + $this->replaceInFile( + 'DB_PORT=3306', + 'DB_PORT='.$defaultPorts[$database], + $directory.'/.env' + ); + + $this->replaceInFile( + 'DB_PORT=3306', + 'DB_PORT='.$defaultPorts[$database], + $directory.'/.env.example' + ); + } + $this->replaceInFile( 'DB_DATABASE=laravel', 'DB_DATABASE='.str_replace('-', '_', strtolower($name)), @@ -330,6 +358,7 @@ protected function promptForDatabaseOptions(InputInterface $input) 'mysql' => 'MySQL', 'sqlite' => 'SQLite', 'pgsql' => 'PostgreSQL', + 'sqlsrv' => 'MSSQL', ], default: $database ); From ae9ca979a3c928e74283a5665acd408e8664efa8 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 22 Aug 2023 20:51:03 +0800 Subject: [PATCH 08/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 9d317cf7..4aa98fb6 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -224,7 +224,7 @@ protected function configureDefaultDatabaseConnection(string $directory, string $directory.'/.env' ); - if (! in_array($database, 'sqlite')) { + if (! in_array($database, ['sqlite'])) { $this->replaceInFile( 'DB_CONNECTION=mysql', 'DB_CONNECTION='.$database, From 6aeeec43fb418253d6595088926c2506399562b3 Mon Sep 17 00:00:00 2001 From: Mior Muhammad Zaki Date: Tue, 22 Aug 2023 20:53:40 +0800 Subject: [PATCH 09/10] wip Signed-off-by: Mior Muhammad Zaki --- src/NewCommand.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index 4aa98fb6..a049db33 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -252,7 +252,6 @@ protected function configureDefaultDatabaseConnection(string $directory, string } $defaultPorts = [ - 'mysql' => '3306', 'pgsql' => '5432', 'sqlsrv' => '1433', ]; From 78213d9c9e62c3cf0d6cb42fc9daaaf8228453a7 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 25 Aug 2023 09:32:55 -0500 Subject: [PATCH 10/10] formatting --- src/NewCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/NewCommand.php b/src/NewCommand.php index a049db33..2bf8824f 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -352,12 +352,12 @@ protected function promptForDatabaseOptions(InputInterface $input) if ($input->isInteractive()) { $database = select( - label: 'Choose Database Driver', + label: 'Which database will your application use?', options: [ 'mysql' => 'MySQL', - 'sqlite' => 'SQLite', 'pgsql' => 'PostgreSQL', - 'sqlsrv' => 'MSSQL', + 'sqlite' => 'SQLite', + 'sqlsrv' => 'SQL Server', ], default: $database );