Commit 75ad490c authored by Nicolas Widart's avatar Nicolas Widart Committed by GitHub

Merge pull request #381 from johnsvenn/feature/refactor-env-file-writer-local

Feature/refactor env file writer local
parents bc9c5001 f507f9d3
......@@ -57,6 +57,7 @@ class InstallCommand extends Command
$success = $this->installer->stack([
\Modules\Core\Console\Installers\Scripts\ProtectInstaller::class,
\Modules\Core\Console\Installers\Scripts\CreateEnvFile::class,
\Modules\Core\Console\Installers\Scripts\ConfigureDatabase::class,
\Modules\Core\Console\Installers\Scripts\SetAppKey::class,
\Modules\Core\Console\Installers\Scripts\ConfigureUserProvider::class,
......
......@@ -48,15 +48,18 @@ class ConfigureDatabase implements SetupScript
$connected = false;
$vars = [];
while (! $connected) {
$driver = $this->askDatabaseDriver();
$host = $this->askDatabaseHost();
$port = $this->askDatabasePort($driver);
$name = $this->askDatabaseName();
$user = $this->askDatabaseUsername();
$password = $this->askDatabasePassword();
$this->setLaravelConfiguration($driver, $host, $port, $name, $user, $password);
$vars['db_driver'] = $this->askDatabaseDriver();
$vars['db_host'] = $this->askDatabaseHost();
$vars['db_port'] = $this->askDatabasePort($vars['db_driver']);
$vars['db_database'] = $this->askDatabaseName();
$vars['db_username'] = $this->askDatabaseUsername();
$vars['db_password'] = $this->askDatabasePassword();
$this->setLaravelConfiguration($vars);
if ($this->databaseConnectionIsValid()) {
$connected = true;
......@@ -65,7 +68,7 @@ class ConfigureDatabase implements SetupScript
}
}
$this->env->write($driver, $host, $port, $name, $user, $password);
$this->env->write($vars);
$command->info('Database successfully configured');
}
......@@ -143,21 +146,19 @@ class ConfigureDatabase implements SetupScript
}
/**
* @param $driver
* @param $name
* @param $port
* @param $user
* @param $password
* @param array $vars
*/
protected function setLaravelConfiguration($driver, $host, $port, $name, $user, $password)
protected function setLaravelConfiguration($vars)
{
$this->config['database.default'] = $driver;
$this->config['database.connections.' . $driver . '.host'] = $host;
$this->config['database.connections.' . $driver . '.port'] = $port;
$this->config['database.connections.' . $driver . '.database'] = $name;
$this->config['database.connections.' . $driver . '.username'] = $user;
$this->config['database.connections.' . $driver . '.password'] = $password;
$driver = $vars['db_driver'];
$this->config['database.default'] = $driver;
$this->config['database.connections.' . $driver . '.host'] = $vars['db_host'];
$this->config['database.connections.' . $driver . '.port'] = $vars['db_port'];
$this->config['database.connections.' . $driver . '.database'] = $vars['db_database'];
$this->config['database.connections.' . $driver . '.username'] = $vars['db_username'];
$this->config['database.connections.' . $driver . '.password'] = $vars['db_password'];
app(DatabaseManager::class)->purge($driver);
app(ConnectionFactory::class)->make($this->config['database.connections.' . $driver], $driver);
}
......@@ -176,4 +177,4 @@ class ConfigureDatabase implements SetupScript
return false;
}
}
}
}
\ No newline at end of file
<?php
namespace Modules\Core\Console\Installers\Scripts;
use Illuminate\Console\Command;
use Modules\Core\Console\Installers\SetupScript;
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
class CreateEnvFile implements SetupScript
{
/**
* @var EnvFileWriter
*/
protected $env;
/**
* @param Config $config
* @param EnvFileWriter $env
*/
public function __construct(EnvFileWriter $env)
{
$this->env = $env;
}
/**
* @var Command
*/
protected $command;
/**
* Fire the install script
* @param Command $command
* @return mixed
*/
public function fire(Command $command)
{
$this->command = $command;
$this->env->create();
$command->info('Successfully created .env file');
}
}
......@@ -3,19 +3,19 @@
namespace Modules\Core\Console\Installers\Scripts;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Modules\Core\Console\Installers\SetupScript;
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
class SetInstalledFlag implements SetupScript
{
/**
* @var Filesystem
* @var EnvFileWriter
*/
private $finder;
protected $env;
public function __construct(Filesystem $finder)
public function __construct(EnvFileWriter $env)
{
$this->finder = $finder;
$this->env = $env;
}
/**
......@@ -25,8 +25,14 @@ class SetInstalledFlag implements SetupScript
*/
public function fire(Command $command)
{
$env = $this->finder->get('.env');
$env = str_replace('INSTALLED=false', 'INSTALLED=true', $env);
$this->finder->put('.env', $env);
$vars = [];
$vars['installed'] = 'true';
$this->env->write($vars);
$command->info('The application is now installed');
}
}
}
\ No newline at end of file
......@@ -12,15 +12,19 @@ class EnvFileWriter
private $finder;
/**
* Whitelist of variables in .env.example that can be written by the installer when it creates the .env file
*
* @var array
*/
protected $search = [
'DB_CONNECTION=mysql',
'DB_HOST=127.0.0.1',
'DB_PORT=3306',
'DB_DATABASE=homestead',
'DB_USERNAME=homestead',
'DB_PASSWORD=secret',
protected $setable_variables = [
'db_driver' => 'DB_CONNECTION=mysql',
'db_host' => 'DB_HOST=127.0.0.1',
'db_port' => 'DB_PORT=3306',
'db_database' => 'DB_DATABASE=homestead',
'db_username' => 'DB_USERNAME=homestead',
'db_password' => 'DB_PASSWORD=secret',
'app_url' => 'APP_URL=http://localhost',
'installed' => 'INSTALLED=false',
];
/**
......@@ -42,29 +46,48 @@ class EnvFileWriter
}
/**
* @param $driver
* @param $host
* @param $port
* @param $name
* @param $username
* @param $password
* Create a new .env file using the contents of .env.example
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @return void
*/
public function write($driver, $host, $port, $name, $username, $password)
public function create()
{
$environmentFile = $this->finder->get($this->template);
$replace = [
"DB_CONNECTION=$driver",
"DB_HOST=$host",
"DB_PORT=$port",
"DB_DATABASE=$name",
"DB_USERNAME=$username",
"DB_PASSWORD=$password",
];
$this->finder->put($this->file, $environmentFile);
}
/**
* Update the .env file
*
* @param array $vars
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
* @return void
*/
public function write($vars)
{
if (!empty($vars)) {
$environmentFile = $this->finder->get($this->file);
foreach ($vars as $key => $value) {
if (isset($this->setable_variables[$key])) {
$env_var_name = explode('=', $this->setable_variables[$key])[0];
$value = $env_var_name . '=' . $value;
$environmentFile = str_replace($this->setable_variables[$key], $value, $environmentFile);
}
}
$this->finder->put($this->file, $environmentFile);
$newEnvironmentFile = str_replace($this->search, $replace, $environmentFile);
}
$this->finder->put($this->file, $newEnvironmentFile);
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment