asseco-voice/laravel-inbox 问题修复 & 功能扩展

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

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

asseco-voice/laravel-inbox

Composer 安装命令:

composer require asseco-voice/laravel-inbox

包简介

Laravel handling incoming communication

关键字:

README 文档

README

Laravel inbox

Purpose of this package is to provide pattern-matching for incoming communication and executing custom callbacks if they match.

Credits to BeyondCode for the initial codebase. The idea for this package later on substantially diverged from the original, leaving no alternative than to separate it as a new package.

Installation

Require the package with composer require asseco-voice/laravel-inbox. Service provider will be registered automatically.

Usage

Interface

Before you start using the package, you need to have a class implementing a CanMatch interface so that package knows what will it validate regex against.

I.e. if you want to validate against 'from', you need to defined where to fetch that piece of information from.

class Message implements CanMatch
{
    ...
    public function getMatchedValues(string $matchBy): array
    {
        switch ($matchBy) {
            case 'from':                return [$this->from()];
            case 'subject':             return [$this->subject()];
            case 'something_custom':    return [$this->custom()];
            default:                    return [];
        }
    }
    ...
}

Inbox API

Inbox is a class in which you provide regex patterns which you'd like to be matched before the given callback is executed.

I.e. the callback defined under action() will execute only if the .*@gmail.com pattern is matched:

$inbox = new Inbox();

$inbox
    ->from('{.*}@gmail.com')
    ->action(function (CanMatch $message) {
        Log::info("Message received");
    });

Patterns need to be surrounded within { }.

  • from($pattern) will target from field

  • to($pattern) will target to field

  • cc($pattern) will target cc field

  • bcc($pattern) will target bcc field

  • subject($pattern) will target subject field

  • setPattern($name, $pattern) is created for every other use case you might need. I.e. from($pattern) is just a shorthand for setPattern('from', $pattern).

  • meta([...]) for adding any other meta-data.

  • action(callable) is a callback to be executed and takes object implementing a CanMatch interface as an only argument (can be omitted though).

  • matchEither() will act as OR gate in case more than one pattern is defined. Default behavior is to match all patterns for a callback to execute.

  • priority($number) will set inbox priority which will be taken into account only if InboxGroup is used.

  • run(CanMatch $message) will take an instance of object implementing CanMatch interface and will return a bool of whether the inbox was hit or not. This method must not be used when using inbox groups. They have their own run() method.

Inbox group API

Should you require multiple cases covered, there is also a higher level concept - InboxGroup which acts as a container for multiple inboxes, having a few fluent API methods as well.

  • add(Inbox $inbox) will add an inbox to a group
  • continuousMatching() will continue matching after a first match is hit. Default behavior is to stop after one inbox is matched.
  • fallback(callable) will add a (non-mandatory) fallback which will execute if no inbox is hit.
  • run(CanMatch $message) will take an instance of object implementing CanMatch interface and will (unlike inbox run() method) return an array of matched inboxes. Inboxes will be ran by priority.

More examples

Examples will cover cases using mail, but it can be adapted to any incoming communication.

Matching functions can be used to either provide an exact match (i.e. from('exact@email.com')) or providing a regex match which needs to be surrounded with curly braces { } to be interpreted as such.

Example:

$inbox = new Inbox();

$inbox
    ->from('{.*}@gmail.com')
    ->to('{.*}@gmail.com')
    ->subject('Subject to match')
    ->action(function (CanMatch $email) {
        Log::info("Mail received");
    })
    ->matchEither()
    ->priority(10);

More examples with outcomes:

  • having an exact match from('your.mail@gmail.com'):
    • only mails coming solely from your.mail@gmail.com will be matched.
  • having a partial match from('{.*}@gmail.com'):
    • any gmail address will be matched like someone@gmail.com and someone-else@gmail.com will be matched, but someone@yahoo.com won't).
  • having a partial match from('your.name@{.*}'):
    • same as last example, but this time the name is fixed and provider is flexible. It would match your.name@gmail.com, your.name@yahoo.com, but it wouldn't match not.your.name@gmail.com.
  • having a full regex match: from('{.*}'):
    • accepts anything.

Group example:

public function receiveEmail($email){

    $inbox1 = ...;
    $inbox2 = ...;
    $inbox3 = ...;

    $group = new InboxGroup(); 
    
    $group
        ->add($inbox1)
        ->add($inbox2)
        ->add($inbox3) 
        ->fallback(function (CanMatch $email) {
            Log::info("Fell back");
        })
        ->continuousMatching()
        ->run($email);
}

If you don't want to use groups, but a single inbox, you can call run method on it directly:

public function receiveEmail($email){

    $inbox = new Inbox();
    
    $inbox
        ->...
        ->...
        ->run($email);
}

asseco-voice/laravel-inbox 适用场景与选型建议

asseco-voice/laravel-inbox 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.48k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 asseco-voice/laravel-inbox 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 4.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 5
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 5
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-11