You are here

function restful_restful_resource_alter in RESTful 7.2

Implements hook_restful_resource_alter().

Decorate an existing resource with other services (e.g. rate limit and render cache).

File

./restful.module, line 529

Code

function restful_restful_resource_alter(ResourceInterface &$resource) {

  // Disable any plugin in the disabled plugins variable.
  $disabled_plugins = array(
    // Disable the Files Upload resource based on the settings variable.
    'files_upload:1.0' => (bool) (!variable_get('restful_file_upload', FALSE)),
    // Disable the Users resources based on the settings variable.
    'users:1.0' => (bool) (!variable_get('restful_enable_users_resource', TRUE)),
    // Disable the Login Cookie resources based on the settings variable.
    'login_cookie:1.0' => (bool) (!variable_get('restful_enable_user_login_resource', TRUE)),
    // Disable the Discovery resource based on the settings variable.
    'discovery:1.0' => (bool) (!variable_get('restful_enable_discovery_resource', TRUE)),
  ) + variable_get('restful_disabled_plugins', array());
  if (!empty($disabled_plugins[$resource
    ->getResourceName()])) {
    $resource
      ->disable();
  }
  elseif (isset($disabled_plugins[$resource
    ->getResourceName()]) && $disabled_plugins[$resource
    ->getResourceName()] === FALSE && !$resource
    ->isEnabled()) {
    $resource
      ->enable();
  }
  $plugin_definition = $resource
    ->getPluginDefinition();

  // If render cache is enabled for the current resource, or there is no render
  // cache information for the resource but render cache is enabled globally,
  // then decorate the resource with cache capabilities.
  if (!empty($plugin_definition['renderCache']['render']) || !isset($plugin_definition['renderCache']['render']) && variable_get('restful_render_cache', FALSE)) {
    $resource = new CacheDecoratedResource($resource);
  }

  // Check for the rate limit configuration.
  if (!empty($plugin_definition['rateLimit']) || variable_get('restful_global_rate_limit', 0)) {
    $resource = new RateLimitDecoratedResource($resource);
  }

  // Disable the discovery endpoint if it's disabled.
  if ($resource
    ->getResourceMachineName() == 'discovery' && !variable_get('restful_enable_discovery_resource', TRUE) && $resource
    ->isEnabled()) {
    $resource
      ->disable();
  }
}