rammewerk/environment

PHP项目的一个简单快速的环境变量处理器。

1.0.1 2024-08-19 08:41 UTC

This package is auto-updated.

Last update: 2024-09-19 08:54:57 UTC


README

项目的一个简单快速的环境变量处理器。

此包是处理项目环境变量的不同方法

  • 解析并自动缓存 .env 文件
  • 不会向 $_ENV 添加变量 - 因为如果不小心调试,可能会导致暴露值。
  • 没有其他依赖 - 小型尺寸。
  • 会自动将值转换为布尔型、整型、null型甚至数组类型(更多内容请参阅下方)
  • 支持闭包以验证环境变量
  • 包括缓存以实现更快的加载。
  • 支持多个文件

重要:.env 文件格式有一些限制。请参阅下方。

入门

$ composer require rammewerk/environment
use Rammewerk\component\environment\src\Environment;

$env = new Environment();

// Load from environment variable file
$env->load( ROOT_DIR . '.env');

// Get value from environment
$debug_mode = $env->get( 'DEBUG_MODE' );

支持多个 .env 文件

您可以为项目添加多个环境文件或动态创建新变量。

文件不一定是 .env。例如,file.txt 也可以作为文件使用,只要它格式正确。

$env->load( ROOT_DIR . '.env');

# Warning: will overwrite value for keys that exist in both files.
$env->load( ROOT_DIR . '.env-app');

# You can also define new variables or overwrite values on the fly.
$env->set('NEW_KEY', 'new value');

缓存

# You can load files from wherever you want.
$env_file = ROOT_DIR . '/app/.env';

# You decide where to put the cache.
$cache_file = CACHE_DIR . 'env-cache.json';

# Load the environment variables
# If cache does not exist it will create one.
# If cache exist, and is newer than the env_file, it will load from cache.
$env->load( $env_file, $cache_file);

# You can reload the file at any time.
$env->load( $env_file, $cache_file);

# But you cannot define the same cache file path for a different env file.
# This will throw a \RuntimeException()
$env->load( $some_other_env_file, $cache_file);

# And, if you want to reload and build new cache for all previous loaded env-files, you can do so
$env->reload();

验证环境变量

use Rammewerk\component\environment\src\Validator;

...

# Validate variables when loading from file.
# If loaded from cache - it will not run validation.
$env->load( $env_file, $cache_file, static function( Validator $env) {
    $env->require('DEBUG_MODE')->isBoolean();
});

# You can validate later of in you want.
# Will validate straight away, not only on load.
$env->validate( static function(Validator $env) {
    $env->ifPresent('APP_URL')->endWith('/');
})

限制

这是一个简单的 env 解析器。您需要相应地格式化您的 env 文件。

变量名称

环境变量名称必须仅由字母、数字和下划线( _ )组成,且不能以数字开头。

注释

注释只能出现在新行上,不能与变量在同一行。

# This is a valid comment
USER=John # Comment like this is not allowed!

变量值

值可以使用引号。

# Values can be quoted. These are all the same values:
KEY1=value
KEY2='value'
KEY3="value"

值将被修剪并转换为类型

# Values will be automatically trimmed. This is the same as KEY2='HELLO'
KEY4=' HELLO '

# TRUE or FALSE will be converted to valid boolean type in PHP. If you use quotes, it will be converted to string.
KEY5=TRUE

# An interger value will be converted to a valid PHP interger. If you use quotes, it will be converted to string.
KEY6=120

# Empty string '' or NULL will be converted to PHP NULL value.
KEY7=NULL

# Add commaseparated string inside brackets to convert to array of strings
KEY9='[value1,value2,value3]'

提示

new Environment() 将返回该类的新实例。因此,如果您使用依赖注入容器或类似的东西,请考虑使 Environment 类成为共享实例。或者创建自己的单例包装器。

类型获取器

您可以使用类型获取器以特定类型获取键的值。例如

$env->getString('KEY1'); // Returns string or null
$env->getInt('KEY2'); // Returns int or null
$env->getFloat('KEY3'); // Returns float or null
$env->getBool('KEY4'); // Returns bool
$env->getArray('KEY5'); // Returns array or null

如果值不是字符串、int、float、bool 或数组,则获取器将返回 null。