for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace sixlive\DotenvEditor;
use sixlive\DotenvEditor\Support\Arr;
class DotenvEditor
{
protected $env = [];
/**
* @var \sixlive\DotenvEditor\EnvFile
*/
protected $envFile;
public function load($path)
$this->envFile = new EnvFile($path);
if ($this->envFile->isEmpty()) {
$this->env = array_merge($this->env, $this->envFile->toArray());
}
return $this;
public function set($key, $value)
$this->env[$key] = $value;
public function getEnv($key = '')
return isset($this->env[$key])
? $this->env[$key]
: $this->env;
public function save()
$this->envFile->write($this->format());
public function addEmptyLine()
$this->env[] = '';
public function heading($heading)
if (! empty(end($this->env))) {
$this->addEmptyLine();
$this->env[] = sprintf('# %s', $heading);
public function has($key)
return isset($this->env[$key]);
public function __destruct()
if ($this->envFile) {
$this->envFile->close();
private function format()
$valuePairs = Arr::mapWithKeys($this->env, function ($item, $key) {
return ! empty($item) && ! is_int($key)
? sprintf('%s=%s', $key, $item)
: $item;
});
return implode("\n", $valuePairs);