feat(v1.1.0): 支付改由 epay 插件承载、插件纳入版本库,补齐媒体上传/LBS/互动/优惠券/后台看板,并修复余额充值资损
## 一、支付:改由已安装的 epay 插件承载(不再自研签名)
- 下单:\addons\epay\library\Service::submitOrder($amount 元, $orderNo, 'wechat', $body, $notifyUrl, null, 'miniapp', $openid)
- 验签:Service::checkNotify('wechat')(内部 $pay->verify());应答:$pay->success()(v2 = XML)
- 业务编排仍在 application/api/controller/Pay.php,不改插件源码:
POST /pay/prepay(取调起参数,下发前剔除 appId);POST /pay/notify(公开路由:插件验签 → 金额核对 → 幂等分派)
- 配置真源 = addons/epay/config.php(后台「插件管理 → 微信支付宝整合插件 → 配置」);
fc_config 的 wx.mchId / wx.apiKey / wx.payNotifyUrl 废弃;后台「系统设置」支付表单改只读并跳插件配置页
- 插件 API 保持 version=v2(PHP 7.4 可跑);certs/* 为 0 字节占位,不含任何真实密钥
- 修复 BUG-015:Pay::notify() 与基类站内信同名且签名不兼容导致 /pay/* 全 500,动作改名 notifyCallback()
## 二、插件纳入 Git 版本管理
- .gitignore 移除 /addons/* 与 /public/assets/addons/*(保留 .gitkeep 占位)
- 入库 addons/epay、addons/wechat、public/assets/addons/*、addons/.htaccess(deny from all)
- config.php 入库为空白模板;服务器请在填好密钥后执行
git update-index --skip-worktree addons/epay/config.php,避免真实密钥回传仓库
## 三、安全修复:余额充值资损(高危,已实测复现)
- 现象:POST /order/recharge {payType:'balance'} 只加余额不扣款(余额 134.50 → 1022.50),等于凭空增发余额,
可再去买会员 / 付费发布 / 解锁联系方式
- 修复:/order/recharge 仅接受 payType=wxpay,balance → 「余额充值仅支持微信支付」,其它 → 「支付方式不支持」;
订单恒为 status=pending、pay_type=wxpay、paytime=null;入账只发生在 /pay/notify 回调(Order::creditRecharge)
或后台「会员管理 → 余额充值」(fc/userext/recharge)
- 小程序充值页同步为「仅微信支付」+ 防重复提交 + onShow 刷新资产;mock / 用例 / 文档同步
## 四、其他 v1.1.0 能力
- 媒体上传:POST /post/upload(图片 ≤10MB / 视频 ≤50MB,真落盘),发布页 uni.uploadFile 真上传
- LBS:fc_post.lat/lng、首页 sort=nearby(距离升序、无坐标排最后)、卡片 distance、详情页地图
- 互动:举报 POST /report(原因字典)、收藏 / 关注(fc_favorite / fc_follow、/favorite/*、/follow/toggle)
- 交易:余额充值 POST /order/recharge、优惠券 /coupon/list|receive(fc_coupon / fc_user_coupon、我的资产券)
- 发布计费:会员发布折扣(discountRate/discountText)+ 免费发布条数(freeQuota*)
- 后台新增:数据总览 fc/dashboard、系统设置 fc/setting、优惠券管理 fc/coupon(含菜单与权限节点)
## 五、本批审核修复
- 详情页:游客不再请求登录态接口(避免 401 被清 token 踢去登录);微信付费查看改为支付后轮询解锁,
不再读取 wxpay 分支未返回的 contact;加防重复提交
- 发布页:freeLeftToday → freeQuotaLeft / freeQuotaCycle / freeUnlimited(对齐后端出参);
上传图预览拼后端域名(新增 format.js#assetUrl,后端返回相对路径)
- 收藏空态「去首页」:switchTab → reLaunch(项目用自绘 fc-tabbar,无 tabBar,switchTab 必然失败)
- 会员券 days=0 显示「永久有效」;manifest.json 补 requiredPrivateInfos(getLocation / chooseLocation)
- 后端:publishConfig 补 discountRate/discountText 与 memberPostDiscountText();优惠券总开关 coupon.enabled
真生效(关闭后 list/receive 拒绝);后台 fc/Coupon 的 add/edit 补 strip_tags 过滤(防存储型 XSS)、
补 searchFields='name',列表 name 列再做一次 HTML 转义
- FcInstall:补清理后台菜单缓存 __menu__(否则新菜单要等缓存过期才显示),并同步 README 说明
- 后台语言包 fc/setting.php 按「支付只读 + 跳插件配置」的新页面重写
- 文档同步:uniapp/docs/{01,03,04}、uniapp/README.md、docs/05-后端实现规范.md、后台 README 上线步骤
## 六、校验结果(全部通过)
- 后端端到端:tools/verify-api.mjs 226/226(含新增「拒绝余额充值(资损防护)」用例)
- 小程序:uniapp/tests/run.mjs 167/167;validate.mjs 0 错 0 警;check-scss 19/19
- 后台:check-admin-i18n 0 错 0 警;check-theme 0 错 0 警;9 个页面 HTTP 200 全中文
- php -l 改动/新增 PHP 文件 0 语法错误;探针复测:余额不再增长、epay 插件下单/验签链路通
这个提交包含在:
4
addons/epay/library/hyperf/engine/.gitattributes
vendored
普通文件
4
addons/epay/library/hyperf/engine/.gitattributes
vendored
普通文件
@@ -0,0 +1,4 @@
|
||||
/.github export-ignore
|
||||
/examples export-ignore
|
||||
/tests export-ignore
|
||||
|
||||
4
addons/epay/library/hyperf/engine/.gitignore
vendored
普通文件
4
addons/epay/library/hyperf/engine/.gitignore
vendored
普通文件
@@ -0,0 +1,4 @@
|
||||
/vendor/
|
||||
composer.lock
|
||||
*.cache
|
||||
*.log
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
$header = <<<'EOF'
|
||||
This file is part of Hyperf.
|
||||
|
||||
@link https://www.hyperf.io
|
||||
@document https://hyperf.wiki
|
||||
@contact group@hyperf.io
|
||||
@license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
EOF;
|
||||
|
||||
return (new PhpCsFixer\Config())
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
'@PSR2' => true,
|
||||
'@Symfony' => true,
|
||||
'@DoctrineAnnotation' => true,
|
||||
'@PhpCsFixer' => true,
|
||||
'header_comment' => [
|
||||
'comment_type' => 'PHPDoc',
|
||||
'header' => $header,
|
||||
'separate' => 'none',
|
||||
'location' => 'after_declare_strict',
|
||||
],
|
||||
'array_syntax' => [
|
||||
'syntax' => 'short'
|
||||
],
|
||||
'list_syntax' => [
|
||||
'syntax' => 'short'
|
||||
],
|
||||
'concat_space' => [
|
||||
'spacing' => 'one'
|
||||
],
|
||||
'blank_line_before_statement' => [
|
||||
'statements' => [
|
||||
'declare',
|
||||
],
|
||||
],
|
||||
'general_phpdoc_annotation_remove' => [
|
||||
'annotations' => [
|
||||
'author'
|
||||
],
|
||||
],
|
||||
'ordered_imports' => [
|
||||
'imports_order' => [
|
||||
'class', 'function', 'const',
|
||||
],
|
||||
'sort_algorithm' => 'alpha',
|
||||
],
|
||||
'single_line_comment_style' => [
|
||||
'comment_types' => [
|
||||
],
|
||||
],
|
||||
'yoda_style' => [
|
||||
'always_move_variable' => false,
|
||||
'equal' => false,
|
||||
'identical' => false,
|
||||
],
|
||||
'phpdoc_align' => [
|
||||
'align' => 'left',
|
||||
],
|
||||
'multiline_whitespace_before_semicolons' => [
|
||||
'strategy' => 'no_multi_line',
|
||||
],
|
||||
'constant_case' => [
|
||||
'case' => 'lower',
|
||||
],
|
||||
'class_attributes_separation' => true,
|
||||
'combine_consecutive_unsets' => true,
|
||||
'declare_strict_types' => true,
|
||||
'linebreak_after_opening_tag' => true,
|
||||
'lowercase_static_reference' => true,
|
||||
'no_useless_else' => true,
|
||||
'no_unused_imports' => true,
|
||||
'not_operator_with_successor_space' => true,
|
||||
'not_operator_with_space' => false,
|
||||
'ordered_class_elements' => true,
|
||||
'php_unit_strict' => false,
|
||||
'phpdoc_separation' => false,
|
||||
'single_quote' => true,
|
||||
'standardize_not_equals' => true,
|
||||
'multiline_comment_opening_closing' => true,
|
||||
])
|
||||
->setFinder(
|
||||
PhpCsFixer\Finder::create()
|
||||
->exclude('vendor')
|
||||
->in(__DIR__)
|
||||
)
|
||||
->setUsingCache(false);
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace PHPSTORM_META {
|
||||
// Reflect
|
||||
override(\Psr\Container\ContainerInterface::get(0), map('@'));
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
# Default Dockerfile
|
||||
#
|
||||
# @link https://www.hyperf.io
|
||||
# @document https://hyperf.wiki
|
||||
# @contact group@hyperf.io
|
||||
# @license https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE
|
||||
|
||||
ARG PHP_VERSION
|
||||
ARG ALPINE_VERSION
|
||||
|
||||
FROM hyperf/hyperf:${PHP_VERSION}-alpine-${ALPINE_VERSION}-swoole
|
||||
LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT" app.name="Hyperf"
|
||||
|
||||
ARG timezone
|
||||
ARG PHP_VERSION
|
||||
|
||||
ENV TIMEZONE=${timezone:-"Asia/Shanghai"}
|
||||
ENV COMPOSER_ROOT_VERSION="v1.2.0"
|
||||
|
||||
# update
|
||||
RUN set -ex \
|
||||
# show php version and extensions
|
||||
&& php -v \
|
||||
&& php -m \
|
||||
&& php --ri swoole \
|
||||
# ---------- some config ----------
|
||||
&& cd "/etc/php${PHP_VERSION%\.*}" \
|
||||
# - config PHP
|
||||
&& { \
|
||||
echo "upload_max_filesize=128M"; \
|
||||
echo "post_max_size=128M"; \
|
||||
echo "memory_limit=1G"; \
|
||||
echo "date.timezone=${TIMEZONE}"; \
|
||||
} | tee conf.d/99_overrides.ini \
|
||||
# - config timezone
|
||||
&& ln -sf /usr/share/zoneinfo/${TIMEZONE} /etc/localtime \
|
||||
&& echo "${TIMEZONE}" > /etc/timezone \
|
||||
# ---------- clear works ----------
|
||||
&& rm -rf /var/cache/apk/* /tmp/* /usr/share/man \
|
||||
&& echo -e "\033[42;37m Build Completed :).\033[0m\n"
|
||||
|
||||
WORKDIR /opt/www
|
||||
|
||||
COPY . /opt/www
|
||||
|
||||
RUN composer install -o
|
||||
@@ -0,0 +1,7 @@
|
||||
# Swoole Engine
|
||||
|
||||

|
||||
|
||||
```
|
||||
composer require hyperf/engine
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "hyperf/engine",
|
||||
"type": "library",
|
||||
"license": "MIT",
|
||||
"keywords": [
|
||||
"php",
|
||||
"hyperf"
|
||||
],
|
||||
"description": "",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Hyperf\\Engine\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"HyperfTest\\": "tests"
|
||||
}
|
||||
},
|
||||
"require": {
|
||||
"php": ">=7.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"friendsofphp/php-cs-fixer": "^3.0",
|
||||
"hyperf/guzzle": "^2.2",
|
||||
"phpstan/phpstan": "^1.0",
|
||||
"phpunit/phpunit": "^9.4",
|
||||
"swoole/ide-helper": "dev-master"
|
||||
},
|
||||
"suggest": {
|
||||
"ext-swoole": ">=4.5"
|
||||
},
|
||||
"minimum-stability": "dev",
|
||||
"prefer-stable": true,
|
||||
"config": {
|
||||
"optimize-autoloader": true,
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.2-dev"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "phpunit -c phpunit.xml --colors=always",
|
||||
"analyse": "phpstan analyse --memory-limit 1024M -l 0 ./src",
|
||||
"cs-fix": "php-cs-fixer fix $1"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="tests/bootstrap.php"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
verbose="true"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false">
|
||||
<testsuite name="Testsuite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
use Hyperf\Engine\Contract\ChannelInterface;
|
||||
use Hyperf\Engine\Exception\RuntimeException;
|
||||
|
||||
if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 50000) {
|
||||
class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
|
||||
{
|
||||
protected bool $closed = false;
|
||||
|
||||
public function push(mixed $data, float $timeout = -1): bool
|
||||
{
|
||||
return parent::push($data, $timeout);
|
||||
}
|
||||
|
||||
public function pop(float $timeout = -1): mixed
|
||||
{
|
||||
return parent::pop($timeout);
|
||||
}
|
||||
|
||||
public function getCapacity(): int
|
||||
{
|
||||
return $this->capacity;
|
||||
}
|
||||
|
||||
public function getLength(): int
|
||||
{
|
||||
return $this->length();
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return ! $this->isClosing();
|
||||
}
|
||||
|
||||
public function close(): bool
|
||||
{
|
||||
$this->closed = true;
|
||||
return parent::close();
|
||||
}
|
||||
|
||||
public function hasProducers(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function hasConsumers(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isReadable(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isWritable(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isClosing(): bool
|
||||
{
|
||||
return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
|
||||
}
|
||||
|
||||
public function isTimeout(): bool
|
||||
{
|
||||
return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
class Channel extends \Swoole\Coroutine\Channel implements ChannelInterface
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $closed = false;
|
||||
|
||||
public function getCapacity(): int
|
||||
{
|
||||
return $this->capacity;
|
||||
}
|
||||
|
||||
public function getLength(): int
|
||||
{
|
||||
return $this->length();
|
||||
}
|
||||
|
||||
public function isAvailable(): bool
|
||||
{
|
||||
return ! $this->isClosing();
|
||||
}
|
||||
|
||||
public function close(): bool
|
||||
{
|
||||
$this->closed = true;
|
||||
return parent::close();
|
||||
}
|
||||
|
||||
public function hasProducers(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function hasConsumers(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isReadable(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isWritable(): bool
|
||||
{
|
||||
throw new RuntimeException('Not supported.');
|
||||
}
|
||||
|
||||
public function isClosing(): bool
|
||||
{
|
||||
return $this->closed || $this->errCode === SWOOLE_CHANNEL_CLOSED;
|
||||
}
|
||||
|
||||
public function isTimeout(): bool
|
||||
{
|
||||
return ! $this->closed && $this->errCode === SWOOLE_CHANNEL_TIMEOUT;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
use Swoole\Coroutine\Http\Server as HttpServer;
|
||||
use Swoole\Coroutine\Server;
|
||||
|
||||
class Constant
|
||||
{
|
||||
public const ENGINE = 'Swoole';
|
||||
|
||||
public static function isCoroutineServer($server): bool
|
||||
{
|
||||
return $server instanceof Server || $server instanceof HttpServer;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Contract;
|
||||
|
||||
if (PHP_VERSION_ID > 80000 && SWOOLE_VERSION_ID >= 50000) {
|
||||
interface ChannelInterface
|
||||
{
|
||||
/**
|
||||
* @param float|int $timeout [optional] = -1
|
||||
*/
|
||||
public function push(mixed $data, float $timeout = -1): bool;
|
||||
|
||||
/**
|
||||
* @param float $timeout seconds [optional] = -1
|
||||
* @return mixed when pop failed, return false
|
||||
*/
|
||||
public function pop(float $timeout = -1): mixed;
|
||||
|
||||
/**
|
||||
* Swow: When the channel is closed, all the data in it will be destroyed.
|
||||
* Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed.
|
||||
*/
|
||||
public function close(): bool;
|
||||
|
||||
public function getCapacity(): int;
|
||||
|
||||
public function getLength(): int;
|
||||
|
||||
public function isAvailable(): bool;
|
||||
|
||||
public function hasProducers(): bool;
|
||||
|
||||
public function hasConsumers(): bool;
|
||||
|
||||
public function isEmpty(): bool;
|
||||
|
||||
public function isFull(): bool;
|
||||
|
||||
public function isReadable(): bool;
|
||||
|
||||
public function isWritable(): bool;
|
||||
|
||||
public function isClosing(): bool;
|
||||
|
||||
public function isTimeout(): bool;
|
||||
}
|
||||
} else {
|
||||
interface ChannelInterface
|
||||
{
|
||||
/**
|
||||
* @param mixed $data [required]
|
||||
* @param float|int $timeout [optional] = -1
|
||||
* @return bool
|
||||
*/
|
||||
public function push($data, $timeout = -1);
|
||||
|
||||
/**
|
||||
* @param float $timeout seconds [optional] = -1
|
||||
* @return mixed when pop failed, return false
|
||||
*/
|
||||
public function pop($timeout = -1);
|
||||
|
||||
/**
|
||||
* Swow: When the channel is closed, all the data in it will be destroyed.
|
||||
* Swoole: When the channel is closed, the data in it can still be popped out, but push behavior will no longer succeed.
|
||||
* @return mixed
|
||||
*/
|
||||
public function close(): bool;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getCapacity();
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getLength();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isAvailable();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasProducers();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function hasConsumers();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isEmpty();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isFull();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isReadable();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isWritable();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isClosing();
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function isTimeout();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Contract;
|
||||
|
||||
use Hyperf\Engine\Exception\CoroutineDestroyedException;
|
||||
use Hyperf\Engine\Exception\RunningInNonCoroutineException;
|
||||
|
||||
interface CoroutineInterface
|
||||
{
|
||||
/**
|
||||
* @param callable $callable [required]
|
||||
*/
|
||||
public function __construct(callable $callable);
|
||||
|
||||
/**
|
||||
* @param mixed ...$data
|
||||
* @return $this
|
||||
*/
|
||||
public function execute(...$data);
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId();
|
||||
|
||||
/**
|
||||
* @param callable $callable [required]
|
||||
* @param mixed ...$data
|
||||
* @return $this
|
||||
*/
|
||||
public static function create(callable $callable, ...$data);
|
||||
|
||||
/**
|
||||
* @return int returns coroutine id from current coroutine, -1 in non coroutine environment
|
||||
*/
|
||||
public static function id();
|
||||
|
||||
/**
|
||||
* Returns the parent coroutine ID.
|
||||
* Returns 0 when running in the top level coroutine.
|
||||
* @throws RunningInNonCoroutineException when running in non-coroutine context
|
||||
* @throws CoroutineDestroyedException when the coroutine has been destroyed
|
||||
*/
|
||||
public static function pid(?int $id = null);
|
||||
|
||||
/**
|
||||
* Set config to coroutine.
|
||||
*/
|
||||
public static function set(array $config);
|
||||
|
||||
/**
|
||||
* @param null|int $id coroutine id
|
||||
* @return null|\ArrayObject
|
||||
*/
|
||||
public static function getContextFor(?int $id = null);
|
||||
|
||||
/**
|
||||
* Execute callback when coroutine destruct.
|
||||
*/
|
||||
public static function defer(callable $callable);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Contract\Http;
|
||||
|
||||
use Hyperf\Engine\Http\RawResponse;
|
||||
|
||||
interface ClientInterface
|
||||
{
|
||||
public function set(array $settings): bool;
|
||||
|
||||
/**
|
||||
* @param string[][] $headers
|
||||
*/
|
||||
public function request(string $method = 'GET', string $path = '/', array $headers = [], string $contents = '', string $version = '1.1'): RawResponse;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Contract\WebSocket;
|
||||
|
||||
interface WebSocketInterface
|
||||
{
|
||||
public const ON_MESSAGE = 'message';
|
||||
|
||||
public const ON_CLOSE = 'close';
|
||||
|
||||
public function on(string $event, callable $callback): void;
|
||||
|
||||
public function start(): void;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
use Hyperf\Engine\Contract\CoroutineInterface;
|
||||
use Hyperf\Engine\Exception\CoroutineDestroyedException;
|
||||
use Hyperf\Engine\Exception\RunningInNonCoroutineException;
|
||||
use Hyperf\Engine\Exception\RuntimeException;
|
||||
use Swoole\Coroutine as SwooleCo;
|
||||
|
||||
class Coroutine implements CoroutineInterface
|
||||
{
|
||||
/**
|
||||
* @var callable
|
||||
*/
|
||||
private $callable;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $id;
|
||||
|
||||
public function __construct(callable $callable)
|
||||
{
|
||||
$this->callable = $callable;
|
||||
}
|
||||
|
||||
public static function create(callable $callable, ...$data)
|
||||
{
|
||||
$coroutine = new static($callable);
|
||||
$coroutine->execute(...$data);
|
||||
return $coroutine;
|
||||
}
|
||||
|
||||
public function execute(...$data)
|
||||
{
|
||||
$this->id = SwooleCo::create($this->callable, ...$data);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
if (is_null($this->id)) {
|
||||
throw new RuntimeException('Coroutine was not be executed.');
|
||||
}
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public static function id()
|
||||
{
|
||||
return SwooleCo::getCid();
|
||||
}
|
||||
|
||||
public static function pid(?int $id = null)
|
||||
{
|
||||
if ($id) {
|
||||
$cid = SwooleCo::getPcid($id);
|
||||
if ($cid === false) {
|
||||
throw new CoroutineDestroyedException(sprintf('Coroutine #%d has been destroyed.', $id));
|
||||
}
|
||||
} else {
|
||||
$cid = SwooleCo::getPcid();
|
||||
}
|
||||
if ($cid === false) {
|
||||
throw new RunningInNonCoroutineException('Non-Coroutine environment don\'t has parent coroutine id.');
|
||||
}
|
||||
return max(0, $cid);
|
||||
}
|
||||
|
||||
public static function set(array $config)
|
||||
{
|
||||
SwooleCo::set($config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|\ArrayObject
|
||||
*/
|
||||
public static function getContextFor(?int $id = null)
|
||||
{
|
||||
if ($id === null) {
|
||||
return SwooleCo::getContext();
|
||||
}
|
||||
|
||||
return SwooleCo::getContext($id);
|
||||
}
|
||||
|
||||
public static function defer(callable $callable)
|
||||
{
|
||||
SwooleCo::defer($callable);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Exception;
|
||||
|
||||
class CoroutineDestroyedException extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Exception;
|
||||
|
||||
class HttpClientException extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Exception;
|
||||
|
||||
class RunningInNonCoroutineException extends RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Exception;
|
||||
|
||||
class RuntimeException extends \RuntimeException
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
class Extension
|
||||
{
|
||||
public static function isLoaded(): bool
|
||||
{
|
||||
return extension_loaded('Swoole');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Http;
|
||||
|
||||
use Hyperf\Engine\Contract\Http\ClientInterface;
|
||||
use Hyperf\Engine\Exception\HttpClientException;
|
||||
use Swoole\Coroutine\Http\Client as HttpClient;
|
||||
|
||||
class Client extends HttpClient implements ClientInterface
|
||||
{
|
||||
public function set(array $settings): bool
|
||||
{
|
||||
return parent::set($settings);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[][] $headers
|
||||
*/
|
||||
public function request(string $method = 'GET', string $path = '/', array $headers = [], string $contents = '', string $version = '1.1'): RawResponse
|
||||
{
|
||||
$this->setMethod($method);
|
||||
$this->setData($contents);
|
||||
$this->setHeaders($this->encodeHeaders($headers));
|
||||
$this->execute($path);
|
||||
if ($this->errCode !== 0) {
|
||||
throw new HttpClientException($this->errMsg, $this->errCode);
|
||||
}
|
||||
return new RawResponse(
|
||||
$this->statusCode,
|
||||
$this->decodeHeaders($this->headers ?? []),
|
||||
$this->body,
|
||||
$version
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $headers
|
||||
* @return string[][]
|
||||
*/
|
||||
private function decodeHeaders(array $headers): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($headers as $name => $header) {
|
||||
// The key of header is lower case.
|
||||
$result[$name][] = $header;
|
||||
}
|
||||
if ($this->set_cookie_headers) {
|
||||
$result['set-cookie'] = $this->set_cookie_headers;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Swoole engine not support two dimensional array.
|
||||
* @param string[][] $headers
|
||||
* @return string[]
|
||||
*/
|
||||
private function encodeHeaders(array $headers): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($headers as $name => $value) {
|
||||
$result[$name] = is_array($value) ? implode(',', $value) : $value;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Http;
|
||||
|
||||
use Swoole\Http\Response;
|
||||
|
||||
class FdGetter
|
||||
{
|
||||
public function get(Response $response): int
|
||||
{
|
||||
return $response->fd;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\Http;
|
||||
|
||||
final class RawResponse
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $statusCode = 0;
|
||||
|
||||
/**
|
||||
* @var string[][]
|
||||
*/
|
||||
public $headers = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $body = '';
|
||||
|
||||
/**
|
||||
* Protocol version.
|
||||
* @var string
|
||||
*/
|
||||
public $version = '';
|
||||
|
||||
/**
|
||||
* @param string[][] $headers
|
||||
*/
|
||||
public function __construct(int $statusCode, array $headers, string $body, string $version)
|
||||
{
|
||||
$this->statusCode = $statusCode;
|
||||
$this->headers = $headers;
|
||||
$this->body = $body;
|
||||
$this->version = $version;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
class Socket extends \Swoole\Coroutine\Socket
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine;
|
||||
|
||||
class WaitGroup extends \Swoole\Coroutine\WaitGroup
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\WebSocket;
|
||||
|
||||
class Frame
|
||||
{
|
||||
public const PING = '27890027';
|
||||
|
||||
public const PONG = '278a0027';
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\WebSocket;
|
||||
|
||||
class Opcode
|
||||
{
|
||||
public const CONTINUATION = 0;
|
||||
|
||||
public const TEXT = 1;
|
||||
|
||||
public const BINARY = 2;
|
||||
|
||||
public const CLOSE = 8;
|
||||
|
||||
public const PING = 9;
|
||||
|
||||
public const PONG = 10;
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Engine\WebSocket;
|
||||
|
||||
use Hyperf\Engine\Contract\WebSocket\WebSocketInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\WebSocket\CloseFrame;
|
||||
|
||||
class WebSocket implements WebSocketInterface
|
||||
{
|
||||
/**
|
||||
* @var Response
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* @var array<string, callable>
|
||||
*/
|
||||
protected $events = [];
|
||||
|
||||
public function __construct(Response $connection, Request $request)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
$this->connection->upgrade();
|
||||
}
|
||||
|
||||
public function on(string $event, callable $callback): void
|
||||
{
|
||||
$this->events[$event] = $callback;
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
while (true) {
|
||||
$frame = $this->connection->recv();
|
||||
if ($frame === false || $frame instanceof CloseFrame || $frame === '') {
|
||||
$callback = $this->events[static::ON_CLOSE];
|
||||
$callback($this->connection, $this->connection->fd);
|
||||
break;
|
||||
}
|
||||
|
||||
$callback = $this->events[static::ON_MESSAGE];
|
||||
$callback($this->connection, $frame);
|
||||
}
|
||||
|
||||
$this->connection = null;
|
||||
$this->events = [];
|
||||
}
|
||||
}
|
||||
在新工单中引用
屏蔽一个用户