-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 新增 TDEngineManager * 新增 PHP 8.1 测试 * 更新代码格式 * 修复测试
- Loading branch information
Showing
9 changed files
with
207 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yurun\TDEngine; | ||
|
||
class TDEngineManager | ||
{ | ||
/** | ||
* 默认客户端名. | ||
* | ||
* @var string|null | ||
*/ | ||
private static $defaultClientName; | ||
|
||
/** | ||
* 客户端配置. | ||
* | ||
* @var ClientConfig[] | ||
*/ | ||
private static $clientConfigs = []; | ||
|
||
/** | ||
* 客户端集合. | ||
* | ||
* @var Client[] | ||
*/ | ||
private static $clients = []; | ||
|
||
private function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* 设置客户端配置. | ||
*/ | ||
public static function setClientConfig(string $clientName, ClientConfig $config): void | ||
{ | ||
static::$clientConfigs[$clientName] = $config; | ||
} | ||
|
||
/** | ||
* 获取客户端配置. | ||
*/ | ||
public static function getClientConfig(?string $clientName = null): ?ClientConfig | ||
{ | ||
$clientName = static::getClientName($clientName); | ||
|
||
return static::$clientConfigs[$clientName] ?? null; | ||
} | ||
|
||
/** | ||
* 移除客户端配置. | ||
*/ | ||
public static function removeClientConfig(?string $clientName = null): void | ||
{ | ||
$clientName = static::getClientName($clientName); | ||
if (isset(static::$clientConfigs[$clientName])) | ||
{ | ||
unset(static::$clientConfigs[$clientName]); | ||
} | ||
} | ||
|
||
/** | ||
* 设置默认客户端名. | ||
*/ | ||
public static function setDefaultClientName(string $clientName): void | ||
{ | ||
static::$defaultClientName = $clientName; | ||
} | ||
|
||
/** | ||
* 获取默认客户端名. | ||
*/ | ||
public static function getDefaultClientName(): ?string | ||
{ | ||
return static::$defaultClientName; | ||
} | ||
|
||
/** | ||
* 获取 InfluxDB 客户端. | ||
*/ | ||
public static function getClient(?string $clientName = null): Client | ||
{ | ||
$clientName = static::getClientName($clientName); | ||
if (isset(static::$clients[$clientName])) | ||
{ | ||
return static::$clients[$clientName]; | ||
} | ||
if (!isset(static::$clientConfigs[$clientName])) | ||
{ | ||
throw new \RuntimeException(sprintf('Client %s config does not found', $clientName)); | ||
} | ||
$client = new Client(static::$clientConfigs[$clientName]); | ||
|
||
return static::$clients[$clientName] = $client; | ||
} | ||
|
||
/** | ||
* 获取客户端名称. | ||
* | ||
* @return string | ||
*/ | ||
public static function getClientName(?string $clientName = null): ?string | ||
{ | ||
return $clientName ?: static::$defaultClientName; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yurun\TDEngine\Test; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yurun\TDEngine\ClientConfig; | ||
use Yurun\TDEngine\TDEngineManager; | ||
|
||
class ManagerTest extends TestCase | ||
{ | ||
public function testClientConfig(): void | ||
{ | ||
$this->assertNull(TDEngineManager::getClientConfig()); | ||
|
||
TDEngineManager::setClientConfig('test', TestUtil::getClientConfig()); | ||
TDEngineManager::setClientConfig('test2', TestUtil::getClientConfig()); | ||
|
||
$this->assertInstanceOf(ClientConfig::class, TDEngineManager::getClientConfig('test')); | ||
} | ||
|
||
public function testRemoveClientConfig(): void | ||
{ | ||
TDEngineManager::setClientConfig('testx', new ClientConfig()); | ||
$this->assertNotNull(TDEngineManager::getClientConfig('testx')); | ||
TDEngineManager::removeClientConfig('testx'); | ||
$this->assertNull(TDEngineManager::getClientConfig('testx')); | ||
} | ||
|
||
public function testDefaultClientName(): void | ||
{ | ||
$this->assertNull(TDEngineManager::getDefaultClientName()); | ||
TDEngineManager::setDefaultClientName('test'); | ||
$this->assertEquals('test', TDEngineManager::getDefaultClientName()); | ||
} | ||
|
||
public function testGetClient(): void | ||
{ | ||
$client = TDEngineManager::getClient(); | ||
$this->assertNotNull($client); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yurun\TDEngine\Test; | ||
|
||
use Yurun\TDEngine\ClientConfig; | ||
use Yurun\TDEngine\Constants\TimeStampFormat; | ||
|
||
class TestUtil | ||
{ | ||
private function __construct() | ||
{ | ||
} | ||
|
||
public static function getClientConfig(): ClientConfig | ||
{ | ||
return new ClientConfig([ | ||
'host' => getenv('TDENGINE_HOST') ?: '127.0.0.1', | ||
'hostName' => getenv('TDENGINE_HOST_NAME') ?: '', | ||
'port' => getenv('TDENGINE_PORT') ?: 6041, | ||
'user' => getenv('TDENGINE_USER') ?: 'root', | ||
'password' => getenv('TDENGINE_PASSWORD') ?: 'taosdata', | ||
'ssl' => getenv('TDENGINE_SSL') ?: false, | ||
'timestampFormat' => getenv('TDENGINE_TIMESTAMP_FORMAT') ?: TimeStampFormat::LOCAL_STRING, | ||
]); | ||
} | ||
} |