【问题标题】:Symfony EventDispatcher subscriber dont workSymfony EventDispatcher 订阅者不工作
【发布时间】:2016-09-27 09:42:10
【问题描述】:

以下代码

use Application\Events\TransactionCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;

class Transaction implements EventSubscriberInterface
{
    protected $date;
    protected $name;
    protected $address;
    protected $phone;
    protected $price_with_vat;
    protected $transaction_type;
    protected $receipt;
    protected $currency;


    protected function __construct($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency)
    {
        $dispatcher = new EventDispatcher();
        $dispatcher->addSubscriber($this);
        $dispatcher->dispatch(TransactionCreatedEvent::NAME, new TransactionCreatedEvent($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency));
    }

    public static function CreateNewTransaction($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency){
        return new Transaction($date, $name, $address, $phone, $price_with_vat, $transaction_type, $receipt, $currency);
    }

    private function onCreateNewTransaction($Event){
        $this->date = $Event->date;
        $this->name = $Event->name;
        $this->address = $Event->address;
        $this->phone = $Event->phone;
        $this->price_with_vat = $Event->price_with_vat;
        $this->transaction_type = $Event->transaction_type;
        $this->receipt = $Event->receipt;
        $this->currency = $Event->currency;
    }

    public static function getSubscribedEvents()
    {
        return array(TransactionCreatedEvent::NAME => 'onCreateNewTransaction');
    }
}

它假设调度一个TransactionCreated 事件并被类本身捕获并调用onCreatedNewTransaction 函数以设置类的属性。

Transaction 类的实例化如下

$Transaction = Transaction::CreateNewTransaction('6/6/2016', 'John'....);

但是当我调试项目时,$Transaction 对象具有null 值。我在onCreateNewTransaction 方法上设置了一个breakpoint,我发现因此函数没有被调用。

更新

问题解决了

`onCreateNewTransaction' 应该是公开的而不是私有的

【问题讨论】:

  • 我可能遗漏了一些东西,但为什么在这种情况下需要事件?在构造函数中分配这些属性不是更有意义吗?除此之外,您应该注入 EventDispatcher 而不是在构造函数中实例化它,这样您就可以创建固定的依赖关系。

标签: php symfony mediator event-dispatching


【解决方案1】:

您的方法 CreateNewTransaction 是静态的,因此不会创建 Transaction 的实例,因此永远不会调用 __constructor

这是关于为什么这段代码不起作用的原因。

但是,除此之外,我必须说这是对 Symfony 的Event 系统的完全滥用。使用框架(没有EventDispatcher 组件),您不能自己创建EventDispatcher。它是由 FrameworkBundle 创建的,你应该只将event_dispatcher 服务注入到你需要的任何东西中。

否则,您可能会很快迷失在不同的范围内(每个调度程序都有自己的订阅者和自己的事件),此外,这是一种资源浪费。

【讨论】:

  • 关于您的第一个问题,我的调试会话证明相反。受保护的 __constructor 被调用并注入所有适当的数据。关于您的第二个问题,我在我的组合根(又名引导程序)中实例化了一个 EventDiaspatcer,并在需要的地方注入。这个 new 在构造函数中仅用于清晰的目的。
  • 无论如何,当你创建像这样$Transaction = Transaction::CreateNewTransaction('6/6/2016', 'John'....); 的事务时,没有人会在你的静态方法中触发这个事件。当您创建实例 Transaction 时,正在创建新事务,但具有空值 - 您在调试器中看到
  • 只是想指出,有时创建自己的事件调度程序是合法的。它实际上可以帮助将您的侦听器与其他框架侦听器隔离开来。所以不要完全排除。
  • 经过大量调试,我终于让它工作了。但是我的域模型现在有一个 Dispatcher 依赖项(所有模型都注入了相同的对象)。你们认为这是一个糟糕的设计吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 2018-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
相关资源
最近更新 更多