Skip to content

Commit

Permalink
Adds trigger component (#239)
Browse files Browse the repository at this point in the history
Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia committed Jun 12, 2023
0 parents commit 9abfee2
Show file tree
Hide file tree
Showing 33 changed files with 1,677 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.github export-ignore
/.vscode export-ignore
/tests export-ignore
.gitattributes export-ignore
13 changes: 13 additions & 0 deletions .github/workflows/close-pull-request.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Close Pull Request

on:
pull_request_target:
types: [opened]

jobs:
run:
runs-on: ubuntu-latest
steps:
- uses: superbrothers/close-pull-request@v3
with:
comment: "Thank you for your pull request. However, you have submitted this PR on the friendsofhyperf organization which is a read-only sub split of `friendsofhyperf/components`. Please submit your PR on the https://github.com/friendsofhyperf/components repository.<br><br>Thanks!"
25 changes: 25 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- 'v*' # Push events to matching v*, i.e. v1.0, v1.0.0

name: Release

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: Release ${{ github.ref }}
draft: false
prerelease: false
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 D.J.Hwang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
73 changes: 73 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Trigger

[![Latest Stable Version](https://poser.pugx.org/friendsofhyperf/trigger/version.png)](https://packagist.org/packages/friendsofhyperf/trigger)
[![Total Downloads](https://poser.pugx.org/friendsofhyperf/trigger/d/total.png)](https://packagist.org/packages/friendsofhyperf/trigger)
[![GitHub license](https://img.shields.io/github/license/friendsofhyperf/trigger)](https://github.com/friendsofhyperf/trigger)

MySQL trigger component for Hyperf, Based on a great work of creators:[moln/php-mysql-replication](https://github.com/moln/php-mysql-replication)

## Installation

- Request

```bash
composer require "friendsofhyperf/trigger:~3.0.0"
```

- Publish

```bash
php bin/hyperf.php vendor:publish friendsofhyperf/trigger
```

## Define a trigger

```php
namespace App\Trigger;

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

#[Trigger(table:"table", on:"*", connection:"default")]
class FooTrigger 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

```php
namespace App\Subscriber;

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

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

## Sponsor

If you like them, Buy me a cup of coffee. [ [Alipay](https://hdj.me/images/alipay.jpg) | [WePay](https://hdj.me/images/wechat-pay.jpg) ]
55 changes: 55 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "friendsofhyperf/trigger",
"description": "The MySQL Trigger component for Hyperf.",
"type": "library",
"require": {
"php": ">=8.0.0",
"friendsofhyperf/command-signals": "~3.0.0",
"hyperf/collection": "~3.0.0",
"hyperf/command": "~3.0.0",
"hyperf/conditionable": "~3.0.0",
"hyperf/context": "~3.0.0",
"hyperf/coordinator": "~3.0.0",
"hyperf/coroutine": "~3.0.0",
"hyperf/di": "~3.0.0",
"hyperf/event": "~3.0.0",
"hyperf/process": "~3.0.0",
"hyperf/stringable": "~3.0.0",
"hyperf/support": "~3.0.0",
"hyperf/tappable": "~3.0.0",
"moln/php-mysql-replication": "^1.2"
},
"autoload": {
"psr-4": {
"FriendsOfHyperf\\Trigger\\": "src"
}
},
"extra": {
"hyperf": {
"config": "FriendsOfHyperf\\Trigger\\ConfigProvider"
}
},
"suggest": {
"hyperf/redis": "Required to use Redis client.",
"hyperf/signal": "Required to use Signal manager."
},
"config": {
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"license": "MIT",
"authors": [
{
"name": "huangdijia",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/friendsofhyperf/components/issues",
"source": "https://github.com/friendsofhyperf/components"
}
}
51 changes: 51 additions & 0 deletions publish/trigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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/3.x/README.md
* @contact [email protected]
*/
use function Hyperf\Support\env;

return [
'connections' => [
'default' => [
'enable' => env('TRIGGER_ENABLE', true),

'host' => env('TRIGGER_HOST', ''),
'port' => (int) env('TRIGGER_PORT', 3306),
'user' => env('TRIGGER_USER', ''),
'password' => env('TRIGGER_PASSWORD', ''),
'databases_only' => env('TRIGGER_DATABASES_ONLY', '') ? explode(',', env('TRIGGER_DATABASES_ONLY')) : [],
'tables_only' => env('TRIGGER_TABLES_ONLY', '') ? explode(',', env('TRIGGER_TABLES_ONLY')) : [],
'heartbeat_period' => (int) env('TRIGGER_HEARTBEAT', 3),

'server_mutex' => [
'enable' => true,
'expires' => 30,
'keepalive_interval' => 10,
'retry_interval' => 10,
],

'health_monitor' => [
'enable' => true,
'interval' => 30,
],

'snapshot' => [
'version' => '1.0',
'expires' => 24 * 3600,
'interval' => 10,
],

'concurrent' => [
'limit' => 1000,
],

'consume_timeout' => 600,
],
],
];
22 changes: 22 additions & 0 deletions src/Annotation/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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/3.x/README.md
* @contact [email protected]
*/
namespace FriendsOfHyperf\Trigger\Annotation;

use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;

#[Attribute(Attribute::TARGET_CLASS)]
class Subscriber extends AbstractAnnotation
{
public function __construct(public string $connection = 'default', public int $priority = 0)
{
}
}
27 changes: 27 additions & 0 deletions src/Annotation/Trigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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/3.x/README.md
* @contact [email protected]
*/
namespace FriendsOfHyperf\Trigger\Annotation;

use Attribute;
use Hyperf\Di\Annotation\AbstractAnnotation;

#[Attribute(Attribute::TARGET_CLASS)]
class Trigger extends AbstractAnnotation
{
public function __construct(
public ?string $database = null,
public ?string $table = null,
public array $events = ['*'],
public string $connection = 'default',
public int $priority = 0
) {
}
}
34 changes: 34 additions & 0 deletions src/Aspect/BinaryDataReaderAspect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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/3.x/README.md
* @contact [email protected]
*/
namespace FriendsOfHyperf\Trigger\Aspect;

use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use MySQLReplication\BinaryDataReader\BinaryDataReader;

/**
* @mixin BinaryDataReader
*/
class BinaryDataReaderAspect extends AbstractAspect
{
public array $classes = [
BinaryDataReader::class . '::readUIntBySize',
];

public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
if ($proceedingJoinPoint->arguments['keys']['size'] == BinaryDataReader::UNSIGNED_INT64_LENGTH) {
return (fn () => (int) $this->readUInt64())->call($proceedingJoinPoint->getInstance());
}

return $proceedingJoinPoint->process();
}
}
Loading

0 comments on commit 9abfee2

Please sign in to comment.