steevanb/php-yaml

为 symfony/yaml 添加一些功能

1.0.2 2018-02-13 17:57 UTC

This package is auto-updated.

Last update: 2024-08-28 11:26:59 UTC


README

version symfony Lines Total Downloads SensionLabsInsight Scrutinizer

php-yaml

symfony/yaml 添加功能。

由于 steevanb\PhpYaml\Parser 继承了 Symfony\Component\Yaml\Parser,因此您拥有所有 Symfony YAML 解析器的功能。

更新日志

安装

使用 composer require

composer require steevanb/php-yaml ^1.0

或者手动添加到 composer.json

{
    "require": {
        "steevanb/php-yaml": "^1.0"
    }
}

如何使用

不是调用 Symfony\Component\Yaml\Yaml::parse($content),而是这样做

use steevanb\PhpYaml\Parser;
$parser = new Parser();
$parser->parse(file_get_contents('foo.yml'));

函数支持

您可以在 yaml 值中调用已注册的函数

foo:
    call_file_get_contents: <file('baz.txt')>
    call_new_DateTime: <date()>
    call_new_DateTime2: <date('2017-12-31')>

默认情况下,没有函数注册到解析器中。您需要手动注册您需要的每个函数。

您可以注册 <file($fileName)> 函数。 $path$fileName 路径前缀

steevanb\PhpYaml\Parser::registerFileFunction($path = null);

您可以注册 <date($format = null)> 函数

steevanb\PhpYaml\Parser::registerDateFunction();

您可以注册自己的函数

steevanb\PhpYaml\Parser::registerFunction('foo', function($bar, $baz) {
    return $bar + $baz;
});

并在 yaml 中调用它

foo:
    bar: <foo(41, 1)>