architectnate/triggr-php
Composer 安装命令:
composer require architectnate/triggr-php
包简介
A simple event handler for php
README 文档
README
A lightweight PHP event handling system.
This system allows you to set events and event handlers, fire the events and control the event flow. This is a great way to enabled developers to add event hooks to software.
Note: This is not a replacement for major frameworks like ReactPHP, but more of a supplement tool for other frameworks and situations where event handling would be convenient.
Installation
The recommended way to install Triggr is through composer(https://getcomposer.org/). Type the following command in your shell environment:
php ~/composer.phar require architectnate/triggr-php
Usage
Event Phrase
Using TriggrPHP requires the creation of event phrases. Essentially, an event phrase is an event name with or without a handler name in this format: EventName:HandlerName (or just EventName alone).
These event phrases are used create events. In the case where no handler name is given, one is automatically created. the developer has the option to include a handler name to make what they are doing more clear if they would like.
Firing Events
When bulding out an application you would use the "fire" method to specify where you want actions to occur, such as a pre, or post save method on an Entity. The event phrase contains only the event name, all handlers on that event are fired.
Triggr::fire("EventName");
If you need to provide data to the event to be used by it's handlers, you may pass an array of arguments.
Triggr::fire("EventName", array($arg1, $arg2, $arg3));
In the case that it is needed to fire a specific handler on an event, the fireHandler method may be used. The event phrase this time must have both event name and handler name. Note that arguments are still available to be passed in.
Triggr::fireHandler("EventName:HandlerName", array($arg1));
Watching Events
Once events are firing within your application you can "watch" them and perform actions when they fire. To do this, you will call the "watch" method. Every "watch" creates a handler. If you provide an event name or the full event phrase determines if TriggrPHP assigns a handler name for you, or not. It is not necessary to assign each handler it's own name, but it can be useful for more complex uses.
To simply watch an event, provide a name and an action. The action can be any callable, so class methods and procedural methods are all usable.
// With event name only Triggr::watch("MyEvent", function(){ echo "Event has fired!"; }); // With full event phrase (nameing the handler ourselves) Triggr::watch("MyEvent:FireMessage", function(){ echo "Event has fired!"; });
Watch actions may return data, the "fire" method will pass the data along. Since there may be multiple handlers per event, the returned data is provided in an array, sorted by the order that the handlers were run. Note: "fireHandler" returns mixed data, whatever the "watch" action returns, it returns since there is no potential for multiple handlers.
Triggr::watch("HelloWorld", function(){ return "Hello World"; }); $output = Triggr::fire("HelloWorld"); // Returns array("Hello World")
Options
When watching an event, options can be provided to the handler for a couple of reasons, to limit the number of times it can be run and to set a sort order or priority Defaults: RunLimit = 0, Priority = 100 (A run limit of 0 is infinite).
Triggr::watch("HelloWorld", function(){ return "Hello World"; }, array("RunLimit"=>1, "Priority"=>101));
architectnate/triggr-php 适用场景与选型建议
architectnate/triggr-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「event」 「listener」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 architectnate/triggr-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 architectnate/triggr-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 architectnate/triggr-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Timestamp parameter in public file uri
The Listener component of the RabbitEvents library.
Event dispatcher package
Symfony bundle for broadway/broadway.
Listener to change layout for different module
The event package provides functionality for notifying a list of observers about an event.
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-09-23