docs: rewrite JsonRpcBundle docs, add SDK docs, move them under docs/api
en/docs/api/json-rpc-bundle/functionality/events/post_execute.md
new file +103
ā0
1
+
# rpc.post_execute
2
+
3
+
::::::block class="floatinginfobox floatinginfobox--fit"
4
+
:::::columns border zebra
5
+
::::row
6
+
:::col width="40%"
7
+
**Event name**
8
+
:::
9
+
:::col
10
+
`rpc.post_execute`
11
+
:::
12
+
::::
13
+
::::row
14
+
:::col width="40%"
15
+
**Constant**
16
+
:::
17
+
:::col
18
+
`RpcEvent::POST_EXECUTE`
19
+
:::
20
+
::::
21
+
::::row
22
+
:::col width="40%"
23
+
**Class**
24
+
:::
25
+
:::col
26
+
`RpcPostExecuteEvent`
27
+
:::
28
+
::::
29
+
::::row
30
+
:::col width="40%"
31
+
**Properties**
32
+
:::
33
+
:::col
34
+
`mixed $result`, `RpcRequest $rpcRequest`, `Service $service`, `array $params`
35
+
:::
36
+
::::
37
+
::::row
38
+
:::col width="40%"
39
+
**Built-in listeners**
40
+
:::
41
+
:::col
42
+
none
43
+
:::
44
+
::::
45
+
:::::
46
+
::::::
47
+
48
+
Dispatched once after **successful** method execution. `$result` is what the method returned, `$params` are the arguments after conversion.
49
+
50
+
:::warning
51
+
The event is dispatched **after** `rpc.pre_response`: the response has already been built. Assigning a new `$result` does not change what the client receives. To change the response, use [`rpc.pre_response`](/docs/api/json-rpc-bundle/functionality/events/pre_response).
52
+
:::
53
+
54
+
**Not dispatched** on error (`rpc.error` is dispatched instead), for a cached response and for the receipt of notifications/callback.
55
+
56
+
## Usage examples
57
+
58
+
### Method execution time
59
+
60
+
```php
61
+
final class TimingListener
62
+
{
63
+
private array $started = [];
64
+
65
+
#[AsEventListener(RpcEvent::PRE_EXECUTE, priority: 2000)]
66
+
public function start(RpcPreExecuteEvent $event): void
67
+
{
68
+
$this->started[spl_object_id($event->rpcRequest)] = hrtime(true);
69
+
}
70
+
71
+
#[AsEventListener(RpcEvent::POST_EXECUTE)]
72
+
public function finish(RpcPostExecuteEvent $event): void
73
+
{
74
+
$key = spl_object_id($event->rpcRequest);
75
+
$ms = (hrtime(true) - ($this->started[$key] ?? hrtime(true))) / 1e6;
76
+
unset($this->started[$key]);
77
+
$this->metrics->histogram('rpc_duration_ms', $ms, ['method' => $event->service->name]);
78
+
}
79
+
}
80
+
```
81
+
82
+
### Domain events
83
+
84
+
After a successful `Order.pay`, publish an event to the application bus:
85
+
86
+
```php
87
+
#[AsEventListener(RpcEvent::POST_EXECUTE)]
88
+
final class OrderPaidListener
89
+
{
90
+
public function __construct(private MessageBusInterface $bus) {}
91
+
92
+
public function __invoke(RpcPostExecuteEvent $event): void
93
+
{
94
+
if ($event->service->name === 'Order.pay' && $event->result === true) {
95
+
$this->bus->dispatch(new OrderPaid($event->params['orderId']));
96
+
}
97
+
}
98
+
}
99
+
```
100
+
101
+
:::tip
102
+
Move heavy work that should not delay the client to [`rpc.post_response`](/docs/api/json-rpc-bundle/functionality/events/post_response) ā it is dispatched after the response has been sent.
103
+
:::