定制 wavelo/const-macro 二次开发

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

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

wavelo/const-macro

Composer 安装命令:

composer require wavelo/const-macro

包简介

Extends Latte templating engine with `{const ?}` macro for destructuring of objects and arrays inspired by ES2015

README 文档

README

Extends Latte templating engine with {const ?} macro for destructuring of objects and arrays inspired by ES2015

Installation

Use composer:

$ composer require wavelo/const-macro

Install in configuration file given Latte\Engine:

$latte = new Latte\Engine;
ConstMacro::install($latte->getCompiler());

When using Nette Dependency Injection, you can install in latte config.neon section:

latte:
  macros:
    - ConstMacro::install

Basic example

Given variable $props = ["title"=>"Hello World", "price"=>123.0] you can take advantage of following syntax:

{const [title:, price:, sale:=false] = $props /}
<h1>{$title}</h1>
<div>Price: {$price}</div>
<div n:if="$sale">Sale: YES</div>

Renders as follows:

<h1>Hello World</h1>
<div>Price: 123</div>

Features

  • {const ?} macros can be nested
  • const macro applies block scope for used variable
  • can be used inside {foreach ?} instead of $value (or both destructuring and $value)
  • every element can have arbitrary default value (even nested arrays or containing function call)
  • supports nested destructuring
  • supports ...rest operator
  • supports destructuring of both arrays and objects

For more examples see tests. Short demonstration:

{foreach $data as $key => [title:, image:[src, width, height], ...rest]}
  {* scope of variables $title, $src, etc. is bounded by foreach block! *}
{/foreach}

{foreach $data as $key => $item, [title:]}
  {* variable $item contains full object/array *}
{/foreach}

{const [title:] = $props}
  {* scope of variable $title is bounded by this block const! *}
{/const}

{const [title:] = $props /} {* does not apply block scope *}

Caveats

Syntax
  • latte macros can't contain curly brackets, object syntax is achieved via colon character.
Syntax examples:
destructuring $props $title equals
[title:] NULL RuntimeException
[title:] abc RuntimeException
[title:] [] NULL
[title:=Hi] [] "Hi"
[title:] ["title" => "Hello"] "Hello"
[title=Hi] [] "Hi"
[title] ["title" => "Hello"] NULL
[title] ["Hello"] "Hello"
[title:] ["Hello"] NULL
Priority when retrieving values of object
  1. ArrayAccess interface
  2. object properties
  3. values from Iterator or IteratorAggregate (only available when appropriate section uses rest operator)
Rest operator
  • rest operator is only possible as last token (i.e. [title:, image:, ...item] = $item)
  • when rest operator is used, value must be array or Traversable object
  • type of rest value corresponds to type of original value (stdClass when Traversable object, array otherwise)
  • keys are preserved when associative array is given or at least one value is accessed via key
Block scope
  • previously non-defined variable will have NULL value after end-of-scope (instead of calling unset function)
  • {foreach ?} has better performance then original Latte {foreach} + {const ?} inside loop ↳ {foreach ?} variable-scope is applied outside of foreach loop
  • unpaired {const ? /} macro does not impose variable scope
Default values
  • opposed to ES2016, default value can't reference previously declared tokens
  • oneword string tokens in default value do not need to be quoted (similar to Latte short-array syntax)
  • when default value contains function/method call, it is called only when required (provided value is NULL or key is not present)
  • this extension does not check full syntactic validity of default value
  • when default value with valid syntax is given, parser should handle it correctly
Others
  • evaluation starts from rightmost token (like PHP7 nested list)
  • requires at least PHP 5.5, fully supports PHP 7.0
  • when destructuring scalar or empty value, ConstMacro\RuntimeException is thrown (applied recursively)
  • thrown exception can be suppressed with default value (i.e. [title:, image:[src, width, height]=[]] = $item)

License

MIT. See full license.

wavelo/const-macro 适用场景与选型建议

wavelo/const-macro 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 67 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「latte」 「const macro」 「destructuring」 「es2015-like」 「rest operator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 wavelo/const-macro 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 67
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-04