diff --git a/dev/generate-facade.sh b/dev/generate-facade.sh index b5fbc1e45..dbe84a79a 100755 --- a/dev/generate-facade.sh +++ b/dev/generate-facade.sh @@ -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 && \ diff --git a/src/Components/connection-center/src/AppConnectionCenter.php b/src/Components/connection-center/src/AppConnectionCenter.php new file mode 100644 index 000000000..d3c72c9c3 --- /dev/null +++ b/src/Components/connection-center/src/AppConnectionCenter.php @@ -0,0 +1,26 @@ + $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)); + } + $this->addConnectionManager($name, $connectionManagerConfig['manager'], $connectionManagerConfig['config']); + } + } +} diff --git a/src/Components/connection-center/src/ConnectionCenter.php b/src/Components/connection-center/src/ConnectionCenter.php index d3c728082..10b51b100 100644 --- a/src/Components/connection-center/src/ConnectionCenter.php +++ b/src/Components/connection-center/src/ConnectionCenter.php @@ -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; @@ -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); } diff --git a/src/Components/connection-center/src/Contract/AbstractConnectionDriver.php b/src/Components/connection-center/src/Contract/AbstractConnectionDriver.php index 6202d594f..fcb9fc4a4 100644 --- a/src/Components/connection-center/src/Contract/AbstractConnectionDriver.php +++ b/src/Components/connection-center/src/Contract/AbstractConnectionDriver.php @@ -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; diff --git a/src/Components/connection-center/src/Contract/AbstractConnectionManager.php b/src/Components/connection-center/src/Contract/AbstractConnectionManager.php index 1dcc52c30..8d38fb907 100644 --- a/src/Components/connection-center/src/Contract/AbstractConnectionManager.php +++ b/src/Components/connection-center/src/Contract/AbstractConnectionManager.php @@ -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 diff --git a/src/Components/connection-center/src/Contract/AbstractConnectionManagerConfig.php b/src/Components/connection-center/src/Contract/ConnectionManagerConfig.php similarity index 95% rename from src/Components/connection-center/src/Contract/AbstractConnectionManagerConfig.php rename to src/Components/connection-center/src/Contract/ConnectionManagerConfig.php index 90e7d52c5..749d97f41 100644 --- a/src/Components/connection-center/src/Contract/AbstractConnectionManagerConfig.php +++ b/src/Components/connection-center/src/Contract/ConnectionManagerConfig.php @@ -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, /** diff --git a/src/Components/connection-center/src/Contract/IConnectionDriver.php b/src/Components/connection-center/src/Contract/IConnectionDriver.php index a4871b367..0f301cc4f 100644 --- a/src/Components/connection-center/src/Contract/IConnectionDriver.php +++ b/src/Components/connection-center/src/Contract/IConnectionDriver.php @@ -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; /** * 获取连接负载均衡器. diff --git a/src/Components/connection-center/src/Facade/ConnectionCenter.php b/src/Components/connection-center/src/Facade/ConnectionCenter.php index 0081733ef..3feb683f6 100644 --- a/src/Components/connection-center/src/Facade/ConnectionCenter.php +++ b/src/Components/connection-center/src/Facade/ConnectionCenter.php @@ -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) ] class ConnectionCenter extends BaseFacade { diff --git a/src/Components/connection-center/src/Handler/AlwaysNew/AlwaysNewConnectionManagerConfig.php b/src/Components/connection-center/src/Handler/AlwaysNew/AlwaysNewConnectionManagerConfig.php index 2e548104b..0f46fef82 100644 --- a/src/Components/connection-center/src/Handler/AlwaysNew/AlwaysNewConnectionManagerConfig.php +++ b/src/Components/connection-center/src/Handler/AlwaysNew/AlwaysNewConnectionManagerConfig.php @@ -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 { } diff --git a/src/Components/connection-center/src/Handler/Pool/PoolConnectionManagerConfig.php b/src/Components/connection-center/src/Handler/Pool/PoolConnectionManagerConfig.php index dd9478a4a..9bf975c26 100644 --- a/src/Components/connection-center/src/Handler/Pool/PoolConnectionManagerConfig.php +++ b/src/Components/connection-center/src/Handler/Pool/PoolConnectionManagerConfig.php @@ -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 = []) { diff --git a/src/Components/connection-center/src/Handler/RequestContextSingleton/RequestContextSingletonConnectionManagerConfig.php b/src/Components/connection-center/src/Handler/RequestContextSingleton/RequestContextSingletonConnectionManagerConfig.php index 7d3a75ac8..80fb5d5bf 100644 --- a/src/Components/connection-center/src/Handler/RequestContextSingleton/RequestContextSingletonConnectionManagerConfig.php +++ b/src/Components/connection-center/src/Handler/RequestContextSingleton/RequestContextSingletonConnectionManagerConfig.php @@ -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 { } diff --git a/src/Components/connection-center/src/Handler/Singleton/SingletonConnectionManagerConfig.php b/src/Components/connection-center/src/Handler/Singleton/SingletonConnectionManagerConfig.php index 9f9bcfb09..4f16b61e6 100644 --- a/src/Components/connection-center/src/Handler/Singleton/SingletonConnectionManagerConfig.php +++ b/src/Components/connection-center/src/Handler/Singleton/SingletonConnectionManagerConfig.php @@ -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 { } diff --git a/src/Components/connection-center/src/Listener/InitConnectionCenterListener.php b/src/Components/connection-center/src/Listener/InitConnectionCenterListener.php index 19990cfab..a63259ce6 100644 --- a/src/Components/connection-center/src/Listener/InitConnectionCenterListener.php +++ b/src/Components/connection-center/src/Listener/InitConnectionCenterListener.php @@ -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; @@ -21,17 +20,7 @@ class InitConnectionCenterListener implements IEventListener */ public function handle(IEvent $e): void { - 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(); } } diff --git a/src/Components/connection-center/tests/Tests/ConnectionCenterTest.php b/src/Components/connection-center/tests/Tests/ConnectionCenterTest.php index 5f1459ea6..238a27a52 100644 --- a/src/Components/connection-center/tests/Tests/ConnectionCenterTest.php +++ b/src/Components/connection-center/tests/Tests/ConnectionCenterTest.php @@ -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])); diff --git a/src/Components/connection-center/tests/Tests/DriverTest.php b/src/Components/connection-center/tests/Tests/DriverTest.php index 8522f3752..8bb9605fe 100644 --- a/src/Components/connection-center/tests/Tests/DriverTest.php +++ b/src/Components/connection-center/tests/Tests/DriverTest.php @@ -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; @@ -15,7 +16,7 @@ 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); @@ -23,6 +24,14 @@ public function testCreateDriver(): IConnectionDriver return $driver; } + /** + * @depends testCreateDriver + */ + public function testGetConnectionManagerConfig(IConnectionDriver $driver): void + { + $this->assertInstanceOf(ConnectionManagerConfig::class, $driver->getConnectionManagerConfig()); + } + /** * @depends testCreateDriver */ @@ -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(); }