Skip to content

Commit

Permalink
新增 TDEngineManager (#1)
Browse files Browse the repository at this point in the history
* 新增 TDEngineManager

* 新增 PHP 8.1 测试

* 更新代码格式

* 修复测试
  • Loading branch information
Yurunsoft authored Dec 8, 2021
1 parent 3621560 commit 43e8ca7
Show file tree
Hide file tree
Showing 9 changed files with 207 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: "3"
services:
tdengine:
restart: always
image: tdengine/tdengine:2.0.20.9
image: tdengine/tdengine:2.2.2.0
hostname: tdengine
container_name: tdengine
privileged: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [7.0, 7.1, 7.2, 7.3, 7.4, 8.0]
php: [7.0, 7.1, 7.2, 7.3, 7.4, '8.0', 8.1]

env:
PHP_DOCKER_VERSION: ${{ matrix.php }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.0]
php: ['8.0']

env:
PHP_DOCKER_VERSION: ${{ matrix.php }}
Expand All @@ -24,4 +24,4 @@ jobs:
docker exec php composer update
- name: Test
run: docker exec php ./vendor/bin/php-cs-fixer fix --dry-run
run: docker exec php ./vendor/bin/php-cs-fixer fix --dry-run --diff
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.0]
php: ['8.0']

env:
PHP_DOCKER_VERSION: ${{ matrix.php }}
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@

## 使用

**使用连接管理器:**

```php
// 增加名称为 test 的连接配置
\Yurun\TDEngine\TDEngineManager::setClientConfig('test', new \Yurun\TDEngine\ClientConfig([
// 'host' => '127.0.0.1',
// 'hostName' => '',
// 'port' => 6041,
// 'user' => 'root',
// 'password' => 'taosdata',
// 'ssl' => false,
// 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING,
// 'keepAlive' => true,
]));
// 设置默认数据库为test
\Yurun\TDEngine\TDEngineManager::setDefaultClientName('test');
// 获取客户端对象(\Yurun\TDEngine\Client)
$client = \Yurun\TDEngine\TDEngineManager::getClient();
```

**直接 new 客户端:**

```php
$client = new \Yurun\TDEngine\Client(new \Yurun\TDEngine\ClientConfig([
// 'host' => '127.0.0.1',
Expand Down
108 changes: 108 additions & 0 deletions src/TDEngineManager.php
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;
}
}
12 changes: 1 addition & 11 deletions tests/unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,12 @@

use PHPUnit\Framework\TestCase;
use Yurun\TDEngine\Client;
use Yurun\TDEngine\ClientConfig;
use Yurun\TDEngine\Constants\TimeStampFormat;

class ClientTest extends TestCase
{
private function getClient(): Client
{
return new Client(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,
]));
return new Client(TestUtil::getClientConfig());
}

private function getDbName(): string
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/ManagerTest.php
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);
}
}
28 changes: 28 additions & 0 deletions tests/unit/TestUtil.php
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,
]);
}
}

0 comments on commit 43e8ca7

Please sign in to comment.