File tree Expand file tree Collapse file tree 5 files changed +129
-4
lines changed
Expand file tree Collapse file tree 5 files changed +129
-4
lines changed Original file line number Diff line number Diff line change @@ -326,4 +326,32 @@ private function normalizeKey($key)
326326
327327 return $ key ;
328328 }
329+
330+ /**
331+ * @param string $key
332+ *
333+ * @return string|array|null
334+ */
335+ public function getYamlValue ($ key )
336+ {
337+ if (is_null ($ key )) {
338+ return null ;
339+ }
340+
341+ if (isset ($ this ->yamlVariables [$ key ])) {
342+ return $ this ->yamlVariables [$ key ];
343+ }
344+
345+ $ array = $ this ->yamlVariables ;
346+
347+ foreach (explode ('. ' , $ key ) as $ segment ) {
348+ if (! is_array ($ array ) || ! array_key_exists ($ segment , $ array )) {
349+ return null ;
350+ }
351+
352+ $ array = $ array [$ segment ];
353+ }
354+
355+ return $ array ;
356+ }
329357}
Original file line number Diff line number Diff line change @@ -73,16 +73,15 @@ public function required($variable)
7373 }
7474
7575 /**
76- * Get loader instance
76+ * Get loader instance.
7777 *
7878 * @throws LoaderException
7979 *
8080 * @return Loader
8181 */
8282 public function getLoader ()
8383 {
84- if (!$ this ->loader )
85- {
84+ if (!$ this ->loader ) {
8685 throw new LoaderException ('Loader has not been initialized yet. ' );
8786 }
8887
@@ -135,4 +134,34 @@ protected function loadData($overload = false)
135134
136135 return $ this ->loader ->load ();
137136 }
137+
138+ /**
139+ * Get an environment value. Returns the default when it is null.
140+ *
141+ * @param string $key
142+ * @param null $default
143+ *
144+ * @return null|string
145+ */
146+ public function getEnv ($ key , $ default = null )
147+ {
148+ $ value = $ this ->getLoader ()->getEnvironmentVariable ($ key );
149+
150+ return is_null ($ value ) ? $ default : $ value ;
151+ }
152+
153+ /**
154+ * Get the raw env value from the Yaml. Can be used to fetch associative arrays from the Yaml file.
155+ *
156+ * @param string $key
157+ * @param null $default
158+ *
159+ * @return array|null|string
160+ */
161+ public function getRawEnv ($ key , $ default = null )
162+ {
163+ $ value = $ this ->getLoader ()->getYamlValue ($ key );
164+
165+ return is_null ($ value ) ? $ default : $ value ;
166+ }
138167}
Original file line number Diff line number Diff line change @@ -13,10 +13,12 @@ class LoaderTest extends PHPUnit_Framework_TestCase
1313 * @var \Yamlenv\Loader
1414 */
1515 private $ immutableLoader ;
16+
1617 /**
1718 * @var array
1819 */
1920 private $ keyVal ;
21+
2022 /**
2123 * @var \Yamlenv\Loader
2224 */
@@ -142,6 +144,34 @@ public function testFlattenNestedValuesThrowsExceptionWithDuplication()
142144 $ immutableLoader ->load ();
143145 }
144146
147+ public function testItCanReturnAnAssociativeArray ()
148+ {
149+ $ expected = [
150+ 'ARRAY_ONE ' => 1 ,
151+ 'ARRAY_TWO ' => 2 ,
152+ ];
153+
154+ $ this ->mutableLoader ->load ();
155+
156+ $ actual = $ this ->mutableLoader ->getYamlValue ('NESTED ' );
157+
158+ $ this ->assertEquals ($ expected , $ actual );
159+ }
160+
161+ public function testItCanReturnAnAssociativeArrayFromADeepLevel ()
162+ {
163+ $ expected = [
164+ 'ARRAY_ONE ' => 1 ,
165+ 'ARRAY_TWO ' => 2 ,
166+ ];
167+
168+ $ this ->mutableLoader ->load ();
169+
170+ $ actual = $ this ->mutableLoader ->getYamlValue ('MULTI.LEVEL.NESTED ' );
171+
172+ $ this ->assertEquals ($ expected , $ actual );
173+ }
174+
145175 /**
146176 * Returns the key from keyVal(), without reset.
147177 *
Original file line number Diff line number Diff line change @@ -129,7 +129,7 @@ public function testYamlenvRequiredArrayEnvironmentVars()
129129 $ yamlenv = new Yamlenv ($ this ->fixturesFolder );
130130 $ yamlenv ->load ();
131131 $ validator = $ yamlenv ->required (['FOO ' , 'BAR ' ]);
132- -
132+
133133 $ this ->assertInstanceOf (Validator::class, $ validator );
134134 }
135135
@@ -451,6 +451,36 @@ public function testGetLoaderGivesNullBeforeLoad()
451451 $ yamlenv ->getLoader ();
452452 }
453453
454+ public function testGetEnvReturnsTheEnvValue ()
455+ {
456+ $ this ->clearEnv ();
457+
458+ $ yamlenv = new Yamlenv ($ this ->fixturesFolder , 'env.yml ' );
459+ $ yamlenv ->load ();
460+
461+ $ expected = 'bar ' ;
462+
463+ $ actual = $ yamlenv ->getEnv ('FOO ' );
464+
465+ $ this ->assertEquals ($ expected , $ actual );
466+ }
467+
468+ public function testGetRawEnvReturnsTheYamlValue ()
469+ {
470+ $ this ->clearEnv ();
471+
472+ $ yamlenv = new Yamlenv ($ this ->fixturesFolder , 'env.yml ' );
473+ $ yamlenv ->load ();
474+
475+ $ expected = [
476+ 'ARRAY_ONE ' => 1 ,
477+ 'ARRAY_TWO ' => 2 ,
478+ ];
479+
480+ $ actual = $ yamlenv ->getRawEnv ('NESTED ' );
481+
482+ $ this ->assertEquals ($ expected , $ actual );
483+ }
454484
455485 /**
456486 * Clear all env vars.
Original file line number Diff line number Diff line change 33FOO : bar
44BAR : baz
55SPACED : with spaces
6+ NESTED :
7+ ARRAY_ONE : 1
8+ ARRAY_TWO : 2
9+ MULTI :
10+ LEVEL :
11+ NESTED :
12+ ARRAY_ONE : 1
13+ ARRAY_TWO : 2
614
715EMPTY :
You can’t perform that action at this time.
0 commit comments