Skip to content

Commit bef86a9

Browse files
authored
Merge pull request #21 from botuniverse/develop
发布 0.3.0
2 parents 9830320 + 8744d03 commit bef86a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1377
-165
lines changed

.github/dependabot.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "composer"
4+
directory: "/"
5+
schedule:
6+
interval: "daily"
7+
target-branch: "develop"
8+
labels:
9+
- "area/dependency"

.github/label-actions.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Configuration for Label Actions - https://github.com/dessant/label-actions
2+
3+
resolution/cannot-reproduce:
4+
comment: >
5+
我们的开发人员无法复现此问题,如有可能,请提供完整的复现用例及截图等资料。
6+
7+
resolution/duplicate:
8+
comment: >
9+
这与现有的一个Issue/PR重复了。
10+
close: true
11+
lock: true
12+
13+
resolution/invalid:
14+
comment: >
15+
我们的开发人员认为这是一个无效的问题,请确保您查阅了我们的贡献指南及提供了必要的信息。
16+
close: true
17+
18+
resolution/rejected:
19+
comment: >
20+
此提案已被我们的开发人员拒绝。
21+
close: true
22+
23+
resoluton/wontfix:
24+
comment: >
25+
抱歉,我们暂时不会处理。
26+
close: true
27+
28+
'accept PRs':
29+
comment: >
30+
我们的开发人员认为这是一个不错的提案,您(或其他有意向的人)可以就此提交 PR。

.github/workflows/close-staled.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Close Stale Issue PR
2+
3+
on:
4+
schedule:
5+
- cron: '0 * * * *'
6+
workflow_dispatch:
7+
8+
permissions:
9+
issues: write
10+
pull-requests: write
11+
12+
jobs:
13+
action:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Lock outdated issues and prs
17+
uses: dessant/lock-threads@v3
18+
with:
19+
issue-inactive-days: '7'
20+
exclude-any-issue-labels: 'lifecycle/keep-open'
21+
add-issue-labels: 'lifecycle/stale'
22+
issue-comment: >
23+
由于在关闭后没有更多信息,此Issue已被自动锁定。如有需要请提出一个新Issue。
24+
pr-comment: >
25+
由于在关闭后没有更多信息,此PR已被自动锁定。如有需要请提出一个新Issue。

.github/workflows/integration-test.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ on:
77
pull_request:
88
branches:
99
- develop
10+
types:
11+
- opened
12+
- synchronize
13+
- reopened
14+
- ready_for_review
15+
- review_requested
1016

1117
jobs:
1218
integration:

