Skip to content

Commit cf5406b

Browse files
author
Maxim Lanin
committed
Fix dot_env & create_database steps
1 parent f01289e commit cf5406b

File tree

5 files changed

+79
-33
lines changed

5 files changed

+79
-33
lines changed

src/Commands/Steps/AbstractStep.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ abstract public function preview($results);
106106
* @return bool
107107
*/
108108
abstract public function finish($results);
109-
}
109+
}

src/Commands/Steps/CreateDatabase.php

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class CreateDatabase extends AbstractStep
66
{
7-
const USE_ENV_USER = 'env';
8-
const SET_NEW_USER = 'new';
7+
const USE_ENV_USER = 'env';
8+
const USE_OTHER_USER = 'other';
99

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

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

4040
switch ($source)
4141
{
42-
case self::SET_NEW_USER:
43-
$this->getNewUser($return);
44-
$this->createUserPrompt($return);
42+
case self::USE_OTHER_USER:
43+
$this->getOtherUser($return);
4544
break;
4645

4746
case self::USE_ENV_USER:
@@ -50,21 +49,31 @@ protected function prepare()
5049
break;
5150
}
5251

52+
$this->generateSqlCommands($return);
53+
5354
return $return;
5455
}
5556

5657
/**
58+
* Get database config.
59+
*
5760
* @param array $return
5861
*/
5962
protected function getEnvDatabase(array &$return)
6063
{
6164
$return['host'] = env('DB_HOST');
6265
$return['database'] = env('DB_DATABASE');
66+
$return['localhost'] = 'localhost';
6367

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

6774
/**
75+
* Get user from environment.
76+
*
6877
* @param array $return
6978
*/
7079
protected function getEnvUser(array &$return)
@@ -74,29 +83,33 @@ protected function getEnvUser(array &$return)
7483
}
7584

7685
/**
86+
* Ask info about 'root' user.
87+
*
7788
* @param array $return
7889
*/
79-
protected function getNewUser(array &$return)
90+
protected function getOtherUser(array &$return)
8091
{
81-
$return['username'] = $this->command->ask('Provide user with <comment>CREATE DATABASE</comment> grants', 'root');
92+
$return['username'] = $this->command->ask('Provide user\'s login with <comment>CREATE DATABASE</comment> grants', 'root');
8293
$return['password'] = $this->command->secret('Password');
8394
}
8495

8596
/**
97+
* Generate SQL commands.
98+
*
8699
* @param array $return
87100
*/
88-
protected function createUserPrompt(array &$return)
101+
private function generateSqlCommands(array &$return)
89102
{
90-
if ($this->command->confirm('Do you want to create user from .env?'))
91-
{
92-
$return['commands'][] = sprintf(
93-
"GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';",
94-
$return['database'],
95-
env('DB_USERNAME'),
96-
gethostname(),
97-
env('DB_PASSWORD')
98-
);
99-
}
103+
$return['commands'] = [];
104+
105+
$return['commands'][] = "CREATE DATABASE IF NOT EXISTS {$return['database']};";
106+
$return['commands'][] = sprintf(
107+
"GRANT ALL PRIVILEGES ON %s.* TO %s@%s IDENTIFIED BY '%s';",
108+
$return['database'],
109+
env('DB_USERNAME'),
110+
$return['localhost'],
111+
env('DB_PASSWORD')
112+
);
100113
}
101114

102115
/**
@@ -125,7 +138,7 @@ protected function prepareCommand($results, $full = false)
125138
*/
126139
public function preview($results)
127140
{
128-
$this->command->info("We are about to run <comment>" . $this->prepareCommand($results) . "</comment>");
141+
$this->command->info("This command will be executed: <comment>" . $this->prepareCommand($results) . "</comment>");
129142
}
130143

131144
/**

src/Commands/Steps/DotEnv.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class DotEnv extends AbstractStep
1818
* @var array
1919
*/
2020
protected $boostrappers = [
21-
'Illuminate\Foundation\Bootstrap\DetectEnvironment',
22-
'Lanin\Laravel\SetupWizard\Support\LoadConfiguration',
21+
'Lanin\Laravel\SetupWizard\Support\Bootstrap\DetectEnvironment',
22+
'Lanin\Laravel\SetupWizard\Support\Bootstrap\LoadConfiguration',
2323
'Illuminate\Foundation\Bootstrap\ConfigureLogging',
2424
];
2525

@@ -53,7 +53,7 @@ protected function prepare()
5353

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

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

139139
/**
140-
* @param string $name
141-
* @param string $prompt
140+
* Generate prompt text.
141+
*
142+
* @param string $name
143+
* @param string $prompt
142144
* @return string
143145
*/
144146
protected function generatePrompt($name, $prompt)
@@ -154,15 +156,17 @@ protected function generatePrompt($name, $prompt)
154156
*/
155157
public function finish($results)
156158
{
157-
$return = $this->saveFile($results);
158-
159-
if ($return)
159+
if ($return = $this->saveFile($results))
160160
{
161161
$this->command->info('New .env file was saved');
162162

163163
/*
164-
* "Rebootstrap" Application to load new env variables to the config using native Application::bootstrapWith([]) method.
164+
* "Rebootstrap" Application to load new env variables to the config
165+
* using native Application::bootstrapWith([]) method with custom bootstrappers.
166+
*
167+
* First of all we have to make Dotenv mutable in order to overwrite environment variables.
165168
*
169+
* Next we have to update config.
166170
* In current Laravel implementation there is no method to reload Application config fully or Application itself.
167171
* Problem is that after reloading Config Repository it looses data from packages' config files
168172
* that are not currently published in /config directory. They are loaded only once after Application is bootstrapped.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace Lanin\Laravel\SetupWizard\Support\Bootstrap;
2+
3+
use Dotenv;
4+
use InvalidArgumentException;
5+
use Illuminate\Contracts\Foundation\Application;
6+
7+
class DetectEnvironment
8+
{
9+
/**
10+
* Bootstrap the given application.
11+
*
12+
* @param \Illuminate\Contracts\Foundation\Application $app
13+
* @return void
14+
*/
15+
public function bootstrap(Application $app)
16+
{
17+
try {
18+
Dotenv::makeMutable();
19+
Dotenv::load($app->environmentPath(), $app->environmentFile());
20+
Dotenv::makeImmutable();
21+
} catch (InvalidArgumentException $e) {
22+
//
23+
}
24+
25+
$app->detectEnvironment(function () {
26+
return env('APP_ENV', 'production');
27+
});
28+
}
29+
}

src/Support/LoadConfiguration.php renamed to src/Support/Bootstrap/LoadConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php namespace Lanin\Laravel\SetupWizard\Support;
1+
<?php namespace Lanin\Laravel\SetupWizard\Support\Bootstrap;
22

33
use Illuminate\Contracts\Foundation\Application;
44

0 commit comments

Comments
 (0)