tflori / envparser
v1.0.0
2020-08-28 11:19 UTC
Requires
- php: ^7.1
- ext-json: *
Requires (Dev)
- mockery/mockery: 1.3.*
- phpunit/phpunit: ^7.5
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-03 22:12:21 UTC
README
这是一个小型库,仅读取bash语法的环境文件,并返回一个数组,该数组与在bash环境中运行此文件得到的结果相同。
功能
变量声明
# a string without spaces STRING1=foo # a string with spaces surrounded by quotes STRING2="foo's bar" STRING3='"$quote" - $author' # string '"$quote" - $author' STRING4=foo"' "bar # string 'foo\' bar' # variable replacement (assuming user=thomas) VAR1=$user # string 'thomas' VAR2="$user's profile" # string 'thomas\'s profile' VAR3="${APP_ENV-production}" # string 'production' # array handling ARRAY1=(foo bar) # array ['foo', 'bar'] ARRAY2=($int 6 7) # array [42, 6, 7] ARRAY3=() # array [] ARRAY5[0]=foo # array ['foo'] ARRAY5[1]=bar # array ['foo', 'bar']
在bash中可用的大量内容在此库中无法使用,但重要的功能将提供。当您发现缺少某些功能时,请在github上提交功能请求或创建合并请求。
使用环境
$_ENV
和 getenv()
(或传递的数组)中的所有变量都在解析文件内部可用。
变量转换
在bash中,每个变量都是一个字符串,类似于*nix中每个变量都是一个文件,其中包含一个字符串。虽然在这个层面上非常有用,但我们不希望在应用程序内部使用。
### null (case insensitive) NULL1= # null (bash typical) NULL2=null # null NULL3="null" # null STRING1='null' # string 'null' ### numbers (checked with is_numeric) INT=42 # int 42 INT2="23" # int 23 STRING2='42' # string '42' FLOAT=23.2 # float 23.2 FLOAT2="42.1" # float 42.1 STRING3='23.2' # string '23.2' ### true (case insensitive) BOOL_TRUE=true # bool true STRING4='true' # string 'true' ### ATTENTION! In php: ('false' != false) BOOL_FALSE=false # bool false STRING5='false' # string 'false' ### json (you should avoid json in environment files. it is just sugar; ext-json required) OBJECT='json:{"foo":"bar"}' # \stdobj ['foo' => 'bar'] ARRAY='jsonArray:{"foo":"bar"}' # array ['foo' => 'bar'] ### base64 (use it for unprintable characters) APP_KEY="base64:dzN0ICJzZWNyZXQiCg=="
连接
在bash中,通过简单地将字符串连接在一起来实现字符串连接。
STRING1="hello"' $user' # string 'hello $user' ### everything is a string when it gets concatenated STRING2=True" Warrior" # string 'True Warrior'
转义
转义引号的工作方式与bash中完全相同
STRING1="foo's \"bar\"" STRING2='foo'"'"'s "bar"' STRING3='foo'\''s "bar"' STRING4=$'foo\'s "bar"'