grasmash/expander
Composer 安装命令:
composer require grasmash/expander
包简介
Expands internal property references in PHP arrays.
README 文档
README
This tool expands dot-notated, string property references into their corresponding values. This is useful for run time expansion of property references in configuration files.
For example implementation, see Yaml Expander.
Installation
composer require grasmash/expander
Example usage:
Property references use dot notation to indicate array keys, and must be wrapped in ${}.
Expansion logic:
<?php $array = [ 'type' => 'book', 'book' => [ 'title' => 'Dune', 'author' => 'Frank Herbert', 'copyright' => '${book.author} 1965', 'protaganist' => '${characters.0.name}', 'media' => [ 0 => 'hardcover', 1 => 'paperback', ], 'nested-reference' => '${book.sequel}', ], 'characters' => [ 0 => [ 'name' => 'Paul Atreides', 'occupation' => 'Kwisatz Haderach', 'aliases' => [ 0 => 'Usul', 1 => 'Muad\'Dib', 2 => 'The Preacher', ], ], 1 => [ 'name' => 'Duncan Idaho', 'occupation' => 'Swordmaster', ], ], 'summary' => '${book.title} by ${book.author}', 'publisher' => '${not.real.property}', 'sequels' => '${book.sequel}, and others.', 'available-products' => '${book.media.1}, ${book.media.0}', 'product-name' => '${${type}.title}', 'boolean-value' => true, 'expand-boolean' => '${boolean-value}', 'null-value' => NULL, 'inline-array' => [ 0 => 'one', 1 => 'two', 2 => 'three', ], 'expand-array' => '${inline-array}', 'env-test' => '${env.test}', ]; $expander = new Expander(); // Optionally set a logger. $expander->setLogger(new Psr\Log\NullLogger()); // Optionally set a Stringifier, used to convert array placeholders into strings. Defaults to using implode() with `,` delimiter. // @see StringifierInterface. $expander->setStringifier(new Grasmash\Expander\Stringifier()); // Parse an array, expanding internal property references. $expanded = $expander->expandArrayProperties($array); // Parse an array, expanding references using both internal and supplementary values. $reference_properties = ['book' => ['sequel' => 'Dune Messiah']]; // Set an environmental variable. putenv("test=gomjabbar"); $expanded = $expander->expandArrayProperties($array, $reference_properties); print_r($expanded);
Resultant array:
Array ( [type] => book [book] => Array ( [title] => Dune [author] => Frank Herbert [copyright] => Frank Herbert 1965 [protaganist] => Paul Atreides [media] => Array ( [0] => hardcover [1] => paperback ) [nested-reference] => Dune Messiah ) [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 [publisher] => ${not.real.property} [sequels] => Dune Messiah, and others. [available-products] => paperback, hardcover [product-name] => Dune [boolean-value] => true, [expand-boolean] => true, [null-value] => [inline-array] => Array ( [0] => one [1] => two [2] => three ) [expand-array] => one,two,three [env-test] => gomjabbar [env] => Array ( [test] => gomjabbar ) )
grasmash/expander 适用场景与选型建议
grasmash/expander 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63.96M 次下载、GitHub Stars 达 139, 最近一次更新时间为 2017 年 12 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yaml」 「configuration」 「array」 「placeholder」 「dot-notation」 「expansion」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 grasmash/expander 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 grasmash/expander 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 grasmash/expander 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of useful PHP classes.
Configuration component
Trait providing methods to set class properties with an array.
Traits to build collections of specific objects
This adds functions about array. If you feel like there few php built-in functions about array, this will be useful.
Convert array or camel case to underscore, or underscore to others.
统计信息
- 总下载量: 63.96M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 140
- 点击次数: 37
- 依赖项目数: 5
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-21