Files
cunkebao_v3/Server/extend/WeChatDeviceApi/Manager.php
2025-05-07 17:43:39 +08:00

94 lines
2.6 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
namespace WeChatDeviceApi;
use think\facade\Config;
use WeChatDeviceApi\Contracts\WeChatServiceInterface;
class Manager
{
/**
* @var array 适配器实例缓存
*/
protected $adapters = [];
/**
* @var array 配置
*/
protected $config;
public function __construct(array $config = null)
{
$this->config = $config ?: Config::get('wechat_device_api.');
if (empty($this->config)) {
throw new \InvalidArgumentException("WeChat Device API configuration not found.");
}
}
/**
* 获取指定的适配器实例
*
* @param string|null $name 适配器名称 (例如 'vendor_a', 'vendor_b')null 则使用默认
* @return WeChatServiceInterface
* @throws \InvalidArgumentException
*/
public function adapter(string $name = null): WeChatServiceInterface
{
$name = $name ?: $this->getDefaultAdapterName();
if (!isset($this->config['adapters'][$name])) {
throw new \InvalidArgumentException("Adapter [{$name}] configuration not found.");
}
if (!isset($this->adapters[$name])) {
$this->adapters[$name] = $this->createAdapter($name);
}
return $this->adapters[$name];
}
/**
* 创建适配器实例
*
* @param string $name
* @return WeChatServiceInterface
*/
protected function createAdapter(string $name): WeChatServiceInterface
{
$adapterConfig = $this->config['adapters'][$name];
$driverClass = $adapterConfig['driver'] ?? null;
if (!$driverClass || !class_exists($driverClass)) {
throw new \InvalidArgumentException("Driver class for adapter [{$name}] not found or not specified.");
}
$adapterInstance = new $driverClass($adapterConfig);
if (!$adapterInstance instanceof WeChatServiceInterface) {
throw new \LogicException("Driver class [{$driverClass}] must implement WeChatServiceInterface.");
}
return $adapterInstance;
}
/**
* 获取默认适配器名称
*
* @return string
*/
public function getDefaultAdapterName(): string
{
return $this->config['default_adapter'] ?? '';
}
/**
* 动态调用默认适配器的方法
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call(string $method, array $parameters)
{
return $this->adapter()->{$method}(...$parameters);
}
}