From 32e4d654b11dc61c5665f6434a7364505cd42d2f Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:54:49 +0800 Subject: [PATCH 1/5] Improve trigger config --- src/trigger/src/Config.php | 101 +++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/src/trigger/src/Config.php b/src/trigger/src/Config.php index 1ea5d90bc..e0d9c1747 100644 --- a/src/trigger/src/Config.php +++ b/src/trigger/src/Config.php @@ -11,6 +11,107 @@ namespace FriendsOfHyperf\Trigger; +use function Hyperf\Support\env; + class Config extends \Hyperf\Config\Config { + public function enable(): bool + { + return $this->get('enable', false); + } + + public function host(): string + { + return $this->get('host', ''); + } + + public function port(): int + { + return $this->get('port', 3306); + } + + public function user(): string + { + return $this->get('user', ''); + } + + public function password(): string + { + return $this->get('password', ''); + } + + public function databasesOnly(): array + { + return $this->get('databases_only', []); + } + + public function tablesOnly(): array + { + return $this->get('tables_only', []); + } + + public function heartbeatPeriod(): int + { + return $this->get('heartbeat_period', 3); + } + + public function connectRetries(): int + { + return $this->get('connect_retries', 10); + } + + /** + * @return class-string[] + */ + public function subscribers(): array + { + return $this->get('subscribers', []); + } + + /** + * @return array{enable: bool, prefix: string, expires: int, keepalive_interval: int, retry_interval: int} + */ + public function serverMutex(): array + { + return array_replace([ + 'enable' => true, + 'prefix' => env('APP_ENV', 'dev') . '_', + 'expires' => 30, + 'keepalive_interval' => 10, + 'retry_interval' => 10, + ], $this->get('server_mutex', [])); + } + + /** + * @return array{enable: bool, interval: int} + */ + public function healthMonitor(): array + { + return array_replace([ + 'enable' => true, + 'interval' => 30, + ], $this->get('health_monitor', [])); + } + + /** + * @return array{version: string, expires: int, interval: int} + */ + public function snapshot(): array + { + return array_replace([ + 'version' => '1.0', + 'expires' => 24 * 3600, + 'interval' => 10, + ], $this->get('snapshot', [])); + } + + /** + * @return array{limit: int} + */ + public function concurrent(): array + { + return array_replace([ + 'limit' => 1, + ], $this->get('concurrent', [])); + } } From b86010b7567fdded1955862872f3ac59bf55128e Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:58:35 +0800 Subject: [PATCH 2/5] refactor(trigger): simplify Config class and remove dependency on Hyperf\Config --- src/trigger/src/Config.php | 13 ++++++++++++- src/trigger/src/Consumer.php | 1 - 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/trigger/src/Config.php b/src/trigger/src/Config.php index e0d9c1747..ffaaf5d1d 100644 --- a/src/trigger/src/Config.php +++ b/src/trigger/src/Config.php @@ -11,10 +11,21 @@ namespace FriendsOfHyperf\Trigger; +use Hyperf\Collection\Arr; + use function Hyperf\Support\env; -class Config extends \Hyperf\Config\Config +class Config { + public function __construct(private array $configs = []) + { + } + + public function get(string $key, mixed $default = null): mixed + { + return Arr::get($this->configs, $key, $default); + } + public function enable(): bool { return $this->get('enable', false); diff --git a/src/trigger/src/Consumer.php b/src/trigger/src/Consumer.php index a4791f38e..6bd8b975b 100644 --- a/src/trigger/src/Consumer.php +++ b/src/trigger/src/Consumer.php @@ -14,7 +14,6 @@ use FriendsOfHyperf\Trigger\Monitor\HealthMonitor; use FriendsOfHyperf\Trigger\Mutex\ServerMutexInterface; use FriendsOfHyperf\Trigger\Snapshot\BinLogCurrentSnapshotInterface; -use Hyperf\Config\Config; use Hyperf\Coordinator\Constants; use Hyperf\Coordinator\CoordinatorManager; use Hyperf\Coroutine\Coroutine; From b65519630807a5e7c2c8e6d0ebaea8126d41abdc Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 23 Dec 2024 20:59:31 +0800 Subject: [PATCH 3/5] feat(trigger): add has() method to Config class for key existence check --- src/trigger/src/Config.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/trigger/src/Config.php b/src/trigger/src/Config.php index ffaaf5d1d..9108a3054 100644 --- a/src/trigger/src/Config.php +++ b/src/trigger/src/Config.php @@ -26,6 +26,11 @@ public function get(string $key, mixed $default = null): mixed return Arr::get($this->configs, $key, $default); } + public function has(string $key): bool + { + return Arr::has($this->configs, $key); + } + public function enable(): bool { return $this->get('enable', false); From 2f5178032dffd0b4305926a2a5b974293634a5cb Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:04:45 +0800 Subject: [PATCH 4/5] fix(trigger): update subscribers method docblock for correct namespace --- src/trigger/src/Config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/trigger/src/Config.php b/src/trigger/src/Config.php index 9108a3054..579fc190d 100644 --- a/src/trigger/src/Config.php +++ b/src/trigger/src/Config.php @@ -77,7 +77,7 @@ public function connectRetries(): int } /** - * @return class-string[] + * @return class-string<\FriendsOfHyperf\Trigger\Subscriber\AbstractSubscriber>[] */ public function subscribers(): array { From 94c36387c0adab01c2f1514f9a58ec8dff4f7801 Mon Sep 17 00:00:00 2001 From: Deeka Wong <8337659+huangdijia@users.noreply.github.com> Date: Mon, 23 Dec 2024 21:06:49 +0800 Subject: [PATCH 5/5] Update Config.php --- src/trigger/src/Config.php | 102 ------------------------------------- 1 file changed, 102 deletions(-) diff --git a/src/trigger/src/Config.php b/src/trigger/src/Config.php index 579fc190d..e6a0eb232 100644 --- a/src/trigger/src/Config.php +++ b/src/trigger/src/Config.php @@ -13,8 +13,6 @@ use Hyperf\Collection\Arr; -use function Hyperf\Support\env; - class Config { public function __construct(private array $configs = []) @@ -30,104 +28,4 @@ public function has(string $key): bool { return Arr::has($this->configs, $key); } - - public function enable(): bool - { - return $this->get('enable', false); - } - - public function host(): string - { - return $this->get('host', ''); - } - - public function port(): int - { - return $this->get('port', 3306); - } - - public function user(): string - { - return $this->get('user', ''); - } - - public function password(): string - { - return $this->get('password', ''); - } - - public function databasesOnly(): array - { - return $this->get('databases_only', []); - } - - public function tablesOnly(): array - { - return $this->get('tables_only', []); - } - - public function heartbeatPeriod(): int - { - return $this->get('heartbeat_period', 3); - } - - public function connectRetries(): int - { - return $this->get('connect_retries', 10); - } - - /** - * @return class-string<\FriendsOfHyperf\Trigger\Subscriber\AbstractSubscriber>[] - */ - public function subscribers(): array - { - return $this->get('subscribers', []); - } - - /** - * @return array{enable: bool, prefix: string, expires: int, keepalive_interval: int, retry_interval: int} - */ - public function serverMutex(): array - { - return array_replace([ - 'enable' => true, - 'prefix' => env('APP_ENV', 'dev') . '_', - 'expires' => 30, - 'keepalive_interval' => 10, - 'retry_interval' => 10, - ], $this->get('server_mutex', [])); - } - - /** - * @return array{enable: bool, interval: int} - */ - public function healthMonitor(): array - { - return array_replace([ - 'enable' => true, - 'interval' => 30, - ], $this->get('health_monitor', [])); - } - - /** - * @return array{version: string, expires: int, interval: int} - */ - public function snapshot(): array - { - return array_replace([ - 'version' => '1.0', - 'expires' => 24 * 3600, - 'interval' => 10, - ], $this->get('snapshot', [])); - } - - /** - * @return array{limit: int} - */ - public function concurrent(): array - { - return array_replace([ - 'limit' => 1, - ], $this->get('concurrent', [])); - } }