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 targetfromfield -
to($pattern)will targettofield -
cc($pattern)will targetccfield -
bcc($pattern)will targetbccfield -
subject($pattern)will targetsubjectfield -
setPattern($name, $pattern)is created for every other use case you might need. I.e.from($pattern)is just a shorthand forsetPattern('from', $pattern). -
meta([...])for adding any other meta-data. -
action(callable)is a callback to be executed and takes object implementing aCanMatchinterface as an only argument (can be omitted though). -
matchEither()will act asORgate 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 ifInboxGroupis used. -
run(CanMatch $message)will take an instance of object implementingCanMatchinterface 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 ownrun()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 groupcontinuousMatching()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 implementingCanMatchinterface and will (unlike inboxrun()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.comwill be matched.
- only mails coming solely from
- having a partial match
from('{.*}@gmail.com'):- any gmail address will be matched like
someone@gmail.comandsomeone-else@gmail.comwill be matched, butsomeone@yahoo.comwon't).
- any gmail address will be matched like
- 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 matchnot.your.name@gmail.com.
- same as last example, but this time the name is fixed and provider is flexible.
It would match
- 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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 asseco-voice/laravel-inbox 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
Laravel package for Accurate Online API integration.
A lightweight plain-PHP framework for database-backed CRUD APIs.
bughq error tracking - PHP SDK
Shared RCX Laravel DataTables UI and configuration helpers.
统计信息
- 总下载量: 4.48k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-11
