2016-09-27 2 views
0

다음 코드 그것은 TransactionCreated 이벤트를 전달하고 클래스 자체와 클래스의 속성을 설정하기 위해 호출 얻을 onCreatedNewTransaction 기능에 의해 잡힐 가정심포니하는 EventDispatcher 가입자는

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'); 
    } 
} 

일을 해달라고.

Transaction 클래스는

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

처럼 인스턴스화하지만 난이 프로젝트를 디버깅 할 때 $Transaction 객체는 null 값이 있습니다. 나는 breakpointonCreateNewTransaction 방법으로 설정 했으므로 함수가 호출되지 않는다는 것을 알았다.

는 업데이트]

문제

`를 해결 onCreateNewTransaction '대신 개인 귀하의 방법 CreateNewTransaction가 정적 인

+0

뭔가가 누락되었지만이 시나리오에서 이벤트가 필요한 이유는 무엇입니까? 생성자에서 이러한 속성을 할당하는 것이 더 바람직하지 않습니까? 그 외에도 EventDispatcher를 생성자에 인스턴스화하는 대신 삽입해야하므로 고정 종속성을 생성하게됩니다. –

답변

2

, 그래서 Transaction의 인스턴스가 만들어지지되고 따라서 __constructor는 결코의 공개한다 라는.

이것은이 코드가 작동하지 않는 이유입니다.

하지만, 나는 Symfony의 Event 시스템의 총 오용이라고 말해야합니다. 프레임 워크 (EventDispatcher 구성 요소 없음)를 사용하면 직접 EventDispatcher를 만들지 않아야합니다. 그것은 FrameworkBundle에 의해 만들어진 beeing이고 당신이 필요로 하는대로 event_dispatcher 서비스를 주입해야합니다.

그렇지 않으면 다른 범위로 매우 빠르게 손실 될 수 있습니다 (각 디스패처는 자신의 가입자와 자체 이벤트가 있습니다). 게다가 리소스가 낭비됩니다.

+0

내 디버깅 세션에 대한 첫 번째 관심사에 대해 반증합니다. 보호 된 __constructor는 적절한 모든 데이터가 주입되도록 호출됩니다. 두 번째 관심사에 대해 컴포지션 루트 (일명 부트 스트랩)에서 단일 EventDiaspatcer 인스턴스를 생성하고 필요한 곳에 주입합니다. 이 'new'는 생성자에서 읽기 전용으로 만 사용할 수 있습니다. – dios231

+0

어쨌든'$ Transaction = Transaction :: CreateNewTransaction ('6/6/2016', 'John'....);과 같은 트랜잭션을 만들면 아무 것도 정적 메서드에서이 이벤트를 발생시킵니다. 그리고'Transaction' 인스턴스를 생성 할 때 새로운 트랜잭션이 만들어 지지만 null 값 - 디버거에서 볼 수 있습니다. –

+0

이벤트 디스패처를 만들 때 합법적이라고 지적하고 싶습니다. 나머지 프레임 워크 리스너와 청취자를 분리하는 데 실제로 도움이 될 수 있습니다. 그러니 완전히 배제하지 마십시오. – Cerad

관련 문제