You are here

function _services_build_resources in Services 6.3

Same name and namespace in other branches
  1. 7.3 includes/services.resource_build.inc \_services_build_resources()

Builds the resource definition array for a endpoint.

Parameters

string $endpoint_name: Optional. The endpoint name.

Return value

array The resource definitions.

2 calls to _services_build_resources()
services_edit_form_endpoint_resources in plugins/export_ui/services_ctools_export_ui.class.php
services_edit_form_endpoint_resources function.
services_get_resources in ./services.module
Gets all resource definitions.

File

./services.resource_build.inc, line 18
Contains functions necessary for building the resource definitions. This is only needed the first time a the resources for a endpoint are fetched, or when the cache has been cleared.

Code

function _services_build_resources($endpoint_name = '') {
  module_load_include('resource_runtime.inc', 'services');
  $resources = array();
  $version_info = services_resource_api_version_info();
  $resource_modules = module_implements('services_resources');

  // Get all installed resources
  foreach ($resource_modules as $resource_module) {
    $module_resources = call_user_func("{$resource_module}_services_resources");
    if (empty($module_resources)) {
      continue;
    }

    // Check resource versions
    $api_version = $version_info['default_version'];
    if (!empty($module_resources['#api_version'])) {
      $api_version = $module_resources['#api_version'];
      unset($module_resources['#api_version']);
    }

    // Upgrade resources if needed.
    if ($api_version < $version_info['current_version']) {
      _services_resource_upgrade($api_version, $module_resources, $version_info);
    }
    $resources = array_merge_recursive($resources, $module_resources);
  }

  // Load the endpoint
  $endpoint = NULL;
  if (!empty($endpoint_name)) {
    $endpoint = services_endpoint_load($endpoint_name);

    // Apply the endpoint on the services
    _services_apply_endpoint($resources, $endpoint, TRUE);
  }
  drupal_alter('services_resources', $resources, $endpoint);

  // Process the resources, and collect all controllers in the process
  $controllers = array();
  $class_info = services_operation_class_info();
  foreach ($resources as $resource_name => &$resource) {
    foreach ($class_info as $class_name => $class) {
      if (!isset($resource[$class_name])) {
        continue;
      }
      foreach (array_keys($resource[$class_name]) as $action_name) {
        $controllers["{$resource_name}/{$class['class_singular']}/{$action_name}"] =& $resource[$class_name][$action_name];
      }
    }
  }

  // Make sure that we got a access callback for all resources
  foreach ($controllers as &$controller) {
    if (!empty($controller['file'])) {

      // Convert old-style file references to something that fits module_load_include() better.
      if (!empty($controller['file']['file']) && empty($controller['file']['type'])) {
        $controller['file']['type'] = $controller['file']['file'];
      }
    }
    if (!isset($controller['access callback'])) {
      $controller['access callback'] = 'user_access';
    }
  }
  drupal_alter('services_resources_controller_post_processing', $controllers, $endpoint);

  // This hook is deprecated and will be removed in next versions of services.
  // Use hook_services_resources_alter instead.
  drupal_alter('services_resources_post_processing', $resources, $endpoint);

  // Do some endpoint-dependent processing
  if ($endpoint) {

    // Let the authentication modules alter our controllers
    foreach ($endpoint->authentication as $auth_module => $auth_settings) {
      services_auth_invoke($auth_module, 'alter_controllers', $auth_settings, $controllers, $endpoint);
    }

    // Apply any aliases from endpoint
    $aliased = array();
    foreach ($resources as $key => $def) {
      if (!empty($def['endpoint']['alias'])) {
        $aliased[$def['endpoint']['alias']] = $def;
      }
      else {
        $aliased[$key] = $def;
      }
    }
    $resources = $aliased;
  }
  return $resources;
}