30 lines
673 B
PHP
30 lines
673 B
PHP
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
class EnvWriter
|
|
{
|
|
/**
|
|
* Create a new class instance.
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
public static function set(array $pairs): void {
|
|
$path = base_path('.env');
|
|
$env = file_exists($path) ? file_get_contents($path) : '';
|
|
foreach ($pairs as $key => $value) {
|
|
$value = (string)$value;
|
|
if (preg_match("/^{$key}=.*/m", $env)) {
|
|
$env = preg_replace("/^{$key}=.*/m", "{$key}={$value}", $env);
|
|
} else {
|
|
$env .= PHP_EOL."{$key}={$value}";
|
|
}
|
|
}
|
|
file_put_contents($path, $env);
|
|
}
|
|
|
|
}
|