dd/evolutioncms-snippets-ddstash
Composer 安装命令:
composer require dd/evolutioncms-snippets-ddstash
包简介
Save data as JSON or QueryString, then extend if needed and use it later without database queries.
README 文档
README
Save data as JSON or Query string, then extend if needed and use it later without database queries.
Requires
- PHP >= 5.6
- (MODX)EvolutionCMS >= 1.1
- (MODX)EvolutionCMS.libraries.ddTools >= 0.49.1
Documentation
Installation
Manually
1. Elements → Snippets: Create a new snippet with the following data
- Snippet name:
ddStash. - Description:
<b>1.3</b> Save data as JSON or QueryString, then extend if needed and use it later without database queries.. - Category:
Core. - Parse DocBlock:
no. - Snippet code (php): Insert content of the
ddStash_snippet.phpfile from the archive.
2. Elements → Manage Files
- Create a new folder
assets/snippets/ddStash/. - Extract the archive to the folder (except
ddStash_snippet.php).
Using (MODX)EvolutionCMS.libraries.ddInstaller
Just run the following PHP code in your sources or Console:
//Include (MODX)EvolutionCMS.libraries.ddInstaller require_once( $modx->getConfig('base_path') . 'assets/libs/ddInstaller/require.php' ); //Install (MODX)EvolutionCMS.snippets.ddStash \DDInstaller::install([ 'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddStash', 'type' => 'snippet' ]);
- If
ddStashis not exist on your site,ddInstallerwill just install it. - If
ddStashis already exist on your site,ddInstallerwill check it version and update it if needed.
Parameters description
-
save- Desctription: Data to save in stash. Nested objects are supported too, see examples below.
- Valid values:
stringJsonObject— as JSONstringHjsonObject— as HJSONstringQueryFormated— as Query string- It can also be set as a native PHP object or array (e. g. for calls through
$modx->runSnippet):arrayAssociativeobject
- Default value: —
-
save_extendExisting- Desctription: Extend an existing object instead of overwriting it.
- Valid values:
01
- Default value:
0
-
save_extendExistingWithEmpty- Desctription: Overwrite fields with empty values (see examples below).
The following values are considered to be empty:""— an empty string[]— an empty array{}— an empty objectnull
- Valid values:
01
- Default value:
1
- Desctription: Overwrite fields with empty values (see examples below).
-
get- Desctription: Data key for getting from stash.
- Valid values:
string - Default value: —
-
get_tpl-
Desctription: Output template.
Available placeholders:
[+snippetResult+]— data from stash
-
Valid values:
stringChunkNamestring— use inline templates starting with@CODE:
-
Default value:
'@CODE:[+snippetResult+]'
-
-
storage- Desctription: Data storage.
- Valid values:
'post'—$_POST'session'—$_SESSION
- Default value:
'post'
Examples
Save some data
[[ddStash?
&save=`{
"userData": {
"firstName": "John",
"lastName": "[[ddGetDocumentField?
&docId=`1`
&docField=`lastName`
]]",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
},
"someData": "someValue"
}`
]]
Get saved data
You can get field value in any depth
[[ddStash? &get=`someData`]]
Returns someValue.
[[ddStash? &get=`userData.firstName`]]
Returns John.
[[ddStash? &get=`userData.children.0.firstName`]]
Returns Alice.
Also you can get objects in JSON
If field value is object or array, it will be returned in JSON format.
Get first John child
[[ddStash? &get=`userData.children.0`]]
Returns:
{
"firstName": "Alice"
}
Get all John children:
[[ddStash? &get=`userData.children`]]
Returns:
[
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
Get all data about John
[[ddStash? &get=`userData`]]
Returns:
{
"firstName": "John",
"lastName": "Doe",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
}
Save: Extend an existing object instead of overwriting it (&save_extendExisting=`1` )
First you save some object:
[[ddStash?
&save=`{
"userData": {
"firstName": "Chuck",
"lastName": "Doe",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
}
}`
]]
Then if you just save object with the same key (userData):
[[ddStash?
&save=`{
"userData": {
"middleName": "Ray",
"lastName": "Norris"
}
}`
]]
The snippet will overwrite previous saved object completely:
[[ddStash? &get=`userData`]]
Returns:
{
"middleName": "Ray",
"lastName": "Norris"
}
So, if you want to extend the first object just use the save_extendExisting parameter:
[[ddStash?
&save=`{
"userData": {
"middleName": "Ray",
"lastName": "Norris"
}
}`
&save_extendExisting=`1`
]]
In this case the snippet will recursive extend the first object with the data from the second:
[[ddStash? &get=`userData`]]
Returns:
{
"firstName": "Chuck",
"middleName": "Ray",
"lastName": "Norris",
"children": [
{
"firstName": "Alice"
},
{
"firstName": "Robert"
}
]
}
Save: Extend without overwriting fields with empty values (&save_extendExistingWithEmpty=`0` )
By default, empty field values (e. g. '') are treated as other values and will replace non-empty ones.
[[ddStash?
&save=`{
"userData": {
"firstName": "John",
"lastName": "Tesla",
"discipline": "Electrical engineering"
}
}`
]]
[[ddStash?
&save=`{
"userData": {
"firstName": "Nikola",
"lastName": ""
}
}`
&save_extendExisting=`1`
]]
Returns:
{
"firstName": "Nikola",
"lastName": "",
"discipline": "Electrical engineering"
}
Empty lastName from the second object replaced non-empty lastName from the first.
If you want to ignore empty values, just use save_extendExistingWithEmpty == 0:
[[ddStash? &save=`{ "userData": { "firstName": "John", "lastName": "Tesla", "discipline": "Electrical engineering" } }` ]] [[ddStash? &save=`{ "userData": { "firstName": "Nikola", "lastName": "" } }` &save_extendExisting=`1` &save_extendExistingWithEmpty=`0` ]]
Returns:
{
"firstName": "Nikola",
"lastName": "Tesla",
"discipline": "Electrical engineering"
}
Run the snippet through \DDTools\Snippet::runSnippet without DB and eval
//Include (MODX)EvolutionCMS.libraries.ddTools require_once( $modx->getConfig('base_path') . 'assets/libs/ddTools/modx.ddtools.class.php' ); //Run (MODX)EvolutionCMS.snippets.ddStash \DDTools\Snippet::runSnippet([ 'name' => 'ddStash', 'params' => [ 'get' => 'userData.firstName' ] ]);
Links
dd/evolutioncms-snippets-ddstash 适用场景与选型建议
dd/evolutioncms-snippets-ddstash 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「stash」 「modx」 「MODX Evo」 「evo」 「modx evolution」 「evo cms」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dd/evolutioncms-snippets-ddstash 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dd/evolutioncms-snippets-ddstash 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dd/evolutioncms-snippets-ddstash 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Pimple 3 ServiceProvider integrating the Stash caching component
Stash Cache pool wrapper
A nice API to write csv files.
Stash Cache middleware for Relay
StashSilex is a Silex service provider and session handler for the popular caching library Stash
A global value manager.
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2020-05-08