grasmash / yaml-expander
在YAML文件中展开内部属性引用。
3.0.3
2024-05-04 17:22 UTC
Requires
- grasmash/expander: ^1 || ^2 || ^3
- symfony/yaml: ^4 || ^5 || ^6 || ^7
Requires (Dev)
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^8.0 || ^9
- squizlabs/php_codesniffer: ^2.7 || ^3.3
This package is auto-updated.
Last update: 2024-09-05 20:07:30 UTC
README
此工具用于展开YAML文件中的属性引用。
安装
composer require grasmash/yaml-expander
示例用法
示例 dune.yml
type: book book: title: Dune author: Frank Herbert copyright: ${book.author} 1965 protaganist: ${characters.0.name} media: - hardcover characters: - name: Paul Atreides occupation: Kwisatz Haderach aliases: - Usul - Muad'Dib - The Preacher - name: Duncan Idaho occupation: Swordmaster summary: ${book.title} by ${book.author} product-name: ${${type}.title} timezone: ${env.TZ}
属性引用使用点符号表示数组键,并且必须用 ${}
括起来。
展开逻辑
<?php // Set an environmental variable, accessible via ${env.TZ}. putenv("TZ=ES"); // Parse a yaml string directly, expanding internal property references. $yaml_string = file_get_contents("dune.yml"); $expanded = \Grasmash\YamlExpander\YamlExpander::parse($yaml_string); print_r($expanded); // Parse an array, expanding internal property references. $array = \Symfony\Component\Yaml\Yaml::parse(file_get_contents("dune.yml")); $expanded = \Grasmash\YamlExpander\YamlExpander::expandArrayProperties($array); print_r($expanded); // Parse an array, expanding references using both internal and supplementary values. $array = \Symfony\Component\Yaml\Yaml::parse(file_get_contents("dune.yml")); $reference_properties = ['book' => ['publication-year' => 1965]]; $expanded = \Grasmash\YamlExpander\YamlExpander::expandArrayProperties($array, $reference_properties); print_r($expanded);
结果数组
<?php array ( 'type' => 'book', 'book' => array ( 'title' => 'Dune', 'author' => 'Frank Herbert', 'copyright' => 'Frank Herbert 1965', 'protaganist' => 'Paul Atreides', 'media' => array ( 0 => 'hardcover', ), ), 'characters' => array ( 0 => array ( 'name' => 'Paul Atreides', 'occupation' => 'Kwisatz Haderach', 'aliases' => array ( 0 => 'Usul', 1 => 'Muad\'Dib', 2 => 'The Preacher', ), ), 1 => array ( 'name' => 'Duncan Idaho', 'occupation' => 'Swordmaster', ), ), 'summary' => 'Dune by Frank Herbert', 'product-name' => 'Dune', 'timezone' => 'ES', );