leedavis81/vent
Composer 安装命令:
composer require leedavis81/vent
包简介
PHP variable events.
README 文档
README
PHP variable event system
| Quality / Metrics | Releases | Downloads | Licence |
| ----- | -------- | ------- | ------------- | -------- |
|
|
|
Installation
Install via composer:
php composer.phar require leedavis81/vent:dev-master
Usage
Have you ever needed to hook an event anytime a PHP variable is read? Maybe you want to ensure complete immutability even within the scope (private) of your class. PHP variable events can be easily created by hooking into the read or write of any variable.
<?php class Foo { use Vent\VentTrait; private $bar; public function __construct() { $this->registerEvent('read', 'bar', function(){ throw new \Exception('Don\'t touch my bar!'); }); } public function touchBar() { $this->bar; } } $foo = new Foo(); $foo->touchBar(); // Fatal error: Uncaught exception 'Exception' with message 'Don't touch my bar!'
Or you can register a write event to protect the variable from being overwritten (even from within the scope of your class)
$this->registerEvent('write', 'bar', function(){ throw new \Exception('Don\'t write to my bar!'); }); public function writeToBar() { $this->bar = 'somethingElse'; } $foo = new Foo(); $foo->writeToBar(); // Fatal error: Uncaught exception 'Exception' with message 'Don't write to my bar!'
You can masquerade any value by returning something from your registered event. Note that if multiple events are registered, execution they will stop once one of them returns a response. They are triggered in the order they're registered (first in, first out).
public $bar = 'Bill'; public function __construct() { $this->registerEvent('read', 'bar', function(){ return 'Ben'; }); } $foo = new Foo(); echo $foo->bar; // "Ben"
To pass in parameters into your method simply provide them when registering the event.
Please note there are two reserved strings that if passed in as a parameter will be replaced
_OVL_NAME_replaced with the name of the variable you're accessing (set or get)_OVL_VALUE_replaced with the value your updating a variable with (set only)_CUR_VALUE_the current value of the variable
public $bar = 'chips'; public function __construct() { $this->registerEvent('write', 'bar', function($var1, $name, $value, $cur_value){ echo 'Why ' . $var1 . ' you\'re trying to overwrite "' . $name . '", which contained "' . $cur_value . '" to contain "' . $value . '"'; }, ['bill', '_OVL_NAME_', '_OVL_VALUE_', '_CUR_VALUE_']); } $foo = new Foo(); $foo->bar = 'cheese'; // Why bill you're trying to overwrite "bar", which contained "chips" to contain "cheese"
If you are returning a response on your event, this can be retained to prevent additional execution on further reads. If you don't return anything from your action (or return a null) then nothing is retained and additional reads will cause re-execution of your registered action.
public $bar = 'Bill'; public function __construct() { $this->registerEvent('read','bar', function(){ sleep(1); return microtime(); }, null, true); // pass in "true" here (defaults to false) } $foo = new Foo(); var_dump($foo->bar === $foo->bar); // true
All events must be registered within the context of your class when using VentTrait. However, if you'd like to register them from a public or protected scope then simply change the scope of the registerEvent method when importing the trait
<?php class Foo { use Vent\VentTrait {registerEvent as public;} // allow public event registration public $bar; } $foo = new Foo(); $foo->registerEvent('read', 'bar', function(){ throw new \Exception('Don\'t touch my bar!'); }); $foo->bar; // Fatal error: Uncaught exception 'Exception' with message 'Don't touch my bar!'
Supported Events
| Event Name | Alias | Triggered |
|---|---|---|
| read | get | Whenever an attempt to read the variable is performed |
| write | set | Whenever an attempt to write to the variable is performed |
| delete | unset | Whenever unset is called on the variable. Setting to null will not work. |
But you stole my magic
It's true that this little trait applies a __get and __set method to your class. If your class already has a little magic and these methods have already been applied then they'll overwrite the trait implementation. Maybe not what you want. To get around this you can simply import them with a different method name, and call them in your own magic methods. For example:
class Foo() { use Vent\VentTrait {__get as get;} public function __get($name) { // You're own magic stuff will go here. return $this->get($name); // Fire off the vent magic (if you need it) } }
todos
- Allow event triggering for array offset reads $foo->bar['offset'];
- Add a method to remove variables from proxy (to increase perf)
leedavis81/vent 适用场景与选型建议
leedavis81/vent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.66k 次下载、GitHub Stars 达 81, 最近一次更新时间为 2014 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「event」 「variable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 leedavis81/vent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 leedavis81/vent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 leedavis81/vent 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony integration for Czech bank account library
An easy way to parse and manipulate identifier names, such as dynamic method names.
Symfony bundle for broadway/broadway.
Store variables in Twig code which will be available to other templates for the duration of the page load.
This is an Event Emitter equivalent to Node.js' Event Emitter.
Publish / Subscribe / Event Manager
统计信息
- 总下载量: 4.66k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-02-25