Skip to content

Commit

Permalink
Support php81
Browse files Browse the repository at this point in the history
  • Loading branch information
iMactool authored and iMactool committed Nov 27, 2023
1 parent a599b9b commit 38ed733
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/Core/BaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions src/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Container implements \ArrayAccess
*/
public $register;

#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
// TODO: Implement offsetExists() method.
Expand All @@ -42,6 +43,7 @@ public function offsetExists($offset)
*
* @return $this|mixed
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
if (isset($this->instances[$offset])) {
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/HikCloud.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,6 +46,7 @@
*/
class HikCloud extends ContainerBase
{
use ClientHttp;

private static $config;

Expand All @@ -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();
}

Expand Down
8 changes: 4 additions & 4 deletions src/Http/AuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ trait AuthService
*/
public function getAccessToken($cacheKey)
{
$accessToken = CacheAdapter::getInstance()->getItem($cacheKey);
$accessToken = self::getInstance()->getItem($cacheKey);

if (!$accessToken->isHit()) {
$result = $this->refreshSelfAccessToken();
if (!empty($result)) {
$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();
Expand All @@ -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();
Expand All @@ -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();
}
Expand Down
67 changes: 34 additions & 33 deletions src/Http/Client.php → src/Http/ClientHttp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use GuzzleHttp\Psr7\Utils;
use Imactool\Hikcloud\Exceptions\Exception;

trait Client
trait ClientHttp
{
use AuthService;

Expand All @@ -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);
}
}

/**
Expand All @@ -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];
}

/**
Expand All @@ -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];
}

/**
Expand All @@ -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];
}

/**
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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',
];

Expand All @@ -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'],
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Isc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,6 +32,7 @@
*/
class Isc extends ContainerBase
{
use ClientHttp;
private static $config;

protected $provider = [
Expand All @@ -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();
}

Expand Down
5 changes: 3 additions & 2 deletions src/Ys7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +37,7 @@
*/
class Ys7 extends ContainerBase
{
use ClientHttp;
private static $config;

/**
Expand All @@ -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();
}

Expand Down

0 comments on commit 38ed733

Please sign in to comment.