ganglio/please
Composer 安装命令:
composer require ganglio/please
包简介
PHP Functional Try
关键字:
README 文档
README
Functional and polite implementation of try/catch
Examples
Let's start with something simple:
$inverse = function ($v) { if (empty($v)) { throw new \Exception("Division by zero"); } return 1/$v; }
Usually what we would do is something like this:
try { $result = $inverse(0); } catch (\Exception $e) { error_log("Error: " . $e->getMessage()); }
Using Please we do:
$result = new Please($inverse, 0);
Now we can check if the callable completed successfully or not.
if ($result->isSuccess) { echo "The result is: " . $result->get(); }
Or we can do it using a callback (much nicer)
$result->onSuccess(function ($v) { echo "The result is: " . $v; });
Unfortunately we tried to divide by zero so the callable didn't succeeded. We can either get the exception:
$exception = $result->get();
Or process it with the callback:
$result->onFailure(function ($e) { error_log("Error: " . $e->getMessage()); });
We can even pass two callbacks using on:
$result->on( function ($v) { echo "Result: " . $v; }, function ($e) { echo "Error: " . $e->getMessage(); } );
onSuccess, onFailure and on return a new instance of Please wrapping the callback. This way, if we want we can do something like this:
echo (new Please($inverse, $unknown_divisor)) ->on( function ($v) { return "Result: " . $v; }, function ($e) { return "Error: " . $e->getMessage; } )->get();
And now a slighly more complex example:
Let's immagine we have an array of strings, some of which are json encoded objects:
$strings = [ 'not json', '{"a":3,"b":4}', 'still not json', '{"c":5}', ];
And a wrapper to json_decode throwing and exception on error:
$json_decoder_exception = function ($string) { $out = json_decode($string); if (json_last_error() != JSON_ERROR_NONE) { throw new \Exception("Invalid JSON"); } return $out; };
We can then decode and filter them using Please:
$results = array_filter( array_map(function ($s) use ($json_decoder_exception) { return new Please($json_decoder_exception, $s); }, $strings), function ($e) { return $e->isSuccess; } );
ganglio/please 适用场景与选型建议
ganglio/please 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 20 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「functional」 「try」 「catch」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ganglio/please 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ganglio/please 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ganglio/please 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Connection 'Db' codeception module to 'Yii2' module database settings
Laravel package for the Airbrake API, which supports Errbit
PHPSlang is a library that allow you to write a purely functional code in PHP
Functional library for php with proper currying
Iteration tools for PHP
Polling library for conditionally retry operations based on a result checker.
统计信息
- 总下载量: 20
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-03