gullevek / dotenv
简单处理和存储 .env 文件到 _ENV 数组
Requires
- php: >=7.4.0
Requires (Dev)
- phan/phan: ^5.4
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^9
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
如何安装
composer require gullevek/dotEnv
运行它
在当前文件夹中创建一个 .env
文件。创建一个类似下面的文件
require '../vendor/autoload.php'; gullevek\dotEnv\DotEnv::readEnvFile(__DIR__);
所有数据都将存储在 $_ENV
数组中
工作原理
将函数放在需要的位置或将其放在文件中并加载它。
如果没有提供参数,它将使用 __DIR__
作为基本路径。第二个参数是文件名覆盖。默认是 .env
数据只加载到 _ENV 中。
如果 _ENV 中已经有条目,则不会覆盖。
.env 文件示例
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 (") characters 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.
示例
FOO="bar" FOOBAR="bar bar" [SecitonA] FOO="other bar" FOOBAR="other bar bar"
将会有的环境变量为
$_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 test/phpUnitTests/