Skip to content

Commit 43e8ca7

Browse files
authored
新增 TDEngineManager (#1)
* 新增 TDEngineManager * 新增 PHP 8.1 测试 * 更新代码格式 * 修复测试
1 parent 3621560 commit 43e8ca7

File tree

9 files changed

+207
-16
lines changed

9 files changed

+207
-16
lines changed

.github/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: "3"
22
services:
33
tdengine:
44
restart: always
5-
image: tdengine/tdengine:2.0.20.9
5+
image: tdengine/tdengine:2.2.2.0
66
hostname: tdengine
77
container_name: tdengine
88
privileged: true

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
php: [7.0, 7.1, 7.2, 7.3, 7.4, 8.0]
12+
php: [7.0, 7.1, 7.2, 7.3, 7.4, '8.0', 8.1]
1313

1414
env:
1515
PHP_DOCKER_VERSION: ${{ matrix.php }}

.github/workflows/phpcs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
php: [8.0]
12+
php: ['8.0']
1313

1414
env:
1515
PHP_DOCKER_VERSION: ${{ matrix.php }}
@@ -24,4 +24,4 @@ jobs:
2424
docker exec php composer update
2525
2626
- name: Test
27-
run: docker exec php ./vendor/bin/php-cs-fixer fix --dry-run
27+
run: docker exec php ./vendor/bin/php-cs-fixer fix --dry-run --diff

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
php: [8.0]
12+
php: ['8.0']
1313

1414
env:
1515
PHP_DOCKER_VERSION: ${{ matrix.php }}

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@
2323

2424
## 使用
2525

26+
**使用连接管理器:**
27+
28+
```php
29+
// 增加名称为 test 的连接配置
30+
\Yurun\TDEngine\TDEngineManager::setClientConfig('test', new \Yurun\TDEngine\ClientConfig([
31+
// 'host' => '127.0.0.1',
32+
// 'hostName' => '',
33+
// 'port' => 6041,
34+
// 'user' => 'root',
35+
// 'password' => 'taosdata',
36+
// 'ssl' => false,
37+
// 'timestampFormat' => \Yurun\TDEngine\Constants\TimeStampFormat::LOCAL_STRING,
38+
// 'keepAlive' => true,
39+
]));
40+
// 设置默认数据库为test
41+
\Yurun\TDEngine\TDEngineManager::setDefaultClientName('test');
42+
// 获取客户端对象(\Yurun\TDEngine\Client)
43+
$client = \Yurun\TDEngine\TDEngineManager::getClient();
44+
```
45+
46+
**直接 new 客户端:**
47+
2648
```php
2749
$client = new \Yurun\TDEngine\Client(new \Yurun\TDEngine\ClientConfig([
2850
// 'host' => '127.0.0.1',

src/TDEngineManager.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yurun\TDEngine;
6+
7+
class TDEngineManager
8+
{
9+
/**
10+
* 默认客户端名.
11+
*
12+
* @var string|null
13+
*/
14+
private static $defaultClientName;
15+
16+
/**
17+
* 客户端配置.
18+
*
19+
* @var ClientConfig[]
20+
*/
21+
private static $clientConfigs = [];
22+
23+
/**
24+
* 客户端集合.
25+
*
26+
* @var Client[]
27+
*/
28+
private static $clients = [];
29+
30+
private function __construct()
31+
{
32+
}
33+
34+
/**
35+
* 设置客户端配置.
36+
*/
37+
public static function setClientConfig(string $clientName, ClientConfig $config): void
38+
{
39+
static::$clientConfigs[$clientName] = $config;
40+
}
41+
42+
/**
43+
* 获取客户端配置.
44+
*/
45+
public static function getClientConfig(?string $clientName = null): ?ClientConfig
46+
{
47+
$clientName = static::getClientName($clientName);
48+
49+
return static::$clientConfigs[$clientName] ?? null;
50+
}
51+
52+
/**
53+
* 移除客户端配置.
54+
*/
55+
public static function removeClientConfig(?string $clientName = null): void
56+
{
57+
$clientName = static::getClientName($clientName);
58+
if (isset(static::$clientConfigs[$clientName]))
59+
{
60+
unset(static::$clientConfigs[$clientName]);
61+
}
62+
}
63+
64+
/**
65+
* 设置默认客户端名.
66+
*/
67+
public static function setDefaultClientName(string $clientName): void
68+
{
69+
static::$defaultClientName = $clientName;
70+
}
71+
72+
/**
73+
* 获取默认客户端名.
74+
*/
75+
public static function getDefaultClientName(): ?string
76+
{
77+
return static::$defaultClientName;
78+
}
79+
80+
/**
81+
* 获取 InfluxDB 客户端.
82+
*/
83+
public static function getClient(?string $clientName = null): Client
84+
{
85+
$clientName = static::getClientName($clientName);
86+
if (isset(static::$clients[$clientName]))
87+
{
88+
return static::$clients[$clientName];
89+
}
90+
if (!isset(static::$clientConfigs[$clientName]))
91+
{
92+
throw new \RuntimeException(sprintf('Client %s config does not found', $clientName));
93+
}
94+
$client = new Client(static::$clientConfigs[$clientName]);
95+
96+
return static::$clients[$clientName] = $client;
97+
}
98+
99+
/**
100+
* 获取客户端名称.
101+
*
102+
* @return string
103+
*/
104+
public static function getClientName(?string $clientName = null): ?string
105+
{
106+
return $clientName ?: static::$defaultClientName;
107+
}
108+
}

tests/unit/ClientTest.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,12 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Yurun\TDEngine\Client;
9-
use Yurun\TDEngine\ClientConfig;
10-
use Yurun\TDEngine\Constants\TimeStampFormat;
119

1210
class ClientTest extends TestCase
1311
{
1412
private function getClient(): Client
1513
{
16-
return new Client(new ClientConfig([
17-
'host' => getenv('TDENGINE_HOST') ?: '127.0.0.1',
18-
'hostName' => getenv('TDENGINE_HOST_NAME') ?: '',
19-
'port' => getenv('TDENGINE_PORT') ?: 6041,
20-
'user' => getenv('TDENGINE_USER') ?: 'root',
21-
'password' => getenv('TDENGINE_PASSWORD') ?: 'taosdata',
22-
'ssl' => getenv('TDENGINE_SSL') ?: false,
23-
'timestampFormat' => getenv('TDENGINE_TIMESTAMP_FORMAT') ?: TimeStampFormat::LOCAL_STRING,
24-
]));
14+
return new Client(TestUtil::getClientConfig());
2515
}
2616

2717
private function getDbName(): string

tests/unit/ManagerTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yurun\TDEngine\Test;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yurun\TDEngine\ClientConfig;
9+
use Yurun\TDEngine\TDEngineManager;
10+
11+
class ManagerTest extends TestCase
12+
{
13+
public function testClientConfig(): void
14+
{
15+
$this->assertNull(TDEngineManager::getClientConfig());
16+
17+
TDEngineManager::setClientConfig('test', TestUtil::getClientConfig());
18+
TDEngineManager::setClientConfig('test2', TestUtil::getClientConfig());
19+
20+
$this->assertInstanceOf(ClientConfig::class, TDEngineManager::getClientConfig('test'));
21+
}
22+
23+
public function testRemoveClientConfig(): void
24+
{
25+
TDEngineManager::setClientConfig('testx', new ClientConfig());
26+
$this->assertNotNull(TDEngineManager::getClientConfig('testx'));
27+
TDEngineManager::removeClientConfig('testx');
28+
$this->assertNull(TDEngineManager::getClientConfig('testx'));
29+
}
30+
31+
public function testDefaultClientName(): void
32+
{
33+
$this->assertNull(TDEngineManager::getDefaultClientName());
34+
TDEngineManager::setDefaultClientName('test');
35+
$this->assertEquals('test', TDEngineManager::getDefaultClientName());
36+
}
37+
38+
public function testGetClient(): void
39+
{
40+
$client = TDEngineManager::getClient();
41+
$this->assertNotNull($client);
42+
}
43+
}

tests/unit/TestUtil.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yurun\TDEngine\Test;
6+
7+
use Yurun\TDEngine\ClientConfig;
8+
use Yurun\TDEngine\Constants\TimeStampFormat;
9+
10+
class TestUtil
11+
{
12+
private function __construct()
13+
{
14+
}
15+
16+
public static function getClientConfig(): ClientConfig
17+
{
18+
return new ClientConfig([
19+
'host' => getenv('TDENGINE_HOST') ?: '127.0.0.1',
20+
'hostName' => getenv('TDENGINE_HOST_NAME') ?: '',
21+
'port' => getenv('TDENGINE_PORT') ?: 6041,
22+
'user' => getenv('TDENGINE_USER') ?: 'root',
23+
'password' => getenv('TDENGINE_PASSWORD') ?: 'taosdata',
24+
'ssl' => getenv('TDENGINE_SSL') ?: false,
25+
'timestampFormat' => getenv('TDENGINE_TIMESTAMP_FORMAT') ?: TimeStampFormat::LOCAL_STRING,
26+
]);
27+
}
28+
}

0 commit comments

Comments
 (0)