|
| 1 | +# Env |
| 2 | + |
| 3 | +[![Latest Version on Packagist][ico-version]][link-packagist] |
| 4 | +[![Software License][ico-license]](LICENSE.md) |
| 5 | +[![Build Status][ico-travis]][link-travis] |
| 6 | +[![Coverage Status][ico-scrutinizer]][link-scrutinizer] |
| 7 | +[![Quality Score][ico-code-quality]][link-code-quality] |
| 8 | +[![Total Downloads][ico-downloads]][link-downloads] |
| 9 | + |
| 10 | +Env is a lightweight library bringing .env file parser compatibility to PHP. In short - it enables you to read .env files with PHP. |
| 11 | + |
| 12 | +Env aims to bring a unified parser for env together for PHP rather than having a few incomplete or buggy parsers written into other libraries. This library is not meant as a complete package for config loading like other libraries as this is out of scope for this library. If you need something like that check out [Vars](http://github.com/m1/Vars) |
| 13 | + |
| 14 | +## Install |
| 15 | + |
| 16 | +Via Composer |
| 17 | + |
| 18 | +``` bash |
| 19 | +$ composer require m1/env |
| 20 | +``` |
| 21 | + |
| 22 | +## Usage |
| 23 | + |
| 24 | +### Basic |
| 25 | + |
| 26 | +test.env |
| 27 | +```env |
| 28 | +TEST_1 = VALUE |
| 29 | +``` |
| 30 | + |
| 31 | +example.php |
| 32 | +``` php |
| 33 | + |
| 34 | +//both examples return the same thing |
| 35 | + |
| 36 | +// example 1 -- standard |
| 37 | +$env = new M1\Env('test.env'); |
| 38 | +$arr = $env->getContents(); |
| 39 | + |
| 40 | +// example 2 -- statically |
| 41 | +$arr = Env::parse('test.env'); |
| 42 | + |
| 43 | +var_dump($arr); |
| 44 | +// [ |
| 45 | +// "TEST_1" => "VALUE" |
| 46 | +// ] |
| 47 | +``` |
| 48 | + |
| 49 | +### .env examples |
| 50 | + |
| 51 | +```env |
| 52 | +# Comments are done like this |
| 53 | +
|
| 54 | +# Standard key=value |
| 55 | +KEY = value |
| 56 | +KEY = value |
| 57 | +KEY = value # You can also comment inline like this |
| 58 | +
|
| 59 | +# Strings |
| 60 | +KEY = "value" |
| 61 | +
|
| 62 | +## The value of the below variable will be TK4 = "value value" |
| 63 | +KEY = "value value" "this sentence in quotes will not be counted" |
| 64 | +
|
| 65 | +## Escape newline |
| 66 | +KEY = "value \n value" |
| 67 | +
|
| 68 | +## Escape double quotes |
| 69 | +KEY = "value \"value\" value" |
| 70 | +
|
| 71 | +## You can also exchange any of the above for single quotes, eg: |
| 72 | +KEY = 'value' |
| 73 | +
|
| 74 | +# Numbers |
| 75 | +KEY = 1 |
| 76 | +KEY = 1.1 |
| 77 | +KEY = 33 33 # Will output as a string -- not a number as two numbers are given |
| 78 | +
|
| 79 | +# Bools -- All of the below are valid booleans |
| 80 | +KEY = true |
| 81 | +KEY = false |
| 82 | +KEY = yes |
| 83 | +KEY = no |
| 84 | +
|
| 85 | +## Booleans are case-insensitive |
| 86 | +KEY = True |
| 87 | +KEY = False |
| 88 | +KEY = YES |
| 89 | +KEY = NO |
| 90 | +
|
| 91 | +# Null values |
| 92 | +KEY = |
| 93 | +KEY = null |
| 94 | +
|
| 95 | +# Variables |
| 96 | +string_1 = 'hello' |
| 97 | +test_variable_1 = ${string_1} # Hello |
| 98 | +
|
| 99 | +int_1 = 1 |
| 100 | +int_2 = 2 |
| 101 | +test_variable_1 = ${int_1} # 1 -- `int` type |
| 102 | +test_variable_2 = "${int_1}" # 1 -- `string` type |
| 103 | +test_variable_2 = ${int_1} ${int_2} # 1 2 -- `string` type |
| 104 | +
|
| 105 | +string_1 = foo |
| 106 | +string_2 = bar |
| 107 | +test_variable_1 = ${string_1}/${string_2} # foo/bar -- `string` type |
| 108 | +
|
| 109 | +string_3 = "foo" |
| 110 | +string_4 = 'bar' |
| 111 | +test_variable_1 = "hello ${string_3} and ${string_4}" # hello foo and bar -- `string` type |
| 112 | +
|
| 113 | +bool_1 = true |
| 114 | +bool_2 = false |
| 115 | +test_variable_1 = ${bool_1} # true -- `bool` type |
| 116 | +test_variable_2 = ${bool_1} ${bool_2} # true false -- `string` type |
| 117 | +``` |
| 118 | + |
| 119 | +## Change log |
| 120 | + |
| 121 | +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. |
| 122 | + |
| 123 | +## Testing |
| 124 | + |
| 125 | +``` bash |
| 126 | +$ composer test |
| 127 | +``` |
| 128 | + |
| 129 | +## Contributing |
| 130 | + |
| 131 | +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. |
| 132 | + |
| 133 | +## Security |
| 134 | + |
| 135 | +If you discover any security related issues, please email [email protected] instead of using the issue tracker. |
| 136 | + |
| 137 | +## Credits |
| 138 | + |
| 139 | +- [m1][link-author] |
| 140 | +- [All Contributors][link-contributors] |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. |
| 145 | + |
| 146 | +[ico-version]: https://img.shields.io/packagist/v/m1/Env.svg?style=flat-square |
| 147 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 148 | +[ico-travis]: https://img.shields.io/travis/m1/Env/master.svg?style=flat-square |
| 149 | +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/m1/Env.svg?style=flat-square |
| 150 | +[ico-code-quality]: https://img.shields.io/scrutinizer/g/m1/Env.svg?style=flat-square |
| 151 | +[ico-downloads]: https://img.shields.io/packagist/dt/m1/Env.svg?style=flat-square |
| 152 | + |
| 153 | +[link-packagist]: https://packagist.org/packages/m1/Env |
| 154 | +[link-travis]: https://travis-ci.org/m1/Env |
| 155 | +[link-scrutinizer]: https://scrutinizer-ci.com/g/:vendor/Env/code-structure |
| 156 | +[link-code-quality]: https://scrutinizer-ci.com/g/m1/Env |
| 157 | +[link-downloads]: https://packagist.org/packages/m1/Env |
| 158 | +[link-author]: https://github.com/m1 |
| 159 | +[link-contributors]: ../../contributors |
0 commit comments