このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
psr:psr14 [2020/07/28 11:07] tanaka [PSR-14: Event Dispatcher] |
psr:psr14 [2020/09/01 11:54] (現在) tanaka [PSR-14: Event Dispatcher] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | > 編集中 | ||
- | |||
====== PSR-14: Event Dispatcher ====== | ====== PSR-14: Event Dispatcher ====== | ||
--- // | --- // | ||
- | 本章は、若干の補足を加筆してはいるものの単に[[https:// | + | 本章は、若干の補足を加筆してはいるものの単に[[https:// |
関連記事 | 関連記事 | ||
行 21: | 行 19: | ||
* PSR-14: Event Dispatcher - イベントディスパッチャー | * PSR-14: Event Dispatcher - イベントディスパッチャー | ||
* [[psr: | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
+ | * [[psr: | ||
----- | ----- | ||
行 86: | 行 88: | ||
===== リスナー ===== | ===== リスナー ===== | ||
- | |||
- | A Listener may be any PHP callable. A Listener MUST have one and only one parameter, which is the Event to which it responds. Listeners SHOULD type hint that parameter as specifically as is relevant for their use case; that is, a Listener MAY type hint against an interface to indicate it is compatible with any Event type that implements that interface, or to a specific implementation of that interface. | ||
リスナーは、任意のPHP callableです。リスナーは、パラメーターを1つだけ持つ必要があります( '' | リスナーは、任意のPHP callableです。リスナーは、パラメーターを1つだけ持つ必要があります( '' | ||
- | |||
- | A Listener SHOULD have a void return, and SHOULD type hint that return explicitly. A Dispatcher MUST ignore return values from Listeners. | ||
リスナーの戻り値は '' | リスナーの戻り値は '' | ||
- | A Listener MAY delegate actions to other code. That includes a Listener being a thin wrapper around an object that runs the actual business logic. | + | リスナーは他のコードにアクションを委任することができます( '' |
- | + | ||
- | リスナーは他のコードにアクションを委任してもよい( '' | + | |
- | A Listener MAY enqueue information from the Event for later processing by a secondary process, using cron, a queue server, or similar techniques. It MAY serialize the Event object itself to do so; however, care should be taken that not all Event objects may be safely serializable. A secondary process MUST assume that any changes it makes to an Event object will NOT propagate to other Listeners. | + | リスナーは、cron、キューサーバー、または同様の手法を使用し、二次プロセスによるて後処理のために、イベントから情報をキューに追加する場合があります( '' |
- | リスナーは、cron、キューサーバー、または同様の手法を使用して、セカンダリプロセスによる後処理のためにイベントから情報をキューに追加する場合があります( | + | >'' |
+ | >A Listener | ||
\\ | \\ | ||
===== ディスパッチャー ===== | ===== ディスパッチャー ===== | ||
- | |||
- | A Dispatcher is a service object implementing EventDispatcherInterface. It is responsible for retrieving Listeners from a Listener Provider for the Event dispatched, and invoking each Listener with that Event. | ||
ディスパッチャーは、'' | ディスパッチャーは、'' | ||
- | |||
- | A Dispatcher: | ||
ディスパッチャーは: | ディスパッチャーは: | ||
- | | + | * ListenerProvider から返された順番で、リスナーを同期的に呼び出さなければなりません( '' |
- | * MUST return the same Event object it was passed after it is done invoking Listeners. | + | |
- | * MUST NOT return to the Emitter until all Listeners have executed. | + | |
- | + | ||
- | | + | |
* リスナーの呼び出し完了後に渡されたものと同じイベントオブジェクトを返さなければなりません( '' | * リスナーの呼び出し完了後に渡されたものと同じイベントオブジェクトを返さなければなりません( '' | ||
- | * すべてのリスナーが実行されるまで、エミッターに戻らないでください( '' | + | * すべてのリスナーが実行されるまで、エミッターに戻ってはいけません( '' |
- | + | ||
- | If passed a Stoppable Event, a Dispatcher | + | |
停止可能なイベントが渡された場合、ディスパッチャーは: | 停止可能なイベントが渡された場合、ディスパッチャーは: | ||
- | * MUST call isPropagationStopped() | + | * そのイベントで各リスナーを呼び出す前に、'' |
- | * そのイベントで各リスナーを呼び出す前に、'' | + | ディスパッチャーは、リスナープロバイダーから返される全てのリスナーがタイプセーフであると仮定すべきです( '' |
- | + | ||
- | A Dispatcher SHOULD assume that any Listener returned to it from a Listener Provider is type-safe. That is, the Dispatcher SHOULD assume that calling $listener($event) will not produce a TypeError. | + | |
- | + | ||
- | ディスパッチャーは、リスナープロバイダーから返されるすべてのリスナーがタイプセーフであると仮定すべきです( '' | + | |
\\ | \\ | ||
行 139: | 行 122: | ||
==== エラー処理 ==== | ==== エラー処理 ==== | ||
- | An Exception or Error thrown by a Listener MUST block the execution of any further Listeners. An Exception or Error thrown by a Listener MUST be allowed to propagate back up to the Emitter. | + | リスナーによってスローされた例外またはエラーは、それ以降のリスナーの実行をブロックする必要があります( '' |
- | + | ||
- | リスナーによってスローされた例外またはエラーは、それ以降のリスナーの実行をブロックする必要があります( '' | + | |
- | + | ||
- | A Dispatcher MAY catch a thrown object to log it, allow additional action to be taken, etc., but then MUST rethrow the original throwable. | + | |
- | ディスパッチャーは、スローされたオブジェクトをキャッチしてログに記録したり、追加のアクションを実行したりできますが( '' | + | ディスパッチャーは、スローされたオブジェクトをキャッチしてログに記録したり、追加のアクションを実行したりできますが( '' |
\\ | \\ | ||
行 151: | 行 130: | ||
===== リスナープロバイダー ===== | ===== リスナープロバイダー ===== | ||
- | A Listener Provider is a service object responsible for determining what Listeners are relevant to and should be called for a given Event. It may determine both what Listeners are relevant and the order in which to return them by whatever means it chooses. That MAY include: | + | リスナープロバイダーは、与えられたイベントに対してどのリスナーが関連し、どのリスナーを呼び出すべきかを決定することに責任があるサービスオブジェクトです。それは、関連するリスナーと、選択した手段によるそれらリスナーを返すべき順序、の両方を決定するかもしれません。それには以下の場合が含まれています( '' |
- | リスナープロバイダーは、与えられたイベントに対してどのリスナーが関連し、どのリスナーを呼び出すべきかを決定することに責任があるサービスオブジェクトです。それは、どのリスナーが関連しているか、そしてそれが選択するどんな手段によってもそれらを返す順序の両方を決定するかもしれません。それには以下の場合が含まれています( '' | + | * 実装者がイベントにリスナーを固定された順序で割り当てることができるように、ある種の登録メカニズムを可能にします。\\ \\ |
+ | * イベントのタイプと実装されたインターフェースに基づいたリフレクションにより、該当するリスナーのリストを導き出します。\\ \\ | ||
+ | * 前もって実行時に参照される可能性のあるリスナーの編集済みリストを生成します。\\ \\ | ||
+ | * 現在のユーザーが特定の権限を持っている場合に特定のリスナーだけが呼び出されるのように、ある種のアクセス制御を実装します。\\ \\ | ||
+ | * エンティティのような、イベントによって参照されるオブジェクトから情報を抽出し、そのオブジェクトで事前定義されたライフサイクルメソッドを呼び出します。\\ \\ | ||
+ | * いくつかの任意のロジックを使用して、その責任を1つ以上の他のリスナープロバイダーに委任します。 | ||
- | * Allowing for some form of registration mechanism so that implementers may assign a Listener to an Event in a fixed order. | + | 上記の任意の組み合わせ、または他のメカニズムが、必要に応じて使用できます( '' |
- | * Deriving a list of applicable Listeners through reflection based on the type and implemented interfaces of the Event. | + | |
- | * Generating a compiled list of Listeners ahead of time that may be consulted at runtime. | + | |
- | * Implementing some form of access control so that certain Listeners will only be called if the current user has a certain permission. | + | |
- | * Extracting some information from an object referenced by the Event, such as an Entity, and calling pre-defined lifecycle methods on that object. | + | |
- | * Delegating its responsibility to one or more other Listener Providers using some arbitrary logic. | + | |
- | * 実装者がイベントにリスナーを固定された順序で割り当てることができるように、ある種の登録メカニズムを可能にします。 | ||
- | * イベントのタイプと実装されたインターフェースに基づいたリフレクションにより、該当するリスナーのリストを導き出します。 | ||
- | * 前もって実行時に参照される可能性のあるリスナーの編集済みリストを生成します。 | ||
- | * 現在のユーザーが特定の権限を持っている場合に特定のリスナーが呼び出されるだけのよう、ある種のアクセス制御を実装します。 | ||
- | * エンティティなどの、イベントによって参照されるオブジェクトから情報を抽出し、そのオブジェクトで事前定義されたライフサイクルメソッドを呼び出します。 | ||
- | * 任意のロジックを使用して、その責任を1つ以上の他のリスナープロバイダーに委任します。 | ||
- | Any combination of the above, or other mechanisms, | + | リスナープロバイダーは、イベントのクラス名を使用して、そのイベントと他のイベントを区別すべきです( '' |
- | 上記の任意の組み合わせ、または他のメカニズムは、必要に応じて使用できます( '' | + | リスナープロバイダーは、リスナーの適用性を決定するときに、親のタイプをイベント自体のタイプと同じように取り扱う必要があります( '' |
- | + | ||
- | Listener Providers SHOULD use the class name of an Event to differentiate one event from another. They MAY also consider any other information on the event as appropriate. | + | |
- | + | ||
- | リスナープロバイダーは、イベントのクラス名を使用して、イベントを区別する必要があります( '' | + | |
- | + | ||
- | Listener Providers MUST treat parent types identically to the Event’s own type when determining listener applicability. In the following case: | + | |
- | + | ||
- | リスナープロバイダーは、リスナーの適用可能性を決定するときに、親のタイプをイベント自体のタイプと同じように取り扱う必要があります( '' | + | |
<code php> | <code php> | ||
行 190: | 行 155: | ||
function listener(A $event): void {}; | function listener(A $event): void {}; | ||
</ | </ | ||
- | |||
- | A Listener Provider MUST treat listener() as an applicable listener for $b, as it is type compatible, unless some other criteria prevents it from doing so. | ||
リスナープロバイダーは、タイプ互換であるため、他の基準によって妨げられない限り、'' | リスナープロバイダーは、タイプ互換であるため、他の基準によって妨げられない限り、'' | ||
行 198: | 行 161: | ||
===== オブジェクト構成 ===== | ===== オブジェクト構成 ===== | ||
- | |||
- | A Dispatcher SHOULD compose a Listener Provider to determine relevant listeners. It is RECOMMENDED that a Listener Provider be implemented as a distinct object from the Dispatcher but that is NOT REQUIRED. | ||
ディスパッチャーは、関連するリスナーを決定するためにリスナープロバイダーを構成する必要があります( '' | ディスパッチャーは、関連するリスナーを決定するためにリスナープロバイダーを構成する必要があります( '' | ||
行 211: | 行 172: | ||
/** | /** | ||
- | * Defines a dispatcher for events. | ||
* イベントのディスパッチャを定義します。 | * イベントのディスパッチャを定義します。 | ||
*/ | */ | ||
行 217: | 行 177: | ||
{ | { | ||
/** | /** | ||
- | * Provide all relevant listeners with an event to process. | ||
* 処理するイベントをすべての関連リスナーに提供します。 | * 処理するイベントをすべての関連リスナーに提供します。 | ||
* | * | ||
* @param object $event | * @param object $event | ||
- | | ||
| | ||
* | * | ||
* @return object | * @return object | ||
- | * The Event that was passed, now modified by listeners. | + | |
- | * | + | |
*/ | */ | ||
public function dispatch(object $event); | public function dispatch(object $event); | ||
行 236: | 行 193: | ||
/** | /** | ||
- | * Mapper from an event to the listeners that are applicable to that event. | ||
* イベントからそのイベントに適用可能なリスナーへのマッパー。 | * イベントからそのイベントに適用可能なリスナーへのマッパー。 | ||
*/ | */ | ||
行 243: | 行 199: | ||
/** | /** | ||
* @param object $event | * @param object $event | ||
- | | ||
| | ||
* @return iterable< | * @return iterable< | ||
- | | + | |
- | | + | |
- | | + | |
- | | + | |
*/ | */ | ||
public function getListenersForEvent(object $event) : iterable; | public function getListenersForEvent(object $event) : iterable; | ||
行 259: | 行 212: | ||
/** | /** | ||
- | * An Event whose processing may be interrupted when the event has been handled. | + | * イベントが操作されたとき、処理が中断される可能性があるイベント。 |
- | * イベントが処理されたときに処理が中断される可能性があるイベント。 | + | * |
+ | * Dispatcherの実装は、各リスナーが呼び出された後にイベントが停止済みとして | ||
+ | * マークされているかどうかを確認する必要があります( MUST )。もしそうであれば、 | ||
+ | * それ以上リスナーを呼び出さずにすぐに戻る必要があります。 | ||
* | * | ||
- | * A Dispatcher implementation MUST check to determine if an Event | ||
- | * is marked as stopped after each listener is called. | ||
- | * return immediately without calling any further Listeners. | ||
- | * Dispatcher実装は、各リスナーが呼び出された後にイベントが停止済みとしてマークされているかどうかを | ||
- | * 確認する必要があります( MUST )。そうであれば、それ以上リスナーを呼び出さずにすぐに戻る必要が | ||
- | * あります。 | ||
*/ | */ | ||
interface StoppableEventInterface | interface StoppableEventInterface | ||
{ | { | ||
/** | /** | ||
- | * Is propagation stopped? | + | * 伝播が停止しているか? |
- | * 伝播は停止していますか? | + | |
* | * | ||
- | * This will typically only be used by the Dispatcher to determine if the | + | * これは通常、前のリスナーが伝播を停止したかどうかを判断するために |
- | * previous listener halted propagation. | + | |
- | * これは通常、前のリスナーが伝播を停止したかどうかを判断するためにDispatcherによってのみ | + | |
- | | + | |
* | * | ||
* @return bool | * @return bool | ||
- | | ||
- | | ||
| | ||
| |