You are here

rest_server.module in Services 6.3

Same filename and directory in other branches
  1. 7.3 servers/rest_server/rest_server.module

File

servers/rest_server/rest_server.module
View source
<?php

/**
 * Implements hook_server_info().
 */
function rest_server_server_info() {
  return array(
    'name' => 'REST',
    'path' => 'rest',
    'settings' => array(
      'file' => array(
        'inc',
        'rest_server',
      ),
      'form' => '_rest_server_settings',
      'submit' => '_rest_server_settings_submit',
    ),
  );
}
function rest_server_autoload_info() {
  module_load_include('inc', 'rest_server');
  return _rest_server_autoload_info();
}

/**
 * Implementation of hook_enable().
 */
function rest_server_enable() {

  // Flush the autoload caches so that our classes are registered
  autoload_flush_caches();
}

/**
 * Starting point of the REST server.
 *
 * @return type
 */
function rest_server_server() {
  $endpoint_path = services_get_server_info('endpoint_path', 'services/rest');
  $endpoint_path_len = drupal_strlen($endpoint_path) + 1;
  $canonical_path = drupal_substr($_GET['q'], $endpoint_path_len, drupal_strlen($_GET['q']) - $endpoint_path_len);
  if (empty($canonical_path)) {
    return;
  }
  try {
    $server = new RESTServer();
    return $server
      ->handle($canonical_path, $endpoint_path);
  } catch (Exception $e) {
    $server
      ->handleException($e);
  }
}

/**
 * Builds a list of request parsers that are available to the RESTServer.
 *
 * @return array
 *  An associative array of parser callbacks keyed by mime-type.
 */
function rest_server_request_parsers() {
  static $parsers = NULL;
  if (!$parsers) {
    $parsers = array(
      'application/x-www-form-urlencoded' => 'RESTServer::parseURLEncoded',
      'application/json' => 'RESTServer::parseJSON',
      'application/vnd.php.serialized' => 'RESTServer::parsePHP',
      'multipart/form-data' => 'RESTServer::parseMultipart',
    );
    if (_rest_server_get_spyc_location() !== false) {
      $parsers['application/x-yaml'] = 'RESTServer::parseYAML';
    }
    drupal_alter('rest_server_request_parsers', $parsers);
  }
  return $parsers;
}

/**
 * Builds a list of response formatters that are available to the RESTServer.
 *
 * @return array
 *  An associative array of formatter info arrays keyed by type extension. The
 *  formatter info specifies an array of 'mime types' that corresponds to the
 *  output format; a 'view' class that is a subclass of RESTServerView; and
 *  'view arguments' that should be passed to the view when it is created;
 *  a 'model' can also be specified which the controller then must declare
 *  support for to be able to serve data in that format.
 */
function rest_server_response_formatters() {
  static $formatters = NULL;
  if (!$formatters) {
    $formatters = array(
      'xml' => array(
        'mime types' => array(
          'application/xml',
          'text/xml',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'xml',
        ),
      ),
      'json' => array(
        'mime types' => array(
          'application/json',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'json',
        ),
      ),
      'jsonp' => array(
        'mime types' => array(
          'text/javascript',
          'application/javascript',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'jsonp',
        ),
      ),
      'php' => array(
        'mime types' => array(
          'application/vnd.php.serialized',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'php',
        ),
      ),
      'bencode' => array(
        'mime types' => array(
          'application/x-bencode',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'bencode',
        ),
      ),
      'rss' => array(
        'model' => 'ResourceFeedModel',
        'mime types' => array(
          'text/xml',
        ),
        'view' => 'RssFormatView',
      ),
    );
    if (_rest_server_get_spyc_location() !== false) {
      $formatters['yaml'] = array(
        'mime types' => array(
          'text/plain',
          'application/x-yaml',
          'text/yaml',
        ),
        'view' => 'RESTServerViewBuiltIn',
        'view arguments' => array(
          'format' => 'yaml',
        ),
      );
    }
    drupal_alter('rest_server_response_formatters', $formatters);
  }
  return $formatters;
}

