Skip to content

Commit

Permalink
Optimized code of trigger (#707)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Sep 4, 2024
1 parent 0123363 commit e2d83d4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
31 changes: 31 additions & 0 deletions src/trigger/src/ConstEventsNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/main/README.md
* @contact [email protected]
*/

namespace FriendsOfHyperf\Trigger;

/**
* @deprecated since v3.1, will removed in 3.2, use \MySQLReplication\Definitions\ConstEventsNames instead
*/
enum ConstEventsNames: string
{
case XID = 'xid';
case DELETE = 'delete';
case QUERY = 'query';
case ROTATE = 'rotate';
case GTID = 'gtid';
case MARIADB_GTID = 'mariadb gtid';
case UPDATE = 'update';
case HEARTBEAT = 'heartbeat';
case TABLE_MAP = 'tableMap';
case WRITE = 'write';
case FORMAT_DESCRIPTION = 'format description';
case ROWS_QUERY = 'rows_query';
}
2 changes: 1 addition & 1 deletion src/trigger/src/Subscriber/SnapshotSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function allEvents(EventDTO $event): void

$eventInfo = $event->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,
};
Expand Down
20 changes: 10 additions & 10 deletions src/trigger/src/Subscriber/TriggerSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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',
];
}

Expand Down Expand Up @@ -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,
};
Expand All @@ -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 => [],
};
Expand All @@ -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,
};

Expand Down

0 comments on commit e2d83d4

Please sign in to comment.