/** * @param Event $event */ public static function addSalts(Event $event) { static::$root = dirname(dirname(__DIR__)); static::$envFilePath = static::$root . '/.env'; static::$io = $event->getIO(); $generate_salts = false; // Only add new/regenerate salts if we are doing a fresh install and specify "yes" to the question. // This way the salts aren't cleared whenever a `composer install` is done on deploy. if (static::$io->isInteractive()) { $generate_salts = static::$io->askConfirmation('<info>Generate salts and append to .env file?</info> [<comment>Y,n</comment>]? ', true); } // No need to continue - exit. if (!$generate_salts) { return; } // Open the file for writing. $handle = fopen(static::$envFilePath, 'a'); $keys = []; // Get the salt keys. foreach (static::$saltKeys as $key) { $keys[] = static::getEnvVar($key, Installer::generateSalt()); } // Append additional keys to the .env file/keys array. foreach (static::setEnvironmentVariables() as $key => $val) { $keys[] = static::getEnvVar($key, $val); } if ($handle) { fwrite($handle, implode($keys, "\n")); fclose($handle); } }
public static function addSalts(Event $event) { $root = dirname(dirname(__DIR__)); $composer = $event->getComposer(); $io = $event->getIO(); if (!$io->isInteractive()) { $generate_salts = $composer->getConfig()->get('generate-salts'); } else { $generate_salts = $io->askConfirmation('<info>Generate salts and append to .env file?</info> [<comment>Y,n</comment>]? ', true); } if (!$generate_salts) { return 1; } $salts = array_map(function ($key) { return sprintf("%s='%s'", $key, Installer::generate_salt()); }, self::$KEYS); $env_file = "{$root}/.env"; if (copy("{$root}/.env.example", $env_file)) { file_put_contents($env_file, implode($salts, "\n"), FILE_APPEND | LOCK_EX); } else { $io->write("<error>An error occured while copying your .env file</error>"); return 1; } }