add all bots proxy for ZMRobot

This commit is contained in:
sunxyw
2022-04-04 00:11:58 +08:00
parent 8335cfed69
commit 306e992789
3 changed files with 141 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace ZM\API\Proxies\Bot;
use ZM\API\ZMRobot;
/**
* @mixin ZMRobot
*/
abstract class AbstractBotProxy
{
/**
* 传入的机器人实例
*
* @var ZMRobot
*/
protected $bot;
/**
* 构造函数
*
* @param AbstractBotProxy|ZMRobot $bot 调用此代理的机器人实例
*/
public function __construct($bot)
{
$this->bot = $bot;
}
/**
* 在传入的机器人实例上调用方法
*
* @param string $name 方法名
* @param array $arguments 参数
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return $this->bot->{$name}(...$arguments);
}
/**
* 获取传入的机器人实例的属性
*
* @param string $name 属性名
* @return mixed
*/
public function __get(string $name)
{
return $this->bot->{$name};
}
/**
* 设置传入的机器人实例的属性
*
* @param string $name 属性名
* @param mixed $value 属性值
*/
public function __set(string $name, $value)
{
$this->bot->{$name} = $value;
}
/**
* 判断传入的机器人实例的属性是否存在
*
* @param string $name 属性名
*/
public function __isset(string $name): bool
{
return isset($this->bot->{$name});
}
}

View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace ZM\API\Proxies\Bot;
use ReflectionException;
use ReflectionMethod;
use ReflectionNamedType;
use ZM\API\ZMRobot;
use ZM\Console\Console;
class AllBotsProxy extends AbstractBotProxy
{
/**
* 在所有机器人实例上调用方法
*
* @param string $name 方法名
* @param array $arguments 参数
* @throws ReflectionException
* @return array<mixed> 返回一个包含所有执行结果的数组键名为机器人ID
*/
public function __call(string $name, array $arguments)
{
// 如果调用的方法为代理方法,则传入当前代理作为参数
// 一般此情况代表用户进行嵌套代理,即 `->all()->allGroups()` 等情况
$reflection = new ReflectionMethod(ZMRobot::class, $name);
if (($return = $reflection->getReturnType()) && $return instanceof ReflectionNamedType && str_contains($return->getName(), 'Proxy')) {
Console::debug("Trying to construct proxy {$name} inside proxy, returning nested proxy.");
// 插入当前代理作为第一个参数
array_unshift($arguments, $this);
return $this->bot->{$name}(...$arguments);
}
$result = [];
// 遍历所有机器人实例
foreach ($this->bot::getAllRobot() as $bot) {
Console::debug("Calling {$name} on bot {$bot->getSelfId()}.");
$result[$bot->getSelfId()] = $bot->{$name}(...$arguments);
}
return $result;
}
}

View File

@@ -4,6 +4,9 @@ declare(strict_types=1);
namespace ZM\API;
use ZM\API\Proxies\Bot as Proxies;
use ZM\Console\Console;
/**
* Class ZMRobot
* @since 1.2
@@ -11,4 +14,25 @@ namespace ZM\API;
*/
class ZMRobot extends OneBotV11
{
/**
* 获取一个会在所有机器人实例上执行的代理
*/
public function all(Proxies\AbstractBotProxy $proxy = null): Proxies\AllBotsProxy
{
$bot = $proxy ?: $this;
$bot_id = implode_when_necessary($bot->getSelfId());
Console::debug("Constructing AllBotsProxy for ZMRobot({$bot_id})");
return new Proxies\AllBotsProxy($bot);
}
/**
* 获取一个会在所有群上执行的代理
*/
public function allGroups(Proxies\AbstractBotProxy $proxy = null): Proxies\AllGroupsProxy
{
$bot = $proxy ?: $this;
$bot_id = implode_when_necessary($bot->getSelfId());
Console::debug("Constructing AllGroupsProxy for ZMRobot({$bot_id})");
return new Proxies\AllGroupsProxy($bot);
}
}