/**
 * Set up settings for a rest server endpoint, fills the settings
 * array with defaults. This is done to ensure that the default state
 * is consistent between what's shown by default in the settings form
 * and used by default by the REST server if it hasn't been configured.
 *
 * @param array $settings
 * @return array
 *  The standardized settings array.
 */
function rest_server_setup_settings($settings = array()) {

  // Apply defaults
  $settings = $settings + array(
    'formatters' => array(
      'jsonp' => FALSE,
    ),
    'parsers' => array(
      'application/x-www-form-urlencoded' => FALSE,
    ),
  );

  // Get all available parsers and formatters.
  $parsers = rest_server_request_parsers();
  $formatters = rest_server_response_formatters();
  _rest_server_add_default_and_remove_unknown($settings['parsers'], array_keys($parsers), TRUE);
  _rest_server_add_default_and_remove_unknown($settings['formatters'], array_keys($formatters), TRUE);
  return $settings;
}

/**
 * Utility function set set up an array with default values for a set
 * of keys and remove all entries that does not match a key in the set.
 *
 * @param array $array
 *  The array to modify.
 * @param array $keys
 *  An array of keys.
 * @param mixed $default
 *  A default value.
 * @return void
 */
function _rest_server_add_default_and_remove_unknown(&$array, $keys, $default) {

  // Add default values to all keys that do not
  // exist in $array but exist in $keys.
  foreach ($keys as $k) {
    if (!isset($array[$k])) {
      $array[$k] = $default;
    }
  }

  // Unset all values that key exist in $array
  // but does not exist in $keys.
  foreach (array_keys($array) as $key) {
    if (!in_array($key, $keys)) {
      unset($array[$key]);
    }
  }
}

/**
 * Implements hook_services_resources_alter().
 */
function rest_server_services_resources_alter(&$resources, $endpoint) {

  // Set the default models for the retrieve and index controllers in the node
  // resource if they are not already set.
  if (isset($resources['node'])) {
    if (isset($resources['node']['retrieve'])) {
      if (!isset($resources['node']['retrieve']['models'])) {
        $resources['node']['retrieve']['models'] = array();
      }
      $resources['node']['retrieve']['models'] += array(
        'ResourceFeedModel' => array(
          'class' => 'NodeResourceFeedModel',
          'arguments' => array(
            'source' => 'single',
          ),
        ),
      );
    }
    if (isset($resources['node']['index'])) {
      if (!isset($resources['node']['index']['models'])) {
        $resources['node']['index']['models'] = array();
      }
      $resources['node']['index']['models'] += array(
        'ResourceFeedModel' => array(
          'class' => 'NodeResourceFeedModel',
        ),
        'ResourceTimeFeedModel' => array(
          'class' => 'NodeResourceFeedModel',
        ),
      );
    }
  }
}

/**
 * Return the location of the spyc library, if any.
 *
 * @return the location of the spyc library, if it exists; else return false.
 */
function _rest_server_get_spyc_location() {
  if (function_exists('libraries_get_path')) {
    $libraries_spyc_loc = libraries_get_path('spyc') . '/spyc.php';
    if (file_exists($libraries_spyc_loc)) {
      return $libraries_spyc_loc;
    }
  }

  // Libraries not in use fall back to straight look up.
  $services_spyc_loc = dirname(__FILE__) . '/lib/spyc.php';
  return file_exists($services_spyc_loc) ? $services_spyc_loc : false;
}

Functions

Namesort descending Description
rest_server_autoload_info
rest_server_enable Implementation of hook_enable().
rest_server_request_parsers Builds a list of request parsers that are available to the RESTServer.
rest_server_response_formatters Builds a list of response formatters that are available to the RESTServer.
rest_server_server Starting point of the REST server.
rest_server_server_info Implements hook_server_info().
rest_server_services_resources_alter Implements hook_services_resources_alter().
rest_server_setup_settings Set up settings for a rest server endpoint, fills the settings array with defaults. This is done to ensure that the default state is consistent between what's shown by default in the settings form and used by default by the REST server if it…
_rest_server_add_default_and_remove_unknown Utility function set set up an array with default values for a set of keys and remove all entries that does not match a key in the set.
_rest_server_get_spyc_location Return the location of the spyc library, if any.