for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace sixlive\DotenvEditor;
use SplFileObject;
use sixlive\DotenvEditor\Support\Arr;
class EnvFile
{
/**
* @var \SplFileObject|null
*/
protected $file;
public function __construct($path)
$this->file = new SplFileObject($path, 'r+');
}
public function write($content)
$this->file->fwrite($content);
return $this;
public function isEmpty()
return $this->file->getSize() > 0;
public function toArray()
return Arr::flatten($this->mapLineValuesAndKeys());
public function close()
$this->file = null;
private function linesFromFile()
return explode(
"\n",
$this->file->fread($this->file->getSize())
);
private function convertLineValuesToArray()
return array_map(function ($line) {
return explode('=', $line);
}, $this->linesFromFile());
private function mapLineValuesAndKeys()
if (count($line) === 2) {
return [$line[0] => $line[1]];
return $line[0];
}, $this->convertLineValuesToArray());