承接 dd/evolutioncms-snippets-ddrunsnippets 相关项目开发

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

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

dd/evolutioncms-snippets-ddrunsnippets

Composer 安装命令:

composer require dd/evolutioncms-snippets-ddrunsnippets

包简介

Snippet runs necessary snippets with necessary params.

README 文档

README

Snippet runs necessary snippets with necessary params. Capabilities:

  • Run a few snippets consequentially.
  • Snippets results can be sent into parameters names and/or into other snippets values (can repeat this as much as you like).
  • Any executed snippet can return either a string or a native PHP array. It is convenient to use with “nested” placeholders (see examples below).
  • The snippet result can be returned into the chunk outputterParams->tpl, have transferring additional data through the parameter outputterParams->placeholders.

See the documentation for a more complete picture.

☝ Please note that snippets are run through \DDTools\Snippet::runSnippet. This increases performance and saves server resources, but unfortunately you can't run snippets that do not use \DDTools\Snippet. Please give us feedback via Telegram chat if this is critical.

Requires

Installation

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.ddRunSnippets
\DDInstaller::install([
	'url' => 'https://github.com/DivanDesign/EvolutionCMS.snippets.ddRunSnippets',
	'type' => 'snippet',
]);

//Install (MODX)EvolutionCMS.plugins.ddRunSnippets (it is required if you want to use the `snippets->{$snippetName}->runParams->cache` parameters)
\DDInstaller::install([
	'url' => 'https://github.com/DivanDesign/EvolutionCMS.plugins.ddRunSnippets',
	'type' => 'plugin',
]);
  • If ddRunSnippets is not exist on your site, ddInstaller will just install it.
  • If ddRunSnippets is already exist on your site, ddInstaller will check it version and update it if needed.

Manually

1. Elements → Snippets: Create a new snippet with the following data

  1. Snippet name: ddRunSnippets.
  2. Description: <b>4.2.1</b> Snippet runs necessary snippets with necessary params..
  3. Category: Core.
  4. Parse DocBlock: no.
  5. Snippet code (php): Insert content of the ddRunSnippets_snippet.php file from the archive.

2. Elements → Manage Files

  1. Create a new folder assets/snippets/ddRunSnippets/.
  2. Extract the archive to the folder (except ddRunSnippets_snippet.php).

3. Install (MODX)EvolutionCMS.plugins.ddRunSnippets

The plugin is required if you want to use the snippets->{$snippetName}->runParams->cache parameters.

Parameters description

Snippets execution parameters

  • snippets

    • Description: List of snippets to be run. Snippets are called in accordance with the specified order.
    • Valid values:
      • stringJsonObject — as JSON
      • stringHjsonObject — as HJSON
      • stringQueryFormatted — as Query string
      • It can also be set as a native PHP object or array (e. g. for calls through \DDTools\Snippet::runSnippet):
        • arrayAssociative
        • object
    • Required
  • snippets->{$snippetName}

    • Description: A snippet, when the key is the snippet name and the value is the snippet parameters.
      • By default a snippet result will be equal to [+snippetName+] in parameters and chunk (where snippetName is the snippet name).
      • But also you can set custom snippet aliases in this parameter using the '=' delimiter (e. g. ddGetDocuments=docs).
      • Every snippet can return either a string or a native PHP array (it is convenient to use with “nested” placeholders, see examples below).
    • Valid values:
      • object — an object with snippet parameters (see below)
      • boolean — for simple snippet calls without parameters or if you need to use default parameters, you can just pass true
    • Required
  • snippets->{$snippetName}->{$paramName}

    • Description: A snippet parameter, when the key is the parameter name and the value is the parameter value.
      • Use [+snippetName+] for substitution by any previous snippet execution result in the parameter name or value (where snippetName is the snippet name).
      • Or use [+snippetAlias+] if specified (e. g. docs if the snippet name set as ddGetDocuments=docs).
    • Valid values:
      • mixed — as opposed to standard CMS calling you can pass not only string parameters to the snippet, any types are supported
    • Default value: —
  • snippets->{$snippetName}->runParams

    • Description: Additional parameters of snippet running.
    • Valid values: object
    • Default value: —
  • snippets->{$snippetName}->runParams->parseResultCompletely

    • Description: Completely parse result of the snippet by CMS parser.
    • Valid values: boolean
    • Default value: — (depends on snippets_parseEachResultCompletely)
  • snippets->{$snippetName}->runParams->cache

    • Description: You can cache snippet result to a specific file.
    • Valid values: object
    • Default value: —
  • snippets->{$snippetName}->runParams->cache->resourceId

    • Description: Resource ID (e. g. document) related to cache.
      It means that the cache file will be destroyed when the document will be updated or deleted.
    • Valid values: string
    • Required
  • snippets->{$snippetName}->runParams->cache->suffix

    • Description: Unique cache name for the document.
    • Valid values: string
    • Required
  • snippets->{$snippetName}->runParams->cache->prefix

    • Description: Cache file prefix. Useful if you want to cache some custom data that is not related to any documents.
    • Valid values: string
    • Default value: 'doc'
  • snippets_parseEachResultCompletely

    • Description: Parse result of each snippet by CMS parser.
      Immediately after running each snippet, its result will be parsed by $modx->parseDocumentSource().
    • Valid values: boolean
    • Default value: false

