From cf5406b606b9f301af2100f6d236c811e30baef8 Mon Sep 17 00:00:00 2001 From: Maxim Lanin Date: Sun, 29 Nov 2015 19:09:32 +0300 Subject: [PATCH] Fix dot_env & create_database steps --- src/Commands/Steps/AbstractStep.php | 2 +- src/Commands/Steps/CreateDatabase.php | 57 ++++++++++++------- src/Commands/Steps/DotEnv.php | 22 ++++--- src/Support/Bootstrap/DetectEnvironment.php | 29 ++++++++++ .../{ => Bootstrap}/LoadConfiguration.php | 2 +- 5 files changed, 79 insertions(+), 33 deletions(-) create mode 100644 src/Support/Bootstrap/DetectEnvironment.php rename src/Support/{ => Bootstrap}/LoadConfiguration.php (85%) diff --git a/src/Commands/Steps/AbstractStep.php b/src/Commands/Steps/AbstractStep.php index e93c02e..c048d2b 100644 --- a/src/Commands/Steps/AbstractStep.php +++ b/src/Commands/Steps/AbstractStep.php @@ -106,4 +106,4 @@ abstract public function preview($results); * @return bool */ abstract public function finish($results); -} \ No newline at end of file +} diff --git a/src/Commands/Steps/CreateDatabase.php b/src/Commands/Steps/CreateDatabase.php index 618febb..42aae79 100644 --- a/src/Commands/Steps/CreateDatabase.php +++ b/src/Commands/Steps/CreateDatabase.php @@ -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. @@ -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 ); @@ -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: @@ -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) @@ -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 CREATE DATABASE grants', 'root'); + $return['username'] = $this->command->ask('Provide user\'s login with CREATE DATABASE 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') + ); } /** @@ -125,7 +138,7 @@ protected function prepareCommand($results, $full = false) */ public function preview($results) { - $this->command->info("We are about to run " . $this->prepareCommand($results) . ""); + $this->command->info("This command will be executed: " . $this->prepareCommand($results) . ""); } /** diff --git a/src/Commands/Steps/DotEnv.php b/src/Commands/Steps/DotEnv.php index 6402afd..d6264c4 100644 --- a/src/Commands/Steps/DotEnv.php +++ b/src/Commands/Steps/DotEnv.php @@ -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', ]; @@ -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); } @@ -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) @@ -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. diff --git a/src/Support/Bootstrap/DetectEnvironment.php b/src/Support/Bootstrap/DetectEnvironment.php new file mode 100644 index 0000000..30d7099 --- /dev/null +++ b/src/Support/Bootstrap/DetectEnvironment.php @@ -0,0 +1,29 @@ +environmentPath(), $app->environmentFile()); + Dotenv::makeImmutable(); + } catch (InvalidArgumentException $e) { + // + } + + $app->detectEnvironment(function () { + return env('APP_ENV', 'production'); + }); + } +} diff --git a/src/Support/LoadConfiguration.php b/src/Support/Bootstrap/LoadConfiguration.php similarity index 85% rename from src/Support/LoadConfiguration.php rename to src/Support/Bootstrap/LoadConfiguration.php index bee5d95..f82d021 100644 --- a/src/Support/LoadConfiguration.php +++ b/src/Support/Bootstrap/LoadConfiguration.php @@ -1,4 +1,4 @@ -