Files
zhamao-framework/src/ZM/API/Proxies/Bot/AllBotsProxy.php
2022-04-04 00:11:58 +08:00

44 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}