andydune/conditional-execution 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

andydune/conditional-execution

Composer 安装命令:

composer require andydune/conditional-execution

包简介

It allows for conditional execution of code fragments more beautiful and testable.

README 文档

README

Build Status Software License Packagist Version Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-03-06