Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLite #304

Merged
merged 12 commits into from
Jan 21, 2024
139 changes: 110 additions & 29 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,15 @@
$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) {
Expand Down Expand Up @@ -231,47 +237,42 @@
* @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);

Check failure on line 267 in src/NewCommand.php

View workflow job for this annotation

GitHub Actions / Static Analysis

Result of method Laravel\Installer\Console\NewCommand::commentDatabaseConfigurationForSqlite() (void) is used.
}

return;
}

// Any commented database configuration options should be uncommented when not on SQLite...
$this->uncommentDatabaseConfiguration($directory);

$defaultPorts = [
'pgsql' => '5432',
'sqlsrv' => '1433',
Expand Down Expand Up @@ -323,6 +324,64 @@
);
}

/**
* 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.
*
Expand Down Expand Up @@ -382,12 +441,14 @@
/**
* 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(
Expand All @@ -399,11 +460,15 @@
'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];
}

/**
Expand Down Expand Up @@ -808,4 +873,20 @@
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))
);
}
}
Loading