Wiki source code of Події життєвого циклу
Last modified by Ashterix on 2024/07/11 12:46
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
5.1 | 1 | {{box cssClass="floatinginfobox" title="**Contents**"}} |
![]() |
1.1 | 2 | {{toc/}} |
3 | {{/box}} | ||
4 | |||
5 | {{info}} | ||
![]() |
5.1 | 6 | Events were added to JsonRpcBundle starting from version 7.0. |
7 | Now the components of the RPC server lifecycle operate through events, and you have the opportunity to subscribe to them to make its operation more flexible. | ||
![]() |
1.1 | 8 | {{/info}} |
9 | |||
![]() |
4.1 | 10 | {{warning}} |
![]() |
5.1 | 11 | Using events requires certain knowledge and skills from you; certain actions during event subscription can lead to unexpected results, so you use events at your own risk. |
![]() |
4.1 | 12 | {{/warning}} |
13 | |||
![]() |
5.1 | 14 | == List of Events == |
![]() |
1.1 | 15 | |
![]() |
5.1 | 16 | During its lifecycle, the RPC server uses its own events, which are based on [[Symfony Event>>https://symfony.com/doc/current/event_dispatcher.html]] |
![]() |
1.1 | 17 | |
18 | (% id="event_table" %) | ||
![]() |
5.1 | 19 | |= Event |= Description | |
![]() |
4.1 | 20 | |((( |
21 | **rpc.request** | ||
![]() |
1.1 | 22 | |
![]() |
4.1 | 23 | {{{class RpcRequestEvent}}} |
24 | )))|((( | ||
![]() |
5.1 | 25 | Occurs when an RPC request is received. Use this event to check or modify the request before processing it. |
![]() |
4.1 | 26 | )))| |
27 | |((( | ||
28 | **rpc.pre_execute** | ||
29 | |||
30 | {{{class RpcPreExecuteEvent}}} | ||
31 | )))|((( | ||
![]() |
5.1 | 32 | Occurs before executing the RPC method. Use this event to prepare or modify calls before their execution. |
![]() |
4.1 | 33 | )))| |
34 | |((( | ||
35 | **rpc.post_execute** | ||
36 | |||
37 | {{{class RpcPostExecuteEvent}}} | ||
![]() |
5.1 | 38 | )))|Occurs after executing the RPC method. You can use this event to process results or perform actions after the method call completes. | |
![]() |
4.1 | 39 | |((( |
40 | **rpc.response** | ||
41 | |||
42 | {{{class RpcResponseEvent}}} | ||
![]() |
5.1 | 43 | )))|Occurs before sending the RPC response. Use this event to check or modify responses before sending them to the client. | |
![]() |
4.1 | 44 | |((( |
45 | **rpc.error** | ||
46 | |||
47 | {{{class RpcErrorEvent}}} | ||
![]() |
5.1 | 48 | )))|Occurs when an error occurs while processing an RPC request. You can use it to handle errors or perform actions in case of an error. | |
![]() |
4.1 | 49 | |((( |
50 | **rpc.async_request** | ||
51 | |||
52 | {{{class RpcAsyncRequestEvent}}} | ||
![]() |
5.1 | 53 | )))|Occurs when an asynchronous RPC request is received. Use it to process or modify asynchronous requests before their execution. | |
![]() |
4.1 | 54 | |((( |
55 | **rpc.async_output** | ||
56 | |||
57 | {{{class RpcAsyncOutputEvent}}} | ||
![]() |
5.1 | 58 | )))|Occurs when an asynchronous response is sent. You can use it to check or modify asynchronous responses before sending them. | |
![]() |
4.1 | 59 | |
![]() |
5.1 | 60 | == Subscribing to Events Using Symfony Listeners == |
![]() |
1.1 | 61 | |
![]() |
5.1 | 62 | To subscribe to these events, you can create Symfony listeners. A basic listener example looks like this: |
![]() |
1.1 | 63 | |
![]() |
5.1 | 64 | {{code language="php" layout="LINENUMBERS" title="Example of a custom event subscriber"}} |
![]() |
1.1 | 65 | <?php |
66 | |||
67 | namespace App\EventListener; | ||
68 | |||
69 | use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
70 | use Ufo\JsonRpcBundle\Event\RpcRequestEvent; | ||
![]() |
4.1 | 71 | use Ufo\JsonRpcBundle\Event\RpcEvent; |
![]() |
1.1 | 72 | |
![]() |
4.1 | 73 | #[AsEventListener(event: RpcEvent::REQUEST, method: 'onRpcRequest')] |
![]() |
1.1 | 74 | class RpcEventListener |
75 | { | ||
76 | public function onRpcRequest(RpcRequestEvent $event): void | ||
77 | { | ||
![]() |
4.1 | 78 | // your own event handler |
![]() |
1.1 | 79 | } |
80 | } | ||
81 | {{/code}} | ||
82 | |||
![]() |
5.1 | 83 | After creating the listener, it will be automatically registered thanks to the attributes. For more detailed information, see the Symfony documentation on events: [[Symfony Event System>>https://symfony.com/doc/current/event_dispatcher.html]] |
![]() |
1.1 | 84 | |
![]() |
5.1 | 85 | This will allow your listener to subscribe to events and perform the necessary actions during the RPC server's lifecycle. |