2017-12-15 06:38:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Services;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Service;
|
2019-07-18 21:26:33 +08:00
|
|
|
use Exception;
|
|
|
|
use function extension_loaded;
|
2018-02-26 05:27:20 +08:00
|
|
|
use Illuminate\Encryption\Encrypter;
|
2019-07-18 21:26:33 +08:00
|
|
|
use Illuminate\Support\Facades\App;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use function is_bool;
|
2018-01-29 08:50:43 +08:00
|
|
|
use Nwidart\Modules\Support\Stub;
|
2018-02-26 05:27:20 +08:00
|
|
|
use PDO;
|
2017-12-18 06:58:53 +08:00
|
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
/**
|
2018-01-30 03:16:39 +08:00
|
|
|
* Class ConfigService
|
2018-01-10 13:05:52 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
class ConfigService extends Service
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
|
|
|
/**
|
2017-12-15 07:12:42 +08:00
|
|
|
* Create the .env file
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-30 03:16:39 +08:00
|
|
|
* @param $attrs
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-10 13:05:52 +08:00
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
|
|
|
* @return bool
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
2018-01-30 03:16:39 +08:00
|
|
|
public function createConfigFiles($attrs): bool
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2017-12-15 07:12:42 +08:00
|
|
|
$opts = [
|
2019-07-18 21:26:33 +08:00
|
|
|
'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' => '',
|
2018-01-10 13:05:52 +08:00
|
|
|
'DB_EMULATE_PREPARES' => false,
|
2017-12-15 06:38:29 +08:00
|
|
|
];
|
|
|
|
|
2018-01-30 03:16:39 +08:00
|
|
|
$opts = array_merge($opts, $attrs);
|
|
|
|
|
2018-01-11 09:15:42 +08:00
|
|
|
$opts = $this->determinePdoOptions($opts);
|
2019-07-18 21:26:33 +08:00
|
|
|
$opts = $this->configCacheDriver($opts);
|
|
|
|
$opts = $this->configQueueDriver($opts);
|
2017-12-15 07:12:42 +08:00
|
|
|
|
2018-01-30 03:16:39 +08:00
|
|
|
$this->writeConfigFiles($opts);
|
2017-12-15 06:38:29 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-19 09:24:06 +08:00
|
|
|
/**
|
|
|
|
* Update the environment file and update certain keys/values
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 09:24:06 +08:00
|
|
|
* @param array $kvp
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-19 09:24:06 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateKeysInEnv(array $kvp)
|
|
|
|
{
|
|
|
|
$app = app();
|
|
|
|
|
|
|
|
$env_file = file_get_contents($app->environmentFilePath());
|
2019-08-27 00:32:46 +08:00
|
|
|
foreach ($kvp as $key => $value) {
|
2018-01-19 10:40:25 +08:00
|
|
|
$key = strtoupper($key);
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// cast for any boolean values
|
|
|
|
if (is_bool($value)) {
|
2018-01-19 09:24:06 +08:00
|
|
|
$value = $value === true ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// surround by quotes if there are any spaces in the value
|
|
|
|
if (strpos($value, ' ') !== false) {
|
2018-01-19 10:40:25 +08:00
|
|
|
$value = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
Log::info('Replacing "'.$key.'" with '.$value);
|
2018-01-19 10:40:25 +08:00
|
|
|
|
2018-01-19 09:24:06 +08:00
|
|
|
$env_file = preg_replace(
|
2019-08-27 00:32:46 +08:00
|
|
|
'/^'.$key.'(.*)?/m',
|
|
|
|
$key.'='.$value,
|
2018-01-19 09:24:06 +08:00
|
|
|
$env_file
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($app->environmentFilePath(), $env_file);
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
/**
|
|
|
|
* Generate a fresh new APP_KEY
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-19 10:40:25 +08:00
|
|
|
protected function createAppKey(): string
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
|
|
|
return base64_encode(Encrypter::generateKey(config('app.cipher')));
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
/**
|
2018-01-19 09:24:06 +08:00
|
|
|
* Change a few options within the PDO driver, depending on the version
|
|
|
|
* of mysql/maria, etc used. ATM, only make a change for MariaDB
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-10 13:05:52 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2018-01-10 13:05:52 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function determinePdoOptions($opts)
|
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
if ($opts['DB_CONN'] !== 'mysql') {
|
2018-01-19 09:24:06 +08:00
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
$dsn = "mysql:host=$opts[DB_HOST];port=$opts[DB_PORT];";
|
2019-08-27 00:32:46 +08:00
|
|
|
Log::info('Connection string: '.$dsn);
|
2018-01-10 13:05:52 +08:00
|
|
|
|
|
|
|
$conn = new PDO($dsn, $opts['DB_USER'], $opts['DB_PASS']);
|
|
|
|
$version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION));
|
2018-01-10 13:17:06 +08:00
|
|
|
Log::info('Detected DB Version: '.$version);
|
2018-01-10 13:05:52 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
// If it's mariadb, enable the emulation for prepared statements
|
|
|
|
// seems to be throwing a problem on 000webhost
|
|
|
|
// https://github.com/nabeelio/phpvms/issues/132
|
|
|
|
if (strpos($version, 'mariadb') !== false) {
|
2018-01-10 13:17:06 +08:00
|
|
|
Log::info('Detected MariaDB, setting DB_EMULATE_PREPARES to true');
|
2018-01-10 13:05:52 +08:00
|
|
|
$opts['DB_EMULATE_PREPARES'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
/**
|
|
|
|
* Determine is APC is installed, if so, then use it as a cache driver
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-07-18 21:26:33 +08:00
|
|
|
protected function configCacheDriver($opts)
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
2019-07-18 21:26:33 +08:00
|
|
|
// Set the cache prefix
|
|
|
|
$opts['CACHE_PREFIX'] = $opts['SITE_NAME'].'_';
|
|
|
|
|
|
|
|
// Figure out what cache driver to initially use, depending on
|
|
|
|
// what is installed. It won't detect redis or anything, though
|
|
|
|
foreach (config('installer.cache.drivers') as $ext => $driver) {
|
|
|
|
if (extension_loaded($ext)) {
|
|
|
|
Log::info('Detected extension "'.$ext.'", setting driver to "'.$driver.'"');
|
|
|
|
$opts['CACHE_DRIVER'] = $driver;
|
|
|
|
return $opts;
|
|
|
|
}
|
2017-12-15 07:12:42 +08:00
|
|
|
}
|
|
|
|
|
2019-07-18 21:26:33 +08:00
|
|
|
Log::info('No extension detected, using file cache');
|
|
|
|
$opts['CACHE_DRIVER'] = config('installer.cache.default');
|
2017-12-15 07:12:42 +08:00
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup a queue driver that's not the default "sync"
|
|
|
|
* driver, if a database is being used
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2019-07-18 21:26:33 +08:00
|
|
|
protected function configQueueDriver($opts)
|
2017-12-15 07:12:42 +08:00
|
|
|
{
|
2019-08-27 00:32:46 +08:00
|
|
|
// If we're setting up a database, then also setup
|
|
|
|
// the default queue driver to use the database
|
2017-12-15 07:12:42 +08:00
|
|
|
if ($opts['DB_CONN'] === 'mysql' || $opts['DB_CONN'] === 'postgres') {
|
|
|
|
$opts['QUEUE_DRIVER'] = 'database';
|
|
|
|
} else {
|
|
|
|
$opts['QUEUE_DRIVER'] = 'sync';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2018-02-26 05:13:04 +08:00
|
|
|
/**
|
|
|
|
* Remove the config files
|
|
|
|
*/
|
|
|
|
public function removeConfigFiles()
|
|
|
|
{
|
2019-07-18 21:26:33 +08:00
|
|
|
$env_file = App::environmentFilePath();
|
|
|
|
$config_file = App::environmentPath().'/config.php';
|
2018-02-26 05:13:04 +08:00
|
|
|
|
|
|
|
if (file_exists($env_file)) {
|
|
|
|
try {
|
|
|
|
unlink($env_file);
|
2019-07-18 21:26:33 +08:00
|
|
|
} catch (Exception $e) {
|
2018-02-26 05:13:04 +08:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
if (file_exists($config_file)) {
|
2018-02-26 05:13:04 +08:00
|
|
|
try {
|
|
|
|
unlink($config_file);
|
2019-07-18 21:26:33 +08:00
|
|
|
} catch (Exception $e) {
|
2018-02-26 05:13:04 +08:00
|
|
|
Log::error($e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Get the template file name and write it out
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
2019-08-27 00:32:46 +08:00
|
|
|
*
|
2017-12-18 06:58:53 +08:00
|
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
2017-12-15 06:38:29 +08:00
|
|
|
*/
|
2018-01-30 03:16:39 +08:00
|
|
|
protected function writeConfigFiles($opts)
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2018-01-30 03:16:39 +08:00
|
|
|
Stub::setBasePath(resource_path('/stubs/installer'));
|
|
|
|
|
2019-07-18 21:26:33 +08:00
|
|
|
$env_file = App::environmentFilePath();
|
2017-12-18 06:58:53 +08:00
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
if (file_exists($env_file) && !is_writable($env_file)) {
|
2017-12-18 06:58:53 +08:00
|
|
|
Log::error('Permissions on existing env.php is not writable');
|
2019-08-27 00:32:46 +08:00
|
|
|
|
2017-12-18 06:58:53 +08:00
|
|
|
throw new FileException('Can\'t write to the env.php file! Check the permissions');
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
/*
|
2018-01-30 03:16:39 +08:00
|
|
|
* First write out the env file
|
|
|
|
*/
|
2018-01-29 08:50:43 +08:00
|
|
|
try {
|
|
|
|
$stub = new Stub('/env.stub', $opts);
|
|
|
|
$stub->render();
|
2019-07-18 21:26:33 +08:00
|
|
|
$stub->saveTo(App::environmentPath(), App::environmentFile());
|
2019-08-27 00:32:46 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new FileException('Couldn\'t write env.php. ('.$e.')');
|
2018-01-29 08:50:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-27 00:32:46 +08:00
|
|
|
/*
|
2018-01-30 03:16:39 +08:00
|
|
|
* Next write out the config file. If there's an error here,
|
|
|
|
* then throw an exception but delete the env file first
|
|
|
|
*/
|
|
|
|
try {
|
|
|
|
$stub = new Stub('/config.stub', $opts);
|
|
|
|
$stub->render();
|
2019-07-18 21:26:33 +08:00
|
|
|
$stub->saveTo(App::environmentPath(), 'config.php');
|
|
|
|
} catch (Exception $e) {
|
2019-08-27 00:32:46 +08:00
|
|
|
unlink(App::environmentPath().'/'.App::environmentFile());
|
|
|
|
|
|
|
|
throw new FileException('Couldn\'t write config.php. ('.$e.')');
|
2017-12-18 06:58:53 +08:00
|
|
|
}
|
2017-12-15 06:38:29 +08:00
|
|
|
}
|
|
|
|
}
|