diff --git a/src/NewCommand.php b/src/NewCommand.php index dd0814a..f1f8601 100644 --- a/src/NewCommand.php +++ b/src/NewCommand.php @@ -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') @@ -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([ @@ -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=.*/', @@ -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' ); } @@ -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?', @@ -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'), + ]; } /**