Output parameters

  • outputterParams

    • Description: Parameters to be passed to the specified outputter.
    • Valid values:
      • stringJsonObject — as JSON
      • stringHjsonObject — as HJSON
      • stringQueryFormatted — as Query string
      • It can also be set as native PHP object or array (e. g. for calls through \DDTools\Snippet::runSnippet or $modx->runSnippet):
        • arrayAssociative
        • object
    • Default value: —
  • outputterParams->tpl

    • Description: Chunk for output results.
      Available placeholders:
      • [+snippetName+] — a snippet result (where snippetName is a snippet name)
      • [+ddRunSnippetsResult.all+] — results of all executed snippets combined by ''
      • [+any placeholders from the outputterParams->placeholders parameter+]
    • Valid values:
      • stringChunkName
      • string — use inline templates starting with @CODE:
      • '' — all snippets will be executed but nothing will be returned
    • Default value: ''
  • outputterParams->placeholders

    • Description: Additional data has to be passed into outputterParams->tpl.
      Nested objects and arrays are supported too:
      • {"someOne": "1", "someTwo": "test" } => [+someOne+], [+someTwo+].
      • {"some": {"a": "one", "b": "two"} } => [+some.a+], [+some.b+].
      • {"some": ["one", "two"] } => [+some.0+], [+some.1+].
    • Valid values: object
    • Default value: —

Examples

All examples are written using HJSON, but if you want you can use vanilla JSON instead.

Basic example

[[ddRunSnippets?
	&snippets=`{
		someSnippet: {
			exampleParam: Example value.
		}
		
		otherSnippet: {
			someParam: "[+someSnippet+]"
		}
		
		anotherSnippet: {
			//Results of previous snippets can be used both in parameters names and their values
			"[+otherSnippet+]": "[+someSnippet+]"
		}
	}`
	&outputterParams=`{
		tpl: "@CODE:[+anotherSnippet+]"
	}`
]]

Use alias in snippet names

[[ddRunSnippets?
	&snippets=`{
		//Run “someSnippet” and save it's result as “snippet1”
		someSnippet=snippet1: {
			exampleParam: First example value.
		}
		
		//Run “someSnippet” and save it's result as “snippet2”
		someSnippet=snippet2: {
			exampleParam: Second example value.
			//Placeholder “[+snippet1+]” will be replaced to results of previous “someSnippet” call
			exampleParam2: "[+snippet1+]"
		}
		
		anotherSnippet: {
			someParam: "[+snippet2+]"
			"[+snippet1+]": "[+snippet2+]"
		}
	}`
	&outputterParams=`{
		tpl: "@CODE:[+anotherSnippet+]"
	}`
]]

We called snippet someSnippet twice with different parameters using two different aliases for each call: snippet1 and snippet2.

Pass objects and arrays as snippet params

As opposed to standard CMS calling you can pass not only string parameters to a snippet, any types are supported.

[[ddRunSnippets?
	&snippets=`{
		someSnippet: {
			//Object as parameter value
			exampleParam1: {
				objectField: “exampleParam1” is an object, not a string.
				otherField: true
			}
			
			//Array as parameter value
			exampleParam2: [
				“exampleParam2” is an array, not a string
				2.71
			]
		}
	}`
]]

Using results of previous snippets in object-values of parameters

[[ddRunSnippets?
	&snippets=`{
		someSnippet: {
			exampleParam: Example value.
		}
		
		otherSnippet: {
			exampleParam1: {
				objectField: “exampleParam1” is an object, not a string.
				otherField: So, “someSnippet” result will be replace the [+someSnippet+] placeholder here.
				anotherField: {
					deepField: Snipepts results placeholders will work fine independent of object depth.
					otherDeepField: The palceholder [+someSnippet+] works here too.
				}
			}
		}
	}`
]]

Native PHP arrays as results of snippets and “nested” placeholders

Let the example snippet named personData that returs a native PHP array instead of usual string:

//Just for example let the snippet do nothing but only return an array :)
return [
	'name' => 'Tamara Eidelman',
	'birthdate' => '1959.12.15',
	'links' => [
		'youtube' => 'https://youtube.com/c/TamaraEidelmanHistory',
		'site' => 'https://eidelman.ru',
	],
];

Then execute some snippets via ddRunSnippets:

[[ddRunSnippets?
	&snippets=`{
		//Run our example snippet that returns a native PHP array (without parameters in this example case)
		personData: true
		
		//Run other snippet
		someSnippet: {
			//If we need to pass whole result of the first snippet, just use usual placeholder
			//ddRunSnippet will convert the native PHP array to JSON in this case
			personData: "[+personData+]"
		}
		
		//Run another snippet
		anotherSnippet: {
			//Also we can use “nested” placeholders to get an item of the snippet result array
			info: "[+personData.name+], [+personData.birthdate+]"
			//And it works with depth
			youtube: "[+personData.links.youtube+]"
		}
	}`
]]

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.ddRunSnippets
\DDTools\Snippet::runSnippet([
	'name' => 'ddRunSnippets',
	'params' => [
		'snippets' => [
			'someSnippet' => [
				'exampleParam' => 'Example value.',
			],
			'otherSnippet' => [
				'someParam' => '[+someSnippet+]',
			],
			'anotherSnippet' => [
				'[+otherSnippet+]' => '[+someSnippet+]',
			],
		],
		'outputterParams' => [
			'tpl' => '@CODE:[+anotherSnippet+]',
		],
	],
]);

Links

dd/evolutioncms-snippets-ddrunsnippets 适用场景与选型建议

dd/evolutioncms-snippets-ddrunsnippets 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 05 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 dd/evolutioncms-snippets-ddrunsnippets 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Unknown
  • 更新时间: 2020-05-13