Wiki source code of Події життєвого циклу
Last modified by Ashterix on 2024/07/11 12:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | {{box cssClass="floatinginfobox" title="**Contents**"}} | ||
2 | {{toc/}} | ||
3 | {{/box}} | ||
4 | |||
5 | {{info}} | ||
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. | ||
8 | {{/info}} | ||
9 | |||
10 | {{warning}} | ||
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. | ||
12 | {{/warning}} | ||
13 | |||
14 | == List of Events == | ||
15 | |||
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]] | ||
17 | |||
18 | (% id="event_table" %) | ||
19 | |= Event |= Description | | ||
20 | |((( | ||
21 | **rpc.request** | ||
22 | |||
23 | {{{class RpcRequestEvent}}} | ||
24 | )))|((( | ||
25 | Occurs when an RPC request is received. Use this event to check or modify the request before processing it. | ||
26 | )))| | ||
27 | |((( | ||
28 | **rpc.pre_execute** | ||
29 | |||
30 | {{{class RpcPreExecuteEvent}}} | ||
31 | )))|((( | ||
32 | Occurs before executing the RPC method. Use this event to prepare or modify calls before their execution. | ||
33 | )))| | ||
34 | |((( | ||
35 | **rpc.post_execute** | ||
36 | |||
37 | {{{class RpcPostExecuteEvent}}} | ||
38 | )))|Occurs after executing the RPC method. You can use this event to process results or perform actions after the method call completes. | | ||
39 | |((( | ||
40 | **rpc.response** | ||
41 | |||
42 | {{{class RpcResponseEvent}}} | ||
43 | )))|Occurs before sending the RPC response. Use this event to check or modify responses before sending them to the client. | | ||
44 | |((( | ||
45 | **rpc.error** | ||
46 | |||
47 | {{{class RpcErrorEvent}}} | ||
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. | | ||
49 | |((( | ||
50 | **rpc.async_request** | ||
51 | |||
52 | {{{class RpcAsyncRequestEvent}}} | ||
53 | )))|Occurs when an asynchronous RPC request is received. Use it to process or modify asynchronous requests before their execution. | | ||
54 | |((( | ||
55 | **rpc.async_output** | ||
56 | |||
57 | {{{class RpcAsyncOutputEvent}}} | ||
58 | )))|Occurs when an asynchronous response is sent. You can use it to check or modify asynchronous responses before sending them. | | ||
59 | |||
60 | == Subscribing to Events Using Symfony Listeners == | ||
61 | |||
62 | To subscribe to these events, you can create Symfony listeners. A basic listener example looks like this: | ||
63 | |||
64 | {{code language="php" layout="LINENUMBERS" title="Example of a custom event subscriber"}} | ||
65 | <?php | ||
66 | |||
67 | namespace App\EventListener; | ||
68 | |||
69 | use Symfony\Component\EventDispatcher\Attribute\AsEventListener; | ||
70 | use Ufo\JsonRpcBundle\Event\RpcRequestEvent; | ||
71 | use Ufo\JsonRpcBundle\Event\RpcEvent; | ||
72 | |||
73 | #[AsEventListener(event: RpcEvent::REQUEST, method: 'onRpcRequest')] | ||
74 | class RpcEventListener | ||
75 | { | ||
76 | public function onRpcRequest(RpcRequestEvent $event): void | ||
77 | { | ||
78 | // your own event handler | ||
79 | } | ||
80 | } | ||
81 | {{/code}} | ||
82 | |||
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]] | ||
84 | |||
85 | This will allow your listener to subscribe to events and perform the necessary actions during the RPC server's lifecycle. |