Download the PHP package constanze-standard/event-dispatcher without Composer

On this page you can find all versions of the php package constanze-standard/event-dispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package event-dispatcher

Constanze standard event dispatcher

GitHub license Coverage 100%

PSR-14 事件派发与监听系统。

An event dispatching system with PSR-14.

简介

event-dispatcher 是一个事件派发系统。它派发一个事件,并以优先级顺序调用预先定义的事件处理程序。

事件系统由以下5个概念构成:

  1. 事件 (Event): Event 是事件信息的载体,它往往围绕一个动作进行描述,例如 “用户被创建了”、“准备导出 excel 文件” 等等,Event 的内部需要包含当前事件的所有信息,以便后续的处理程序使用。
  2. 监听器 (Listener): Listener 是事件处理程序,负责在发生某一事件(Event)时执行特定的操作。
  3. Listener Provider: 它负责将事件(Event)与监听器(Listener)进行关联,在触发一个事件时,Listener Provider 需要提供绑定在该事件上的所有监听器。
  4. 派发器 (EventDispatcher): 负责通知某一事件发生了。我们所说的“向某一目标派发一个事件”,这里的“目标”指的是 Listener Provider,也就是说,EventDispatcher 向 Listener Provider 派发了 Event。
  5. 订阅器 (Subscriber): 订阅器是 Listener Provider 的扩展,它可以将不同的事件和订阅器里的方法进行自由绑定,这些操作都在订阅器内部进行,这样可以将同类事件的绑定与处理内聚,便于管理。

安装

使用

创建 Event

\ConstanzeStandard\EventDispatcher\Event 基于 \ConstanzeStandard\EventDispatcher\Interfaces\EventInterface,并实现了StoppableEventInterface

\ConstanzeStandard\EventDispatcher\Interfaces\EventInterface::getName 方法返回当前事件的唯一标识,默认为类名称。

定制的 CustomEvent:

如果你的事件需要在网络间传输,你可能还需要实现 Serializable 接口,以便将有用的信息序列化:

定义 Listener

Listener 可以是任意可调用对象,或任意对象的方法,它可以接受一个 \ConstanzeStandard\EventDispatcher\Interfaces\EventInterface 的实例作为唯一参数,Listener 必须返回传入的 event 相同类型的实例对象:

\ConstanzeStandard\EventDispatcher\Event::withPropagationStopped 将返回一个与当前 event 对象相同,但携带了终止信号的 event,如果将它作为返回值,将终止后续的 event 派发。

定义 Listener Provider

Listener Provider 负责确定哪些 listener 与当前派发的事件相关,并将相关的 listener 提供给 dispatcher. 添加一个 Listener:

如上所示,通过 ConstanzeStandard\EventDispatcher\ListenerProvider::addListener 方法绑定关系,第一个参数是事件 id;第二个参数是监听器对象,可以直接传入可调用对象,或形如 [对象, 方法名称] 的数组;第三个参数是优先级,必须为数字,数字越大优先级越高(越先调用),默认优先级为 0。

事件派发

ConstanzeStandard\EventDispatcher\EventDispatcher 在初始化时绑定一个 Listener Provider 实例,然后可以调用 EventDispatcher::dispatch 方法将事件发送给 Listener Provider:

上例会直接触发 $event 事件,并且按优先级调用 $listenerProvider 中绑定到该事件上的所有监听器。

使用订阅器

我们之前利用 ListenerProvider::addListener 添加事件和监听器的关系,这种方式比较过程化,也无法体现出一组事件之间的关系,所以在实践中往往会提出“订阅器”的概念。

订阅器(Subscriber)实际上是对 ListenerProvider::addListener 的一种装饰。使用订阅器需要实现 ConstanzeStandard\EventDispatcher\Interfaces\SubscriberInterface 接口:

ConstanzeStandard\EventDispatcher\Interfaces\SubscriberInterface::subscribe 方法会提供一个订阅器闭包函数,这个函数的第一个参数是事件 id,后续的参数都是当前类中方法的信息,你可以直接指定方法名称,或用一个数组提供方法名称和优先级。

ConstanzeStandard\EventDispatcher\ListenerProvider::addSubscriber 的过程中,$subscriber 函数会将当前类中的方法转化为普通的 Listener.


All versions of event-dispatcher with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
psr/event-dispatcher Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package constanze-standard/event-dispatcher contains the following files

Loading the files please wait ....