Maison > Article > développement back-end > Symfony2 EventDispatcher组件,eventdispatcher_PHP教程
<span>1</span> <span>use Symfony\Component\EventDispatcher\EventDispatcher; </span><span>2</span> <span>3</span> $dispatcher = <span>new</span> EventDispatcher();关联监听器 把监听器添加到dispatcher上,监听特定的事件,那么该事件被调度的时候,dispatcher就会通知监听器工作了。dispatcher使用addListener方法把一个监听器(PHP callable)添加到一个事件上。
<span>1</span> $listener = <span>new</span><span> AcmeListener(); </span><span>2</span> $dispatcher->addListener(<span>'</span><span>foo.action</span><span>'</span>, array($listener, <span>'</span><span>onFooAction</span><span>'</span>));addListener方法有三个参数: * 监听器需要监听是的事件的名称; * 监听器(一个PHP callable); * 一个可选的表示优先级的整数(数值越高优先级越高,监听器就会更早的被触发),默认为0,如果优先级一样,那么谁先添加就先触发;
PHP callable是指能作为参数传入call_user_func()或者传入is_callable()函数执行后返回true的PHP 变量。PHP callable可以是 \Closure实例,一个实现了__invoke方法的对象,或者是表示一个函数的字符串,或者一个表示对象方法或者类方法的数组。 到目前为止,我们看过把一个PHP对象作为监听器,我们也可以把Closure对象作为监听器。 <pre class="code"><span>1</span> <span>use Symfony\Component\EventDispatcher\Event; </span><span>2</span> <span>3</span> $dispatcher->addListener(<span>'</span><span>foo.action</span><span>'</span>, function (Event $<span>event</span><span>) { </span><span>4</span> <span>//</span><span> will be executed when the foo.action event is dispatched</span> <span>5</span> });在上面的例子中,foo.action事件被调度,dispatcher就调用AcmeListener::onFooAction方法,并把Event对象作为唯一的参数传入方法中。
<span> 1</span> <span>use Symfony\Component\EventDispatcher\Event; </span><span> 2</span> <span> 3</span> <span>class</span><span> AcmeListener </span><span> 4</span> <span>{ </span><span> 5</span> <span>//</span><span> ...</span> <span> 6</span> <span> 7</span> <span>public</span> function onFooAction(Event $<span>event</span><span>) </span><span> 8</span> <span> { </span><span> 9</span> <span>//</span><span> ... do something</span> <span>10</span> <span> } </span><span>11</span> }
在实际使用中,都是传入一个特定的Event子类的对象到监听器,例如FilterResponseEvent:
<span>1</span> <span>use Symfony\Component\HttpKernel\Event\FilterResponseEvent; </span><span>2</span> <span>3</span> <span>public</span> function onKernelResponse(FilterResponseEvent $<span>event</span><span>) </span><span>4</span> <span>{ </span><span>5</span> $response = $<span>event</span>-><span>getResponse(); </span><span>6</span> $request = $<span>event</span>-><span>getRequest(); </span><span>7</span> <span>8</span> <span>//</span><span> ...</span> <span>9</span> }创建和调度事件 除了系统内置的事件,我们也可以创建和调度自定义的事件。这是很有好处的,当我们使用第三方类库的时,还有可以使不同的组件之间解耦,使系统更灵活健壮。 静态的Events类 假如我们要创建一个事件——store.order——当订单被创建的时候就会被触发。
<span>namespace</span><span> Acme\StoreBundle; final </span><span>class</span><span> StoreEvents { </span><span>/*</span><span>* * The store.order event is thrown each time an order is created * in the system. * * The event listener receives an * Acme\StoreBundle\Event\FilterOrderEvent instance. * * @var string </span><span>*/</span> <span>const</span> STORE_ORDER = <span>'</span><span>store.order</span><span>'</span><span>; }</span>这个类并没有什么方法,也不做什么操作,只是定义了事件名称,方便管理和组织事件。监听这个事件的监听器都会被传入一个FilterOrderEvent对象。 创建一个Event对象 接着,当你调度这个新的事件的时候,会创建一个Event对象传如到dispatcher的dispatch()方法,dispatcher就把这个Event对象传给所有的监听该事件的监听器。如果我们不需要向监听器传入任何信息,那么可以使用系统默认的Symfony\Component\EventDispatcher\Event 类。然而,很多时候,我们都需要传入特定的信息到监听器,那么我们可以创建一个类继承Symfony\Component\EventDispatcher\Event。 例如,我们需要在所有的监听器中传入order对象:
<span> 1</span> <span>namespace</span><span> Acme\StoreBundle\Event; </span><span> 2</span> <span> 3</span> <span>use Symfony\Component\EventDispatcher\Event; </span><span> 4</span> <span>use Acme\StoreBundle\Order; </span><span> 5</span> <span> 6</span> <span>class</span><span> FilterOrderEvent extends Event </span><span> 7</span> <span>{ </span><span> 8</span> <span>protected</span><span> $order; </span><span> 9</span> <span>10</span> <span>public</span><span> function __construct(Order $order) </span><span>11</span> <span> { </span><span>12</span> $<span>this</span>->order =<span> $order; </span><span>13</span> <span> } </span><span>14</span> <span>15</span> <span>public</span><span> function getOrder() </span><span>16</span> <span> { </span><span>17</span> <span>return</span> $<span>this</span>-><span>order; </span><span>18</span> <span> } </span><span>19</span> }所有监听器都可以通过FilterOrderEvent的getOrder方法获得order对象。 调度事件 dispatcher的dispatch()方法通知监听给定的事件的所有监听器,有两个参数,一个是需要调度的事件名,另一个就是传给所有监听器的Event对象。
<span> 1</span> <span>use Acme\StoreBundle\StoreEvents; </span><span> 2</span> <span>use Acme\StoreBundle\Order; </span><span> 3</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span> 4</span> <span> 5</span> <span>//</span><span> the order is somehow created or retrieved</span> <span> 6</span> $order = <span>new</span><span> Order(); </span><span> 7</span> <span>//</span><span> ... </span><span> 8</span> <span> 9</span> <span>//</span><span> create the FilterOrderEvent and dispatch it</span> <span>10</span> $<span>event</span> = <span>new</span><span> FilterOrderEvent($order); </span><span>11</span> $dispatcher->dispatch(StoreEvents::STORE_ORDER, $<span>event</span>);
FilterOrderEvent对象作为参数传入到dispatch方法,现在,任何监听store.order事件的监听器都会接收到FilterOrderEvent对象,并通过调用getOrder方法获得order对象。
<span>1</span> <span>//</span><span> some listener class that's been registered for "store.order" event</span> <span>2</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span>3</span> <span>4</span> <span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) </span><span>5</span> <span>{ </span><span>6</span> $order = $<span>event</span>-><span>getOrder(); </span><span>7</span> <span>//</span><span> do something to or with the order</span> <span>8</span> }Event Subscribers 最普遍的监听事件的方法是注册一个监听器到dispatcher中,一个监听器可以监听一个或者多个事件。 还有另一种监听事件的方法是使用Event SubScriber,Event SubScriber是一个PHP类,能够准确的告诉dispatcher它订阅了那些事件。实现EventSubscriberInterface接口,该接口有一个静态的方法getSubscriberdEvents。
<span>namespace</span><span> Acme\StoreBundle\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; </span><span>class</span><span> StoreSubscriber implements EventSubscriberInterface { </span><span>public</span> <span>static</span><span> function getSubscribedEvents() { </span><span>return</span><span> array( </span><span>'</span><span>kernel.response</span><span>'</span> =><span> array( array(</span><span>'</span><span>onKernelResponsePre</span><span>'</span>, <span>10</span><span>), array(</span><span>'</span><span>onKernelResponseMid</span><span>'</span>, <span>5</span><span>), array(</span><span>'</span><span>onKernelResponsePost</span><span>'</span>, <span>0</span><span>), ), </span><span>'</span><span>store.order</span><span>'</span> => array(<span>'</span><span>onStoreOrder</span><span>'</span>, <span>0</span><span>), ); } </span><span>public</span> function onKernelResponsePre(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onKernelResponseMid(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onKernelResponsePost(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } }</span>
这个监听器类很简单,告诉了dispatcher监听了什么事件,还有监听的事件触发的方法。addSubscriber()方法把subscriber注册到dispatcher。
<span>1</span> <span>use Acme\StoreBundle\Event\StoreSubscriber; </span><span>2</span> <span>3</span> $subscriber = <span>new</span><span> StoreSubscriber(); </span><span>4</span> $dispatcher->addSubscriber($subscriber);dispatcher准确的把Subscriber注册到EventSubscriberInterface::getSubscriberdEvents()返回的事件里,EventSubscriberInterface::getSubscriberdEvents()方法返回一个数组,数组的键对应Subscriber监听的事件,值对应这Subscriber处理该事件调用的一个方法或者一组方法。上面的例子中,一组监听器的方法对应这一个事件,同时我们也可以设置优先级来控制这组方法的执行先后顺序。当kernel.response事件被触发,
<code>onKernelResponsePre
, <code>onKernelResponseMid
, 和 <code><span>onKernelResponsePost三个方法就会先后执行。</span>
停止事件的传递
在一些情况下,监听器可以停止事件传递下去,防止后续的监听器被调用,换句话说,监听器必须通知dispatcher停止传递事件给后续的监听器。在监听器里面实现stopPropagation()方法:
<span>1</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span>2</span> <span>3</span> <span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) </span><span>4</span> <span>{ </span><span>5</span> <span>//</span><span> ...</span> <span>6</span> <span>7</span> $<span>event</span>-><span>stopPropagation(); </span><span>8</span> }那么,监听了store.order事件的还没有执行的监听器就不会在被执行。 通过isPropagationStopped()方法可以判断一个事件是否被停止。
<span>1</span> $dispatcher->dispatch(<span>'</span><span>foo.event</span><span>'</span>, $<span>event</span><span>); </span><span>2</span> <span>if</span> ($<span>event</span>-><span>isPropagationStopped()) { </span><span>3</span> <span>//</span><span> ...</span> <span>4</span> }
EventDispatcher这个就是as3里面事件发送者,这个也是相对as2增加一个很好的功能,只有继承自这个类的对象才可以发送事件,dispatchEvent()就是发送事件的方法。比如你新建了一个对象A,你想让这个对象发生了某些变化后,通知别的对象,就可以用 dispatchEvent(new Event("yourEvent"))然后你在别的地方调用到A地方就可以对A加一个侦听器
A.addEventListener("yourEvent",yourfunction) 这个事件可以自定义,一般的对象都是EventDispatcher的子类的;
下面的网址有官方比较详细的说明可以去参考一下;help.adobe.com/...r.html
有两种方法1.通过id直接调用另一个组件中接收值的对象,对其赋值!ex: