class RequestHandler

Acts as intermediate request forwarder for resource plugins.

Hierarchy

Expanded class hierarchy of RequestHandler

File

drupal/core/modules/rest/lib/Drupal/rest/RequestHandler.php, line 18
Definition of Drupal\rest\RequestHandler.

Namespace

Drupal\rest
View source
class RequestHandler extends ContainerAware {

  /**
   * Handles a web API request.
   *
   * @param string $plugin
   *   The resource type plugin.
   * @param Symfony\Component\HttpFoundation\Request $request
   *   The HTTP request object.
   * @param mixed $id
   *   The resource ID.
   *
   * @todo Remove $plugin as argument. After http://drupal.org/node/1793520 is
   *   committed we would be able to access route object as
   *   $request->attributes->get('_route'). Then we will get plugin as
   *   '_plugin' property of route object.
   */
  public function handle($plugin, Request $request, $id = NULL) {
    $method = strtolower($request
      ->getMethod());
    $resource = $this->container
      ->get('plugin.manager.rest')
      ->getInstance(array(
      'id' => $plugin,
    ));
    $received = $request
      ->getContent();

    // @todo De-serialization should happen here if the request is supposed
    // to carry incoming data.
    try {
      $response = $resource
        ->{$method}($id, $received);
    } catch (HttpException $e) {
      return new Response($e
        ->getMessage(), $e
        ->getStatusCode(), $e
        ->getHeaders());
    }
    $data = $response
      ->getResponseData();
    if ($data != NULL) {

      // Serialize the response data.
      $serializer = $this->container
        ->get('serializer');

      // @todo Replace the format here with something we get from the HTTP
      //   Accept headers. See http://drupal.org/node/1833440
      $output = $serializer
        ->serialize($data, 'drupal_jsonld');
      $response
        ->setContent($output);
      $response->headers
        ->set('Content-Type', 'application/vnd.drupal.ld+json');
    }
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerAware::$container protected property @api
ContainerAware::setContainer public function Sets the Container associated with this Controller. Overrides ContainerAwareInterface::setContainer
RequestHandler::handle public function Handles a web API request.