streamcommon/promise
Composer 安装命令:
composer require streamcommon/promise
包简介
PHP-CLI promise implementation
README 文档
README
This package provides Promise/A+ PHP implementation.
Branches
Installation
Console run:
composer require streamcommon/promise
Or add into your composer.json:
"require": { "streamcommon/promise": "*" }
If you want see TRUE promise then install Swoole extension. For more info visit the Swoole repo
NOTE: TRUE promise work only in CLI mode
Promise
Promise is a library which provides Promise/A+ PHP implementation.
All Promise it a special PHP classes that contains its state:
pending- PromiseInterface::STATE_PENDINGfulfilled- PromiseInterface::STATE_FULFILLEDrejected- PromiseInterface::STATE_REJECTED
To initiate a new promise, you can use static method PromiseInterface::create or create with new.
All resulting Promise has PromiseInterface::STATE_PENDING state.
$promise = new Promise(function(callable $resolve, callable $reject)); // OR $promise = Promise::create(function(callable $resolve, callable $reject))
When function($resolve, $reject) executor finishes the job, it should call one of the functions:
$resolveto indicate that the job finished successfully and setPromisestate toPromiseInterface::STATE_FULFILLED
$resolve = function ($value) { $this->setState(PromiseInterface::STATE_FULFILLED); $this->setResult($value); };
$rejectto indicate that an error occurred and setPromisestate toPromiseInterface::STATE_REJECTED
$reject = function ($value) { $this->setState(PromiseInterface::STATE_REJECTED); $this->setResult($value); };
Method PromiseInterface::then() it be called after promise change stage. In terms of our analogy: this is the “subscription".
public function then(?callable $onFulfilled = null, ?callable $onRejected = null): PromiseInterface;
$onFulfilledrun when thePromiseis resolved and it hasPromiseInterface::STATE_FULFILLEDstate.$onFulfilledrun when thePromiseis rejected and it hasPromiseInterface::STATE_REJECTEDstate.
NOTE: If
$onFulfilledor$onFulfilledis not a callable function it was ignore
Calling PromiseInterface::resolve() creates a successfully executed promise with the result value.
public static function resolve($value): PromiseInterface;
It is similar to:
$promise = new Promise(function(callable $resolve) { $resolve($value) });
Similarly PromiseInterface::reject() creates an already executed promise with an error value.
public static function reject($value): PromiseInterface;
It is similar to:
$promise = new Promise(function(callable $resolve, callable $reject) { $reject($value) });
Sub promise
When function($resolve, $reject) executor finishes the job, it can return PromiseInterface.
$promise = Promise::create(function (callable $resolve) { $resolve(Promise::create(function (callable $subResolve) { $subResolve(42); })); });
In this case, it will wait for the execution of sub promise.
Method PromiseInterface::then() can return PromiseInterface to.
$promise->then(function ($value) { return Promise::create(function (callable $resolve) use ($value) { $resolve($value + 1); }); });
For more info check example scripts.
Example
Standard Promise
use Streamcommon\Promise\Promise; $promise = Promise::create(function (callable $resolve) { $resolve(41); }); $newPromise = $promise->then(function ($value) { return $value + 1; }); $promise->then(function ($value) { echo $value . ' === 41' . PHP_EOL; }); $newPromise->then(function ($value) { echo $value . ' === 42' . PHP_EOL; }); $promise->wait(); // promise execution
If you want see TRUE promise then install Swoole extension. For more info visit the Swoole repo
NOTE: TRUE promise work only in CLI mode
use Streamcommon\Promise\ExtSwoolePromise; // be careful with this \Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC $promise = ExtSwoolePromise::create(function (callable $resolve) { // the function is executed automatically when the promise is constructed $resolve(41); }); $promise->then(function ($value) { // the function is executed automatically after __constructor job return $value + 1; })->then(function ($value) { // the function is executed automatically after ::then() echo $value . PHP_EOL; });
Sub promise
use Streamcommon\Promise\Promise; $promise = Promise::create(function (callable $resolve) { $resolve(Promise::create(function (callable $resolve) { $resolve(42); })); }); $newPromise = $promise->then(function ($value) { return $value + 1; }); $superNewPromise = $promise->then(function ($value) { return Promise::create(function (callable $resolve) use ($value) { $resolve($value + 2); }); }); $promise->then(function ($value) { echo $value . ' === 42' . PHP_EOL; }); $newPromise->then(function ($value) { echo $value . ' === 43' . PHP_EOL; }); $superNewPromise->then(function ($value) { echo $value . ' === 44' . PHP_EOL; }); $promise->wait();
Sub async promise
use Streamcommon\Promise\ExtSwoolePromise; // be careful with this \Swoole\Runtime::enableCoroutine(); // IF YOU WANT REALY ASYNC $promise = ExtSwoolePromise::create(function (callable $resolve) { $promise = ExtSwoolePromise::create(function (callable $resolve) { $resolve(41); }); $promise->then(function ($value) use ($resolve) { $resolve($value); }); }); $promise->then(function ($value) { return $value + 1; })->then(function ($value) { echo $value . PHP_EOL; });
If use
ExtSwoolePromisewithdaemon|cycle|loopyou must useSwoole\Runtime::wait()
\Swoole\Runtime::enableCoroutine(); while (true) { /// Some code with ExtSwoolePromise /// \Swoole\Runtime::wait(); }
streamcommon/promise 适用场景与选型建议
streamcommon/promise 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.04k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2019 年 03 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「streamcommon」 「php promise」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 streamcommon/promise 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 streamcommon/promise 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 streamcommon/promise 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Lightweight library that provides regular PHP methods is a non-blocking implementation.
Server side promises
Safely namespaced guzzle promises for WP2Static
Provide default object based configuration
Doctrine 2 container interop
Generic PHP Threads library using only pure PHP
统计信息
- 总下载量: 2.04k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2019-03-03