Skip to content

Commit

Permalink
修复一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Dec 14, 2023
1 parent 54ef5d8 commit b6ba3ad
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 30 deletions.
2 changes: 1 addition & 1 deletion dev/generate-facade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ src/Cli/bin/imi-cli --app-namespace "Imi\Swoole" --bootstrap "src/Components/swo

src/Cli/bin/imi-cli --app-namespace "Imi" generate/facade "Imi\Server\Session\Session" "SessionManager" --request=true && \

src/Cli/bin/imi-cli --app-namespace "Imi\ConnectionCenter" --bootstrap "src/Components/connection-center/vendor/autoload.php" generate/facade "Imi\ConnectionCenter\Facade\ConnectionCenter" "Imi\ConnectionCenter\ConnectionCenter" && \
src/Cli/bin/imi-cli --app-namespace "Imi\ConnectionCenter" --bootstrap "src/Components/connection-center/vendor/autoload.php" generate/facade "Imi\ConnectionCenter\Facade\ConnectionCenter" "Imi\ConnectionCenter\AppConnectionCenter" && \

dev/rector.sh core jwt queue swoole connection-center && \

Expand Down
26 changes: 26 additions & 0 deletions src/Components/connection-center/src/AppConnectionCenter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Imi\ConnectionCenter;

use Imi\Config;

class AppConnectionCenter extends ConnectionCenter
{
public function __construct()

Check warning on line 11 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L11

Added line #L11 was not covered by tests
{
foreach (Config::get('@app.connectionCenter', []) as $name => $connectionManagerConfig)

Check warning on line 13 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L13

Added line #L13 was not covered by tests
{
if (!isset($connectionManagerConfig['manager']))

Check warning on line 15 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L15

Added line #L15 was not covered by tests
{
throw new \InvalidArgumentException(sprintf('Config @app.connectionCenter.%s.manager not found', $name));

Check warning on line 17 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L17

Added line #L17 was not covered by tests
}
if (!isset($connectionManagerConfig['config']))

Check warning on line 19 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L19

Added line #L19 was not covered by tests
{
throw new \InvalidArgumentException(sprintf('Config @app.connectionCenter.%s.config not found', $name));

Check warning on line 21 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L21

Added line #L21 was not covered by tests
}
$this->addConnectionManager($name, $connectionManagerConfig['manager'], $connectionManagerConfig['config']);

Check warning on line 23 in src/Components/connection-center/src/AppConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/AppConnectionCenter.php#L23

Added line #L23 was not covered by tests
}
}
}
6 changes: 6 additions & 0 deletions src/Components/connection-center/src/ConnectionCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Imi\App;
use Imi\ConnectionCenter\Contract\IConnection;
use Imi\ConnectionCenter\Contract\IConnectionManager;
use Imi\ConnectionCenter\Contract\IConnectionManagerConfig;
use Imi\ConnectionCenter\Enum\ConnectionStatus;
use Imi\RequestContext;

Expand All @@ -24,6 +25,11 @@ public function addConnectionManager(string $name, string $connectionManagerClas
throw new \RuntimeException(sprintf('Connection manager %s already exists', $name));
}

if (!$config instanceof IConnectionManagerConfig)
{
$config = $connectionManagerClass::createConfig($config);
}

return $this->connectionManagers[$name] = App::newInstance($connectionManagerClass, $config);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@

abstract class AbstractConnectionDriver implements IConnectionDriver
{
public function __construct(protected IConnectionLoadBalancer $connectionLoadBalancer)
public function __construct(protected IConnectionManagerConfig $connectionManagerConfig, protected IConnectionLoadBalancer $connectionLoadBalancer)
{
}

public function getConnectionManagerConfig(): IConnectionManagerConfig
{
return $this->connectionManagerConfig;
}

public function getConnectionLoadBalancer(): IConnectionLoadBalancer
{
return $this->connectionLoadBalancer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getDriver(): IConnectionDriver
}
$connectionLoadBalancer = App::newInstance($this->config->getLoadBalancer(), $connectionConfigs);

return $this->driver = App::newInstance($driver, $connectionLoadBalancer);
return $this->driver = App::newInstance($driver, $this->config, $connectionLoadBalancer);
}

