Skip to content

Commit

Permalink
Fix dot_env & create_database steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Lanin committed Nov 29, 2015
1 parent f01289e commit cf5406b
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/Commands/Steps/AbstractStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ abstract public function preview($results);
* @return bool
*/
abstract public function finish($results);
}
}
57 changes: 35 additions & 22 deletions src/Commands/Steps/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class CreateDatabase extends AbstractStep
{
const USE_ENV_USER = 'env';
const SET_NEW_USER = 'new';
const USE_ENV_USER = 'env';
const USE_OTHER_USER = 'other';

/**
* Return command prompt text.
Expand All @@ -25,8 +25,8 @@ public function prompt()
protected function prepare()
{
$source = $this->command->choice(
'Do you want to use user from .env or provide new one?',
[self::USE_ENV_USER, self::SET_NEW_USER],
'Do you want to use user from .env or provide another one?',
[self::USE_ENV_USER, self::USE_OTHER_USER],
0
);

Expand All @@ -39,9 +39,8 @@ protected function prepare()

switch ($source)
{
case self::SET_NEW_USER:
$this->getNewUser($return);
$this->createUserPrompt($return);
case self::USE_OTHER_USER:
$this->getOtherUser($return);
break;

case self::USE_ENV_USER:
Expand All @@ -50,21 +49,31 @@ protected function prepare()
break;
}

$this->generateSqlCommands($return);

return $return;
}

/**
* Get database config.
*
* @param array $return
*/
protected function getEnvDatabase(array &$return)
{
$return['host'] = env('DB_HOST');
$return['database'] = env('DB_DATABASE');
$return['localhost'] = 'localhost';

$return['commands'][] = "CREATE DATABASE IF NOT EXISTS {$return['database']};";
if ( ! in_array($return['host'], ['localhost', '127.0.0.1']))
{
$return['localhost'] = $this->command->ask('Database is on the other server. Provide local IP/hostname.');
}
}

/**
* Get user from environment.
*
* @param array $return
*/
protected function getEnvUser(array &$return)
Expand All @@ -74,29 +83,33 @@ protected function getEnvUser(array &$return)
}

/**
* Ask info about 'root' user.
*
* @param array $return
*/
protected function getNewUser(array &$return)
protected function getOtherUser(array &$return)
{
$return['username'] = $this->command->ask('Provide user with <comment>CREATE DATABASE</comment> grants', 'root');
$return['username'] = $this->command->ask('Provide user\'s login with <comment>CREATE DATABASE</comment> grants', 'root');
$return['password'] = $this->command->secret('Password');
}

/**
* Generate SQL commands.
*
* @param array $return
*/
protected function createUserPrompt(array &$return)
private function generateSqlCommands(array &$return)
{
if ($this->command->confirm('Do you want to create user from .env?'))
{
$return['commands'][] = sprintf(
"GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';",
$return['database'],
env('DB_USERNAME'),
gethostname(),
env('DB_PASSWORD')
);
}
$return['commands'] = [];

$return['commands'][] = "CREATE DATABASE IF NOT EXISTS {$return['database']};";
$return['commands'][] = sprintf(
"GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';",
$return['database'],
env('DB_USERNAME'),
$return['localhost'],
env('DB_PASSWORD')
);
}

/**
Expand Down Expand Up @@ -125,7 +138,7 @@ protected function prepareCommand($results, $full = false)
*/
public function preview($results)
{
$this->command->info("We are about to run <comment>" . $this->prepareCommand($results) . "</comment>");
$this->command->info("This command will be executed: <comment>" . $this->prepareCommand($results) . "</comment>");
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/Commands/Steps/DotEnv.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class DotEnv extends AbstractStep
* @var array
*/
protected $boostrappers = [
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
'Lanin\Laravel\SetupWizard\Support\LoadConfiguration',
'Lanin\Laravel\SetupWizard\Support\Bootstrap\DetectEnvironment',
'Lanin\Laravel\SetupWizard\Support\Bootstrap\LoadConfiguration',
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
];

Expand Down Expand Up @@ -53,7 +53,7 @@ protected function prepare()

foreach (\Lanin\Laravel\SetupWizard\Support\DotEnv::$variables as $name => $default)
{
$options = config('setup.dot_env.variables.' . $name, ['type' => self::INPUT]);
$options = config('setup.dot_env.variables.' . $name, ['type' => self::INPUT, 'prompt' => 'Provide value for environment variable']);

$result[$name] = $this->{'run' . $options['type']}($name, $options, $default);
}
Expand Down Expand Up @@ -137,8 +137,10 @@ public function preview($results)
}

/**
* @param string $name
* @param string $prompt
* Generate prompt text.
*
* @param string $name
* @param string $prompt
* @return string
*/
protected function generatePrompt($name, $prompt)
Expand All @@ -154,15 +156,17 @@ protected function generatePrompt($name, $prompt)
*/
public function finish($results)
{
$return = $this->saveFile($results);

if ($return)
if ($return = $this->saveFile($results))
{
$this->command->info('New .env file was saved');

/*
* "Rebootstrap" Application to load new env variables to the config using native Application::bootstrapWith([]) method.
* "Rebootstrap" Application to load new env variables to the config
* using native Application::bootstrapWith([]) method with custom bootstrappers.
*
* First of all we have to make Dotenv mutable in order to overwrite environment variables.
*
* Next we have to update config.
* In current Laravel implementation there is no method to reload Application config fully or Application itself.
* Problem is that after reloading Config Repository it looses data from packages' config files
* that are not currently published in /config directory. They are loaded only once after Application is bootstrapped.
Expand Down
29 changes: 29 additions & 0 deletions src/Support/Bootstrap/DetectEnvironment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Lanin\Laravel\SetupWizard\Support\Bootstrap;

use Dotenv;
use InvalidArgumentException;
use Illuminate\Contracts\Foundation\Application;

class DetectEnvironment
{
/**
* Bootstrap the given application.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
try {
Dotenv::makeMutable();
Dotenv::load($app->environmentPath(), $app->environmentFile());
Dotenv::makeImmutable();
} catch (InvalidArgumentException $e) {
//
}

$app->detectEnvironment(function () {
return env('APP_ENV', 'production');
});
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php namespace Lanin\Laravel\SetupWizard\Support;
<?php namespace Lanin\Laravel\SetupWizard\Support\Bootstrap;

use Illuminate\Contracts\Foundation\Application;

Expand Down

0 comments on commit cf5406b

Please sign in to comment.