Skip to content

Commit

Permalink
Improvements to the EnvFile parser
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeTowers committed Feb 17, 2022
1 parent 57eafe4 commit 9c0057c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
37 changes: 11 additions & 26 deletions src/Parse/EnvFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,22 @@
class EnvFile implements DataFileInterface
{
/**
* @var array contains the env during modification
* @var array Lines of env data
*/
protected $env = [];

/**
* @var array contains the env lookup map
* @var array Map of variable names to line indexes
*/
protected $map = [];

/**
* @var string|null contains the filepath used to read / write
* @var string|null Filepath currently being worked on
*/
protected $filePath = null;

/**
* EnvFile constructor.
* @param array $env
* @param string $filePath
* EnvFile constructor
*/
public function __construct(string $filePath)
{
Expand Down Expand Up @@ -59,7 +57,6 @@ public static function open(?string $filePath = null): ?EnvFile
* ```
* @param array|string $key
* @param mixed|null $value
* @return $this
*/
public function set($key, $value = null): EnvFile
{
Expand Down Expand Up @@ -89,10 +86,8 @@ public function set($key, $value = null): EnvFile

/**
* Push a newline onto the end of the env file
*
* @return $this
*/
public function addNewLine(): EnvFile
public function addEmptyLine(): EnvFile
{
$this->env[] = [
'type' => 'nl'
Expand All @@ -102,9 +97,7 @@ public function addNewLine(): EnvFile
}

/**
* Write the current env to a file
*
* @param string|null $filePath
* Write the current env lines to a fileh
*/
public function write(string $filePath = null): void
{
Expand All @@ -116,9 +109,7 @@ public function write(string $filePath = null): void
}

/**
* Get the env as a string
*
* @return string
* Get the env lines data as a string
*/
public function render(): string
{
Expand All @@ -142,8 +133,7 @@ public function render(): string
/**
* Wrap a value in quotes if needed
*
* @param $value
* @return string
* @param mixed $value
*/
protected function escapeValue($value): string
{
Expand Down Expand Up @@ -175,10 +165,7 @@ protected function escapeValue($value): string
}

/**
* Parse a .env file, returns an array of the env file data and a key => pos map
*
* @param string $filePath
* @return array
* Parse a .env file, returns an array of the env file data and a key => position map
*/
protected function parse(string $filePath): array
{
Expand Down Expand Up @@ -232,11 +219,9 @@ protected function parse(string $filePath): array
}

/**
* Get the current env array
*
* @return array
* Get the variables from the current env lines data as an associative array
*/
public function getEnv(): array
public function getVariables(): array
{
$env = [];

Expand Down
2 changes: 1 addition & 1 deletion tests/Parse/EnvFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function testReadFile()

$this->assertInstanceOf(EnvFile::class, $env);

$arr = $env->getEnv();
$arr = $env->getVariables();

$this->assertArrayHasKey('APP_URL', $arr);
$this->assertArrayHasKey('APP_KEY', $arr);
Expand Down

0 comments on commit 9c0057c

Please sign in to comment.