function restful_delivery in RESTful 7.2
Same name and namespace in other branches
- 7 restful.module \restful_delivery()
Returns data in JSON format.
We do not use drupal_json_output(), in order to maintain the "Content-Type" header.
Parameters
mixed $var: (optional) If set, the variable will be converted to JSON and output.
See also
restful_menu_process_callback()
2 calls to restful_delivery()
- restful_deliver_menu_not_found in ./
restful.module - Delivers a not found (404) error.
- restful_menu_access_callback in ./
restful.module - Access callback; Determine access for an API call.
1 string reference to 'restful_delivery'
- restful_menu in ./
restful.module - Implements hook_menu().
File
- ./
restful.module, line 305
Code
function restful_delivery($var = NULL) {
if (!isset($var)) {
return;
}
$request = restful()
->getRequest();
$response = restful()
->getResponse();
if (!empty($var['status'])) {
$response
->setStatusCode($var['status']);
}
if (is_int($var)) {
_restful_get_data_from_menu_status($var);
if (!empty($var['status'])) {
$response
->setStatusCode($var['status']);
}
try {
// Adhere to the API Problem draft proposal.
$formatter_id = variable_get('restful_default_output_formatter', 'json');
// Get the data in the default output format.
$data = restful()
->getFormatterManager()
->negotiateFormatter(NULL, $formatter_id)
->format($var);
$response
->setContent($data);
$response
->prepare($request);
$response
->send();
} catch (RestfulException $e) {
// If there is an exception during delivery, just JSON encode this.
$output = _restful_build_http_api_error($e, $response);
$response
->setStatusCode($e
->getCode());
$response
->setContent(drupal_json_encode($output));
$response
->send();
return;
}
return;
}
try {
// Get the formatter for the current resource.
$resource = restful()
->getResourceManager()
->negotiate();
// Get a new formatter manager.
$formatter_manager = restful()
->getFormatterManager();
$formatter_manager
->setResource($resource);
$plugin_definition = $resource
->getPluginDefinition();
if ($request
->getMethod() == RequestInterface::METHOD_OPTIONS) {
// There is no guarantee that other formatters can process the
// auto-discovery output correctly.
$formatter_name = 'json';
}
else {
$formatter_name = isset($plugin_definition['formatter']) ? $plugin_definition['formatter'] : NULL;
}
$output = $formatter_manager
->format($var, $formatter_name);
$response
->setContent($output);
} catch (RestfulException $e) {
// Handle if the formatter does not exist.
$output = _restful_build_http_api_error($e, $response);
$response
->setStatusCode($e
->getCode());
$response
->setContent(drupal_json_encode($output));
$response
->send();
return;
}
$response
->prepare($request);
$response
->send();
}