You are here

protected function Handler::doRequest in JSON-RPC 2.x

Same name and namespace in other branches
  1. 8 src/Handler.php \Drupal\jsonrpc\Handler::doRequest()

Executes an RPC call and returns a JSON-RPC response.

Parameters

\Drupal\jsonrpc\Object\Request $request: The JSON-RPC request.

Return value

\Drupal\jsonrpc\Object\Response|null The JSON-RPC response.

1 call to Handler::doRequest()
Handler::batch in src/Handler.php
Executes a batch of remote procedure calls.

File

src/Handler.php, line 112

Class

Handler
Manages all the JSON-RPC business logic.

Namespace

Drupal\jsonrpc

Code

protected function doRequest(Request $request) {

  // Helper closure to handle eventual exceptions.
  $handle_exception = function ($e, Request $request) {
    if (!$e instanceof JsonRpcException) {
      $id = $request
        ->isNotification() ? FALSE : $request
        ->id();
      $e = JsonRpcException::fromPrevious($e, $id);
    }
    return $e
      ->getResponse();
  };
  try {
    $context = new RenderContext();
    $result = $this->renderer
      ->executeInRenderContext($context, function () use ($request) {
      return $this
        ->doExecution($request);
    });
    if ($request
      ->isNotification()) {
      return NULL;
    }
    $rpc_response = $result instanceof Response ? $result : new Response(static::SUPPORTED_VERSION, $request
      ->id(), $result);
    $methodPluginClass = $this
      ->getMethod($request
      ->getMethod())
      ->getClass();
    $result_schema = call_user_func([
      $methodPluginClass,
      'outputSchema',
    ]);
    $rpc_response
      ->setResultSchema($result_schema);
    $response_headers = $this
      ->getMethod($request
      ->getMethod())->responseHeaders;
    $rpc_response
      ->getHeaders()
      ->add($response_headers);
    if (!$context
      ->isEmpty()) {

      /** @var \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata */
      $bubbleable_metadata = $context
        ->pop();
      $rpc_response
        ->addCacheableDependency($bubbleable_metadata);
      if ($rpc_response instanceof AttachmentsInterface) {
        $rpc_response
          ->addAttachments($bubbleable_metadata
          ->getAttachments());
      }
    }
    return $rpc_response;
  } catch (\Throwable $e) {
    return $handle_exception($e, $request);
  } catch (\Exception $e) {
    return $handle_exception($e, $request);
  }
}