topolis / yaml
symfony yaml 类的扩展
1.1.1
2021-10-05 12:35 UTC
Requires
- ext-openssl: *
- symfony/yaml: ^4.1|^5.0
Requires (Dev)
- phpunit/phpunit: 5.*
This package is not auto-updated.
Last update: 2024-09-18 02:18:51 UTC
README
这是一个针对 symfony yaml 的小型扩展库。此库通过使用 symfony 的 TaggedValue 类,向 yaml 格式添加自定义命令。
Yaml 示例
secure:
message: !decrypt WaXEqRY+dgNjsC/75nAE5k2JlizY1GWRY1FuageWAFI=
environment:
name: !env COMPUTERNAME
dates:
current: !date now "Y-m-d H:i:s"
formatted: !date now Y-m-d
currentIso: !date now
imported:
external: !import test/samples/test.yml
custom-external: !import test/samples/test.json json
用法
$yaml = new Yaml();
$handler = new \Topolis\Yaml\Handler\Decrypt();
$handler->addKey("This is my key");
$yaml->addHandler($handler);
$handler = new \Topolis\Yaml\Handler\Date();
$yaml->addHandler($handler);
$handler = new \Topolis\Yaml\Handler\Import();
$handler->addParser('yaml2', [$yaml,'parse'], 'D:\\Webserver\www');
$yaml->addHandler($handler);
$parsed = $yaml->parse($data);
日期
参数可以是字符串或包含 {format: , value: } 的数组。字符串可以是 Unix 时间戳或可由 PHP 的 strtotime 函数解析的字符串。
示例
!date 1264322
!date '+1 day'
!date now 'Y-m-d'
解密
解密各种格式的加密变量。支持的格式可以通过 openssl_get_cipher_methods() 确定。加密数据由数据、前缀 IV 和 base64 编码组成。
在使用之前,向处理器添加要使用的密钥。您永远不应将密钥本身添加到 Yaml 文件中
$handler = new Decrypt();
$handler->addKey("This is my key", "aes-256-cbc", "default"); // 2md & 3rd parameters are optional
您可以使用此示例 PHP 脚本进行 AES-256-CBC 加密
$ivlen = openssl_cipher_iv_length("aes-256-cbc");
$iv = openssl_random_pseudo_bytes($ivlen);
$encrypted = openssl_encrypt("Hello world!", "aes-256-cbc", "This is my key", OPENSSL_RAW_DATA, $iv);
echo base64_encode($iv.$encrypted);
用法
# use default key
!decrypt WaXEqRY+dgNjsC/75nAE5k2JlizY1GWRY1FuageWAFI=
# Use the key with id 'greenKey'
!decrypt greenKey WaXEqRY+dgNjsC/75nAE5k2JlizY1GWRY1FuageWAFI=
环境
返回指定环境变量的值
示例
!env COMPUTERNAME
您可以在构造函数中指定一个允许访问的字符串数组
导入
在此位置导入各种格式的文件。默认情况下,以下解析器可用
- php - 由 PHP 的 unserialize 函数解析
- yaml - 由 Symfony Yaml 解析器解析(可以轻松替换为此包的解析器)
- json - 由 json_decode 解析,并设置为返回关联数组
您可以使用 addParser() 设置自己的解析器
$handler = new Import();
$handler->addParser('myformat', [$MyClass, 'myparse'], '/only/inside/here', ['other','params']);
用法
!import <filepath> <optional mode - defaults to yaml>
示例
!import myfile
!import myfile json