joomla-x/event
Composer 安装命令:
composer require joomla-x/event
包简介
Joomla Event Package
关键字:
README 文档
README
The event package provides foundations to build event systems and an implementation supporting prioritized listeners.
Events
Example
An event has a name and can transport arguments.
namespace MyApp; use Joomla\Event\Event; // Creating an Event called "onSomething". $event = new Event('onSomething'); // Adding an argument named "foo" with value "bar". $event->addArgument('foo', 'bar'); // Setting the "foo" argument with a new value. $event->setArgument('foo', new \stdClass); // Getting the "foo" argument value. $foo = $event->getArgument('foo');
Its propagation can be stopped
$event->stop();
Event Listeners
An event listener can listen to one or more Events.
You can create two types of listeners : using a class or a closure (anonymous function).
The functions MUST take an EventInterface (or children) as unique parameter.
Classes
The listener listens to events having names matching its method names.
namespace MyApp; use Joomla\Event\EventInterface; /** * A listener listening to content manipulation events. */ class ContentListener { /** * Listens to the onBeforeContentSave event. */ public function onBeforeContentSave(EventInterface $event) { // Do something with the event, you might want to inspect its arguments. } /** * Listens to the onAfterContentSave event. */ public function onAfterContentSave(EventInterface $event) { } }
Closures
Closures can listen to any Event, it must be declared when adding them to the Dispatcher (see below).
namespace MyApp; use Joomla\Event\EventInterface; $listener = function (EventInterface $event) { // Do something with the event, you might want to inspect its arguments. };
The Dispatcher
The Dispatcher is the central point of the Event system, it manages the registration of Events, listeners and the triggering of Events.
Registering Object Listeners
Following the example above, you can register the ContentListener to the dispatcher :
namespace MyApp; use Joomla\Event\Dispatcher; // Creating a dispatcher. $dispatcher = new Dispatcher; /** * Adding the ContentListener to the Dispatcher. * By default, it will be registered to all events matching it's method names. * So, it will be registered to the onBeforeContentSave and onAfterContentSave events. */ $dispatcher->addListener(new ContentListener);
If the object contains other methods that should not be registered, you will need to explicitly list the events to be registered. For example:
$dispatcher->addListener( new ContentListener, array( 'onBeforeContentSave' => Priority::NORMAL, 'onAfterContentSave' => Priority::NORMAL, ) ); // Alternatively, include a helper method: $listener = new ContentListener; $dispatcher->addListener($listener, $listener->getEvents());
Registering Closure Listeners
namespace MyApp; use Joomla\Event\Dispatcher; use Joomla\Event\Priority; // Of course, it shouldn't be empty. $listener = function (EventInterface $event) { }; $dispatcher = new Dispatcher; /** * Adding a Closure Listener to the Dispatcher. * You must specify the event name and the priority of the listener. * Here, we register it for the onContentSave event with a normal Priority. */ $dispatcher->addListener( $listener, array('onContentSave' => Priority::NORMAL) );
As you noticed, it is possible to specify a listener's priority for a given Event. It is also possible to do so with "object" Listeners.
Filtering Listeners
DEPRECATED
Listeners class can become quite complex, and may support public methods other than those required for event handling. The setListenerFilter method can be used to set a regular expression that is used to check the method names of objects being added as listeners.
// Ensure the dispatcher only registers "on*" methods. $dispatcher->setListenerFilter('^on');
Registration with Priority
namespace MyApp; use Joomla\Event\Dispatcher; use Joomla\Event\Priority; /** * Adding the ContentListener to the Dispatcher. * It will be registered with a high priority for the onBeforeContentSave, and * an "Above normal" priority for the onAfterContentSave event. */ $dispatcher->addListener( new ContentListener, array( 'onBeforeContentSave' => Priority::HIGH, 'onAfterContentSave' => Priority::ABOVE_NORMAL ) );
The default priority is the Priority::NORMAL.
When you add an "object" Listener without specifying the event names, it is registered with a NORMAL priority to all events.
/** * Here, it won't be registered to the onAfterContentSave event because * it is not specified. * * If you specify a priority for an Event, * then you must specify the priority for all Events. * * It is good pracctice to do so, it will avoid to register the listener * to "useless" events and by consequence save a bit of memory. */ $dispatcher->addListener( new ContentListener, array('onBeforeContentSave' => Priority::NORMAL) );
If some listeners have the same priority for a given event, they will be called in the order they were added to the Dispatcher.
Registering Events
You can register Events to the Dispatcher, if you need custom ones.
namespace MyApp; use Joomla\Event\Dispatcher; use Joomla\Event\Event; // Creating an event with a "foo" argument. $event = new Event('onBeforeContentSave'); $event->setArgument('foo', 'bar'); // Registering the event to the Dispatcher. $dispatcher = new Dispatcher; $dispatcher->addEvent($event);
By default, an Event object is created with no arguments, when triggering the Event.
Triggering Events
Once you registered your listeners (and eventually events to the Dispatcher), you can trigger the events.
The listeners will be called in a queue according to their priority for that Event.
// Triggering the onAfterSomething Event. $dispatcher->triggerEvent('onAfterSomething');
If you registered an Event object having the onAfterSomething name, then it will be passed to all listeners instead of the default one.
You can also pass a custom Event when triggering it
namespace MyApp; use Joomla\Event\Dispatcher; use Joomla\Event\Event; // Creating an event called "onAfterSomething" with a "foo" argument. $event = new Event('onAfterSomething'); $event->setArgument('foo', 'bar'); $dispatcher = new Dispatcher; // Triggering the onAfterSomething Event. $dispatcher->triggerEvent($event);
If you already added an Event with the onAfterSomething name using addEvent, then the event passed to the triggerEvent method will be chosen instead.
Stopping the Propagation
As said above, you can stop the Event propagation if you are listening to an Event supporting it, it is the case for the Event class.
namespace MyApp; use Joomla\Event\Event; class ContentListener { public function onBeforeContentSave(Event $event) { // Stopping the Event propagation. $event->stop(); } }
When stopping the Event propagation, the next listeners in the queue won't be called.
Observable classes
Observable classes depend on a Dispatcher, and they may implement the DispatcherAwareInterface interface.
Example of a Model class :
namespace MyApp; use Joomla\Event\DispatcherAwareInterface; use Joomla\Event\DispatcherInterface; use Joomla\Event\Event; class ContentModel implements DispatcherAwareInterface { const ON_BEFORE_SAVE_EVENT = 'onBeforeSaveEvent'; const ON_AFTER_SAVE_EVENT = 'onAfterSaveEvent'; /** * The underlying dispatcher. * * @var DispatcherInterface */ protected $dispatcher; public function save() { $this->dispatcher->triggerEvent(self::ON_BEFORE_SAVE_EVENT); // Perform the saving. $this->dispatcher->triggerEvent(self::ON_AFTER_SAVE_EVENT); } /** * Set the dispatcher to use. * * @param DispatcherInterface $dispatcher The dispatcher to use. * * @return DispatcherAwareInterface This method is chainable. */ public function setDispatcher(DispatcherInterface $dispatcher) { $this->dispatcher = $dispatcher; } }
Immutable Events
An immutable event cannot be modified after its instantiation:
- its arguments cannot be modified
- its propagation can't be stopped
It is useful when you don't want the listeners to manipulate it (they can only inspect it).
namespace MyApp; use Joomla\Event\EventImmutable; // Creating an immutable event called onSomething with an argument "foo" with value "bar" $event = new EventImmutable('onSomething', array('foo' => 'bar'));
The Delegating Dispatcher
A dispatcher that delegates its method to an other Dispatcher. It is an easy way to achieve immutability for a Dispatcher.
namespace MyApp; use Joomla\Event\DelegatingDispatcher; use Joomla\Event\Dispatcher; $dispatcher = new Dispatcher; // Here you add you listeners and your events.... // Instanciating a delegating dispatcher. $delegatingDispatcher = new DelegatingDispatcher($dispatcher); // Now you inject this dispatcher in your system, and it has only the triggerEvent method.
This is useful when you want to make sure that 3rd party applications, won't register or remove listeners from the Dispatcher.
Installation via Composer
Add "joomla-x/event": "3.0.*@dev" to the require block in your composer.json and then run composer install.
{
"require": {
"joomla-x/event": "3.0.*@dev"
}
}
Alternatively, you can simply run the following from the command line:
composer require joomla-x/event "3.0.*@dev"
joomla-x/event 适用场景与选型建议
joomla-x/event 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 240 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 08 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「event」 「joomla」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 joomla-x/event 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joomla-x/event 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 joomla-x/event 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
Symfony bundle for broadway/broadway.
Joomla Preload Package
This is an Event Emitter equivalent to Node.js' Event Emitter.
Publish / Subscribe / Event Manager
A PHP library to implement the observer pattern
统计信息
- 总下载量: 240
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2017-08-17