.github/workflows/label-actions.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: 'Label Actions'
2+
3+
on:
4+
issues:
5+
types: [ labeled, unlabeled ]
6+
pull_request:
7+
types: [ labeled, unlabeled ]
8+
discussion:
9+
types: [ labeled, unlabeled ]
10+
11+
permissions:
12+
contents: read
13+
issues: write
14+
pull-requests: write
15+
discussions: write
16+
17+
jobs:
18+
action:
19+
runs-on: ubuntu-latest
20+
steps:
21+
- uses: dessant/label-actions@v2
22+
with:
23+
config-path: '.github/label-actions.yaml'

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
'single_quote' => true,
6767
'standardize_not_equals' => true,
6868
'multiline_comment_opening_closing' => true,
69+
'phpdoc_summary' => false,
6970
])
7071
->setFinder(
7172
PhpCsFixer\Finder::create()

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<!--suppress HtmlDeprecatedAttribute -->
12
<p align="center">
23
<a href="https://github.com/botuniverse/php-libonebot/releases">
34
<img alt="Version" src="https://img.shields.io/github/v/release/botuniverse/php-libonebot?include_prereleases&logo=github&style=flat-square" />
@@ -31,11 +32,15 @@ composer require onebot/libonebot
3132
```php
3233
<?php
3334

34-
require_once "vendor/autoload.php";
35+
declare(strict_types=1);
3536

36-
$ob = new \OneBot\V12\OneBot('repl', 'qq');
37-
$ob->setServerDriver(
38-
new \OneBot\V12\Driver\WorkermanDriver(),
37+
require_once 'vendor/autoload.php';
38+
39+
$ob = new \OneBot\V12\OneBot('repl', 'qq', 'REPL-1');
40+
$ob->setLogger(new \OneBot\Logger\Console\ConsoleLogger());
41+
$ob->setDriver(
42+
// 此处也可以在 Linux 系统下安装 swoole 扩展后使用 SwooleDriver() 拥有协程能力
43+
new \OneBot\V12\Driver\WorkermanDriver(),
3944
new \OneBot\V12\Config\Config('demo.json')
4045
);
4146
$ob->setActionHandler(\OneBot\V12\Action\ReplAction::class);
@@ -46,6 +51,9 @@ $ob->run();
4651

4752
```json
4853
{
54+
"lib": {
55+
"db": false
56+
},
4957
"communications": {
5058
"http": {
5159
"enable": true,
@@ -56,7 +64,6 @@ $ob->run();
5664
}
5765
}
5866
}
59-
6067
```
6168

6269
此 Demo 以一个命令行交互的方式使用 LibOneBot 快速完成了一个 OneBot 实现,命令行中输入内容即可发送到 OneBot,使用 HTTP 或 WebSocket 发送给 LibOneBot 后可以将信息显示在终端内。

cache/.gitkeep

Whitespace-only changes.

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@
3333
"require": {
3434
"php": "^7.2|^7.3|^7.4|^8.0|^8.1",
3535
"ext-json": "*",
36+
"ext-pdo": "*",
3637
"ext-posix": "*",
3738
"hassankhan/config": "^2.2",
3839
"lezhnev74/pasvl": "^1.0",
40+
"psr/cache": "^1.0",
41+
"psr/log": "^1.1",
3942
"rybakit/msgpack": "^0.9.0",
4043
"symfony/var-dumper": "^5.3",
4144
"workerman/workerman": "^4.0"
@@ -65,10 +68,10 @@
6568
"hooks": {
6669
"pre-commit": [
6770
"echo committing as $(git config user.name)",
68-
"./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation ./src"
71+
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src"
6972
],
7073
"pre-push": [
71-
"./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation ./src",
74+
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src",
7275
"composer analyse"
7376
],
7477
"post-merge": "composer install"

demo.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"lib": {
3+
"db": false
4+
},
25
"communications": {
36
"http": {
47
"enable": true,

demo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
require_once 'vendor/autoload.php';
66

7-
$ob = new \OneBot\V12\OneBot('repl', 'qq');
7+
$ob = new \OneBot\V12\OneBot('repl', 'qq', 'REPL-1');
88
$ob->setLogger(new \OneBot\Logger\Console\ConsoleLogger());
99
$ob->setDriver(
1010
new \OneBot\V12\Driver\WorkermanDriver(),

docs/update.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# 更新日志
22

3+
## v0.3.0 (2021-11-29)
4+
5+
- 实现 Event 事件相关对象 @sunxyw #14
6+
- 添加连接池和 SQLite 连接组件及相关配置项 `lib` @sunxyw #16 #17
7+
- 加入 Dependabot @sunxyw #18
8+
- 修改全局方法 `logger()` -> `ob_logger()` @crazywhalecc #19
9+
- 完善 SwooleDriver 的实现 @crazywhalecc #20
10+
- Driver 类新增 `emitHttpRequest()` 方法,减少驱动实现代码重复
11+
- 更新 README
12+
13+
## v0.2.0 (2021-11-18)
14+
15+
- 添加贡献指南 @sunxyw #2
16+
- 添加日志组件 @sunxyw #3
17+
- 清理、优化代码
18+
- 添加 GitHub Workflows 工作流
19+
- 更新 README
20+
321
## v0.1.0 (2021-11-8)
422

523
- 初始版本

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
parameters:
22
reportUnmatchedIgnoredErrors: false
3+
level: 0
4+
paths:
5+
- ./src/
36
ignoreErrors:
47
- '#Used constant OS_TYPE_(LINUX|WINDOWS) not found#'
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OneBot\Database\SQLite;
6+
7+
use OneBot\ObjectPool\AbstractObjectPool;
8+
9+
/**
10+
* Class ConnectionPool.
11+
*/
12+
class ConnectionPool extends AbstractObjectPool
13+
{
14+
protected function makeObject(): object
15+
{
16+
return new SQLite();
17+
}
18+
}

src/OneBot/Database/SQLite/SQLite.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OneBot\Database\SQLite;
6+
7+
use OneBot\V12\Exception\OneBotException;
8+
9+
/**
10+
* Class SQLite.
11+
*/
12+
class SQLite extends \PDO
13+
{
14+
public function __construct()
15+
{
16+
if (ob_config('lib.db', false)) {
17+
parent::__construct('sqlite:' . __DIR__ . '/../../../../cache/db');
18+
} else {
19+
throw new OneBotException('数据库支持未启用');
20+
}
21+
}
22+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OneBot\ObjectPool;
6+
7+
use Exception;
8+
use Swoole\Coroutine\Channel;
9+
10+
/**
11+
* 抽象对象池
12+
* 只能在Swoole协程中使用
13+
*/
14+
abstract class AbstractObjectPool
15+
{
16+
/** @var Channel 队列 */
17+
private $queue;
18+
19+
/** @var array 活跃对象 */
20+
private $actives;
21+
22+
public function __construct()
23+
{
24+
// TODO: 添加更多可配置项
25+
$this->queue = new Channel(swoole_cpu_num());
26+
}
27+
28+
/**
29+
* 取出对象
30+
*
31+
* @throws Exception
32+
*/
33+
public function take(): object
34+
{
35+
if ($this->getFreeCount() > 0) {
36+
// 如有可用对象则取用
37+
$object = $this->queue->pop(5);
38+
if (!$object) {
39+
throw new Exception('取出对象时等待超时');
40+
}
41+
} else {
42+
// 没有就整个新的
43+
$object = $this->makeObject();
44+
}
45+
$hash = spl_object_hash($object);
46+
// 为方便在归还时删除,使用数组key存储
47+
$this->actives[$hash] = '';
48+
49+
return $object;
50+
}
51+
52+
/**
53+
* 归还对象
54+
*/
55+
public function return(object $object): bool
56+
{
57+
$hash = spl_object_hash($object);
58+
unset($this->actives[$hash]);
59+
60+
// 放回队列里
61+
return $this->queue->push($object, 5);
62+
}
63+
64+
abstract protected function makeObject(): object;
65+
66+
/**
67+
* 获取可用的对象数量
68+
*/
69+
protected function getFreeCount(): int
70+
{
71+
$count = $this->queue->stats()['queue_num'];
72+
return $count < 0 ? 0 : $count;
73+
}
74+
75+
/**
76+
* 获取活跃(已被取用)的对象数量
77+
*/
78+
protected function getActiveCount(): int
79+
{
80+
return count($this->actives);
81+
}
82+
83+
/**
84+
* 获取所有的对象数量
85+
*/
86+
protected function getTotalCount(): int
87+
{
88+
return $this->getFreeCount() + $this->getActiveCount();
89+
}
90+
}

src/OneBot/V12/Action/ActionBase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use OneBot\V12\OneBot;
99
use OneBot\V12\RetCode;
1010
use OneBot\V12\Utils;
11+
use ReflectionClass;
1112

1213
abstract class ActionBase
1314
{
@@ -79,7 +80,7 @@ public function onGetLatestEvents(ActionObject $action): ActionResponse
7980

8081
public function onGetSupportedActions(ActionObject $action): ActionResponse
8182
{
82-
$reflection = new \ReflectionClass($this);
83+
$reflection = new ReflectionClass($this);
8384
$list = [];
8485
foreach ($reflection->getMethods() as $v) {
8586
$sep = Utils::camelToSeparator($v->getName());

src/OneBot/V12/Action/ReplAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ReplAction extends ActionBase
1111
{
1212
public function onSendMessage(ActionObject $action): ActionResponse
1313
{
14-
logger()->info(Utils::msgToString($action->params['message']));
14+
ob_logger()->info(Utils::msgToString($action->params['message']));
1515
return ActionResponse::create($action->echo)->ok(['message_id' => mt_rand(0, 9999999)]);
1616
}
1717
}

src/OneBot/V12/Config/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ public function getEnabledCommunications(): array
7979
protected function validateConfig()
8080
{
8181
$pattern = [
82+
'lib' => [
83+
'db' => ':bool',
84+
],
8285
'communications' => [
8386
'http?' => [
8487
'enable' => ':bool',

0 commit comments

Comments
 (0)