consolidation/comments

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

1.0.2 2019-07-30 05:52 UTC

This package is auto-updated.

Last update: 2024-08-25 07:49:24 UTC


README

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

Build Status Windows Build Scrutinizer Code Quality Coverage Status License

组件状态

原型

动机

在 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