docs: rewrite JsonRpcBundle docs, add SDK docs, move them under docs/api
en/docs/api/json-rpc-bundle/functionality/events/pre_response.md
new file +95
−0
rpc.pre_response
Event name
rpc.pre_response
Constant
RpcEvent::PRE_RESPONSE
Class
RpcPreResponseEvent
Properties
RpcResponse $response, RpcRequest $rpcRequest, ?Service $service
The response has been built but not sent yet. Dispatched both for a result and for an error (in that case $service is null).
When it is dispatched
- After successful method execution (before
rpc.post_execute). - After an execution error (from
rpc.error). - On receipt of a notification or a request with
$rpc.callback— with a placeholder response (nullor{"async": true, "callback": …}); later, when the method runs, — once more with the real response.
Not dispatched for a cached response.
Built-in listeners
| Priority | Listener | What it does |
|---|---|---|
| 1000 | ParamsConverterEventListener::objectToScalar |
Converts properties of the result object into scalars (enums, dates, UUIDs, entities) and replaces the response |
| -100000 | RpcServer |
Takes $event->rpcRequest->getResponseObject() as the response that goes to the client |
What can be changed
RpcResponse is immutable. To replace the response, set a new one through the request with a priority above -100000:
$event->rpcRequest->setResponse(new RpcResponse($event->rpcRequest->getId(), $newResult));
- Assigning
$event->responsedoes not change the response to the client (but this is exactly whatrpc.post_responsereceives). - Listeners with a priority above 1000 see the raw result object, below 1000 — an already normalized array.
- Replacement has no effect on the notification/callback placeholder.
- Do not stop propagation above -100000: the response will not be picked up.
Usage examples
Masking sensitive data
#[AsEventListener(RpcEvent::PRE_RESPONSE, priority: 500)]
final class MaskEmailListener
{
public function __invoke(RpcPreResponseEvent $event): void
{
$result = $event->response->getResult();
if ($event->response->getError() !== null || !is_array($result) || !isset($result['email'])) {
return;
}
$result['email'] = preg_replace('/(?<=.).(?=[^@]*@)/', '*', $result['email']);
$event->rpcRequest->setResponse(new RpcResponse($event->rpcRequest->getId(), $result));
}
}
Other ideas
- logging responses (taking errors into account:
$event->response->getError()); - adding service fields to the result for a specific API group;
- collecting response size metrics.