Acts as intermediate request forwarder for resource plugins.
Expanded class hierarchy of RequestHandler
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;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContainerAware:: |
protected | property | @api | |
ContainerAware:: |
public | function |
Sets the Container associated with this Controller. Overrides ContainerAwareInterface:: |
|
RequestHandler:: |
public | function | Handles a web API request. |