2017-12-15 06:38:29 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Installer\Services;
|
|
|
|
|
|
|
|
use Log;
|
2018-01-10 13:05:52 +08:00
|
|
|
use PDO;
|
2018-01-29 08:50:43 +08:00
|
|
|
use Nwidart\Modules\Support\Stub;
|
2018-01-10 13:05:52 +08:00
|
|
|
use Illuminate\Encryption\Encrypter;
|
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
|
|
|
* @package Modules\Installer\Services
|
|
|
|
*/
|
2018-01-30 03:16:39 +08:00
|
|
|
class ConfigService
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
|
|
|
/**
|
2017-12-15 07:12:42 +08:00
|
|
|
* Create the .env file
|
2018-01-30 03:16:39 +08:00
|
|
|
* @param $attrs
|
2017-12-15 06:38:29 +08:00
|
|
|
* @return boolean
|
2018-01-10 13:05:52 +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
|
|
|
public function createConfigFiles($attrs): bool
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
2017-12-15 07:12:42 +08:00
|
|
|
$opts = [
|
2017-12-15 11:34:11 +08:00
|
|
|
'APP_ENV' => 'dev',
|
2017-12-15 07:12:42 +08:00
|
|
|
'APP_KEY' => $this->createAppKey(),
|
2018-01-30 03:16:39 +08:00
|
|
|
'SITE_NAME' => '',
|
|
|
|
'SITE_URL' => 'http://phpvms.test',
|
|
|
|
'DB_CONN' => '',
|
|
|
|
'DB_HOST' => '',
|
2018-02-10 07:11:55 +08:00
|
|
|
'DB_PORT' => 3306,
|
2018-01-30 03:16:39 +08:00
|
|
|
'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);
|
2017-12-15 07:12:42 +08:00
|
|
|
$opts = $this->getCacheDriver($opts);
|
|
|
|
$opts = $this->getQueueDriver($opts);
|
|
|
|
|
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
|
|
|
|
* @param array $kvp
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function updateKeysInEnv(array $kvp)
|
|
|
|
{
|
|
|
|
$app = app();
|
|
|
|
|
|
|
|
$env_file = file_get_contents($app->environmentFilePath());
|
|
|
|
foreach($kvp as $key => $value) {
|
|
|
|
|
2018-01-19 10:40:25 +08:00
|
|
|
$key = strtoupper($key);
|
|
|
|
|
|
|
|
# cast for any boolean values
|
2018-01-19 09:24:06 +08:00
|
|
|
if(\is_bool($value)) {
|
|
|
|
$value = $value === true ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
2018-01-19 10:40:25 +08:00
|
|
|
# surround by quotes if there are any spaces in the value
|
|
|
|
if(strpos($value, ' ') !== false) {
|
|
|
|
$value = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
Log::info('Replacing "' . $key . '" with ' . $value);
|
|
|
|
|
2018-01-19 09:24:06 +08:00
|
|
|
$env_file = preg_replace(
|
|
|
|
'/^' . $key . '(.*)?/m',
|
|
|
|
$key . '=' . $value,
|
|
|
|
$env_file
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
file_put_contents($app->environmentFilePath(), $env_file);
|
|
|
|
}
|
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
/**
|
|
|
|
* Generate a fresh new APP_KEY
|
|
|
|
* @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
|
2018-01-10 13:05:52 +08:00
|
|
|
* @param $opts
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function determinePdoOptions($opts)
|
|
|
|
{
|
2018-01-19 09:24:06 +08:00
|
|
|
if($opts['DB_CONN'] !== 'mysql') {
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2018-01-10 13:05:52 +08:00
|
|
|
$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']);
|
|
|
|
$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
|
|
|
|
|
|
|
# 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
|
|
|
|
* @param $opts
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getCacheDriver($opts)
|
|
|
|
{
|
|
|
|
if(\extension_loaded('apc')) {
|
|
|
|
$opts['CACHE_DRIVER'] = 'apc';
|
|
|
|
} else {
|
2017-12-18 06:58:53 +08:00
|
|
|
$opts['CACHE_DRIVER'] = 'array';
|
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
|
|
|
|
* @param $opts
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function getQueueDriver($opts)
|
|
|
|
{
|
|
|
|
# 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') {
|
|
|
|
$opts['QUEUE_DRIVER'] = 'database';
|
|
|
|
} else {
|
|
|
|
$opts['QUEUE_DRIVER'] = 'sync';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $opts;
|
|
|
|
}
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
/**
|
|
|
|
* Get the template file name and write it out
|
2017-12-15 07:12:42 +08:00
|
|
|
* @param $opts
|
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'));
|
|
|
|
|
2017-12-18 06:58:53 +08:00
|
|
|
$env_file = \App::environmentFilePath();
|
|
|
|
|
|
|
|
if(file_exists($env_file) && !is_writable($env_file)) {
|
|
|
|
Log::error('Permissions on existing env.php is not writable');
|
|
|
|
throw new FileException('Can\'t write to the env.php file! Check the permissions');
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
$stub->saveTo(\App::environmentPath(), \App::environmentFile());
|
|
|
|
} catch(\Exception $e) {
|
2018-01-30 03:16:39 +08:00
|
|
|
throw new FileException('Couldn\'t write env.php. (' . $e . ')');
|
2018-01-29 08:50:43 +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();
|
|
|
|
$stub->saveTo(\App::environmentPath(), 'config.php');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|