patienceman/synca 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

patienceman/synca

Composer 安装命令:

composer require patienceman/synca

包简介

Provide a convenient way to interact with Notifications, including Emails, Dbnotification, and one signal notifications, it just helps your declaration and interaction easier.

README 文档

README

Provide a convenient way to interact with Notifications, including Emails, Dbnotification, and one signal notifications, it just helps your declaration and interaction easier.

Installation

Install the package doesn't require much requirement except to use the following command in the laravel terminal, and you're good to go.

composer require patienceman/synca

Before get started let first create Queue table and Notifications:

php artisan queue:table
php artisan notification:table
php artisan migrate

Finally, don't forget to instruct your application to use the databasedriver by updating the QUEUE_CONNECTION variable in your application's env or any queue drive you use, consider Laravel queue doc file:

QUEUE_CONNECTION=database 

Usage

To start working with Noptifier, u need to run command 🎉 in your custom directories:

php artisan make:notifier EmailNotification

so it will create the filter file for you, Just in Notifiers directory

App\Notifiers

But in this doc, we'll be using

App\Notifications

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class EmailNotification extends NotifyHandler {
    /**
     * Execute notification actions
     * 
     * @return mixed
     */
    public function handle() {
        // do whatever action inside handler
    }
}

So you may want even to specify the custom path for your Notifier, Just relax and add it in front of your notifier name. Let's take again our current example.

php artisan make:notifier Model/EmailNotification

To communicate/use your Notifier, you only need to call Notifier class, Let take a quick example in our CandidateController class to notify about application between creator and seeker

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {

    /**
     * Handle User Notifications
     */
    public function notifications(Notifier $notifier) {
        // ... Other Codes

        $notifier->handle([
            EmailNotification::process([ 
                'message' => 'Application sent to job sent' 
            ]),
        ]);
    }

}

Now on, we are able send our email notification anytime, any place. So there is many feature comes with notifier, includes

->onQueue()

->to($user1, $user2, ....)

let take a look:

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {
    /**
     * Handle User Notifications
     */
    public function notifications(Notifier $notifier, User $user) {
        // ... Other Codes

        $application = Application::findById('1')->belongsToCompany()->user_id;
        $notification = [ 'message' => 'Application sent to job sent' ];
        
        $users = [
            'user' => $user
            'applicant' => $application
        ];

        $notifier->handle([
            EmailNotification::process($notification)
                ->to($users)
                ->onQueue(),
        ]);
    }

}

So to access the passed users you need to just call one by one using indexes: for example: with

->to($users);

$this->user;
$this->applicant;

This is so cool, but there might be a time where you need to queue all notifier, not single one like above, let see how: but let support we have also OneSignalNotification:

namespace App\Http\Controllers;

use App\Notifications\EmailNotification;
use App\Notifications\OneSignalNotification;
use Patienceman\Synca\Facades\Notifier;

class UsersController extends Controller {

    public function notifications(Notifier $notifier, User $user) {
        $application = Application::findById('1')->belongsToCompany()->user_id;
        $notification = [ 'message' => 'Application sent to job sent' ];
        
        $users = [
            'user' => $user
            'applicant' => $application
        ];

        $notifier->handle([
            EmailNotification::process($notification)->to($users),
            OneSignalNotification::process($notification)->to([$user]),
        ])->onQueue();
    }

}

As u see above, we're working with payloads to notifier, Let see how to get all payload and all targeted user:

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class EmailNotification extends NotifyHandler {
    /**
     * Execute notification actions
     * @return mixed
     */
    public function handle() {
        $this->message; // this will get single passed payload
        $this->payload(); // this will get all payload as object
        $this->recipients(); // this will get all targeted users
    }
}

There is tile also you want to send notification to all recipients without chose who: by just use function

$this->foreachUser() 
$this->foreachUser(fn($user) => $this->sendToDatabase($user)); 

You held this function right!!?, This function can be used in Laravel DBNotification to store custom notification in table: So let see full implementation:

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class DatabaseNotification extends NotifyHandler {
    /**
     * Execute notification
     * @return mixed
     */
    public function handle() {
        $this->foreachUser(
            fn($user) => $this->sendToDatabase($user, $this)
        );
    }

    /**
     * Get the array to database representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toDatabase($notifiable) {
        return $this->payloadAsArray();
    }
}

or use customized way:

namespace App\Notifications;

use Patienceman\Synca\NotifyHandler;

class DatabaseNotification extends NotifyHandler {
    /**
     * Execute notification
     * @return mixed
     */
    public function handle() {
        $this->foreachUser(function($user) {
            $this->dbNotification($user, fn ($notifiable) => [
                'header' => $this->subject,
                'message' => $this->message,
                'action' => $this->action,
            ]);
        });
    }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

patienceman/synca 适用场景与选型建议

patienceman/synca 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.39k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「notification」 「laravel」 「notifier」 「database notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 patienceman/synca 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 patienceman/synca 我们能提供哪些服务?
定制开发 / 二次开发

基于 patienceman/synca 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-21