MySQL Trigger for hyperf.

2.0.1 2021-08-19 08:37 UTC

README

Open in Visual Studio Code 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;

/**
 * single
 * @Trigger(table="table", on="write", replication="default")
 * or multi events by array
 * @Trigger(table="table", on={"write", "update", "delete"}, replication="default")
 * or multi events by string
 * @Trigger(table="table", on="write,update,delete", replication="default")
 * or all events
 * @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,
];