定制 grasmash/yaml-expander 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

grasmash/yaml-expander

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.

Requirements

  • PHP 8.1+

Installation

composer require grasmash/yaml-expander

Example usage:

Example dune.yml:

type: book
book:
  title: Dune
  author: Frank Herbert
  copyright: ${book.author} 1965
  protagonist: ${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

use Grasmash\YamlExpander\YamlExpander;
use Symfony\Component\Yaml\Yaml;

// Set an environmental variable, accessible via ${env.TZ}.
putenv("TZ=ES");

// Optionally pass any PSR-3 logger to see which placeholders were expanded
// or could not be resolved. Defaults to a no-op logger.
$expander = new YamlExpander();

// Parse a yaml string directly, expanding internal property references.
$yaml_string = file_get_contents("dune.yml");
$expanded = $expander->parse($yaml_string);
print_r($expanded);

// Parse an array, expanding internal property references.
$array = Yaml::parse(file_get_contents("dune.yml"));
$expanded = $expander->expandArrayProperties($array);
print_r($expanded);

// Expand references using both internal and supplementary values:
// a placeholder such as ${book.sequel} that exists nowhere in the YAML
// itself is resolved from the reference array.
$yaml_string = "sequels: \${book.sequel}, and others.";
$reference_properties = ['book' => ['sequel' => 'Dune Messiah']];
$expanded = $expander->parse($yaml_string, $reference_properties);
// $expanded['sequels'] === 'Dune Messiah, and others.'

Unresolvable placeholders are left intact (and reported to the logger). Invalid YAML throws Symfony\Component\Yaml\Exception\ParseException, and YAML that parses to a non-array value (e.g., a bare scalar) throws UnexpectedValueException.

Security

Only parse YAML from trusted sources. Placeholder expansion can reference any value in the document or reference array, including environment variables via ${env.*}, and deeply self-referencing placeholders can expand to very large strings. This library is designed for trusted configuration files, not for arbitrary user-supplied input.

Resultant array:

<?php

array (
  'type' => 'book',
  'book' =>
  array (
    'title' => 'Dune',
    'author' => 'Frank Herbert',
    'copyright' => 'Frank Herbert 1965',
    'protagonist' => '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',
);

Development

Local development commands:

composer install
composer test      # lint + unit tests + code sniffer
composer unit      # PHPUnit only
composer cs        # code sniffer only
composer cbf       # auto-fix code style
composer coverage  # generate clover coverage report

grasmash/yaml-expander 适用场景与选型建议

grasmash/yaml-expander 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.27M 次下载、GitHub Stars 达 154, 最近一次更新时间为 2017 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 grasmash/yaml-expander 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 grasmash/yaml-expander 我们能提供哪些服务?
定制开发 / 二次开发

基于 grasmash/yaml-expander 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 32.27M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 155
  • 点击次数: 22
  • 依赖项目数: 9
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-13