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;
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Class EnvironmentService
|
|
|
|
* @package Modules\Installer\Services
|
|
|
|
*/
|
2017-12-15 06:38:29 +08:00
|
|
|
class EnvironmentService
|
|
|
|
{
|
|
|
|
/**
|
2017-12-15 07:12:42 +08:00
|
|
|
* Create the .env file
|
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
|
|
|
*/
|
2017-12-15 12:32:52 +08:00
|
|
|
public function createEnvFile($driver, $host, $port, $name, $user, $pass)
|
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(),
|
2017-12-15 12:32:52 +08:00
|
|
|
'DB_CONN' => $driver,
|
2017-12-15 07:12:42 +08:00
|
|
|
'DB_HOST' => $host,
|
|
|
|
'DB_PORT' => $port,
|
|
|
|
'DB_NAME' => $name,
|
|
|
|
'DB_USER' => $user,
|
|
|
|
'DB_PASS' => $pass,
|
2018-01-10 13:05:52 +08:00
|
|
|
'DB_EMULATE_PREPARES' => false,
|
2017-12-15 06:38:29 +08:00
|
|
|
];
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
$this->writeEnvFile($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) {
|
|
|
|
|
|
|
|
# cast
|
|
|
|
if(\is_bool($value)) {
|
|
|
|
$value = $value === true ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
protected function createAppKey()
|
|
|
|
{
|
|
|
|
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-20 05:19:06 +08:00
|
|
|
/**
|
|
|
|
* Update a key/value pair in the env file
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public static function changeEnvironmentVariable($key, $value, $quoted=false)
|
|
|
|
{
|
|
|
|
$env_file = \App::environmentFilePath();
|
|
|
|
|
|
|
|
if (\is_bool(env($key))) {
|
|
|
|
$old = env($key) ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
|
|
|
|
if($quoted) {
|
|
|
|
$value = '"'.$value.'"';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists($env_file)) {
|
|
|
|
file_put_contents($env_file, str_replace(
|
|
|
|
"$key=" . $old, "$key=" . $value, file_get_contents($env_file)
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2017-12-15 07:12:42 +08:00
|
|
|
protected function writeEnvFile($opts)
|
2017-12-15 06:38:29 +08:00
|
|
|
{
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
$fp = fopen($env_file, 'wb');
|
|
|
|
if($fp === false) {
|
|
|
|
throw new FileException('Couldn\'t write the env.php. (' . error_get_last() .')');
|
|
|
|
}
|
2017-12-15 06:38:29 +08:00
|
|
|
|
2017-12-15 07:12:42 +08:00
|
|
|
# render it within Blade and log the contents
|
|
|
|
$env_contents = view('installer::stubs/env', $opts);
|
2017-12-15 06:38:29 +08:00
|
|
|
Log::info($env_contents);
|
|
|
|
|
2018-01-09 21:09:16 +08:00
|
|
|
$env_contents = "<?php exit(); ?>\n\n"
|
|
|
|
.$env_contents;
|
|
|
|
|
2017-12-15 06:38:29 +08:00
|
|
|
fwrite($fp, $env_contents);
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
}
|