diff --git a/src/NewCommand.php b/src/NewCommand.php index b2bf395..295d1a6 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -181,9 +181,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int $directory.'/.env' ); - $database = $this->promptForDatabaseOptions($input); + [$database, $migrate] = $this->promptForDatabaseOptions($directory, $input); - $this->configureDefaultDatabaseConnection($directory, $database, $name); + $this->configureDefaultDatabaseConnection($directory, $database, $name, $migrate); + + if ($migrate) { + $this->runCommands([ + $this->phpBinary().' artisan migrate', + ], $input, $output, workingPath: $directory); + } } if ($input->getOption('git') || $input->getOption('github') !== false) { @@ -231,47 +237,42 @@ protected function defaultBranch() * @param string $directory * @param string $database * @param string $name + * @param bool $migrate * @return void */ - protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name) + protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, bool $migrate) { // MariaDB configuration only exists as of Laravel 11... if ($database === 'mariadb' && ! $this->hasMariaDBConfig($directory)) { $database = 'mysql'; } - $this->replaceInFile( - 'DB_CONNECTION=mysql', + $this->pregReplaceInFile( + '/DB_CONNECTION=.*/', 'DB_CONNECTION='.$database, $directory.'/.env' ); - if (! in_array($database, ['sqlite'])) { - $this->replaceInFile( - 'DB_CONNECTION=mysql', - 'DB_CONNECTION='.$database, - $directory.'/.env.example' - ); - } - - $defaults = [ - 'DB_HOST=127.0.0.1', - 'DB_PORT=3306', - 'DB_DATABASE=laravel', - 'DB_USERNAME=root', - 'DB_PASSWORD=', - ]; + $this->pregReplaceInFile( + '/DB_CONNECTION=.*/', + 'DB_CONNECTION='.$database, + $directory.'/.env.example' + ); if ($database === 'sqlite') { - $this->replaceInFile( - $defaults, - collect($defaults)->map(fn ($default) => "# {$default}")->all(), - $directory.'/.env' - ); + $environment = file_get_contents($directory.'/.env'); + + // If database options aren't commented, comment them for SQLite... + if (! str_contains($environment, '# DB_HOST=127.0.0.1')) { + return $this->commentDatabaseConfigurationForSqlite($directory); + } return; } + // Any commented database configuration options should be uncommented when not on SQLite... + $this->uncommentDatabaseConfiguration($directory); + $defaultPorts = [ 'pgsql' => '5432', 'sqlsrv' => '1433', @@ -323,6 +324,64 @@ protected function hasMariaDBConfig(string $directory): bool ); } + /** + * Comment the irrelevant database configuration entries for SQLite applications. + * + * @param string $directory + * @return void + */ + protected function commentDatabaseConfigurationForSqlite(string $directory) + { + $defaults = [ + 'DB_HOST=127.0.0.1', + 'DB_PORT=3306', + 'DB_DATABASE=laravel', + 'DB_USERNAME=root', + 'DB_PASSWORD=', + ]; + + $this->replaceInFile( + $defaults, + collect($defaults)->map(fn ($default) => "# {$default}")->all(), + $directory.'/.env' + ); + + $this->replaceInFile( + $defaults, + collect($defaults)->map(fn ($default) => "# {$default}")->all(), + $directory.'/.env.example' + ); + } + + /** + * Uncomment the relevant database configuration entries for non SQLite applications. + * + * @param string $directory + * @return void + */ + protected function uncommentDatabaseConfiguration(string $directory) + { + $defaults = [ + '# DB_HOST=127.0.0.1', + '# DB_PORT=3306', + '# DB_DATABASE=laravel', + '# DB_USERNAME=root', + '# DB_PASSWORD=', + ]; + + $this->replaceInFile( + $defaults, + collect($defaults)->map(fn ($default) => substr($default, 2))->all(), + $directory.'/.env' + ); + + $this->replaceInFile( + $defaults, + collect($defaults)->map(fn ($default) => substr($default, 2))->all(), + $directory.'/.env.example' + ); + } + /** * Install Laravel Breeze into the application. * @@ -382,12 +441,14 @@ protected function installJetstream(string $directory, InputInterface $input, Ou /** * Determine the default database connection. * + * @param string $directory * @param \Symfony\Component\Console\Input\InputInterface $input * @return string */ - protected function promptForDatabaseOptions(InputInterface $input) + protected function promptForDatabaseOptions(string $directory, InputInterface $input) { - $database = 'mysql'; + // Laravel 11.x appliations use SQLite as default... + $defaultDatabase = $this->hasMariaDBConfig($directory) ? 'sqlite' : 'mysql'; if ($input->isInteractive()) { $database = select( @@ -399,11 +460,15 @@ protected function promptForDatabaseOptions(InputInterface $input) 'sqlite' => 'SQLite', 'sqlsrv' => 'SQL Server', ], - default: $database + default: $defaultDatabase ); + + if ($database !== $defaultDatabase) { + $migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true); + } } - return $database; + return [$database ?? $defaultDatabase, $migrate ?? false]; } /** @@ -808,4 +873,20 @@ protected function replaceInFile(string|array $search, string|array $replace, st str_replace($search, $replace, file_get_contents($file)) ); } + + /** + * Replace the given string in the given file using regular expressions. + * + * @param string|array $search + * @param string|array $replace + * @param string $file + * @return void + */ + protected function pregReplaceInFile(string $pattern, string $replace, string $file) + { + file_put_contents( + $file, + preg_replace($pattern, $replace, file_get_contents($file)) + ); + } }