MySQL Trigger for hyperf.

v3.0.0-beta.1 2022-08-16 01:15 UTC

README

Latest Stable Version Total Downloads GitHub license

MySQL trigger component for hyperf, Based on a great work of creators:krowinski/php-mysql-replication

Installation

  • Request
composer require "friendsofhyperf/trigger"
  • Publish
php bin/hyperf.php vendor:publish friendsofhyperf/trigger

Define a trigger

namespace App\Trigger;

use FriendsOfHyperf\Trigger\Annotation\Trigger;
use FriendsOfHyperf\Trigger\Trigger\AbstractTrigger;
use MySQLReplication\Event\DTO\EventDTO;

#[Trigger(table:"table", on:"*", replication:"default")]
class SomeTableTrigger extends AbstractTrigger
{
    public function onWrite(array $new)
    {
        var_dump($new);
    }

    public function onUpdate(array $old, array $new)
    {
        var_dump($old, $new);
    }

    public function onDelete(array $old)
    {
        var_dump($old);
    }
}

Define a subscriber

namespace App\Subscriber;

use FriendsOfHyperf\Trigger\Annotation\Subscriber;
use FriendsOfHyperf\Trigger\Subscriber\AbstractEventSubscriber;
use MySQLReplication\Event\DTO\EventDTO;

#[Subscriber(replication:"default")]
class DemoSubscriber extends AbstractEventSubscriber
{
    protected function allEvents(EventDTO $event): void
    {
        // some code
    }
}

Setup Process

  • Default
namespace App\Process;

use FriendsOfHyperf\Trigger\Process\ConsumeProcess;
use Hyperf\Process\Annotation\Process;

#[Process]
class TriggerProcess extends ConsumeProcess
{
}
  • Custom replication
namespace App\Process;

use FriendsOfHyperf\Trigger\Process\ConsumeProcess;
use Hyperf\Process\Annotation\Process;

#[Process]
class CustomProcess extends ConsumeProcess
{
    protected $replication = 'custom_replication';
}

Signal

// config/autoload/signal.php
return [
    FriendsOfHyperf\Trigger\Handler\TriggerStopHandler::class => PHP_INT_MAX,
];