public function TripalWebService::getResponse

3.x TripalWebService.inc public TripalWebService::getResponse()

Returns the full web service response.

The response includes both the @context and data sections of the JSON-LD response.

Return value

An associative array containing that can be converted to JSON.

File

tripal_ws/includes/TripalWebService.inc, line 203

Class

TripalWebService

Code

public function getResponse() {
  $class = get_called_class();

  $context_filename = $class::$type . '.' . $this->getVersion(TRUE);
  foreach ($this->path as $element) {
    $context_filename .= '.' . $element;
  }
  $context_filename .= '.json';
  $context_filename = strtolower($context_filename);
  $context_dir = 'public://tripal/ws/context';
  $context_file_path = $context_dir . '/' . $context_filename;

  // Make sure the user directory exists
  if (!file_prepare_directory($context_dir, FILE_CREATE_DIRECTORY)) {
    throw new Exception('Could not access the tripal/ws/context directory on the server for storing web services context files.');
  }

  $context = $this->resource ? $this->resource->getContext() : array();
  $context = array(
    '@context' => $context
  );
  $cfh = fopen($context_file_path, "w");
  if (flock($cfh, LOCK_EX)) {
    fwrite($cfh, json_encode($context));
    flock($cfh, LOCK_UN);
    fclose($cfh);
  }
  else {
    throw new Exception(t('Error locking file: @file.', array('@file' => $context_file_path)));
  }

  $fid = db_select('file_managed', 'fm')
    ->fields('fm', array('fid'))
    ->condition('uri', $context_file_path)
    ->execute()
    ->fetchField();

  // Save the context file so Drupal can manage it and remove the file later.
  if (!$fid) {
    $context_file = new stdClass();
    $context_file->uri = $context_file_path;
    $context_file->filename = $context_filename;
    $context_file->filemime = 'application/ld+json';
    $context_file->uid = 0;
    file_save($context_file);
  }

  $type = $this->resource ? $this->resource->getType() : 'unknown';
  $json_ld = array(
    '@context' => file_create_url($context_file_path),
    '@id' => '',
    '@type' => $type,
  );
  $data = $this->getData();
  return array_merge($json_ld, $data);
}