Skip to content

Commit

Permalink
Merge pull request #21 from botuniverse/develop
Browse files Browse the repository at this point in the history
发布 0.3.0
  • Loading branch information
crazywhalecc authored Nov 29, 2021
2 parents 9830320 + 8744d03 commit bef86a9
Show file tree
Hide file tree
Showing 53 changed files with 1,377 additions and 165 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2
updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "daily"
target-branch: "develop"
labels:
- "area/dependency"
30 changes: 30 additions & 0 deletions .github/label-actions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Configuration for Label Actions - https://github.com/dessant/label-actions

resolution/cannot-reproduce:
comment: >
我们的开发人员无法复现此问题,如有可能,请提供完整的复现用例及截图等资料。
resolution/duplicate:
comment: >
这与现有的一个Issue/PR重复了。
close: true
lock: true

resolution/invalid:
comment: >
我们的开发人员认为这是一个无效的问题,请确保您查阅了我们的贡献指南及提供了必要的信息。
close: true

resolution/rejected:
comment: >
此提案已被我们的开发人员拒绝。
close: true

resoluton/wontfix:
comment: >
抱歉,我们暂时不会处理。
close: true

'accept PRs':
comment: >
我们的开发人员认为这是一个不错的提案,您(或其他有意向的人)可以就此提交 PR。
25 changes: 25 additions & 0 deletions .github/workflows/close-staled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Close Stale Issue PR

on:
schedule:
- cron: '0 * * * *'
workflow_dispatch:

permissions:
issues: write
pull-requests: write

jobs:
action:
runs-on: ubuntu-latest
steps:
- name: Lock outdated issues and prs
uses: dessant/lock-threads@v3
with:
issue-inactive-days: '7'
exclude-any-issue-labels: 'lifecycle/keep-open'
add-issue-labels: 'lifecycle/stale'
issue-comment: >
由于在关闭后没有更多信息,此Issue已被自动锁定。如有需要请提出一个新Issue。
pr-comment: >
由于在关闭后没有更多信息,此PR已被自动锁定。如有需要请提出一个新Issue。
6 changes: 6 additions & 0 deletions .github/workflows/integration-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ on:
pull_request:
branches:
- develop
types:
- opened
- synchronize
- reopened
- ready_for_review
- review_requested

jobs:
integration:
Expand Down
23 changes: 23 additions & 0 deletions .github/workflows/label-actions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: 'Label Actions'

on:
issues:
types: [ labeled, unlabeled ]
pull_request:
types: [ labeled, unlabeled ]
discussion:
types: [ labeled, unlabeled ]

permissions:
contents: read
issues: write
pull-requests: write
discussions: write

