199 lines
5.5 KiB
PHP
199 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace Modules\Installer\Services;
|
|
|
|
use Log;
|
|
use PDO;
|
|
use Nwidart\Modules\Support\Stub;
|
|
use Illuminate\Encryption\Encrypter;
|
|
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
|
|
|
/**
|
|
* Class EnvironmentService
|
|
* @package Modules\Installer\Services
|
|
*/
|
|
class EnvironmentService
|
|
{
|
|
/**
|
|
* Create the .env file
|
|
* @param $driver
|
|
* @param $host
|
|
* @param $port
|
|
* @param $name
|
|
* @param $user
|
|
* @param $pass
|
|
* @return boolean
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
|
*/
|
|
public function createEnvFile($driver, $host, $port, $name, $user, $pass): bool
|
|
{
|
|
$opts = [
|
|
'APP_ENV' => 'dev',
|
|
'APP_KEY' => $this->createAppKey(),
|
|
'DB_CONN' => $driver,
|
|
'DB_HOST' => $host,
|
|
'DB_PORT' => $port,
|
|
'DB_NAME' => $name,
|
|
'DB_USER' => $user,
|
|
'DB_PASS' => $pass,
|
|
'DB_EMULATE_PREPARES' => false,
|
|
];
|
|
|
|
$opts = $this->determinePdoOptions($opts);
|
|
$opts = $this->getCacheDriver($opts);
|
|
$opts = $this->getQueueDriver($opts);
|
|
|
|
$this->writeEnvFile($opts);
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 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) {
|
|
|
|
$key = strtoupper($key);
|
|
|
|
# cast for any boolean values
|
|
if(\is_bool($value)) {
|
|
$value = $value === true ? 'true' : 'false';
|
|
}
|
|
|
|
# surround by quotes if there are any spaces in the value
|
|
if(strpos($value, ' ') !== false) {
|
|
$value = '"'.$value.'"';
|
|
}
|
|
|
|
Log::info('Replacing "' . $key . '" with ' . $value);
|
|
|
|
$env_file = preg_replace(
|
|
'/^' . $key . '(.*)?/m',
|
|
$key . '=' . $value,
|
|
$env_file
|
|
);
|
|
}
|
|
|
|
file_put_contents($app->environmentFilePath(), $env_file);
|
|
}
|
|
|
|
/**
|
|
* Generate a fresh new APP_KEY
|
|
* @return string
|
|
*/
|
|
protected function createAppKey(): string
|
|
{
|
|
return base64_encode(Encrypter::generateKey(config('app.cipher')));
|
|
}
|
|
|
|
/**
|
|
* Change a few options within the PDO driver, depending on the version
|
|
* of mysql/maria, etc used. ATM, only make a change for MariaDB
|
|
* @param $opts
|
|
* @return mixed
|
|
*/
|
|
protected function determinePdoOptions($opts)
|
|
{
|
|
if($opts['DB_CONN'] !== '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']);
|
|
$version = strtolower($conn->getAttribute(PDO::ATTR_SERVER_VERSION));
|
|
Log::info('Detected DB Version: '.$version);
|
|
|
|
# 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) {
|
|
Log::info('Detected MariaDB, setting DB_EMULATE_PREPARES to true');
|
|
$opts['DB_EMULATE_PREPARES'] = true;
|
|
}
|
|
|
|
return $opts;
|
|
}
|
|
|
|
/**
|
|
* 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 {
|
|
$opts['CACHE_DRIVER'] = 'array';
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/**
|
|
* Get the template file name and write it out
|
|
* @param $opts
|
|
* @throws \Symfony\Component\HttpFoundation\File\Exception\FileException
|
|
*/
|
|
protected function writeEnvFile($opts)
|
|
{
|
|
$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');
|
|
}
|
|
|
|
try {
|
|
$stub = new Stub('/env.stub', $opts);
|
|
$stub->render();
|
|
$stub->saveTo(\App::environmentPath(), \App::environmentFile());
|
|
} catch(\Exception $e) {
|
|
throw new FileException('Couldn\'t write the env.php. (' . $e . ')');
|
|
}
|
|
|
|
/*$fp = fopen($env_file, 'wb');
|
|
if($fp === false) {
|
|
throw new FileException('Couldn\'t write the env.php. (' . error_get_last() .')');
|
|
}
|
|
|
|
# render it within Blade and log the contents
|
|
$env_contents = view('installer::stubs/env', $opts);
|
|
Log::info($env_contents);
|
|
|
|
$env_contents = "<?php exit(); ?>\n\n"
|
|
.$env_contents;
|
|
|
|
fwrite($fp, $env_contents);
|
|
fclose($fp);*/
|
|
}
|
|
}
|