From e2d83d442d8ca4adbeca87838d76de9405d42840 Mon Sep 17 00:00:00 2001 From: Deeka Wong Date: Wed, 4 Sep 2024 23:04:49 +0800 Subject: [PATCH] Optimized code of trigger (#707) --- src/trigger/src/ConstEventsNames.php | 31 +++++++++++++++++++ .../src/Subscriber/SnapshotSubscriber.php | 2 +- .../src/Subscriber/TriggerSubscriber.php | 20 ++++++------ 3 files changed, 42 insertions(+), 11 deletions(-) create mode 100644 src/trigger/src/ConstEventsNames.php diff --git a/src/trigger/src/ConstEventsNames.php b/src/trigger/src/ConstEventsNames.php new file mode 100644 index 000000000..2f006f064 --- /dev/null +++ b/src/trigger/src/ConstEventsNames.php @@ -0,0 +1,31 @@ +getEventInfo(); $binLogCurrent = match (true) { - method_exists($eventInfo, 'getBinLogCurrent') => $eventInfo->getBinLogCurrent(), + method_exists($eventInfo, 'getBinLogCurrent') => $eventInfo->getBinLogCurrent(), // v7.x, @deprecated, will removed in v3.2 property_exists($eventInfo, 'binLogCurrent') => $eventInfo->binLogCurrent, // @phpstan-ignore-line default => null, }; diff --git a/src/trigger/src/Subscriber/TriggerSubscriber.php b/src/trigger/src/Subscriber/TriggerSubscriber.php index a2283a627..f3cba76de 100644 --- a/src/trigger/src/Subscriber/TriggerSubscriber.php +++ b/src/trigger/src/Subscriber/TriggerSubscriber.php @@ -12,6 +12,7 @@ namespace FriendsOfHyperf\Trigger\Subscriber; use Closure; +use FriendsOfHyperf\Trigger\ConstEventsNames; use FriendsOfHyperf\Trigger\Consumer; use FriendsOfHyperf\Trigger\Traits\Logger; use FriendsOfHyperf\Trigger\TriggerManager; @@ -20,7 +21,6 @@ use Hyperf\Coroutine\Concurrent; use Hyperf\Engine\Channel; use Hyperf\Engine\Coroutine; -use MySQLReplication\Definitions\ConstEventsNames; use MySQLReplication\Event\DTO\EventDTO; use MySQLReplication\Event\DTO\RowsDTO; use Psr\Container\ContainerInterface; @@ -58,9 +58,9 @@ public function __construct( public static function getSubscribedEvents(): array { return [ - 'update' => 'onUpdate', // ConstEventsNames::UPDATE->value - 'delete' => 'onDelete', // ConstEventsNames::DELETE->value - 'write' => 'onWrite', // ConstEventsNames::WRITE->value + ConstEventsNames::UPDATE->value => 'onUpdate', + ConstEventsNames::DELETE->value => 'onDelete', + ConstEventsNames::WRITE->value => 'onWrite', ]; } @@ -122,13 +122,13 @@ protected function allEvents(EventDTO $event): void $this->loop(); $database = match (true) { - method_exists($event, 'getTableMap') => $event->getTableMap()->getDatabase(), + method_exists($event, 'getTableMap') => $event->getTableMap()->getDatabase(), // v7.x, @deprecated, will removed in v3.2 property_exists($event, 'tableMap') => $event->tableMap->database, // @phpstan-ignore-line default => null, }; $table = match (true) { - method_exists($event, 'getTableMap') => $event->getTableMap()->getTable(), + method_exists($event, 'getTableMap') => $event->getTableMap()->getTable(), // v7.x, @deprecated, will removed in v3.2 property_exists($event, 'tableMap') => $event->tableMap->table, // @phpstan-ignore-line default => null, }; @@ -144,7 +144,7 @@ protected function allEvents(EventDTO $event): void foreach ($this->triggerManager->get($key) as $callable) { $values = match (true) { - method_exists($event, 'getValues') => $event->getValues(), + method_exists($event, 'getValues') => $event->getValues(), // v7.x, @deprecated, will removed in v3.2 property_exists($event, 'values') => $event->values, // @phpstan-ignore-line default => [], }; @@ -158,9 +158,9 @@ protected function allEvents(EventDTO $event): void } $args = match ($eventType) { - 'write' => [$value], - 'update' => [$value['before'], $value['after']], - 'delete' => [$value], + ConstEventsNames::WRITE->value => [$value], + ConstEventsNames::UPDATE->value => [$value['before'], $value['after']], + ConstEventsNames::DELETE->value => [$value], default => null, };