Commit f6d21d28 authored by John Elliott's avatar John Elliott

Refactor EnvFileWriter and associated files

Update EnvFileWriter so that it can be used to write any of the variables to the .env file not just the database connection variables. This means that it will be possible to easily and consistently add prompts for additional variables going forwards (e.g. APP_URL). Additionally, update ConfigureDatabase and SetInstalled flag so that they both use the updated CreateEnvFile. The .env file is now created by CreateEnvFile.
parent 93f91ac1
...@@ -57,6 +57,7 @@ class InstallCommand extends Command ...@@ -57,6 +57,7 @@ class InstallCommand extends Command
$success = $this->installer->stack([ $success = $this->installer->stack([
\Modules\Core\Console\Installers\Scripts\ProtectInstaller::class, \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\ConfigureDatabase::class,
\Modules\Core\Console\Installers\Scripts\SetAppKey::class, \Modules\Core\Console\Installers\Scripts\SetAppKey::class,
\Modules\Core\Console\Installers\Scripts\ConfigureUserProvider::class, \Modules\Core\Console\Installers\Scripts\ConfigureUserProvider::class,
......
...@@ -48,15 +48,18 @@ class ConfigureDatabase implements SetupScript ...@@ -48,15 +48,18 @@ class ConfigureDatabase implements SetupScript
$connected = false; $connected = false;
$vars = [];
while (! $connected) { 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()) { if ($this->databaseConnectionIsValid()) {
$connected = true; $connected = true;
...@@ -65,7 +68,7 @@ class ConfigureDatabase implements SetupScript ...@@ -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'); $command->info('Database successfully configured');
} }
...@@ -143,21 +146,19 @@ class ConfigureDatabase implements SetupScript ...@@ -143,21 +146,19 @@ class ConfigureDatabase implements SetupScript
} }
/** /**
* @param $driver * @param array $vars
* @param $name
* @param $port
* @param $user
* @param $password
*/ */
protected function setLaravelConfiguration($driver, $host, $port, $name, $user, $password) protected function setLaravelConfiguration($vars)
{ {
$this->config['database.default'] = $driver; $driver = $vars['db_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;
$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(DatabaseManager::class)->purge($driver);
app(ConnectionFactory::class)->make($this->config['database.connections.' . $driver], $driver); app(ConnectionFactory::class)->make($this->config['database.connections.' . $driver], $driver);
} }
...@@ -176,4 +177,4 @@ class ConfigureDatabase implements SetupScript ...@@ -176,4 +177,4 @@ class ConfigureDatabase implements SetupScript
return false; 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 @@ ...@@ -3,19 +3,19 @@
namespace Modules\Core\Console\Installers\Scripts; namespace Modules\Core\Console\Installers\Scripts;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
use Modules\Core\Console\Installers\SetupScript; use Modules\Core\Console\Installers\SetupScript;
use Modules\Core\Console\Installers\Writers\EnvFileWriter;
class SetInstalledFlag implements SetupScript 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 ...@@ -25,8 +25,14 @@ class SetInstalledFlag implements SetupScript
*/ */
public function fire(Command $command) public function fire(Command $command)
{ {
$env = $this->finder->get('.env');
$env = str_replace('INSTALLED=false', 'INSTALLED=true', $env); $vars = [];
$this->finder->put('.env', $env);
$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 ...@@ -12,15 +12,19 @@ class EnvFileWriter
private $finder; private $finder;
/** /**
* Whitelist of variables in .env.example that can be written by the installer when it creates the .env file
*
* @var array * @var array
*/ */
protected $search = [ protected $setable_variables = [
'DB_CONNECTION=mysql', 'db_driver' => 'DB_CONNECTION=mysql',
'DB_HOST=127.0.0.1', 'db_host' => 'DB_HOST=127.0.0.1',
'DB_PORT=3306', 'db_port' => 'DB_PORT=3306',
'DB_DATABASE=homestead', 'db_database' => 'DB_DATABASE=homestead',
'DB_USERNAME=homestead', 'db_username' => 'DB_USERNAME=homestead',
'DB_PASSWORD=secret', 'db_password' => 'DB_PASSWORD=secret',
'app_url' => 'APP_URL=http://localhost',
'installed' => 'INSTALLED=false',
]; ];
/** /**
...@@ -42,29 +46,48 @@ class EnvFileWriter ...@@ -42,29 +46,48 @@ class EnvFileWriter
} }
/** /**
* @param $driver * Create a new .env file using the contents of .env.example
* @param $host *
* @param $port
* @param $name
* @param $username
* @param $password
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException * @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); $environmentFile = $this->finder->get($this->template);
$replace = [ $this->finder->put($this->file, $environmentFile);
"DB_CONNECTION=$driver", }
"DB_HOST=$host",
"DB_PORT=$port", /**
"DB_DATABASE=$name", * Update the .env file
"DB_USERNAME=$username", *
"DB_PASSWORD=$password", * @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