jobs:
action:
runs-on: ubuntu-latest
steps:
- uses: dessant/label-actions@v2
with:
config-path: '.github/label-actions.yaml'
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'single_quote' => true,
'standardize_not_equals' => true,
'multiline_comment_opening_closing' => true,
'phpdoc_summary' => false,
])
->setFinder(
PhpCsFixer\Finder::create()
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!--suppress HtmlDeprecatedAttribute -->
<p align="center">
<a href="https://github.com/botuniverse/php-libonebot/releases">
<img alt="Version" src="https://img.shields.io/github/v/release/botuniverse/php-libonebot?include_prereleases&logo=github&style=flat-square" />
Expand Down Expand Up @@ -31,11 +32,15 @@ composer require onebot/libonebot
```php
<?php

require_once "vendor/autoload.php";
declare(strict_types=1);

$ob = new \OneBot\V12\OneBot('repl', 'qq');
$ob->setServerDriver(
new \OneBot\V12\Driver\WorkermanDriver(),
require_once 'vendor/autoload.php';

$ob = new \OneBot\V12\OneBot('repl', 'qq', 'REPL-1');
$ob->setLogger(new \OneBot\Logger\Console\ConsoleLogger());
$ob->setDriver(
// 此处也可以在 Linux 系统下安装 swoole 扩展后使用 SwooleDriver() 拥有协程能力
new \OneBot\V12\Driver\WorkermanDriver(),
new \OneBot\V12\Config\Config('demo.json')
);
$ob->setActionHandler(\OneBot\V12\Action\ReplAction::class);
Expand All @@ -46,6 +51,9 @@ $ob->run();

```json
{
"lib": {
"db": false
},
"communications": {
"http": {
"enable": true,
Expand All @@ -56,7 +64,6 @@ $ob->run();
}
}
}

```

此 Demo 以一个命令行交互的方式使用 LibOneBot 快速完成了一个 OneBot 实现,命令行中输入内容即可发送到 OneBot,使用 HTTP 或 WebSocket 发送给 LibOneBot 后可以将信息显示在终端内。
Expand Down
Empty file added cache/.gitkeep
Empty file.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@
"require": {
"php": "^7.2|^7.3|^7.4|^8.0|^8.1",
"ext-json": "*",
"ext-pdo": "*",
"ext-posix": "*",
"hassankhan/config": "^2.2",
"lezhnev74/pasvl": "^1.0",
"psr/cache": "^1.0",
"psr/log": "^1.1",
"rybakit/msgpack": "^0.9.0",
"symfony/var-dumper": "^5.3",
"workerman/workerman": "^4.0"
Expand Down Expand Up @@ -65,10 +68,10 @@
"hooks": {
"pre-commit": [
"echo committing as $(git config user.name)",
"./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation ./src"
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src"
],
"pre-push": [
"./vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation ./src",
"./vendor/bin/php-cs-fixer fix --dry-run --diff ./src",
"composer analyse"
],
"post-merge": "composer install"
Expand Down
3 changes: 3 additions & 0 deletions demo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"lib": {
"db": false
},
"communications": {
"http": {
"enable": true,
Expand Down
2 changes: 1 addition & 1 deletion demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

require_once 'vendor/autoload.php';

$ob = new \OneBot\V12\OneBot('repl', 'qq');
$ob = new \OneBot\V12\OneBot('repl', 'qq', 'REPL-1');
$ob->setLogger(new \OneBot\Logger\Console\ConsoleLogger());
$ob->setDriver(
new \OneBot\V12\Driver\WorkermanDriver(),
Expand Down
18 changes: 18 additions & 0 deletions docs/update.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# 更新日志

## v0.3.0 (2021-11-29)

- 实现 Event 事件相关对象 @sunxyw #14
- 添加连接池和 SQLite 连接组件及相关配置项 `lib` @sunxyw #16 #17
- 加入 Dependabot @sunxyw #18
- 修改全局方法 `logger()` -> `ob_logger()` @crazywhalecc #19
- 完善 SwooleDriver 的实现 @crazywhalecc #20
- Driver 类新增 `emitHttpRequest()` 方法,减少驱动实现代码重复
- 更新 README

## v0.2.0 (2021-11-18)

- 添加贡献指南 @sunxyw #2
- 添加日志组件 @sunxyw #3
- 清理、优化代码
- 添加 GitHub Workflows 工作流
- 更新 README

## v0.1.0 (2021-11-8)

- 初始版本
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
parameters:
reportUnmatchedIgnoredErrors: false
level: 0
paths:
- ./src/
ignoreErrors:
- '#Used constant OS_TYPE_(LINUX|WINDOWS) not found#'
18 changes: 18 additions & 0 deletions src/OneBot/Database/SQLite/ConnectionPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace OneBot\Database\SQLite;

use OneBot\ObjectPool\AbstractObjectPool;

/**
* Class ConnectionPool.
*/
class ConnectionPool extends AbstractObjectPool
{
protected function makeObject(): object
{
return new SQLite();
}
}
22 changes: 22 additions & 0 deletions src/OneBot/Database/SQLite/SQLite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace OneBot\Database\SQLite;

use OneBot\V12\Exception\OneBotException;

/**
* Class SQLite.
*/
class SQLite extends \PDO
{
public function __construct()
{
if (ob_config('lib.db', false)) {
parent::__construct('sqlite:' . __DIR__ . '/../../../../cache/db');
} else {
throw new OneBotException('数据库支持未启用');
}
}
}
90 changes: 90 additions & 0 deletions src/OneBot/ObjectPool/AbstractObjectPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

declare(strict_types=1);

namespace OneBot\ObjectPool;

use Exception;
use Swoole\Coroutine\Channel;

/**
* 抽象对象池
* 只能在Swoole协程中使用
*/
abstract class AbstractObjectPool
{
/** @var Channel 队列 */
private $queue;

/** @var array 活跃对象 */
private $actives;

public function __construct()
{
// TODO: 添加更多可配置项
$this->queue = new Channel(swoole_cpu_num());
}

/**
* 取出对象
*
* @throws Exception
*/
public function take(): object
{
if ($this->getFreeCount() > 0) {
// 如有可用对象则取用
$object = $this->queue->pop(5);
if (!$object) {
throw new Exception('取出对象时等待超时');
}
} else {
// 没有就整个新的
$object = $this->makeObject();
}
$hash = spl_object_hash($object);
// 为方便在归还时删除,使用数组key存储
$this->actives[$hash] = '';

return $object;
}

/**
* 归还对象
*/
public function return(object $object): bool
{
$hash = spl_object_hash($object);
unset($this->actives[$hash]);

// 放回队列里
return $this->queue->push($object, 5);
}

abstract protected function makeObject(): object;

/**
* 获取可用的对象数量
*/
protected function getFreeCount(): int
{
$count = $this->queue->stats()['queue_num'];
return $count < 0 ? 0 : $count;
}

/**
* 获取活跃(已被取用)的对象数量
*/
protected function getActiveCount(): int
{
return count($this->actives);
}

/**
* 获取所有的对象数量
*/
protected function getTotalCount(): int
{
return $this->getFreeCount() + $this->getActiveCount();
}
}
3 changes: 2 additions & 1 deletion src/OneBot/V12/Action/ActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use OneBot\V12\OneBot;
use OneBot\V12\RetCode;
use OneBot\V12\Utils;
use ReflectionClass;

abstract class ActionBase
{
Expand Down Expand Up @@ -79,7 +80,7 @@ public function onGetLatestEvents(ActionObject $action): ActionResponse

public function onGetSupportedActions(ActionObject $action): ActionResponse
{
$reflection = new \ReflectionClass($this);
$reflection = new ReflectionClass($this);
$list = [];
foreach ($reflection->getMethods() as $v) {
$sep = Utils::camelToSeparator($v->getName());
Expand Down
2 changes: 1 addition & 1 deletion src/OneBot/V12/Action/ReplAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ReplAction extends ActionBase
{
public function onSendMessage(ActionObject $action): ActionResponse
{
logger()->info(Utils::msgToString($action->params['message']));
ob_logger()->info(Utils::msgToString($action->params['message']));
return ActionResponse::create($action->echo)->ok(['message_id' => mt_rand(0, 9999999)]);
}
}
3 changes: 3 additions & 0 deletions src/OneBot/V12/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public function getEnabledCommunications(): array
protected function validateConfig()
{
$pattern = [
'lib' => [
'db' => ':bool',
],
'communications' => [
'http?' => [
'enable' => ':bool',
Expand Down
Loading

0 comments on commit bef86a9

Please sign in to comment.