Symfony / EventDispatcher COMPONENTS

Introduction

For a long time, to ensure the flexibility of object-oriented programming code. By creating well-organized and clear division of class, your code becomes more flexible, while other developers can extend them with subclasses and modify the behavior of the base class. But if someone else wants to have their own sub-class developer "Share this change", then the code will no longer apply inherited.
Thinking a real world example, you have to provide a plug-in system in your project. A plug-in to be able to add a method, or other method is executed prior to "do something" and not to interfere with other plug-ins. This is not a simple problem can be solved single inheritance, and multiple inheritance can become even PHP, or there will be a lot less points.
The EventDispatcher Symfony components in a simple and efficient way to achieve the Mediator design pattern, so that all these things become possible, even more of your project with a strong flexibility.

Detail view their own official document: Symfony / at The EventDispatcher the Component

General Event Listener

use Symfony\Component\EventDispatcher\EventDispatcher;
// 实例化派遣器
$dispatcher = new EventDispatcher();
// 添加监听事件
$dispatcher->addListener('order.creater', function($event, $eventName, $dispatcher){
    echo "订单号是:{$event->orderNumber()}\n";
});

$dispatcher->addListener('order.creater', function($event, $eventName, $dispatcher){
    echo "有一个新的订单被创建\n";
}, 10); // 第三个参数是优先级

$dispatcher->addListener('order.pay', function($event, $eventName, $dispatcher){
    echo "订单已经付款\n";
});

// 创建事件
// use Symfony\Component\EventDispatcher\Event; // @deprecated since Symfony 4.3, use "Symfony\Contracts\EventDispatcher\Event" instead
use Symfony\Contracts\EventDispatcher\Event;
class OrderEvent extends Event
{  
    protected $order;
    
    public function __construct($order)
    {
        $this->order = $order;
    }
    public function orderNumber()
    {
        return $this->order;
    }
}

// 派遣事件
$dispatcher->dispatch(new OrderEvent('1234567890'), 'order.creater');
$dispatcher->dispatch(new OrderEvent('1234567890'), 'order.pay');

// 输出
# 有一个新的订单被创建
# 订单号是:1234567890
# 订单已经付款

Use event subscriber

namespace Acme\Store\Event;
 
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Acme\Store\Event\OrderPlacedEvent;

// 创建一个订阅器,必须继承 EventSubscriberInterface 接口
class StoreSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::RESPONSE => array(
                array('onKernelResponsePre', 10),
                array('onKernelResponsePost', -10),
            ),
            OrderEvent::NAME => 'onStoreOrder',
        );
    }
 
    public function onKernelResponsePre(FilterResponseEvent $event)
    {
        // ...
    }
 
    public function onKernelResponsePost(FilterResponseEvent $event)
    {
        // ...
    }
 
    public function onStoreOrder(OrderEvent $event)
    {
        echo $event->orderNumber();
    }
}

// 创建事件
use Symfony\Contracts\EventDispatcher\Event;
class OrderEvent extends Event
{  
    const NAME = 'order.create';
    
    protected $order;
    
    public function __construct($order)
    {
        $this->order = $order;
    }
    public function orderNumber()
    {
        return $this->order;
    }
}

// 实例化派遣器
$dispatcher = new EventDispatcher();
// 添加事件订阅器
$dispatcher->addSubscriber(new StoreSubscriber());
// 派遣事件
$dispatcher->dispatch(new OrderEvent('1234567890'), OrderEvent::NAME);

// 输出:
# 1234567890
Published 41 original articles · won praise 21 · views 70000 +

Guess you like

Origin blog.csdn.net/u010324331/article/details/93974231