文档地址: http://symfony.com/doc/current/event_dispatcher.html
讲两者区别的时候:
Listeners or Subscribers
Listeners and subscribers can be used in the same application indistinctly. The decision to use either of them is usually a matter of personal taste. However, there are some minor advantages for each of them:
Subscribers are easier to reuse because the knowledge of the events is kept in the class rather than in the service definition. This is the reason why Symfony uses subscribers internally; Listeners are more flexible because bundles can enable or disable each of them conditionally depending on some configuration value.
看 Symfony 的源码:
HttpKernel -> handleRaw 中,当往 requestStack 中 push 完一个 Request 之后,会通过 EventDispatcherInterface::dispatch('kernel.request', new GetResponseEvent()),来触发所有监听 kernel.request 的 Listener 。
这里有一些疑问,首先 GetResponseEvent 事件对象是事件源( HttpKernel )需要触发 kernel.request 这个事件的时候自己创建的,那监听 kernel.request 的 Listeners 是什么时候注册进去的?那 EventDispatcher 扮演了什么角色?另外发现 HttpKernel 中的 eventListener 是实现的 EventSubscriberInterface 接口,这个 Subscriber 不是订阅者的意思吗?那 Listener 实现 Subscriber ,是说明这两者是同一个概念?
后来搜了设计模式,发现还有观察者模式
事件源 /事件 /事件监听 /事件分发,发布 /订阅,观察者,这些概念之间是个什么关系?发现网上各种解释都有,都是一家之言,有的甚至还产生混淆。。。
求解~~~