Skip to content

Commit

Permalink
Merge pull request #1 from Maatwebsite/feature/add-getters
Browse files Browse the repository at this point in the history
Added getEnv and getRawEnv methods on the Yamlenv.
  • Loading branch information
jochemfuchs authored Jul 11, 2017
2 parents 7cf10ab + e01caf4 commit e57f381
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 4 deletions.
28 changes: 28 additions & 0 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,4 +326,32 @@ private function normalizeKey($key)

return $key;
}

/**
* @param string $key
*
* @return string|array|null
*/
public function getYamlValue($key)
{
if (is_null($key)) {
return null;
}

if (isset($this->yamlVariables[$key])) {
return $this->yamlVariables[$key];
}

$array = $this->yamlVariables;

foreach (explode('.', $key) as $segment) {
if (! is_array($array) || ! array_key_exists($segment, $array)) {
return null;
}

$array = $array[$segment];
}

return $array;
}
}
35 changes: 32 additions & 3 deletions src/Yamlenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,15 @@ public function required($variable)
}

/**
* Get loader instance
* Get loader instance.
*
* @throws LoaderException
*
* @return Loader
*/
public function getLoader()
{
if(!$this->loader)
{
if (!$this->loader) {
throw new LoaderException('Loader has not been initialized yet.');
}

Expand Down Expand Up @@ -135,4 +134,34 @@ protected function loadData($overload = false)

return $this->loader->load();
}

/**
* Get an environment value. Returns the default when it is null.
*
* @param string $key
* @param null $default
*
* @return null|string
*/
public function getEnv($key, $default = null)
{
$value = $this->getLoader()->getEnvironmentVariable($key);

return is_null($value) ? $default : $value;
}

/**
* Get the raw env value from the Yaml. Can be used to fetch associative arrays from the Yaml file.
*
* @param string $key
* @param null $default
*
* @return array|null|string
*/
public function getRawEnv($key, $default = null)
{
$value = $this->getLoader()->getYamlValue($key);

return is_null($value) ? $default : $value;
}
}
30 changes: 30 additions & 0 deletions tests/Yamlenv/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class LoaderTest extends PHPUnit_Framework_TestCase
* @var \Yamlenv\Loader
*/
private $immutableLoader;

/**
* @var array
*/
private $keyVal;

/**
* @var \Yamlenv\Loader
*/
Expand Down Expand Up @@ -142,6 +144,34 @@ public function testFlattenNestedValuesThrowsExceptionWithDuplication()
$immutableLoader->load();
}

public function testItCanReturnAnAssociativeArray()
{
$expected = [
'ARRAY_ONE' => 1,
'ARRAY_TWO' => 2,
];

$this->mutableLoader->load();

$actual = $this->mutableLoader->getYamlValue('NESTED');

$this->assertEquals($expected, $actual);
}

public function testItCanReturnAnAssociativeArrayFromADeepLevel()
{
$expected = [
'ARRAY_ONE' => 1,
'ARRAY_TWO' => 2,
];

$this->mutableLoader->load();

$actual = $this->mutableLoader->getYamlValue('MULTI.LEVEL.NESTED');

$this->assertEquals($expected, $actual);
}

/**
* Returns the key from keyVal(), without reset.
*
Expand Down
32 changes: 31 additions & 1 deletion tests/Yamlenv/YamlenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testYamlenvRequiredArrayEnvironmentVars()
$yamlenv = new Yamlenv($this->fixturesFolder);
$yamlenv->load();
$validator = $yamlenv->required(['FOO', 'BAR']);
-

$this->assertInstanceOf(Validator::class, $validator);
}

Expand Down Expand Up @@ -451,6 +451,36 @@ public function testGetLoaderGivesNullBeforeLoad()
$yamlenv->getLoader();
}

public function testGetEnvReturnsTheEnvValue()
{
$this->clearEnv();

$yamlenv = new Yamlenv($this->fixturesFolder, 'env.yml');
$yamlenv->load();

$expected = 'bar';

$actual = $yamlenv->getEnv('FOO');

$this->assertEquals($expected, $actual);
}

public function testGetRawEnvReturnsTheYamlValue()
{
$this->clearEnv();

$yamlenv = new Yamlenv($this->fixturesFolder, 'env.yml');
$yamlenv->load();

$expected = [
'ARRAY_ONE' => 1,
'ARRAY_TWO' => 2,
];

$actual = $yamlenv->getRawEnv('NESTED');

$this->assertEquals($expected, $actual);
}

/**
* Clear all env vars.
Expand Down
8 changes: 8 additions & 0 deletions tests/fixtures/valid/env.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@
FOO: bar
BAR: baz
SPACED: with spaces
NESTED:
ARRAY_ONE: 1
ARRAY_TWO: 2
MULTI:
LEVEL:
NESTED:
ARRAY_ONE: 1
ARRAY_TWO: 2

EMPTY:

0 comments on commit e57f381

Please sign in to comment.