Skip to content

Commit 82b85c1

Browse files
committed
Prevent setup to run again if already configured when using bake
1 parent 2e7013b commit 82b85c1

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

app/system/Bakery/Command/Bake.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
5656
{
5757
$this->io->writeln("<info>{$this->title}</info>");
5858

59-
$command = $this->getApplication()->find('setup');
59+
$command = $this->getApplication()->find('setup:db');
60+
$command->run($input, $output);
61+
62+
$command = $this->getApplication()->find('setup:smtp');
6063
$command->run($input, $output);
6164

6265
$command = $this->getApplication()->find('debug');

app/system/Bakery/Command/SetupDbCommand.php

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function configure()
3636
$this->setName("setup:db")
3737
->setDescription("UserFrosting Database Configuration Wizard")
3838
->setHelp("Helper command to setup the database configuration. This can also be done manually by editing the <comment>app/.env</comment> file or using global server environment variables.")
39+
->addOption('force', null, InputOption::VALUE_NONE, "Force setup if db is already configured")
3940
->addOption('db_driver', null, InputOption::VALUE_OPTIONAL, "The database driver {$this->getDatabaseDriversList()}")
4041
->addOption('db_name', null, InputOption::VALUE_OPTIONAL, "The database name")
4142
->addOption('db_host', null, InputOption::VALUE_OPTIONAL, "The database hostname")
@@ -56,6 +57,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
5657

5758
// Display header,
5859
$this->io->title("UserFrosting's Database Setup Wizard");
60+
61+
// Check if db is already setup
62+
if (!$input->getOption('force') && $this->testDatabase($config["db.default"], false)) {
63+
$this->io->note("Database already setup. Use the `php bakery setup:db --force` command to run db setup again.");
64+
return;
65+
}
66+
5967
$this->io->note("Database credentials will be saved in `{$this->envPath}`");
6068

6169
// Get an instance of the DotenvEditor
@@ -89,7 +97,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
8997
$dbParams = $this->askForDatabase($input);
9098

9199
// Test database
92-
$this->testDatabase($dbParams);
100+
if (!$this->testDatabase($dbParams)) {
101+
exit(1);
102+
} else {
103+
$this->io->success("Database connection successful");
104+
}
93105

94106
// Time to save
95107
$this->io->section("Saving data");
@@ -189,9 +201,10 @@ protected function askForDatabase(InputInterface $args)
189201
* Test new database connecion
190202
*
191203
* @param array $dbParams Database params
192-
* @return void (Exit if fails)
204+
* @param bool $displayMessage Display io message
205+
* @return bool Return true if db is successful
193206
*/
194-
protected function testDatabase($dbParams)
207+
protected function testDatabase($dbParams, $displayMessage = true)
195208
{
196209
// Setup a new db connection
197210
$capsule = new Capsule;
@@ -201,14 +214,17 @@ protected function testDatabase($dbParams)
201214
try {
202215
$conn = $capsule->getConnection();
203216
$conn->getPdo();
204-
$this->io->success("Database connection successful");
205217
} catch (\PDOException $e) {
206-
$message = "Could not connect to the database '{$dbParams['username']}@{$dbParams['host']}/{$dbParams['database']}':".PHP_EOL;
207-
$message .= "Exception: " . $e->getMessage() . PHP_EOL.PHP_EOL;
208-
$message .= "Please check your database configuration and/or google the exception shown above and run the command again.";
209-
$this->io->error($message);
210-
exit(1);
218+
if ($displayMessage) {
219+
$message = "Could not connect to the database '{$dbParams['username']}@{$dbParams['host']}/{$dbParams['database']}':".PHP_EOL;
220+
$message .= "Exception: " . $e->getMessage() . PHP_EOL.PHP_EOL;
221+
$message .= "Please check your database configuration and/or google the exception shown above and run the command again.";
222+
$this->io->error($message);
223+
}
224+
return false;
211225
}
226+
227+
return true;
212228
}
213229

214230
/**

app/system/Bakery/Command/SetupSmtpCommand.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ protected function configure()
5050
$this->setName('setup:smtp')
5151
->setDescription('UserFrosting SMTP Configuration Wizard')
5252
->setHelp('Helper command to setup outgoing email configuration. This can also be done manually by editing the <comment>app/.env</comment> file or using global server environment variables.')
53+
->addOption('force', null, InputOption::VALUE_NONE, "Force setup if SMTP appears to be already configured")
5354
->addOption('smtp_host', null, InputOption::VALUE_OPTIONAL, 'The SMTP server hostname')
5455
->addOption('smtp_user', null, InputOption::VALUE_OPTIONAL, 'The SMTP server user')
5556
->addOption('smtp_password', null, InputOption::VALUE_OPTIONAL, 'The SMTP server password');
@@ -67,13 +68,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
6768

6869
// Display header,
6970
$this->io->title("UserFrosting's SMTP Setup Wizard");
70-
$this->io->note("SMTP credentials will be saved in `{$this->envPath}`");
7171

7272
// Get an instance of the DotenvEditor
7373
$dotenvEditor = new DotenvEditor(\UserFrosting\APP_DIR, false);
7474
$dotenvEditor->load($this->envPath);
7575
$dotenvEditor->save(); // Save make sure empty file is created if none exist before reading it
7676

77+
// Check if db is already setup
78+
if (!$input->getOption('force') && $this->isSmtpConfigured($dotenvEditor)) {
79+
$this->io->note("SMTP already setup. Use the `php bakery setup:smtp --force` command to run SMTP setup again.");
80+
return;
81+
}
82+
7783
// Get keys
7884
$keys = [
7985
'SMTP_HOST' => ($dotenvEditor->keyExists('SMTP_HOST')) ? $dotenvEditor->getValue('SMTP_HOST') : '',
@@ -94,6 +100,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
94100
}
95101
}
96102

103+
$this->io->note("SMTP credentials will be saved in `{$this->envPath}`");
104+
97105
// Ask for SMTP info
98106
$smtpParams = $this->askForSmtpMethod($input);
99107

@@ -206,4 +214,19 @@ protected function askForNone(InputInterface $input)
206214
$this->askForSmtpMethod($input);
207215
}
208216
}
217+
218+
/**
219+
* Check if the app/.env SMTP portion is defined or not.
220+
*
221+
* @param DotenvEditor $dotenvEditor
222+
* @return bool true if SMTP is configured in .env file
223+
*/
224+
protected function isSmtpConfigured(DotenvEditor $dotenvEditor)
225+
{
226+
if ($dotenvEditor->keyExists('SMTP_HOST') && $dotenvEditor->keyExists('SMTP_USER') && $dotenvEditor->keyExists('SMTP_PASSWORD')) {
227+
return true;
228+
} else {
229+
return false;
230+
}
231+
}
209232
}

0 commit comments

Comments
 (0)