protected function createInstance(bool $connect = true): object
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Imi\ConnectionCenter\LoadBalancer\RandomLoadBalancer;

abstract class AbstractConnectionManagerConfig implements IConnectionManagerConfig
class ConnectionManagerConfig implements IConnectionManagerConfig
{
public function __construct(protected ?string $driver = null, protected ?string $loadBalancer = null, protected ?bool $enableStatistics = null,
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ interface IConnectionDriver
*/
public static function createConnectionConfig(string|array $config): IConnectionConfig;

public function __construct(IConnectionLoadBalancer $connectionLoadBalancer);
public function __construct(IConnectionManagerConfig $connectionManagerConfig, IConnectionLoadBalancer $connectionLoadBalancer);

/**
* 获取连接管理器配置.
*/
public function getConnectionManagerConfig(): IConnectionManagerConfig;

/**
* 获取连接负载均衡器.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @method static \Imi\ConnectionCenter\Contract\IConnection getRequestContextConnection(string $name)
*/
#[
\Imi\Facade\Annotation\Facade(class: \Imi\ConnectionCenter\ConnectionCenter::class)
\Imi\Facade\Annotation\Facade(class: \Imi\ConnectionCenter\AppConnectionCenter::class)
]

Check warning on line 21 in src/Components/connection-center/src/Facade/ConnectionCenter.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/Facade/ConnectionCenter.php#L21

Added line #L21 was not covered by tests
class ConnectionCenter extends BaseFacade
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Imi\ConnectionCenter\Handler\AlwaysNew;

use Imi\ConnectionCenter\Contract\AbstractConnectionManagerConfig;
use Imi\ConnectionCenter\Contract\ConnectionManagerConfig;

/**
* 总是创建新连接管理器配置.
*/
class AlwaysNewConnectionManagerConfig extends AbstractConnectionManagerConfig
class AlwaysNewConnectionManagerConfig extends ConnectionManagerConfig
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Imi\ConnectionCenter\Handler\Pool;

use Imi\ConnectionCenter\Contract\AbstractConnectionManagerConfig;
use Imi\ConnectionCenter\Contract\ConnectionManagerConfig;

/**
* 连接池连接管理器配置.
*/
class PoolConnectionManagerConfig extends AbstractConnectionManagerConfig
class PoolConnectionManagerConfig extends ConnectionManagerConfig
{
public function __construct(?string $driver = null, ?bool $enableStatistics = null, protected ?PoolConfig $pool = null, array $config = [])
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Imi\ConnectionCenter\Handler\RequestContextSingleton;

use Imi\ConnectionCenter\Contract\AbstractConnectionManagerConfig;
use Imi\ConnectionCenter\Contract\ConnectionManagerConfig;

/**
* 请求上下文单例连接管理器配置.
*/
class RequestContextSingletonConnectionManagerConfig extends AbstractConnectionManagerConfig
class RequestContextSingletonConnectionManagerConfig extends ConnectionManagerConfig
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Imi\ConnectionCenter\Handler\Singleton;

use Imi\ConnectionCenter\Contract\AbstractConnectionManagerConfig;
use Imi\ConnectionCenter\Contract\ConnectionManagerConfig;

/**
* 单例连接管理器配置.
*/
class SingletonConnectionManagerConfig extends AbstractConnectionManagerConfig
class SingletonConnectionManagerConfig extends ConnectionManagerConfig
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Imi\ConnectionCenter\Listener;

use Imi\Bean\Annotation\Listener;
use Imi\Config;
use Imi\ConnectionCenter\Facade\ConnectionCenter;
use Imi\Event\Contract\IEvent;
use Imi\Event\IEventListener;
Expand All @@ -21,17 +20,7 @@ class InitConnectionCenterListener implements IEventListener
*/
public function handle(IEvent $e): void

Check warning on line 21 in src/Components/connection-center/src/Listener/InitConnectionCenterListener.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/Listener/InitConnectionCenterListener.php#L21

Added line #L21 was not covered by tests
{
foreach (Config::get('@app.connectionCenter', []) as $name => $connectionManagerConfig)
{
if (!isset($connectionManagerConfig['manager']))
{
throw new \InvalidArgumentException(sprintf('Config @app.connectionCenter.%s.manager not found', $name));
}
if (!isset($connectionManagerConfig['config']))
{
throw new \InvalidArgumentException(sprintf('Config @app.connectionCenter.%s.config not found', $name));
}
ConnectionCenter::addConnectionManager($name, $connectionManagerConfig['manager'], $connectionManagerConfig['config']);
}
// 构造方法会自动初始化,从配置中加载
ConnectionCenter::__getFacadeInstance();

Check warning on line 24 in src/Components/connection-center/src/Listener/InitConnectionCenterListener.php

View check run for this annotation

Codecov / codecov/patch

src/Components/connection-center/src/Listener/InitConnectionCenterListener.php#L24

Added line #L24 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ public function testAddConnectionManager(): void
{
self::$connectionCenter->addConnectionManager('alwaysNew', AlwaysNewConnectionManager::class, AlwaysNewConnectionManager::createConfig(['driver' => TestDriver::class, 'enableStatistics' => true, 'resources' => [['test' => true]], 'checkStateWhenGetResource' => true, 'requestResourceCheckInterval' => 0]));

self::$connectionCenter->addConnectionManager('requestContextSingleton', RequestContextSingletonConnectionManager::class, RequestContextSingletonConnectionManager::createConfig(['driver' => TestDriver::class, 'enableStatistics' => true, 'resources' => [['test' => true]], 'checkStateWhenGetResource' => true, 'requestResourceCheckInterval' => 0]));
// 测试连接管理器配置传入数组
self::$connectionCenter->addConnectionManager('requestContextSingleton', RequestContextSingletonConnectionManager::class, ['driver' => TestDriver::class, 'enableStatistics' => true, 'resources' => [['test' => true]], 'checkStateWhenGetResource' => true, 'requestResourceCheckInterval' => 0]);

self::$connectionCenter->addConnectionManager('singleton', SingletonConnectionManager::class, SingletonConnectionManager::createConfig(['driver' => TestDriver::class, 'enableStatistics' => true, 'resources' => [['test' => true]], 'checkStateWhenGetResource' => true, 'requestResourceCheckInterval' => 0]));

Expand Down
13 changes: 11 additions & 2 deletions src/Components/connection-center/tests/Tests/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Imi\ConnectionCenter\Test\Tests;

use Imi\ConnectionCenter\Contract\ConnectionManagerConfig;
use Imi\ConnectionCenter\Contract\IConnectionDriver;
use Imi\ConnectionCenter\Contract\IConnectionLoadBalancer;
use Imi\ConnectionCenter\LoadBalancer\RandomLoadBalancer;
Expand All @@ -15,14 +16,22 @@ class DriverTest extends TestCase
{
public function testCreateDriver(): IConnectionDriver
{
$driver = new TestDriver(new RandomLoadBalancer([
$driver = new TestDriver(new ConnectionManagerConfig(TestDriver::class), new RandomLoadBalancer([
TestDriverConfig::create(['test' => true]),
]));
$this->assertTrue(true);

return $driver;
}

/**
* @depends testCreateDriver
*/
public function testGetConnectionManagerConfig(IConnectionDriver $driver): void
{
$this->assertInstanceOf(ConnectionManagerConfig::class, $driver->getConnectionManagerConfig());
}

/**
* @depends testCreateDriver
*/
Expand All @@ -44,7 +53,7 @@ public function testCreateInstance(IConnectionDriver $driver): array

public function testCreateInstanceFailed(): void
{
$driver = new TestDriver(new RandomLoadBalancer([]));
$driver = new TestDriver(new ConnectionManagerConfig(TestDriver::class), new RandomLoadBalancer([]));
$this->expectExceptionMessage('No connection config available');
$driver->createInstance();
}
Expand Down

0 comments on commit b6ba3ad

Please sign in to comment.