* Migrate all configs into the env.php file #1075 * Style fixes * Fix config names in CreateConfig * Fix installer/installer error messages
This commit is contained in:
@@ -4,9 +4,9 @@ namespace App\Console\Commands;
|
||||
|
||||
use App;
|
||||
use App\Contracts\Command;
|
||||
use App\Services\Installer\ConfigService;
|
||||
use App\Services\Installer\SeederService;
|
||||
use DatabaseSeeder;
|
||||
use Modules\Installer\Services\ConfigService;
|
||||
|
||||
/**
|
||||
* Create the config files
|
||||
@@ -81,13 +81,13 @@ class CreateConfigs extends Command
|
||||
|
||||
$this->info('Regenerating the config files');
|
||||
$cfgSvc->createConfigFiles([
|
||||
'APP_ENV' => 'dev',
|
||||
'SITE_NAME' => $this->argument('name'),
|
||||
'DB_CONN' => 'mysql',
|
||||
'DB_HOST' => $this->argument('db_host'),
|
||||
'DB_NAME' => $this->argument('db_name'),
|
||||
'DB_USER' => $this->argument('db_user'),
|
||||
'DB_PASS' => $this->argument('db_pass'),
|
||||
'APP_ENV' => 'dev',
|
||||
'SITE_NAME' => $this->argument('name'),
|
||||
'DB_CONNECTION' => 'mysql',
|
||||
'DB_HOST' => $this->argument('db_host'),
|
||||
'DB_DATABASE' => $this->argument('db_name'),
|
||||
'DB_USERNAME' => $this->argument('db_user'),
|
||||
'DB_PASSWORD' => $this->argument('db_pass'),
|
||||
]);
|
||||
|
||||
$this->info('Config files generated!');
|
||||
|
||||
@@ -79,9 +79,9 @@ class DevInstall extends Command
|
||||
|
||||
$this->info('Regenerating the config files');
|
||||
$cfgSvc->createConfigFiles([
|
||||
'APP_ENV' => 'dev',
|
||||
'SITE_NAME' => 'phpvms test',
|
||||
'DB_CONN' => 'sqlite',
|
||||
'APP_ENV' => 'dev',
|
||||
'SITE_NAME' => 'phpvms test',
|
||||
'DB_CONNECTION' => 'sqlite',
|
||||
]);
|
||||
|
||||
$this->info('Config files generated!');
|
||||
|
||||
25
app/Console/Commands/RewriteConfigs.php
Normal file
25
app/Console/Commands/RewriteConfigs.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Contracts\Command;
|
||||
use App\Services\Installer\ConfigService;
|
||||
|
||||
/**
|
||||
* Command to rewrite the config files
|
||||
*/
|
||||
class RewriteConfigs extends Command
|
||||
{
|
||||
protected $signature = 'phpvms:rewrite-configs';
|
||||
protected $description = 'Rewrite the config files';
|
||||
|
||||
/**
|
||||
* Run dev related commands
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
/** @var ConfigService $configSvc */
|
||||
$configSvc = app(ConfigService::class);
|
||||
$configSvc->rewriteConfigFiles();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
use App\Contracts\Migration;
|
||||
use App\Services\Installer\ConfigService;
|
||||
|
||||
/**
|
||||
* Migrate the configuration files
|
||||
*/
|
||||
class MigrateConfigs extends Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
/** @var ConfigService $configSvc */
|
||||
$configSvc = app(ConfigService::class);
|
||||
$configSvc->rewriteConfigFiles();
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -203,21 +203,21 @@ class InstallerController extends Controller
|
||||
Log::error('Testing db before writing configs failed');
|
||||
Log::error($e->getMessage());
|
||||
|
||||
Flash::error($e->getMessage());
|
||||
flash()->error($e->getMessage());
|
||||
return redirect(route('installer.step2'))->withInput();
|
||||
}
|
||||
|
||||
// Now write out the env file
|
||||
$attrs = [
|
||||
'SITE_NAME' => $request->post('site_name'),
|
||||
'SITE_URL' => $request->post('site_url'),
|
||||
'DB_CONN' => $request->post('db_conn'),
|
||||
'DB_HOST' => $request->post('db_host'),
|
||||
'DB_PORT' => $request->post('db_port'),
|
||||
'DB_NAME' => $request->post('db_name'),
|
||||
'DB_USER' => $request->post('db_user'),
|
||||
'DB_PASS' => $request->post('db_pass'),
|
||||
'DB_PREFIX' => $request->post('db_prefix'),
|
||||
'SITE_NAME' => $request->post('site_name'),
|
||||
'SITE_URL' => $request->post('site_url'),
|
||||
'DB_CONNECTION' => $request->post('db_conn'),
|
||||
'DB_HOST' => $request->post('db_host'),
|
||||
'DB_PORT' => $request->post('db_port'),
|
||||
'DB_DATABASE' => $request->post('db_name'),
|
||||
'DB_USERNAME' => $request->post('db_user'),
|
||||
'DB_PASSWORD' => $request->post('db_pass'),
|
||||
'DB_PREFIX' => $request->post('db_prefix'),
|
||||
];
|
||||
|
||||
/*
|
||||
@@ -231,7 +231,7 @@ class InstallerController extends Controller
|
||||
Log::error('Config files failed to write');
|
||||
Log::error($e->getMessage());
|
||||
|
||||
Flash::error($e->getMessage());
|
||||
flash()->error($e->getMessage());
|
||||
return redirect(route('installer.step2'))->withInput();
|
||||
}
|
||||
|
||||
@@ -257,7 +257,7 @@ class InstallerController extends Controller
|
||||
Log::error('Error on db setup: '.$e->getMessage());
|
||||
//dd($e);
|
||||
$this->envSvc->removeConfigFiles();
|
||||
Flash::error($e->getMessage());
|
||||
flash()->error($e->getMessage());
|
||||
return redirect(route('installer.step2'))->withInput();
|
||||
}
|
||||
|
||||
|
||||
@@ -15,34 +15,49 @@ use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
|
||||
class ConfigService extends Service
|
||||
{
|
||||
protected static $defaultValues = [
|
||||
'APP_ENV' => 'dev',
|
||||
'APP_KEY' => '',
|
||||
'APP_DEBUG' => true,
|
||||
'APP_LOCALE' => 'en',
|
||||
'DEBUG_TOOLBAR' => false,
|
||||
'SITE_NAME' => '',
|
||||
'SITE_URL' => 'http://phpvms.test',
|
||||
'DB_CONNECTION' => '',
|
||||
'DB_HOST' => '',
|
||||
'DB_PORT' => 3306,
|
||||
'DB_DATABASE' => '',
|
||||
'DB_USERNAME' => '',
|
||||
'DB_PASSWORD' => '',
|
||||
'DB_PREFIX' => '',
|
||||
'DB_EMULATE_PREPARES' => false,
|
||||
'CACHE_DRIVER' => 'array',
|
||||
'CACHE_PREFIX' => '',
|
||||
'MAIL_DRIVER' => 'smtp',
|
||||
'MAIL_HOST' => '',
|
||||
'MAIL_PORT' => 587,
|
||||
'MAIL_ENCRYPTION' => '',
|
||||
'MAIL_USERNAME' => '',
|
||||
'MAIL_PASSWORD' => '',
|
||||
'MAIL_FROM_NAME' => 'phpVMS Admin',
|
||||
'MAIL_FROM_ADDRESS' => 'no-reply@phpvms.net',
|
||||
];
|
||||
|
||||
/**
|
||||
* Create the .env file
|
||||
* Create the .env file. This is called by an initial install
|
||||
*
|
||||
* @param $attrs
|
||||
*
|
||||
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
||||
* @throws FileException
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function createConfigFiles($attrs): bool
|
||||
{
|
||||
$opts = [
|
||||
'APP_ENV' => 'dev',
|
||||
'APP_KEY' => $this->createAppKey(),
|
||||
'SITE_NAME' => '',
|
||||
'SITE_URL' => 'http://phpvms.test',
|
||||
'CACHE_PREFIX' => '',
|
||||
'DB_CONN' => '',
|
||||
'DB_HOST' => '',
|
||||
'DB_PORT' => 3306,
|
||||
'DB_NAME' => '',
|
||||
'DB_USER' => '',
|
||||
'DB_PASS' => '',
|
||||
'DB_PREFIX' => '',
|
||||
'DB_EMULATE_PREPARES' => false,
|
||||
];
|
||||
|
||||
$opts = array_merge($opts, $attrs);
|
||||
$opts = array_merge(static::$defaultValues, $attrs);
|
||||
if (empty($opts['APP_KEY'])) {
|
||||
$opts['APP_KEY'] = $this->createAppKey();
|
||||
}
|
||||
|
||||
$opts = $this->determinePdoOptions($opts);
|
||||
$opts = $this->configCacheDriver($opts);
|
||||
@@ -53,6 +68,61 @@ class ConfigService extends Service
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewrite the config files - this means mapping the values that are currently
|
||||
* loaded in the config and rewriting them into the env.php file, and then renaming
|
||||
* the config.php files to config.bak.php
|
||||
*
|
||||
* This is called from the migrations which removes the old config.php file
|
||||
*/
|
||||
public function rewriteConfigFiles()
|
||||
{
|
||||
/*$cfg_file = App::environmentPath().'/config.php';
|
||||
if (!file_exists($cfg_file)) {
|
||||
Log::info('Main config.php file is missing, migration already completed');
|
||||
return;
|
||||
}*/
|
||||
|
||||
$db_opts = config('database.connections.mysql.options');
|
||||
$emulate_prepares = $db_opts[PDO::ATTR_EMULATE_PREPARES] ? 'true' : 'false';
|
||||
|
||||
$opts = array_merge(static::$defaultValues, [
|
||||
'APP_ENV' => config('app.env'),
|
||||
'APP_KEY' => config('app.key'),
|
||||
'APP_DEBUG' => config('app.debug') ? 'true' : 'false',
|
||||
'APP_LOCALE' => config('app.locale'),
|
||||
'DEBUG_TOOLBAR' => config('app.debug_toolbar') ? 'true' : 'false',
|
||||
'SITE_NAME' => config('app.name'),
|
||||
'SITE_URL' => config('app.url'),
|
||||
'DB_CONNECTION' => config('database.default'),
|
||||
'DB_HOST' => config('database.connections.mysql.host'),
|
||||
'DB_PORT' => config('database.connections.mysql.port'),
|
||||
'DB_DATABASE' => config('database.connections.mysql.database'),
|
||||
'DB_USERNAME' => config('database.connections.mysql.username'),
|
||||
'DB_PASSWORD' => config('database.connections.mysql.password'),
|
||||
'DB_PREFIX' => config('database.connections.mysql.prefix'),
|
||||
'DB_EMULATE_PREPARES' => $emulate_prepares,
|
||||
'CACHE_DRIVER' => config('cache.default'),
|
||||
'CACHE_PREFIX' => config('cache.prefix'),
|
||||
'MAIL_DRIVER' => config('mail.default'),
|
||||
'MAIL_HOST' => config('mail.mailers.smtp.host'),
|
||||
'MAIL_PORT' => config('mail.mailers.smtp.port'),
|
||||
'MAIL_ENCRYPTION' => config('mail.mailers.smtp.encryption'),
|
||||
'MAIL_USERNAME' => config('mail.mailers.smtp.username'),
|
||||
'MAIL_PASSWORD' => config('mail.mailers.smtp.password'),
|
||||
'MAIL_FROM_NAME' => config('mail.from.name'),
|
||||
'MAIL_FROM_ADDRESS' => config('mail.from.address'),
|
||||
]);
|
||||
|
||||
$this->writeConfigFiles($opts);
|
||||
|
||||
// Rename the old config file
|
||||
$cfg_file = App::environmentPath().'/config.php';
|
||||
if (file_exists($cfg_file)) {
|
||||
rename($cfg_file, App::environmentPath().'/config.bak.php');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the environment file and update certain keys/values
|
||||
*
|
||||
@@ -97,7 +167,7 @@ class ConfigService extends Service
|
||||
*/
|
||||
protected function createAppKey(): string
|
||||
{
|
||||
return base64_encode(Encrypter::generateKey(config('app.cipher')));
|
||||
return 'base64:'.base64_encode(Encrypter::generateKey(config('app.cipher')));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,14 +180,14 @@ class ConfigService extends Service
|
||||
*/
|
||||
protected function determinePdoOptions($opts)
|
||||
{
|
||||
if ($opts['DB_CONN'] !== 'mysql') {
|
||||
if ($opts['DB_CONNECTION'] !== 'mysql') {
|
||||
return $opts;
|
||||
}
|
||||
|
||||
$dsn = "mysql:host=$opts[DB_HOST];port=$opts[DB_PORT];";
|
||||
Log::info('Connection string: '.$dsn);
|
||||
|
||||
$conn = new PDO($dsn, $opts['DB_USER'], $opts['DB_PASS']);
|
||||
$conn = new PDO($dsn, $opts['DB_USERNAME'], $opts['DB_PASSWORD']);
|
||||
$version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION));
|
||||
Log::info('Detected DB Version: '.$version);
|
||||
|
||||
@@ -171,7 +241,7 @@ class ConfigService extends Service
|
||||
{
|
||||
// If we're setting up a database, then also setup
|
||||
// the default queue driver to use the database
|
||||
if ($opts['DB_CONN'] === 'mysql' || $opts['DB_CONN'] === 'postgres') {
|
||||
if ($opts['DB_CONNECTION'] === 'mysql' || $opts['DB_CONNECTION'] === 'postgres') {
|
||||
$opts['QUEUE_DRIVER'] = 'database';
|
||||
} else {
|
||||
$opts['QUEUE_DRIVER'] = 'sync';
|
||||
@@ -210,14 +280,13 @@ class ConfigService extends Service
|
||||
*
|
||||
* @param $opts
|
||||
*
|
||||
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
||||
* @throws FileException
|
||||
*/
|
||||
protected function writeConfigFiles($opts)
|
||||
{
|
||||
Stub::setBasePath(resource_path('/stubs/installer'));
|
||||
|
||||
$env_file = App::environmentFilePath();
|
||||
|
||||
if (file_exists($env_file) && !is_writable($env_file)) {
|
||||
Log::error('Permissions on existing env.php is not writable');
|
||||
|
||||
@@ -239,7 +308,7 @@ class ConfigService extends Service
|
||||
* Next write out the config file. If there's an error here,
|
||||
* then throw an exception but delete the env file first
|
||||
*/
|
||||
try {
|
||||
/*try {
|
||||
$stub = new Stub('/config.stub', $opts);
|
||||
$stub->render();
|
||||
$stub->saveTo(App::environmentPath(), 'config.php');
|
||||
@@ -247,6 +316,6 @@ class ConfigService extends Service
|
||||
unlink(App::environmentPath().'/'.App::environmentFile());
|
||||
|
||||
throw new FileException('Couldn\'t write config.php. ('.$e.')');
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user