2016-05-31 13:50:41 +02:00
|
|
|
<?php namespace System\Console;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
|
2017-03-16 17:08:20 +11:00
|
|
|
/**
|
|
|
|
* Console command to convert configuration to use .env files.
|
|
|
|
*
|
|
|
|
* This creates an .env file with some default configuration values, it also converts
|
|
|
|
* the existing PHP-based configuration files to use the `env` function for values.
|
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2016-05-31 13:50:41 +02:00
|
|
|
class OctoberEnv extends Command
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command name.
|
|
|
|
*/
|
|
|
|
protected $name = 'october:env';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*/
|
|
|
|
protected $description = 'Creates .env file with default configuration values.';
|
|
|
|
|
2018-04-06 01:18:07 -06:00
|
|
|
/**
|
|
|
|
* @var array The env keys that need to have their original values removed from the config files
|
|
|
|
*/
|
|
|
|
protected $protectedKeys = [
|
|
|
|
'APP_KEY',
|
|
|
|
'DB_USERNAME',
|
|
|
|
'DB_PASSWORD',
|
|
|
|
'MAIL_USERNAME',
|
|
|
|
'MAIL_PASSWORD',
|
|
|
|
'REDIS_PASSWORD',
|
|
|
|
];
|
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
/**
|
|
|
|
* The current config cursor.
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The current database connection cursor.
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
2016-05-31 13:50:41 +02:00
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
2017-07-14 16:28:47 +10:00
|
|
|
public function handle()
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
|
|
|
if (file_exists('.env')) {
|
|
|
|
return $this->error('.env file already exists.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->overwriteConfig();
|
|
|
|
|
|
|
|
$this->info('.env configuration file has been created.');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overwrite config file
|
|
|
|
*/
|
|
|
|
private function overwriteConfig()
|
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
foreach (array_keys($this->config()) as $config) {
|
|
|
|
$this->config = $config;
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
$this->configToEnv();
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace config values with env() syntax
|
2016-06-05 15:58:54 +02:00
|
|
|
*/
|
|
|
|
private function configToEnv()
|
|
|
|
{
|
|
|
|
$content = $this->parseConfigFile();
|
|
|
|
|
|
|
|
$this->writeToConfigFile($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse config file line by line
|
2016-05-31 13:50:41 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-05 15:58:54 +02:00
|
|
|
private function parseConfigFile()
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
$lines = [];
|
|
|
|
|
|
|
|
foreach ($this->lines() as $line) {
|
|
|
|
$keys = $this->config()[$this->config];
|
|
|
|
|
|
|
|
$lines[] = $this->parseLine($line, $keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->writeToEnv("\n");
|
|
|
|
|
|
|
|
return implode('', $lines);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $keys
|
|
|
|
* @param $line
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function parseLine($line, $keys)
|
|
|
|
{
|
|
|
|
$line = $this->replaceConfigLine($line, $keys);
|
|
|
|
|
|
|
|
$line = $this->replaceDbConfigLine($line);
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $line
|
|
|
|
* @param $keys
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function replaceConfigLine($line, $keys)
|
|
|
|
{
|
2016-05-31 13:50:41 +02:00
|
|
|
foreach ($keys as $envKey => $configKey) {
|
|
|
|
$pattern = $this->buildPattern($configKey);
|
2016-06-05 15:58:54 +02:00
|
|
|
$callback = $this->buildCallback($envKey, $configKey);
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
if (preg_match($pattern, $line)) {
|
|
|
|
$line = preg_replace_callback($pattern, $callback, $line);
|
|
|
|
}
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $line
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function replaceDbConfigLine($line)
|
|
|
|
{
|
|
|
|
if ($this->config == 'database') {
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
foreach ($this->dbConfig() as $connection => $settings) {
|
|
|
|
$this->setCurrentConnection($line, $connection);
|
|
|
|
|
|
|
|
if ($this->connection == $connection) {
|
|
|
|
$line = $this->replaceConfigLine($line, $settings);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $line
|
|
|
|
* @param $connection
|
|
|
|
*/
|
|
|
|
private function setCurrentConnection($line, $connection)
|
|
|
|
{
|
|
|
|
if (preg_match("/['\"]" . $connection . "['\"]" . "\s*=>/", $line)) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
}
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $configKey
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function buildPattern($configKey)
|
|
|
|
{
|
|
|
|
return "/['\"]" . $configKey . "['\"]" . "\s*=>\s*[^,\[]+,/";
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $envKey
|
|
|
|
* @param $configKey
|
|
|
|
* @return \Closure
|
|
|
|
*/
|
2016-06-05 15:58:54 +02:00
|
|
|
private function buildCallback($envKey, $configKey)
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
return function ($matches) use ($envKey, $configKey) {
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
$value = $this->envValue($configKey);
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
$this->saveEnvSettings($envKey, $value);
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2018-04-06 01:18:07 -06:00
|
|
|
// Remove protected values from the config files
|
|
|
|
if (in_array($envKey, $this->protectedKeys) && !empty($value)) {
|
|
|
|
$value = "''";
|
|
|
|
}
|
|
|
|
|
2016-05-31 13:50:41 +02:00
|
|
|
return $this->isEnv($matches[0]) ? $matches[0] : "'$configKey' => env('$envKey', {$value}),";
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 15:58:54 +02:00
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
private function saveEnvSettings($key, $value)
|
|
|
|
{
|
2017-04-24 13:38:19 +02:00
|
|
|
if (! $this->envKeyExists($key)) {
|
2016-06-05 15:58:54 +02:00
|
|
|
$line = sprintf("%s=%s\n", $key, $this->stripQuotes($value));
|
|
|
|
|
|
|
|
if ($this->config == 'database' && $key != 'DB_CONNECTION') {
|
|
|
|
$this->writeDbEnvSettings($line);
|
|
|
|
} else {
|
|
|
|
$this->writeToEnv($line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $line
|
|
|
|
*/
|
|
|
|
private function writeDbEnvSettings($line)
|
|
|
|
{
|
|
|
|
if ($this->connection == config('database.default') || $this->connection == 'redis') {
|
|
|
|
$this->writeToEnv($line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-31 13:50:41 +02:00
|
|
|
* @param $configKey
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-06-05 15:58:54 +02:00
|
|
|
private function envValue($configKey)
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
$value = config("$this->config.$configKey");
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
if ($this->config == 'database') {
|
2016-05-31 13:50:41 +02:00
|
|
|
$value = $this->databaseConfigValue($configKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->normalize($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $configKey
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function databaseConfigValue($configKey)
|
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
if ($configKey == 'default') {
|
|
|
|
return config('database.default');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->connection == 'redis') {
|
|
|
|
return config("database.redis.default.$configKey");
|
|
|
|
}
|
2016-05-31 13:50:41 +02:00
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
return config("database.connections.$this->connection.$configKey");
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function normalize($value)
|
|
|
|
{
|
|
|
|
if (is_string($value)) {
|
|
|
|
return "'$value'";
|
|
|
|
} elseif (is_bool($value)) {
|
|
|
|
return $value ? 'true' : 'false';
|
2018-08-15 18:54:46 +02:00
|
|
|
} elseif ($value === null) {
|
2016-05-31 13:50:41 +02:00
|
|
|
return 'null';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $string
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function stripQuotes($string)
|
|
|
|
{
|
|
|
|
return strtr($string, ['"' => '', "'" => '']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $matches
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isEnv($matches)
|
|
|
|
{
|
|
|
|
return strpos($matches, 'env') !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $content
|
|
|
|
*/
|
|
|
|
private function writeToEnv($content)
|
|
|
|
{
|
|
|
|
file_put_contents('.env', $content, FILE_APPEND);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function readEnvFile()
|
|
|
|
{
|
|
|
|
return file_exists('.env') ? file_get_contents('.env') : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $content
|
|
|
|
*/
|
2016-06-05 15:58:54 +02:00
|
|
|
private function writeToConfigFile($content)
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
file_put_contents(config_path($this->config . '.php'), $content);
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-05 15:58:54 +02:00
|
|
|
* @return array
|
2016-05-31 13:50:41 +02:00
|
|
|
*/
|
2016-06-05 15:58:54 +02:00
|
|
|
private function lines()
|
2016-05-31 13:50:41 +02:00
|
|
|
{
|
2016-06-05 15:58:54 +02:00
|
|
|
return file(config_path($this->config . '.php'));
|
2016-05-31 13:50:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function envKeyExists($key)
|
|
|
|
{
|
|
|
|
return strpos($this->readEnvFile(), $key) !== false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function config()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'app' => [
|
|
|
|
'APP_DEBUG' => 'debug',
|
|
|
|
'APP_URL' => 'url',
|
|
|
|
'APP_KEY' => 'key',
|
|
|
|
],
|
|
|
|
'database' => [
|
|
|
|
'DB_CONNECTION' => 'default',
|
|
|
|
],
|
|
|
|
'cache' => [
|
|
|
|
'CACHE_DRIVER' => 'default',
|
|
|
|
],
|
|
|
|
'session' => [
|
|
|
|
'SESSION_DRIVER' => 'driver',
|
|
|
|
],
|
|
|
|
'queue' => [
|
|
|
|
'QUEUE_DRIVER' => 'default',
|
|
|
|
],
|
|
|
|
'mail' => [
|
|
|
|
'MAIL_DRIVER' => 'driver',
|
|
|
|
'MAIL_HOST' => 'host',
|
|
|
|
'MAIL_PORT' => 'port',
|
|
|
|
'MAIL_USERNAME' => 'username',
|
|
|
|
'MAIL_PASSWORD' => 'password',
|
|
|
|
'MAIL_ENCRYPTION' => 'encryption',
|
|
|
|
],
|
|
|
|
'cms' => [
|
|
|
|
'ROUTES_CACHE' => 'enableRoutesCache',
|
|
|
|
'ASSET_CACHE' => 'enableAssetCache',
|
|
|
|
'LINK_POLICY' => 'linkPolicy',
|
|
|
|
'ENABLE_CSRF' => 'enableCsrfProtection',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-05 15:58:54 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function dbConfig()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'sqlite' => [
|
|
|
|
'DB_DATABASE' => 'database',
|
|
|
|
],
|
|
|
|
'mysql' => [
|
|
|
|
'DB_HOST' => 'host',
|
|
|
|
'DB_PORT' => 'port',
|
|
|
|
'DB_DATABASE' => 'database',
|
|
|
|
'DB_USERNAME' => 'username',
|
|
|
|
'DB_PASSWORD' => 'password',
|
|
|
|
],
|
|
|
|
'pgsql' => [
|
|
|
|
'DB_HOST' => 'host',
|
|
|
|
'DB_PORT' => 'port',
|
|
|
|
'DB_DATABASE' => 'database',
|
|
|
|
'DB_USERNAME' => 'username',
|
|
|
|
'DB_PASSWORD' => 'password',
|
|
|
|
],
|
|
|
|
'redis' => [
|
|
|
|
'REDIS_HOST' => 'host',
|
|
|
|
'REDIS_PASSWORD' => 'password',
|
|
|
|
'REDIS_PORT' => 'port',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-04-24 13:38:19 +02:00
|
|
|
}
|