标签:style blog http color io os 使用 ar for
1 use Symfony\Component\EventDispatcher\EventDispatcher; 2 3 $dispatcher = new EventDispatcher();
1 $listener = new AcmeListener(); 2 $dispatcher->addListener(‘foo.action‘, array($listener, ‘onFooAction‘));
PHP callable是指能作为参数传入call_user_func()或者传入is_callable()函数执行后返回true的PHP 变量。PHP callable可以是 \Closure实例,一个实现了__invoke方法的对象,或者是表示一个函数的字符串,或者一个表示对象方法或者类方法的数组。到目前为止,我们看过把一个PHP对象作为监听器,我们也可以把Closure对象作为监听器。1 use Symfony\Component\EventDispatcher\Event; 2 3 $dispatcher->addListener(‘foo.action‘, function (Event $event) { 4 // will be executed when the foo.action event is dispatched 5 });
1 use Symfony\Component\EventDispatcher\Event; 2 3 class AcmeListener 4 { 5 // ... 6 7 public function onFooAction(Event $event) 8 { 9 // ... do something 10 } 11 }
在实际使用中,都是传入一个特定的Event子类的对象到监听器,例如FilterResponseEvent:
1 use Symfony\Component\HttpKernel\Event\FilterResponseEvent; 2 3 public function onKernelResponse(FilterResponseEvent $event) 4 { 5 $response = $event->getResponse(); 6 $request = $event->getRequest(); 7 8 // ... 9 }
namespace Acme\StoreBundle; final class StoreEvents { /** * 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 */ const STORE_ORDER = ‘store.order‘; }
1 namespace Acme\StoreBundle\Event; 2 3 use Symfony\Component\EventDispatcher\Event; 4 use Acme\StoreBundle\Order; 5 6 class FilterOrderEvent extends Event 7 { 8 protected $order; 9 10 public function __construct(Order $order) 11 { 12 $this->order = $order; 13 } 14 15 public function getOrder() 16 { 17 return $this->order; 18 } 19 }
1 use Acme\StoreBundle\StoreEvents; 2 use Acme\StoreBundle\Order; 3 use Acme\StoreBundle\Event\FilterOrderEvent; 4 5 // the order is somehow created or retrieved 6 $order = new Order(); 7 // ... 8 9 // create the FilterOrderEvent and dispatch it 10 $event = new FilterOrderEvent($order); 11 $dispatcher->dispatch(StoreEvents::STORE_ORDER, $event);
FilterOrderEvent对象作为参数传入到dispatch方法,现在,任何监听store.order事件的监听器都会接收到FilterOrderEvent对象,并通过调用getOrder方法获得order对象。
1 // some listener class that‘s been registered for "store.order" event 2 use Acme\StoreBundle\Event\FilterOrderEvent; 3 4 public function onStoreOrder(FilterOrderEvent $event) 5 { 6 $order = $event->getOrder(); 7 // do something to or with the order 8 }
namespace Acme\StoreBundle\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; class StoreSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents() { return array( ‘kernel.response‘ => array( array(‘onKernelResponsePre‘, 10), array(‘onKernelResponseMid‘, 5), array(‘onKernelResponsePost‘, 0), ), ‘store.order‘ => array(‘onStoreOrder‘, 0), ); } public function onKernelResponsePre(FilterResponseEvent $event) { // ... } public function onKernelResponseMid(FilterResponseEvent $event) { // ... } public function onKernelResponsePost(FilterResponseEvent $event) { // ... } public function onStoreOrder(FilterOrderEvent $event) { // ... } }
这个监听器类很简单,告诉了dispatcher监听了什么事件,还有监听的事件触发的方法。addSubscriber()方法把subscriber注册到dispatcher。
1 use Acme\StoreBundle\Event\StoreSubscriber; 2 3 $subscriber = new StoreSubscriber(); 4 $dispatcher->addSubscriber($subscriber);
onKernelResponsePre
, onKernelResponseMid
, 和 onKernelResponsePost三个方法就会先后执行。
1 use Acme\StoreBundle\Event\FilterOrderEvent; 2 3 public function onStoreOrder(FilterOrderEvent $event) 4 { 5 // ... 6 7 $event->stopPropagation(); 8 }
1 $dispatcher->dispatch(‘foo.event‘, $event); 2 if ($event->isPropagationStopped()) { 3 // ... 4 }
标签:style blog http color io os 使用 ar for
原文地址:http://www.cnblogs.com/szuyuan/p/4015843.html