gullevek / dotenv
Simple .env file processing and storing in _ENV array
Requires
- php: >=8.1.0
Requires (Dev)
- phan/phan: ^6.0
- phpstan/phpdoc-parser: ^2.1
- phpstan/phpstan: ^2.1
- phpstan/phpstan-deprecation-rules: ^2.0
- phpunit/phpunit: ^13
README
A simple implementation of https://github.com/vlucas/phpdotenv
This is not a functional replacement, but a very simple implementation of the basic functions.
It is recommended to create a .env.example example file that is checked into the
repository. The .env should NEVER be checked into anything
How to install
composer require gullevek/dotEnv
Run it
Create a .env file in the current folder.
Create a file like below
require '../vendor/autoload.php'; $status = \gullevek\dotEnv\DotEnv::readEnvFile(__DIR__);
All data will be in the $_ENV array. Note that existing $_ENV data is never overwritten. If this is needed make a copy fo the $_ENV file and reset it, or if the data is not needed just reset it.
SUCCESS with problem errors can be read from DotEnv::getLastReadEnvFileErrors()
readEnvFile: Options
public static function readEnvFile( string $path = __DIR__, string $env_file = '.env', bool $load_outside_env = false, bool $throw_exception = false, ): DotEnvLevel
path: Where to look for the env file, default__DIR__env_file: File name to load, default.envload_outside_env: If set to true, will first load via getenv all the names found in the env file, will return "SUCCESS_ENV_EXIST_SKIP" if ther is an existing variable set alreadythrow_exception: instead of returining DotEnvLevel will throw exceptions, see Exception
read outside environment data
Normal environment data is not loaded into the $_ENV variable. If this is needed this can be done with \gullevek\dotEnv\DotEnv::loadOutsideGetEnv()
loadOutsideGetEnv: Options
public static function loadOutsideGetEnv( array $env_list = [], int $merge_flag = self::OVERWRITE ): void
env_list: if set only those environment variables will be loaded, case sensitivemerge_flag: defines how existing data is handled, default is to overwrite all of itOVERWRITE: overwrite all set data in$_ENVMERGE_KEEP_EXISTING: merge but keep data with the same keyMERGE_OVERWRITE_EXISTING: merge but overwrite data with the same key
Note to env_list, entries can be fnmatch notated
The wildcard style does not support matching with "*" in the middle
How it works
Put the function where it is needed or put it in a file and load it.
if not parameter is given it will use __DIR__ as base path.
Second parameter is file name override. Default is .env
Data is loaded into _ENV only.
If there is already an entry in _ENV then it will not be overwritten.
Errors
The readEnvFile will return a DotEnvLevel status message
SUCCESS: load successfulSUCCESS_DOUBLE_KEY: load successful, but some keys where double in the fileSUCCESS_ENV_EXIST_SKIP: load successful, but some keys already existed in the _ENV arraySUCCESS_DOUBLE_KEY_ENV_EXIST_SKIP: load successful, but we have double keys and some keys already existed in the _ENV arrayWARNING_FILE_LOADED_NO_DATA: file exists, but no dataERROR_FILE_NOT_FOUND: file does not exist
Reading SUCESS_* status block errors
If we have anything other than plain SUCCESS we can check the keys that had errors with DotEnv::getLastReadEnvFileErrors(). The returned array key is the DotEnvLevel name as string and has a list of all loaded keys that fall into this error.
Example:
Array
(
[SUCCESS_ENV_EXIST_SKIP] => Array
(
[0] => TEST
[1] => FOOBAR
)
)
Exceptions
Optional the method can be set to throw exceptions for file reading issuses by setting "throw_exceptions" to true
require '../vendor/autoload.php'; use gullevek\dotEnv\Exceptions; try { $status = gullevek\dotEnv\DotEnv::readEnvFile(throw_exceptions: true); } catch (Exception\DotEnvFileOpenFailedException $e) { // file not found/not readable/open failed }
.env file example
A valid entry has to start with an alphanumeric string, underscores are allowed and then have an equal sign (=). After the equal sign the data block starts. Data can be quoted with double quotes (") and if this is done can stretch over multiple lines. The openeing double quote must be on the same lign as the requal sign (=). If double quoted (") charcters are used it will read each line until another double quote (") character is found. Everything after that is ignored.
Any spaces before the variable or before and after the equal sign (=) are ignored.
Line is read until PHP_EOL. So any trailing spaces are read too.
Any line that is not valid is ignored.
# this line is ignored SOMETHING=A OTHER="A B C" MULTI_LINE="1 2 3 4 5 6 7 8 9" ; and this is ignored ESCAPE="String \" inside \" other " DOUBLE="I will be used" DOUBLE="This will be ignored"
A prefix name can be set with [PrefixName]. Tne name rules are like for variables, but spaces
are allowed, but will be converted to "_".
The prefix is valid from the time set until the next prefix block appears or the file ends.
Example
FOO="bar" FOOBAR="bar bar" [SecitonA] FOO="other bar" FOOBAR="other bar bar"
Will have environmen variables as
$_ENV["FOO"]; $_ENV["FOOBAR"]; $_ENV["SecitonA.FOO"]; $_ENV["SecitonA.FOOBAR"];
Development
Phan
vendor/bin/phan --analyze-twice
PHPstan
vendor/bin/phpstan
PHPUnit
Unit tests have to be run from base folder with
vendor/bin/phpunit