Commit 04db65a1 authored by 杨培's avatar 杨培

优化 API 集的请求实例,每个API集将只有一个实例

parent 70a0059b
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
namespace internalApi\base; namespace internalApi\base;
abstract class Client class Client
{ {
public $debug = false; public $debug = false;
...@@ -22,23 +22,39 @@ abstract class Client ...@@ -22,23 +22,39 @@ abstract class Client
protected static $instance = []; protected static $instance = [];
protected static $config = []; protected static $config = [];
/**
* 获取 API 集的客户端实例
* 每个 API 集有一个实例用于签名和请求处理
*
* @return static
*/
protected static function getInstance() protected static function getInstance()
{ {
$className = static::class; $name = self::name();
if (!isset(self::$instance[$className])) { if (!isset(self::$instance[$name])) {
self::$instance[$className] = new $className(); self::$instance[$name] = new self($name);
} }
return self::$instance[$className]; return self::$instance[$name];
} }
public function __construct() /**
* 获取 API 集的名称
*
* 如 internalApi\ucenter\Member 则名称为 ucenter
*
* @return bool|string
*/
public static function name()
{ {
$className = static::class; $className = static::class;
$subName = strstr($className, '\\'); $subName = strstr($className, '\\');
$name = substr($subName, 1, strpos($subName, '\\', 1)-1); return substr($subName, 1, strpos($subName, '\\', 1)-1);
}
public function __construct($name)
{
if (isset(static::$config[$name])) { if (isset(static::$config[$name])) {
foreach (static::$config[$name] as $k => $v) { foreach (static::$config[$name] as $k => $v) {
if ($this->$k === null) { if ($this->$k === null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment