rancoud/environment

2.1.12 2024-09-02 11:39 UTC

README

Packagist PHP Version Support Packagist Version Packagist Downloads Composer dependencies Test workflow Codecov

读取 Environment 文件 (.env)。
可以完成或覆盖来自 getenv() / $_ENV / $_SERVER 的数据

安装

composer require rancoud/environment

.env 文件示例

# type of variables used
STRING=STRING
STRING_QUOTES=" STRING QUOTES "
INTEGER=9
FLOAT=9.0

BOOL_TRUE=TRUE
BOOL_FALSE=FALSE
NULL_VALUE=NULL

# variable in value
HOME=/user/www
CORE=$HOME/core
; $HOME won't be interpreted
USE_DOLLAR_IN_STRING="$HOME"

#comment line 1
;comment line 2

# import another env file use @ and the filename
@database.env

# multilines
RGPD="
i understand

    enough of email for \"me\"    

thanks
"

如何使用它?

警告,调用构造函数不会加载值,你可以

简单示例

// search .env file
$env = new Environment(__DIR__);
$values = $env->getAll();
$value = $env->get('a', 'defaultvalue');

检查键和值

// check if a key exists
$env = new Environment(__DIR__);
$isExists = $env->exists('key1');
$isExists = $env->exists(['key1', 'key2']);

// check if value is set with allowed values
$isAllowed = $env->allowedValues('key1', ['value1', NULL, 'value2']);

完成和覆盖值

仅对这些变量执行类型转换(不使用 $ 替换)。

你有 3 个不同的标志

  • Environment::GETENV
  • Environment::ENV
  • Environment::SERVER

完成用于填充属于空字符串或没有值的键的值。
覆盖用于删除属于键的值。
标志的处理总是按相同的顺序进行

  1. getenv()
  2. $_ENV
  3. $_SERVER

你还可以使用其他 3 个标志。
这些标志将注入找到的所有键和值,你的 env 文件不用于检查键。

  • Environment::GETENV_ALL
  • Environment::ENV_ALL
  • Environment::SERVER_ALL
$env = new Environment(__DIR__);

// complete with only getenv()
$env->complete(Environment::GETENV);

// complete with $_ENV then $_SERVER
$env->complete(Environment::SERVER | Environment::ENV);

// override with getenv() will erase values
$env->override(Environment::GETENV);

启用缓存

缓存的文件将不包含来自 getenv() / $_ENV / $_SERVER 的信息

// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->enableCache();
$values = $env->getAll();

何时调用 load()?

为了简单起见,load() 在使用这些函数时会自动调用

  • get
  • getAll
  • exists
  • complete

多行

你可以检查它使用的换行符类型,默认为 PHP_EOL
你可以使用 <br> 来更改它

// force using cache (if not exist it will be created)
$env = new Environment(__DIR__);
$env->setEndline('<br />');

包含另一个 .env

在 .env 文件中,你可以使用 @ 操作符在行首包含另一个 .env 文件

构造函数变体

// search .env file
$env = new Environment(__DIR__);

// search dev.env file
$env = new Environment(__DIR__, 'dev.env');

// search .env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr']);

// search dev.env file in folders __DIR__ then '/usr'
$env = new Environment([__DIR__, '/usr'], 'dev.env');

Environment 构造函数

设置

必填项

可选项

Environment 方法

通用命令

  • load():void
  • get(name: string, [default: mixed = null]): mixed|null
  • getAll(): array
  • exists(name: string|array): bool
  • allowedValues(name: string, values: array): bool

缓存文件

  • enableCache(): void
  • disableCache(): void
  • flushCache(): void

环境变量

  • complete(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void
  • override(flags: Environment::GETENV | Environment::ENV | Environment::SERVER): void

多行换行符解释

  • setEndline(endline: string): void
  • getEndline(): string

如何开发

使用 composer ci 进行 php-cs-fixer 和 phpunit 以及覆盖率测试
使用 composer lint 进行 php-cs-fixer
使用 composer test 进行 phpunit 和覆盖率测试