承接 grasmash/yaml-expander 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grasmash/yaml-expander

最新稳定版本:3.0.3

Composer 安装命令:

composer require grasmash/yaml-expander

包简介

Expands internal property references in a yaml file.

README 文档

README

CI Packagist Total Downloads Coverage Status

This tool expands property references in YAML files.

Installation

composer require grasmash/yaml-expander

Example usage:

Example 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}

Property references use dot notation to indicate array keys, and must be wrapped in ${}.

Expansion logic:

<?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);

Resultant array:

<?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',
);

统计信息

  • 总下载量: 31.39M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 154
  • 点击次数: 1
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 153
  • Watchers: 2
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04