SynfonyのEventとEvent Listener
docs
#WIP
なんかいまいち構成要素を掴めていない無いなmrsekut.icon
Listenerとなるclass
2つある
Event Listener
ref
listenするEventが動的に決まる(?)
Event subscriberより柔軟(?) ref
Event Subscriber
ref
EventSubscriberInterfaceを継承して定義する
内部で、複数のlistenerをmethodとして定義できる
listenするEventが静的に決まる(?)
同じeventを異なるsubscriberがlistenすることもある
その場合の順序は数値で優先づけする
code:php
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::EXCEPTION => [
'processException', 10,
'logException', 0,
'notifyException', -10,
],
];
}
Event listenerより再利用しやすい
Eventとなるclass
が、別々に存在する
一般にEvent Listenerというと以下のような構成要素がある
listenする対象
e.g. divタグ
action
eventが起きた時に、起こしたいもの
e.g. 色を変える
listener
上2つを結びつけるもの
listernする対象と、actionを結びつける
event
e.g. clickされた
dispatch
listern対象に、eventを引き起こさせる
こうすることで、listenerがそれを検知して、actionが生じる
table:対応
synfony Redux
event event class
listenする対象 store
action action
listener listener class、subscliber class
dispatch this->eventDispatcher->dispatch dispatch
↓たぶんまちがってる
EventSubscriberInterfaceを継承してListenerを定義する
ここに呼ばれるEventの定義をする
Eventは、getSubscribedEvents()で登録する
code:php
public static function getSubscribedEvents(){
return [
HogeEvents::HOGE => 'hoge'
];
}
以下で呼び出される
code:php
$event = new InteractiveLoginEvent($request, $newToken);
$this->eventDispatcher->dispatch(HogeEvents::HOGE, $event);
#??
なぜ関数ではなくevent listienrを使用する?
わかりにくさが増している気がするが
どういう時に使うのが正解?\
dispatcherって何?
https://symfony.com/doc/3.4/components/event_dispatcher.html
debug方法
https://symfony.com/doc/3.4/event_dispatcher.html#debugging-event-listeners
なんか思ってたのと違うな
eventをlistenしてるlistenerを一覧するだけか
みたいな感じで呼び出すやつ
https://symfony.com/doc/current/components/http_kernel.html
https://symfony.com/doc/current/reference/events.html
https://doc4.ec-cube.net/customize_symfony
eccubeのdocs
何の参考にもならない
DoctrineのEvent
https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
https://symfony.com/doc/current/doctrine/events.html
https://symfony.com/doc/3.4/event_dispatcher.html#creating-an-event-listener
https://symfony.com/doc/3.4/event_dispatcher/before_after_filters.html
https://symfony.com/doc/3.4/event_dispatcher/method_behavior.html
https://symfony.com/doc/current/components/http_kernel.html
https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
https://symfony.com/doc/current/doctrine/events.html
https://symfony.com/doc/current/reference/events.html
https://stackoverflow.com/questions/60541522/functional-testing-events-and-subscribers-in-symfony-4
https://engineering.otobank.co.jp/entry/2021/12/11/091909
https://stackoverflow.com/questions/32506647/symfony2-event-listener-breaks-debug-toolbar-on-login-page
https://github.com/symfony/event-dispatcher/blob/5.4/Tests/Debug/TraceableEventDispatcherTest.php