From 38ed733816a84540846879298dcc5b27a5596977 Mon Sep 17 00:00:00 2001 From: iMactool Date: Mon, 27 Nov 2023 14:59:56 +0800 Subject: [PATCH] Support php81 --- src/Core/BaseService.php | 6 +-- src/Core/Container.php | 4 ++ src/HikCloud.php | 5 +- src/Http/AuthService.php | 8 +-- src/Http/{Client.php => ClientHttp.php} | 67 +++++++++++++------------ src/Isc.php | 5 +- src/Ys7.php | 5 +- 7 files changed, 54 insertions(+), 46 deletions(-) rename src/Http/{Client.php => ClientHttp.php} (88%) diff --git a/src/Core/BaseService.php b/src/Core/BaseService.php index 2883364..d908dc0 100644 --- a/src/Core/BaseService.php +++ b/src/Core/BaseService.php @@ -8,11 +8,11 @@ namespace Imactool\Hikcloud\Core; -use Imactool\Hikcloud\Http\Client; +use Imactool\Hikcloud\Http\ClientHttp; class BaseService { - use Client; + use ClientHttp; protected $app; public $nowApp; @@ -31,7 +31,7 @@ public function __construct(Container $app) $this->nowApp = basename(str_replace('\\','/',$class)); if ($this->nowApp == "HikCloud"){ - $this->appRunConfig = self::getAppConfig(); + $this->appRunConfig = self::getHikConfig(); $this->baseUri = 'https://api2.hik-cloud.com'; }else if($this->nowApp == "Isc"){ $this->appRunConfig = self::getIsConfig(); diff --git a/src/Core/Container.php b/src/Core/Container.php index 8135d00..0437b3e 100644 --- a/src/Core/Container.php +++ b/src/Core/Container.php @@ -32,6 +32,7 @@ class Container implements \ArrayAccess */ public $register; + #[\ReturnTypeWillChange] public function offsetExists($offset) { // TODO: Implement offsetExists() method. @@ -42,6 +43,7 @@ public function offsetExists($offset) * * @return $this|mixed */ + #[\ReturnTypeWillChange] public function offsetGet($offset) { if (isset($this->instances[$offset])) { @@ -59,11 +61,13 @@ public function offsetGet($offset) * @param mixed $offset * @param mixed $value */ + #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { $this->values[$offset] = $value; } + #[\ReturnTypeWillChange] public function offsetUnset($offset) { // TODO: Implement offsetUnset() method. diff --git a/src/HikCloud.php b/src/HikCloud.php index d9b99d7..816003b 100644 --- a/src/HikCloud.php +++ b/src/HikCloud.php @@ -20,7 +20,7 @@ use Imactool\Hikcloud\Core\ContainerBase; use Imactool\Hikcloud\Device\DeviceProvider; use Imactool\Hikcloud\FaceDB\FaceDBProvider; - use Imactool\Hikcloud\Http\Client; + use Imactool\Hikcloud\Http\ClientHttp; use Imactool\Hikcloud\Msg\MsgProvider; use Imactool\Hikcloud\Person\PersonProvider; use Imactool\Hikcloud\Property\PropertyProvider; @@ -46,6 +46,7 @@ */ class HikCloud extends ContainerBase { + use ClientHttp; private static $config; @@ -72,7 +73,7 @@ class HikCloud extends ContainerBase public function __construct (array $config) { self::$config = $config; - Client::setAppConfig('config',$config); + self::setAppConfig('hikconfig',$config); parent::__construct(); } diff --git a/src/Http/AuthService.php b/src/Http/AuthService.php index 22108d9..7c5eda1 100644 --- a/src/Http/AuthService.php +++ b/src/Http/AuthService.php @@ -22,7 +22,7 @@ trait AuthService */ public function getAccessToken($cacheKey) { - $accessToken = CacheAdapter::getInstance()->getItem($cacheKey); + $accessToken = self::getInstance()->getItem($cacheKey); if (!$accessToken->isHit()) { $result = $this->refreshSelfAccessToken(); @@ -30,7 +30,7 @@ public function getAccessToken($cacheKey) $this->access_token = $result['access_token']; $accessToken->set($this->access_token); $accessToken->expiresAfter((int) $result['expires_in'] - 3); - CacheAdapter::getInstance()->save($accessToken); + self::getInstance()->save($accessToken); } } else { $this->access_token = $accessToken->get(); @@ -41,7 +41,7 @@ public function getAccessToken($cacheKey) public function getYsAccessToken($cacheKey) { - $accessToken = CacheAdapter::getInstance()->getItem($cacheKey); + $accessToken = self::getInstance()->getItem($cacheKey); if (!$accessToken->isHit()){ $result = $this->refreshYsAccessToken(); @@ -54,7 +54,7 @@ public function getYsAccessToken($cacheKey) $this->ys_access_token = $result['data']['accessToken']; $accessToken->set($this->ys_access_token); $accessToken->expiresAfter((int) $result['data']['expireTime'] - 3); - CacheAdapter::getInstance()->save($accessToken); + self::getInstance()->save($accessToken); }else{ $this->ys_access_token = $accessToken->get(); } diff --git a/src/Http/Client.php b/src/Http/ClientHttp.php similarity index 88% rename from src/Http/Client.php rename to src/Http/ClientHttp.php index df9d7d1..102650c 100644 --- a/src/Http/Client.php +++ b/src/Http/ClientHttp.php @@ -14,7 +14,7 @@ use GuzzleHttp\Psr7\Utils; use Imactool\Hikcloud\Exceptions\Exception; -trait Client +trait ClientHttp { use AuthService; @@ -27,17 +27,21 @@ trait Client public function httpClient() { - if (!self::$client) { - self::$client = new Http(); - self::$client->setUrl($this->baseUri); + if (!static::$client) { + static::$client = new Http(); + static::$client->setUrl($this->baseUri); } - return self::$client; + return static::$client; } - public static function setAppConfig($key, $appConfig) + public static function setAppConfig($cachePrefix ,$appConfig) { - self::$appConfig[$key] = $appConfig; + $config = self::getInstance()->getItem($cachePrefix); + if (!$config->isHit()){ + $config->set($appConfig); + self::getInstance()->save($config); + } } /** @@ -47,19 +51,14 @@ public static function setAppConfig($key, $appConfig) * @return mixed * @author cc */ - public static function getAppConfig($key = null) + public static function getHikConfig($key = null) { + $config = self::getInstance()->getItem('hikconfig'); + $nowConfig = $config->get(); if (is_null($key)) { - return self::$appConfig['config']; + return $nowConfig; } - - return self::$appConfig['config'][$key]; - } - - - public static function getAllConfig() - { - return self::$appConfig; + return $nowConfig[$key]; } /** @@ -71,11 +70,12 @@ public static function getAllConfig() */ public static function getYsConfig($key = null) { + $config = self::getInstance()->getItem('ysconfig'); + $nowConfig = $config->get(); if (is_null($key)) { - return self::$appConfig['ysconfig']; + return $nowConfig; } - - return self::$appConfig['ysconfig'][$key]; + return $nowConfig[$key]; } /** @@ -87,11 +87,12 @@ public static function getYsConfig($key = null) */ public static function getIsConfig($key = null) { + $config = self::getInstance()->getItem('isConfig'); + $nowConfig = $config->get(); if (is_null($key)) { - return self::$appConfig['isConfig']; + return $nowConfig; } - - return self::$appConfig['isConfig'][$key]; + return $nowConfig[$key]; } /** @@ -399,7 +400,7 @@ public function generateYsCommonParam($params = []) */ public function authorizerTokenKey() { - return $this->access_token_key . self::getAppConfig('client_id'); + return $this->access_token_key . static::getHikConfig('client_id'); } /** @@ -409,14 +410,14 @@ public function authorizerTokenKey() */ public function authorizerYsTokenKey() { - return $this->access_token_ys_key . self::getYsConfig('appKey'); + return $this->access_token_ys_key . static::getYsConfig('appKey'); } public function refreshSelfAccessToken() { $params = [ - 'client_id' => self::getAppConfig('client_id'), - 'client_secret' => self::getAppConfig('client_secret'), + 'client_id' => static::getHikConfig('client_id'), + 'client_secret' => static::getHikConfig('client_secret'), 'grant_type' => 'client_credentials', ]; @@ -441,8 +442,8 @@ public function refreshSelfAccessToken() public function refreshYsAccessToken() { $params = [ - 'appKey' => self::getYsConfig('appKey'), - 'appSecret' => self::getYsConfig('appSecret') + 'appKey' => static::getYsConfig('appKey'), + 'appSecret' => static::getYsConfig('appSecret') ]; $options = [ 'headers' => ['Content-Type'=>'application/x-www-form-urlencoded'], @@ -458,7 +459,7 @@ public function iscPostJson($endpoint,$params=[]) $header = [ 'Accept' => '*/*', 'Content-Type' => 'application/json', - 'X-Ca-Key' => trim(self::getIsConfig('appKey')), + 'X-Ca-Key' => trim(static::getIsConfig('appKey')), 'X-Ca-Signature' => $this->getSignResult($endpoint), 'X-Ca-Timestamp' => $this->geTimeStamp(), 'X-Ca-Signature-Headers'=> 'x-ca-key,x-ca-timestamp', @@ -478,7 +479,7 @@ public function iscPost($endpoint,$params=[]) $header = [ 'Accept' => '*/*', 'Content-Type' => 'application/json', - 'X-Ca-Key' => trim(self::getIsConfig('appKey')), + 'X-Ca-Key' => trim(static::getIsConfig('appKey')), 'X-Ca-Signature' => $this->getSignResult($endpoint), 'X-Ca-Timestamp' => $this->geTimeStamp(), 'X-Ca-Signature-Headers'=> 'x-ca-key,x-ca-timestamp', @@ -503,7 +504,7 @@ public function iscPost($endpoint,$params=[]) */ public function getSignResult($url,$httpMethod='post') { $signString = $this->generateSignString($url,$httpMethod); - $sign = hash_hmac('sha256',$signString, self::getIsConfig('appSecret'),true); + $sign = hash_hmac('sha256',$signString, static::getIsConfig('appSecret'),true); $result = base64_encode($sign); return $result; } @@ -522,7 +523,7 @@ public function generateSignString($url,$httpMethod) { $string = strtoupper($httpMethod) . $enter; $string .= "*/*" . $enter; $string .= "application/json" . $enter; - $string .= "x-ca-key:" . trim(self::getIsConfig('appKey')).$enter; + $string .= "x-ca-key:" . trim(static::getIsConfig('appKey')).$enter; $string .= "x-ca-timestamp:" . $this->geTimeStamp().$enter; $string .= $url; return $string; diff --git a/src/Isc.php b/src/Isc.php index fd90a44..0926efc 100644 --- a/src/Isc.php +++ b/src/Isc.php @@ -14,7 +14,7 @@ use Imactool\Hikcloud\Core\ContainerBase; use Imactool\Hikcloud\Event\EventProvider; - use Imactool\Hikcloud\Http\Client; + use Imactool\Hikcloud\Http\ClientHttp; use Imactool\Hikcloud\Org\OrgProvider; use Imactool\Hikcloud\Person\PersonProvider; use Imactool\Hikcloud\Resource\ResourceProvider; @@ -32,6 +32,7 @@ */ class Isc extends ContainerBase { + use ClientHttp; private static $config; protected $provider = [ @@ -44,7 +45,7 @@ class Isc extends ContainerBase public function __construct (array $config) { self::$config = $config; - Client::setAppConfig('isConfig',$config); + ClientHttp::setAppConfig('isConfig',$config); parent::__construct(); } diff --git a/src/Ys7.php b/src/Ys7.php index 1dd1de8..cbf4872 100644 --- a/src/Ys7.php +++ b/src/Ys7.php @@ -14,7 +14,7 @@ use Imactool\Hikcloud\Auth\AuthProvider; use Imactool\Hikcloud\Core\ContainerBase; use Imactool\Hikcloud\Device\DeviceProvider; - use Imactool\Hikcloud\Http\Client; + use Imactool\Hikcloud\Http\ClientHttp; use Imactool\Hikcloud\Live\LiveProvider; use Imactool\Hikcloud\Msg\MsgProvider; use Imactool\Hikcloud\Ptz\PtzProvider; @@ -37,6 +37,7 @@ */ class Ys7 extends ContainerBase { + use ClientHttp; private static $config; /** @@ -56,7 +57,7 @@ class Ys7 extends ContainerBase public function __construct (array $config) { self::$config = $config; - Client::setAppConfig('ysconfig',$config); + ClientHttp::setAppConfig('ysconfig',$config); parent::__construct(); }