t2l/comments

用于保留注释的工具,例如在解析 YAML 文件时。

1.1.0 2020-04-27 15:47 UTC

This package is auto-updated.

Last update: 2024-08-28 01:05:16 UTC


README

用于保留注释的工具,例如在解析 YAML 文件时。

Build Status codecov

组件状态

原型。

动机

对 YAML 文件进行轻量级编辑,然后重写文件,同时不丢失嵌入的注释。

使用方法

// First step: read the file, parse the yaml, edit and dump the results.

$original_contents = file_get_contents($filepath);
$parsed_data = Yaml::parse($original_contents);
$processed_data = $this->my_processing_function($parsed_data);
$altered_contents = Yaml::dump($parsed_data, PHP_INT_MAX, 2, Yaml::DUMP_MULTI_LINE_LITERAL_BLOCK);

// Second step: collect comments from original document and inject them into result.

$commentManager = new Comments();
$commentManager->collect(explode("\n", $original_contents));
$altered_with_comments = $commentManager->inject(explode("\n", $altered_contents));

$result = implode("\n", $altered_with_comments);

限制

注释管理器收集一组注释行,并将它们与注释后跟随的第一个非空白内容行相关联。如果有多个完全相同的不空白内容行,则注释行将按照它们在原始文件中出现的顺序重新注入。

安装

$ composer require consolidation/comments

与现有解决方案的比较

查看 Klausi 的 yaml_comments