Download the PHP package socarrat/environment without Composer
On this page you can find all versions of the php package socarrat/environment. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download socarrat/environment
More information about socarrat/environment
Files in socarrat/environment
Package environment
Short Description A super simple dotenv file parser that is compatible with getenv().
License MIT
Informations about the package environment
Socarrat\Environment
A super simple .env
file parser that is compatible with getenv()
.
Features
- [x] Parses
.env
files and does it well - [x] Parses
.env
files from the filesystem and from string input - [x] Special characters
- [x] Compatible with
getenv()
: no need to adapt your existing applications - [x] Fault-tolerant
- [x] Well-tested
- [x] Multi-line values
- [ ] Support for comments (WIP)
- [ ] Invalid file warnings (WIP)
Installation
Download this package on Packagist:
Usage
The API) to load your environment variables.
How about getenv()
?
One of the parameters of the parse functions is bool $putenv
. If you set this to true (default), putenv()
will be called for every parsed variable. If you already use getenv()
, there's no need to adapt your program: you only need to call a parse function once at the start.
Examples
See the examples/
directory for some usage examples.
.env
file format
The basics
A .env
file contains key-value pairs of environment-specific settings. Each line contains a key-value pair. Keys should be capitalised, and should only contain A-Z characters and underscores. Thereafter comes an =
sign, and then the value. See the following example:
Quoted values
To avoid confusion, you could enclose the value in double quotes "
.
Note that single quotes '
are NOT stripped from the value! For example, the value of DB_TABLE='users'
is 'users'
and not users
.
Multi-line values
Multi-line values are supported, provided that the value is enclosed in double quotes.
Special characters
You can use the following special characters in your values:
Name | Notation |
---|---|
Backslash (\ ) |
\\ |
Double quote (" ) |
\" |
Newline | \n |
Tab | \t |
Example:
Whitespace handling
Whitespace is handled thus:
-
Leading whitespace before keys is ignored.
-
Trailing whitespace after values is always trimmed, except when the value including whitespace is enclosed by double quotes.
-
Leading whitespace on a newline that is part of the value is honoured.
- Trailing whitespace on a newline that is part of the value is always ignored, except when the line is the last line that is part of the value and the whitespace comes before the closing double quote.
API
class Socarrat\Environment\EnvironmentManager
static public function getParsedEnv(): array
Returns all parsed environment variables as an associative array.
static public function parseFS(string $rootDir, bool $putenv = true)
Parses .env
files from the filesystem.
This method reads the files in the order specified in EnvironmentManager::$fileOrder. You can set this order using setFileOrder.
Parameter name | Type | Default value | Description |
---|---|---|---|
$rootDir |
string |
- | The root directory which contains your .env file/s. |
$putenv |
bool |
true |
Whether to register the values with PHP's environment, so that they can be retrieved using getenv() . |
static public function parseString(string $envFile, bool $putenv = true)
Parses a single .env
file passed as a string.
Parameter name | Type | Default value | Description |
---|---|---|---|
$envFile |
string |
- | The string to parse |
$putenv |
bool |
true |
Whether to register the values with PHP's environment, so that they can be retrieved using getenv() . |
static public function setFileOrder(array $order): void
Sets the order (passed as the $order
parameter) in which .env
files are loaded. Lower index means higher importance.
The default file order is:
Testing
This library is unit-tested by PHPunit using the tests in the on GitHub.
Copyright
(c) 2023 Romein van Buren. Licensed under the MIT license.
For the full copyright and license information, please view the license.md
file that was distributed with this source code.