You are here

function _restws_determine_router_item in RESTful Web Services 7.2

Reroute requests that come from *.{format} paths. For example /node/2.json will need a correct page callback to be treated as a restws request.

Also, the restws_basic_auth module will need to use this function as well to perform this again after logging a user in.

2 calls to _restws_determine_router_item()
restws_basic_auth_init in restws_basic_auth/restws_basic_auth.module
Implements hook_init().
restws_init in ./restws.module
Implements hook_init().

File

./restws.module, line 216
RESTful web services module.

Code

function _restws_determine_router_item() {

  // Determine the position of the resource and resource id in the path.
  if (strpos(request_path(), '.') === FALSE) {
    return;
  }
  $menu_paths = array();
  foreach (restws_get_resource_info() as $resource => $info) {
    $menu_paths[] = isset($info['menu_path']) ? $info['menu_path'] : $resource;
  }
  $formats = array_keys(restws_get_format_info());

  // The pattern matches menu paths like 'node', 'user' followed by an ID.
  // This ID cannot start with a 0 but can contain any digit.
  $pattern = '#^((?:';
  $pattern .= implode('|', $menu_paths);
  $pattern .= ')\\/[1-9][0-9]*)\\.(?:';

  // The path will end with a format that is supported by restws, for example
  // 'json' or 'xml'.
  $pattern .= implode('|', $formats);
  $pattern .= ')$#i';

  // Replace pattern precisely once.
  $count = 0;
  $path = preg_replace($pattern, '\\1', request_path(), 1, $count);

  // When the pattern matches and there is no menu router for the request
  // path, substitute this module's page callback.
  if ($count && !menu_get_item()) {
    $router_item = menu_get_item($path);
    menu_set_item(NULL, $router_item);
  }
}