From fb7e7ff1647ad65545ac2682ef9c1b12074b7226 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20P=C5=82odowski?= Date: Tue, 31 May 2016 13:50:41 +0200 Subject: [PATCH] Command for changing configuration to env syntax --- modules/system/ServiceProvider.php | 1 + modules/system/console/OctoberEnv.php | 265 ++++++++++++++++++++++++++ 2 files changed, 266 insertions(+) create mode 100644 modules/system/console/OctoberEnv.php diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index d1afcfdbe..63040b869 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -219,6 +219,7 @@ class ServiceProvider extends ModuleServiceProvider $this->registerConsoleCommand('october.util', 'System\Console\OctoberUtil'); $this->registerConsoleCommand('october.mirror', 'System\Console\OctoberMirror'); $this->registerConsoleCommand('october.fresh', 'System\Console\OctoberFresh'); + $this->registerConsoleCommand('october.env', 'System\Console\OctoberEnv'); $this->registerConsoleCommand('october.install', 'System\Console\OctoberInstall'); $this->registerConsoleCommand('plugin.install', 'System\Console\PluginInstall'); diff --git a/modules/system/console/OctoberEnv.php b/modules/system/console/OctoberEnv.php new file mode 100644 index 000000000..1a29f37dd --- /dev/null +++ b/modules/system/console/OctoberEnv.php @@ -0,0 +1,265 @@ +error('.env file already exists.'); + } + + $this->overwriteConfig(); + + $this->info('.env configuration file has been created.'); + } + + /** + * Overwrite config file + */ + private function overwriteConfig() + { + foreach ($this->config() as $file => $keys) { + $content = $this->configToEnv($file, $keys); + + $this->writeToConfigFile($file, $content); + } + } + + /** + * Replace config values with env() syntax + * + * @param $file + * @param $keys + * @return string + */ + private function configToEnv($file, $keys) + { + $content = $this->readConfigFile($file); + + foreach ($keys as $envKey => $configKey) { + $pattern = $this->buildPattern($configKey); + $callback = $this->buildCallback($file, $envKey, $configKey); + + $content = preg_replace_callback($pattern, $callback, $content); + } + + $this->writeToEnv("\n"); + + return $content; + } + + /** + * @param $configKey + * @return string + */ + private function buildPattern($configKey) + { + return "/['\"]" . $configKey . "['\"]" . "\s*=>\s*[^,\[]+,/"; + } + + /** + * @param $file + * @param $envKey + * @param $configKey + * @return \Closure + */ + private function buildCallback($file, $envKey, $configKey) + { + return function ($matches) use ($envKey, $configKey, $file) { + + $value = $this->envValue($file, $configKey); + + if ( ! $this->envKeyExists($envKey)) { + $envLine = sprintf("%s=%s\n", $envKey, $this->stripQuotes($value)); + $this->writeToEnv($envLine); + } + + return $this->isEnv($matches[0]) ? $matches[0] : "'$configKey' => env('$envKey', {$value}),"; + }; + } + + /** + * @param $config + * @param $configKey + * @return string + */ + private function envValue($config, $configKey) + { + $value = config("$config.$configKey"); + + if ($config == 'database') { + $value = $this->databaseConfigValue($configKey); + } + + return $this->normalize($value); + } + + /** + * @param $configKey + * @return string + */ + private function databaseConfigValue($configKey) + { + $defaultConnection = config('database.default'); + + return $configKey == 'default' + ? $defaultConnection + : config("database.connections.$defaultConnection.$configKey"); + } + + /** + * @param $value + * @return string + */ + private function normalize($value) + { + if (is_string($value)) { + return "'$value'"; + } elseif (is_bool($value)) { + return $value ? 'true' : 'false'; + } elseif (is_null($value)) { + return 'null'; + } + + return $value; + } + + /** + * Strip single and double quotes + * + * @param $string + * @return string + */ + private function stripQuotes($string) + { + return strtr($string, ['"' => '', "'" => '']); + } + + /** + * @param $matches + * @return bool + */ + private function isEnv($matches) + { + return strpos($matches, 'env') !== false; + } + + /** + * Write content to .env file + * + * @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') : ''; + } + + /** + * Write content to config file + * + * @param $file + * @param $content + */ + private function writeToConfigFile($file, $content) + { + file_put_contents(config_path($file . '.php'), $content); + } + + /** + * @param $file + * @return string + */ + private function readConfigFile($file) + { + return file_get_contents(config_path($file . '.php')); + } + + /** + * @param $key + * @return bool + */ + private function envKeyExists($key) + { + return strpos($this->readEnvFile(), $key) !== false; + } + + /** + * Configuration array for search and replace + * + * @return array + */ + private function config() + { + return [ + 'app' => [ + 'APP_DEBUG' => 'debug', + 'APP_URL' => 'url', + 'APP_KEY' => 'key', + ], + 'database' => [ + 'DB_CONNECTION' => 'default', + 'DB_HOST' => 'host', + 'DB_PORT' => 'port', + 'DB_DATABASE' => 'database', + 'DB_USERNAME' => 'username', + 'DB_PASSWORD' => 'password', + ], + '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', + ], + ]; + } + +}