File tree Expand file tree Collapse file tree 6 files changed +178
-0
lines changed Expand file tree Collapse file tree 6 files changed +178
-0
lines changed Original file line number Diff line number Diff line change
1
+ vendor
2
+ nbproject
3
+ composer.lock
Original file line number Diff line number Diff line change 1
1
# php-env-dereference
2
+
2
3
Allow dereferencing of environment variables in PHP.
4
+
5
+ ## Overview
6
+
7
+ This is generally useful, in cloud environments, where different backing
8
+ services are injected into PHP with some strange names. At that point the
9
+ developer is either using that strange names in his code or starting to do
10
+ workarounds.
11
+
12
+ This particular library solves this based on the idea from variable dereference
13
+ feature that is (already in PHP engine)[ http://php.net/manual/en/language.variables.variable.php ] .
14
+ ```
15
+ <?php
16
+ $a = 'hello';
17
+ $$a = 'world';
18
+ // Same output.
19
+ echo "$a ${$a}\n", "hello world\n";
20
+ ```
21
+
22
+ This is very similar to the (phpdotenv)[ https://github.com/vlucas/phpdotenv ]
23
+ nested variables functionality, but exposing only it with a simpler syntax and
24
+ allowing recursive dereferencing.
25
+
26
+ ## Library example
27
+ ```
28
+ // We have this environment variables.
29
+ putenv('MY_VAR_1=1');
30
+ putenv('MY_VAR_2=#MY_VAR_1');
31
+ putenv('MY_VAR=#MY_VAR_2');
32
+
33
+ echo EnvDereference\Variable::get('MY_VAR'); // Should output '#MY_VAR_1'.
34
+ echo EnvDereference\Variable::getRecursive('MY_VAR'); // Should output '1'.
35
+ ```
36
+
37
+ ## Install
38
+ ```
39
+ composer require ndobromirov/php-env-dereference
40
+ ```
41
+
42
+ ## Contribute.
43
+
44
+ The library complies with PSR-2. Validate with ``` composer cs ``` .
45
+ The run tests with ``` composer test ``` .
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " ndobromirov/php-env-dereference" ,
3
+ "description" : " LCache library." ,
4
+ "license" : " MIT" ,
5
+ "authors" : [
6
+ {
7
+ "name" : " Nikolay Dobromirov" ,
8
+
9
+ }
10
+ ],
11
+ "autoload" :{
12
+ "psr-4" :{
13
+ "EnvDereference\\ " : " src/"
14
+ }
15
+ },
16
+ "autoload-dev" :{
17
+ "psr-4" :{
18
+ "EnvDereference\\ " : " tests/"
19
+ }
20
+ },
21
+ "require" : {
22
+ "php" : " >=5"
23
+ },
24
+ "require-dev" : {
25
+ "phpunit/phpunit" : " 4.*" ,
26
+ "squizlabs/php_codesniffer" : " 2.*"
27
+ },
28
+ "scripts" : {
29
+ "cs" : " phpcs --standard=PSR2 -n src tests" ,
30
+ "test" : " phpunit"
31
+ },
32
+ "extra" : {
33
+ "branch-alias" : {
34
+ "dev-master" : " master"
35
+ }
36
+ }
37
+ }
Original file line number Diff line number Diff line change
1
+ <phpunit verbose =" true" >
2
+ <testsuites >
3
+ <testsuite >
4
+ <directory >tests</directory >
5
+ </testsuite >
6
+ </testsuites >
7
+ <php >
8
+ <includePath >.</includePath >
9
+ </php >
10
+ <logging >
11
+ <log type =" coverage-clover" target =" build/logs/clover.xml" />
12
+ </logging >
13
+ <filter >
14
+ <whitelist processUncoveredFilesFromWhitelist =" true" >
15
+ <directory suffix =" .php" >src</directory >
16
+ </whitelist >
17
+ </filter >
18
+ </phpunit >
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * @file
5
+ * Utility library implementation.
6
+ */
7
+
8
+ namespace EnvDereference ;
9
+
10
+ /**
11
+ * Class containing the static metods provided by the library.
12
+ *
13
+ * @author ndobromirov
14
+ */
15
+ class Variable
16
+ {
17
+ const DEFAULT_PREFIX = '# ' ;
18
+
19
+ public static function get ($ name , $ prefix = self ::DEFAULT_PREFIX )
20
+ {
21
+ $ value = getenv ($ name );
22
+ if (strpos ($ value , $ prefix ) === 0 ) {
23
+ $ value = getenv (substr ($ value , strlen ($ prefix )));
24
+ }
25
+ return $ value ;
26
+ }
27
+
28
+ public static function getRecursive ($ name , $ prefix = self ::DEFAULT_PREFIX )
29
+ {
30
+ $ value = getenv ($ name );
31
+ while (strpos ($ value , $ prefix ) === 0 ) {
32
+ $ value = getenv (substr ($ value , strlen ($ prefix )));
33
+ }
34
+ return $ value ;
35
+ }
36
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ /**
4
+ * @file
5
+ * Testing utility.
6
+ */
7
+
8
+ namespace EnvDereference ;
9
+
10
+ /**
11
+ * Tests to ensure that the library works as expected.
12
+ *
13
+ * @todo: Have more tests added to cover customized prefixes.
14
+ * @author ndobromirov
15
+ */
16
+ class VariableTest extends \PHPUnit \Framework \TestCase
17
+ {
18
+ public static function setUpBeforeClass ()
19
+ {
20
+ putenv ('MY_VAR_1=1 ' );
21
+ putenv ('MY_VAR_2=#MY_VAR_1 ' );
22
+ putenv ('MY_VAR=#MY_VAR_2 ' );
23
+ }
24
+
25
+ public static function tearDownAfterClass ()
26
+ {
27
+ putenv ('MY_VAR ' );
28
+ putenv ('MY_VAR_1 ' );
29
+ putenv ('MY_VAR_2 ' );
30
+ }
31
+
32
+ public function testSingleDereference ()
33
+ {
34
+ $ this ->assertEquals ('#MY_VAR_1 ' , Variable::get ('MY_VAR ' ));
35
+ }
36
+
37
+ public function testRecursiveDereference ()
38
+ {
39
+ $ this ->assertEquals ('1 ' , Variable::getRecursive ('MY_VAR ' ));
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments