andydune/conditional-execution
Composer 安装命令:
composer require andydune/conditional-execution
包简介
It allows for conditional execution of code fragments more beautiful and testable.
关键字:
README 文档
README
It allows for conditional execution of code fragments more beautiful and testable. Line code with no indentation prevents appearance of errors and improves readability.
Requirements
- PHP version >= 5.6
- A candy for your friend in store
Installation
Installation using composer:
composer require andydune/conditional-execution
Or if composer was not installed globally:
php composer.phar require andydune/conditional-execution
Or edit your composer.json:
"require" : {
"andydune/conditional-execution": "^1"
}
And execute command:
php composer.phar update
See a problem
Here condition for execution. I meet something like this it in many CMS ofter:
if ((empty($arParams["PAGER_PARAMS_NAME"]) || !preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["PAGER_PARAMS_NAME"])) && $arParams["SECTION_ID"] > 0 && $arParams["SECTION_ID"]."" != $arParams["~SECTION_ID"] ) { // some php code // fuction call or method }
It's difficult to read, search error or edit.
This is better way library offers:
use AndyDune\ConditionalExecution\ConditionHolder; $instanceOr = new ConditionHolder(); $instanceOr->bindOr() ->add(empty($arParams["PAGER_PARAMS_NAME"])) ->add(!preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["PAGER_PARAMS_NAME"])); $instanceTotal = = new ConditionHolder(); // default bind AND $instanceTotal->executeIfTrue(function(){ // some php code // fuction call or method }); $instanceTotal->add($instanceOr) ->add($arParams["SECTION_ID"] > 0) ->add($arParams["SECTION_ID"]."" != $arParams["~SECTION_ID"]); $result = $instanceTotal->doIt(); // yes, do it!
Methods
add($condition)
It adds condition to a queue. Condition is not check immediately. It can be callable.
bindAnd()
Change bind of conditions to AND logic. AND is used as default.
bindOr()
Change bind of conditions to OR logic.
check()
It executes check of all collected conditions.
doIt()
It checks of all collected conditions and execute appropriate function and triggers.
Benefit
Simple add or delete conditions
You don't need to count brackets. Add, remove conditions is simple.
use AndyDune\ConditionalExecution\ConditionHolder; $instance = new ConditionHolder(); $instance->add($val1 > $val2); $instance->add($someObject->isGood()); $instance->check(); // true $instance->add(''); $instance->check(); // false $instance->bindOr(); $instance->check(); // true
Closure as condition and params
You can use closure as condition. Function can receive params witch was inserted with doIt or check methods.
$instance = new ConditionHolder(); $instance->add(function ($value) { if ($value > 2) { return true; } return false; }); $instance->executeIfTrue(function () { return 'Y'; }); $instance->executeIfFalse(function () { return 'N'; }); $instance->doIt(3); // returns 'Y' $instance->chack(3); // returns true $instance->doIt(1); // returns 'N' $instance->chack(1); // returns false
Check classes for checks of any complexity
There is mechanic for check of any complexity. It is describes as instance of AndyDune\ConditionalExecution\Check\CheckAbstract.
You may create your own custom class and use it.
ArrayValueWithKeyNotEmpty
It checks array value with key is not empty.
use AndyDune\ConditionalExecution\Check\ArrayValueWithKeyNotEmpty; use AndyDune\ConditionalExecution\ConditionHolder; // Source array $array = [ 'one' => 1 ]; $condition = new ConditionHolder(); $condition->add(new ArrayValueWithKeyNotEmpty('one')); $condition->check($array); // result is true $condition->setNegative(); $condition->check($array); // result is false
ArrayHasNotEmptyValueOrKeyNotExist
Given array key must not exist or keep value == true
use AndyDune\ConditionalExecution\Check\ArrayHasNotEmptyValueOrKeyNotExist; use AndyDune\ConditionalExecution\ConditionHolder; $array = [ 'one' => 1, 'two' => '', 'three' => 0 ]; $condition = new ConditionHolder(); $condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('one'))->check($array); // true $condition = new ConditionHolder(); $condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('two'))->check($array); // false $condition = new ConditionHolder(); $condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('three'))->check($array); // false $condition = new ConditionHolder(); $condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('four'))->check($array); // true
Execute functions in list for get first result
use AndyDune\ConditionalExecution\GetFirstSuccessResult; $instance = new GetFirstSuccessResult(); $instance->add(function () { return ''; }); $instance->add(function () { return 'two'; }); $instance->get(); // resturns 'two'
With params:
$instance = new GetFirstSuccessResult(); $instance->add(function ($string, $length = 5) { if (strlen($string) < $length) { return $string . '<'; } return false; }); $instance->add(function ($string, $length = 5) { if (strlen($string) > $length) { return $string . '>'; } return false; }); $instance->get('two', 4); // returns 'two<' $instance->get('two', 2); // returns 'two>' $instance->get('onetwo', 4); // returns onetwo> $instance->get('tw', 2); returns false
andydune/conditional-execution 适用场景与选型建议
andydune/conditional-execution 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 498 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「middleware」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andydune/conditional-execution 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andydune/conditional-execution 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andydune/conditional-execution 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
PSR-15 middleware to handle CSRF-token verification
A simple HTTP application builder using PSR-15 HTTP Server Request Handler and Middleware.
Alfabank REST API integration
统计信息
- 总下载量: 498
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-03-06