You are here

rest_server.inc in Services 6.3

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

Autoload classes and server settings.

File

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

/**
 * @file
 * Autoload classes and server settings.
 */

/**
 * REST server settings form. Generates the form fragment for configuring the REST server
 * for an endpoint.
 *
 * @param array $form
 *  The form fragment from services that we should add our fields to.
 * @param object $endpoint
 *  The endpoint that we're configuring the REST server for.
 * @param array $settings
 *  The current settings.
 * @return void
 */
function _rest_server_settings(&$form, $endpoint, $settings) {
  $settings = rest_server_setup_settings($settings);
  $form['formatters'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Response formatters'),
    '#required' => TRUE,
    '#description' => t('Select the response formats you want to enable for the rest server.'),
  ) + _rest_server_settings_checkboxes_attributes($settings['formatters']);
  $form['parsers'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Request parsing'),
    '#required' => TRUE,
    '#description' => t('Select the request parser types you want to enable for the rest server.'),
  ) + _rest_server_settings_checkboxes_attributes($settings['parsers']);
}

/**
 * Utility function that creates attributes for a checkboxes-type form
 * element from a rest server settings array.
 *
 * @param array $settings
 * @return array
 */
function _rest_server_settings_checkboxes_attributes($settings) {
  $keys = array_keys($settings);
  $options = array_combine($keys, $keys);
  $default = array();
  foreach ($settings as $key => $enabled) {
    if ($enabled) {
      $default[] = $key;
    }
  }
  ksort($options);
  return array(
    '#options' => $options,
    '#default_value' => $default,
  );
}

/**
 * Submit handler for the services REST server settings form.
 *
 * @param object $endpoint
 *  The endpoint that's being configured.
 * @param array $values
 *  The partial form-state from services.
 * @return array
 *  The settings for the REST server in this endpoint.
 */
function _rest_server_settings_submit($endpoint, $values) {
  $values['formatters'] = array_map('_rest_server_settings_not_zero', $values['formatters']);
  $values['parsers'] = array_map('_rest_server_settings_not_zero', $values['parsers']);
  return $values;
}

/**
 * Utility function intended for use with array_map to change everything that
 * isn't === 0 into TRUE.
 *
 * @param string $value
 *  The value to map.
 * @return bool
 *  FALSE if the $value is === 0 otherwise TRUE is returned.
 */
function _rest_server_settings_not_zero($value) {
  return $value !== 0;
}

/**
 * Implementation of hook_autoload_info().
 */
function _rest_server_autoload_info() {
  return array(
    'RESTServer' => array(
      'file' => 'includes/RESTServer.inc',
    ),
    'RESTServerViewBuiltIn' => array(
      'file' => 'includes/rest_server.views.inc',
    ),
    'RESTServerView' => array(
      'file' => 'includes/rest_server.views.inc',
    ),
    'RssFormatView' => array(
      'file' => 'formats/RssFormatView.inc',
    ),
    'ResourceFeedModel' => array(
      'file' => 'includes/models.inc',
    ),
    'ResourceFeedModelItem' => array(
      'file' => 'includes/models.inc',
    ),
    'ResourceTimeFeedModel' => array(
      'file' => 'includes/models.inc',
    ),
    'ResourceTimeFeedModelItem' => array(
      'file' => 'includes/models.inc',
    ),
    'NodeResourceFeedModel' => array(
      'file' => 'includes/node_resource.models.inc',
    ),
    'NodeResourceViewFeedModel' => array(
      'file' => 'includes/node_resource.models.inc',
    ),
  );
}

Functions

Namesort descending Description
_rest_server_autoload_info Implementation of hook_autoload_info().
_rest_server_settings REST server settings form. Generates the form fragment for configuring the REST server for an endpoint.
_rest_server_settings_checkboxes_attributes Utility function that creates attributes for a checkboxes-type form element from a rest server settings array.
_rest_server_settings_not_zero Utility function intended for use with array_map to change everything that isn't === 0 into TRUE.
_rest_server_settings_submit Submit handler for the services REST server settings form.