$values keyed by environment variable name * * @throws PterodactylException */ public function writeToEnvironment(array $values = []): void { $path = base_path('.env'); throw_unless(file_exists($path), PterodactylException::class, 'Cannot locate .env file, was this software installed correctly?'); $saveContents = file_get_contents($path); throw_if($saveContents === false, PterodactylException::class, 'Unable to read the contents of the .env file.'); collect($values)->each(function ($value, $key) use (&$saveContents): void { $key = mb_strtoupper($key); $saveValue = sprintf('%s=%s', $key, $this->escapeEnvironmentValue($value === null ? null : (string) $value)); if (preg_match_all('/^'.$key.'=(.*)$/m', $saveContents) < 1) { $saveContents = $saveContents.PHP_EOL.$saveValue; } else { // preg_replace() only returns null on a PCRE engine failure (e.g. the // backtrack limit); keep the existing contents rather than losing them. $saveContents = preg_replace('/^'.$key.'=(.*)$/m', $saveValue, $saveContents) ?? $saveContents; } }); file_put_contents($path, $saveContents); } /** * The answer to a prompt, or the option passed in its place. Symfony returns * either one untyped: a string, or null for a blank answer without a default. */ protected function answer(Closure $prompt): ?string { $answer = $prompt(); return is_scalar($answer) ? (string) $answer : null; } /** * The value currently configured for a key, offered as a prompt's default. */ protected function configured(string $key, int|string|null $default = null): ?string { $value = config($key, $default); return is_scalar($value) ? (string) $value : null; } }