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

Add database credentials support #353

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 84 additions & 6 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ protected function configure()
->addOption('github', null, InputOption::VALUE_OPTIONAL, 'Create a new repository on GitHub', false)
->addOption('organization', null, InputOption::VALUE_REQUIRED, 'The GitHub organization to create the new repository for')
->addOption('database', null, InputOption::VALUE_REQUIRED, 'The database driver your application will use')
->addOption('dbname', null, InputOption::VALUE_OPTIONAL, 'Change the name of the database')
->addOption('dbuser', null, InputOption::VALUE_OPTIONAL, 'Change the database user')
->addOption('dbpass', null, InputOption::VALUE_OPTIONAL, 'Define a password for the database user')
->addOption('stack', null, InputOption::VALUE_OPTIONAL, 'The Breeze / Jetstream stack that should be installed')
->addOption('breeze', null, InputOption::VALUE_NONE, 'Installs the Laravel Breeze scaffolding')
->addOption('jet', null, InputOption::VALUE_NONE, 'Installs the Laravel Jetstream scaffolding')
Expand Down Expand Up @@ -203,9 +206,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$directory.'/.env'
);

[$database, $migrate] = $this->promptForDatabaseOptions($directory, $input);
[$database, $dbname, $dbuser, $dbpass, $migrate] = $this->promptForDatabaseOptions($directory, $input);

$this->configureDefaultDatabaseConnection($directory, $database, $name);
$this->configureDefaultDatabaseConnection($directory, $database, $name, $dbname, $dbuser, $dbpass);

if ($migrate) {
$this->runCommands([
Expand Down Expand Up @@ -271,9 +274,12 @@ protected function defaultBranch()
* @param string $directory
* @param string $database
* @param string $name
* @param string $dbname
* @param string $dbuser
* @param string $dbpass
* @return void
*/
protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name)
protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, $dbname = '', $dbuser = '', $dbpass = '')
{
$this->pregReplaceInFile(
'/DB_CONNECTION=.*/',
Expand Down Expand Up @@ -322,15 +328,45 @@ protected function configureDefaultDatabaseConnection(string $directory, string
);
}

$databaseName = $dbname ?? str_replace('-', '_', strtolower($name));

$this->replaceInFile(
'DB_DATABASE=laravel',
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
'DB_DATABASE='.$databaseName,
$directory.'/.env'
);

$this->replaceInFile(
'DB_DATABASE=laravel',
'DB_DATABASE='.str_replace('-', '_', strtolower($name)),
'DB_DATABASE='.$databaseName,
$directory.'/.env.example'
);

$databaseUsername = $dbuser ?? 'root';

$this->replaceInFile(
'DB_USERNAME=root',
'DB_USERNAME='.$databaseUsername,
$directory.'/.env'
);

$this->replaceInFile(
'DB_USERNAME=root',
'DB_USERNAME='.$databaseUsername,
$directory.'/.env.example'
);

$databasePassword = $dbpass ?? '';

$this->replaceInFile(
'DB_PASSWORD=',
'DB_PASSWORD='.$databasePassword,
$directory.'/.env'
);

$this->replaceInFile(
'DB_PASSWORD=',
'DB_PASSWORD='.$databasePassword,
$directory.'/.env.example'
);
}
Expand Down Expand Up @@ -477,6 +513,8 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
$databaseOptions = $this->databaseOptions()
)->keys()->first();

$dbname = $dbuser = $dbpass = '';

if (! $input->getOption('database') && $input->isInteractive()) {
$input->setOption('database', select(
label: 'Which database will your application use?',
Expand All @@ -492,7 +530,47 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
);
}

return [$input->getOption('database') ?? $defaultDatabase, $migrate ?? $input->hasOption('database')];
if (! $input->getOption('dbname') && $input->isInteractive()) {
$dbname = $input->setOption('dbname', text(
label: 'What should the name of the database be? Leave blank to use the name of the application (default).',
placeholder: 'E.g. laravel_database',
required: false,
validate: function ($value) {
if (preg_match('/[^\pL\pN\_]/', $value) !== 0) {
return 'The name may only contain letters, numbers, and underscores.';
}
},
));
}

if (! $input->getOption('dbuser') && $input->isInteractive()) {
$dbuser = $input->setOption('dbuser', text(
label: "What should the database username be? Leave blank to use 'root' (default).",
// placeholder: 'E.g. ',
required: false,
validate: function ($value) {
if (preg_match('/[^\pL\pN\_]/', $value) !== 0) {
return 'The name may only contain letters, numbers, and underscores.';
}
},
));
}

if (! $input->getOption('dbpass') && $input->isInteractive()) {
$dbpass = $input->setOption('dbpass', text(
label: 'What is the password for the database user? Leave blank for no password (default).',
// placeholder: 'E.g. supersecretp455w0rd',
required: false,
));
}

return [
$input->getOption('database') ?? $defaultDatabase,
$input->getOption('dbname') ?? $dbname,
$input->getOption('dbuser') ?? $dbuser,
$input->getOption('dbpass') ?? $dbpass,
$migrate ?? $input->hasOption('database'),
];
}

/**
Expand Down