From f43a37ed056afb17e79be8e8b36838768246e3e7 Mon Sep 17 00:00:00 2001
From: Taihao Zhang <zhang.taihao@gmail.com>
Date: Wed, 10 Nov 2010 14:42:46 +1100
Subject: [PATCH] Fixed resolver inclusion (see http://github.com/anu-science/uuid_resolver/issues/#issue/1).

---
 uuid_resolver.module |  601 +++++++++++++++++++++++++-------------------------
 1 files changed, 306 insertions(+), 295 deletions(-)

diff --git a/uuid_resolver.module b/uuid_resolver.module
index be57013..d98b085 100644
--- a/uuid_resolver.module
+++ b/uuid_resolver.module
@@ -1,295 +1,306 @@
-<?php
-// $Id$
-/**
- * @file
- * UUID resolver
- * Matches UUIDs using menu callback paths to redirect to entity.
- */
-
-/**
- * Implements hook_init().
- */
-function uuid_resolver_init() {
-  // Load common
-  module_load_include('inc', 'uuid_resolver', 'common');
-  // Load include files
-  $files = file_scan_directory(drupal_get_path('module', 'uuid_resolver') . '/includes', '\.inc$');
-  foreach ($files as $file) {
-    include_once($file->filename);
-  }
-}
-
-/**
- * Implements hook_perm().
- */
-function uuid_resolver_perm() {
-  return array('administer uuid resolver');
-}
-
-/**
- * Implements hook_help().
- */
-function uuid_resolver_help($path, $arg) {
-  switch ($path) {
-    case 'admin/settings/uuid_resolver':
-    case 'admin/settings/uuid_resolver/overview':
-      return '<p>' . t('All UUID resolvers are listed below. Enable a resolver for it to listen on its base path for UUIDs and redirect to the path of the actual object.') . '</p>';
-  }
-}
-
-/**
- * Implements hook_menu().
- */
-function uuid_resolver_menu() {
-  $access = array('administer uuid resolver');
-
-  $items['admin/settings/uuid_resolver'] = array(
-    'title' => 'UUID resolver',
-    'description' => 'List all UUID path resolvers.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('uuid_resolver_overview_form'),
-    'access arguments' => $access,
-    'type' => MENU_NORMAL_ITEM,
-    'file' => 'admin.inc',
-  );
-
-  $items['admin/settings/uuid_resolver/overview'] = array(
-    'title' => 'Overview',
-    'type' => MENU_DEFAULT_LOCAL_TASK,
-  ) + $items['admin/settings/uuid_resolver'];
-
-  $items['admin/settings/uuid_resolver/refresh'] = array(
-    'title' => 'Refresh UUID resolvers',
-    'page callback' => 'uuid_resolver_refresh',
-    'access arguments' => $access,
-    'type' => MENU_CALLBACK,
-    'file' => 'admin.inc',
-  );
-
-  $items['admin/settings/uuid_resolver/%uuid_resolver_info'] = array(
-    'title' => 'Resolver',
-    'description' => 'Configure resolver settings.',
-    'page callback' => 'uuid_resolver_resolver_settings',
-    'page arguments' => array(3),
-    'access arguments' => $access,
-    'type' => MENU_CALLBACK,
-    'file' => 'admin.inc',
-  );
-
-  $items['admin/settings/uuid_resolver/settings'] = array(
-    'title' => 'Settings',
-    'description' => 'Configure settings for UUID path resolution.',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('uuid_resolver_settings_form'),
-    'access arguments' => $access,
-    'type' => MENU_LOCAL_TASK,
-    'file' => 'admin.inc',
-  );
-
-  return $items;
-}
-
-/**
- * Implements hook_menu_alter().
- * Injects menu callbacks for each enabled resolver.
- */
-function uuid_resolver_menu_alter(&$items) {
-  // Register path resolvers
-  $bins = _uuid_resolver_path_bins();
-  foreach ($bins as $path => $types) {
-    $path = trim($path, '/');
-    $argi = substr_count($path, '/') + 1;
-    $items[$path . '/%'] = array(
-      'title' => 'UUID resolver callback',
-      'page callback' => 'uuid_resolver_resolve',
-      'page arguments' => array($types, $argi),
-      'access callback' => TRUE,
-      'type' => MENU_CALLBACK,
-    );
-  }
-}
-
-/**
- * Wildcard loader for 'uuid_resolver_info'.
- * @return  Resolver info.
- */
-function uuid_resolver_info_load($name) {
-  $resolvers = uuid_resolver_get_resolvers();
-  return $resolvers[$name];
-}
-
-/**
- * Implements hook_theme().
- */
-function uuid_resolver_theme() {
-  return array(
-    'uuid_resolver_overview_form' => array(
-      'file' => 'admin.inc',
-    ),
-  );
-}
-
-/**
- * Get title of resolver.
- * @param $resolver  Resolver info.
- * @return  Title of resolver.
- */
-function uuid_resolver_get_title($resolver) {
-  return $resolver['title'];
-}
-
-/**
- * Get a well-formed HTTP response code from settings.
- * @return  HTTP response code.
- */
-function uuid_resolver_get_http_code() {
-  $code = (int) variable_get('uuid_resolver_http_code', 302);
-  if ($code < 100 || $code >= 600) {
-    // Use default for bad code
-    $code = UUID_RESOLVER_HTTP_CODE;
-  }
-  return $code;
-}
-
-/**
- * Resolve UUID. This is the main handler for UUID resolution.
- * @param $types  Array of object types to resolve.
- * @param $uuid  UUID to resolve.
- */
-function uuid_resolver_resolve($types, $uuid) {
-  if (uuid_is_valid($uuid)) {
-    $resolvers = uuid_resolver_get_resolvers();
-    foreach ($types as $type) {
-      $callback = $resolvers[$type]['callback'];
-      if (function_exists($callback)) {
-        $args = is_array($resolvers[$type]['callback arguments']) ?
-            $resolvers[$type]['callback arguments'] : array();
-        array_unshift($args, $uuid);
-        $path = call_user_func_array($callback, $args);
-        if (is_string($path) && !empty($path)) {
-          drupal_goto($path, NULL, NULL, uuid_resolver_get_http_code());
-        }
-      }
-    }
-  }
-
-  drupal_not_found();
-}
-
-/**
- * Get whether a resolver exists.
- * @param $resolver  Name of resolver.
- * @return  TRUE if resolver exists, FALSE otherwise.
- */
-function uuid_resolver_exists($resolver) {
-  return in_array((string) $resolver, array_keys(uuid_resolver_get_resolvers()), TRUE);
-}
-
-/**
- * Get whether a resolver is enabled.
- * @param $resolver  Name of resolver.
- * @param $skip_check  If TRUE, this function will query the variable regardless of validity.
- * @return  TRUE/FALSE indicating whether the resolver is enabled, or NULL if resolver does not exist.
- */
-function uuid_resolver_is_enabled($resolver, $skip_check = FALSE) {
-  return $skip_check || uuid_resolver_exists($resolver) ?
-      variable_get('uuid_resolver_' . $resolver . '_enabled', FALSE) :
-      NULL;
-}
-
-/**
- * Set whether a resolver is enabled.
- * @param $resolver  Name of resolver.
- * @param $enabled  TRUE/FALSE indicating whether the resolver is enabled, or NULL if resolver does not exist.
- * @param $skip_check  If TRUE, this function will set the variable regardless of validity.
- */
-function uuid_resolver_set_enabled($resolver, $enabled, $skip_check = FALSE) {
-  if (!$skip_check && !uuid_resolver_exists($resolver)) {
-    return;
-  }
-  variable_set('uuid_resolver_' . $resolver . '_enabled', $enabled);
-}
-
-/**
- * Get the base path a resolver uses.
- * @param $resolver  Name of resolver.
- * @param $skip_check  If TRUE, this function will query the variable regardless of validity.
- * @return  String base path for resolving UUIDs, or NULL if resolver does not exist.
- */
-function uuid_resolver_get_base_path($resolver, $skip_check = FALSE) {
-  if (!$skip_check && !uuid_resolver_exists($resolver)) {
-    return NULL;
-  }
-  return variable_get('uuid_resolver_' . $resolver . '_base_path', 'uuid');
-}
-
-/**
- * Set the base path a resolver uses.
- * @param $resolver  Name of resolver.
- * @param $path  String base path for resolving UUIDs.
- * @param $skip_check  If TRUE, this function will set the variable regardless of validity.
- */
-function uuid_resolver_set_base_path($resolver, $path, $skip_check = FALSE) {
-  if (!$skip_check && !uuid_resolver_exists($resolver)) {
-    return;
-  }
-  return variable_set('uuid_resolver_' . $resolver . '_base_path', $path);
-}
-
-/**
- * Invoke all hook_uuid_resolver to build a list of resolvers.
- * @param $full  If TRUE, resolver status and basic settings are attached, specifically 'enabled' and 'base_path'.
- * @return  An array of resolvers keyed by name.
- */
-function uuid_resolver_get_resolvers($full = FALSE) {
-  // Check cache
-  $resolvers = cache_get('uuid_resolver_info');
-  if (!is_array($resolvers)) {
-    // Rebuild cache
-    $resolvers = module_invoke_all('uuid_resolver_info');
-    ksort($resolvers);
-    variable_set('uuid_resolver_available', array_keys($resolvers));
-
-    foreach ($resolvers as $name => $info) {
-      if (!isset($info['title']) || !isset($info['callback'])) {
-        unset($resolvers[$name]);
-        continue;
-      }
-      // Add name
-      $resolvers[$name] = array('name' => $name) + (array) $info;
-    }
-
-    // Cache and return
-    cache_set('uuid_resolver_info', $resolvers);
-  }
-  if ($full) {
-    // Load values
-    foreach ($resolvers as $name => &$info) {
-      $info['enabled'] = uuid_resolver_is_enabled($name, TRUE);
-      $info['base_path'] = uuid_resolver_get_base_path($name, TRUE);
-    }
-  }
-  return $resolvers;
-}
-
-/**
- * Flush cached resolver elements.
- */
-function uuid_resolver_cache_clear() {
-  cache_clear_all('uuid_resolver_info', 'cache', TRUE);
-  menu_rebuild();
-}
-
-/**
- * Map base paths to the type of entity to resolve UUIDs for.
- * @return  An array of groups of resolvers keyed by common base paths.
- */
-function _uuid_resolver_path_bins() {
-  $bins = array();
-  foreach (uuid_resolver_get_resolvers() as $resolver => $info) {
-    if (uuid_resolver_is_enabled($resolver)) {
-      $bins[uuid_resolver_get_base_path($resolver)][] = $resolver;
-    }
-  }
-  return $bins;
-}
+<?php
+// $Id$
+/**
+ * @file
+ * UUID resolver
+ * Matches UUIDs using menu callback paths to redirect to entity.
+ */
+
+/**
+ * Implements hook_init().
+ */
+function uuid_resolver_init() {
+  _uuid_resolver_load_includes();
+}
+
+/**
+ * Implements hook_perm().
+ */
+function uuid_resolver_perm() {
+  return array('administer uuid resolver');
+}
+
+/**
+ * Implements hook_help().
+ */
+function uuid_resolver_help($path, $arg) {
+  switch ($path) {
+    case 'admin/settings/uuid_resolver':
+    case 'admin/settings/uuid_resolver/overview':
+      return '<p>' . t('All UUID resolvers are listed below. Enable a resolver for it to listen on its base path for UUIDs and redirect to the path of the actual object.') . '</p>';
+  }
+}
+
+/**
+ * Implements hook_menu().
+ */
+function uuid_resolver_menu() {
+  $access = array('administer uuid resolver');
+
+  $items['admin/settings/uuid_resolver'] = array(
+    'title' => 'UUID resolver',
+    'description' => 'List all UUID path resolvers.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uuid_resolver_overview_form'),
+    'access arguments' => $access,
+    'type' => MENU_NORMAL_ITEM,
+    'file' => 'admin.inc',
+  );
+
+  $items['admin/settings/uuid_resolver/overview'] = array(
+    'title' => 'Overview',
+    'type' => MENU_DEFAULT_LOCAL_TASK,
+  ) + $items['admin/settings/uuid_resolver'];
+
+  $items['admin/settings/uuid_resolver/refresh'] = array(
+    'title' => 'Refresh UUID resolvers',
+    'page callback' => 'uuid_resolver_refresh',
+    'access arguments' => $access,
+    'type' => MENU_CALLBACK,
+    'file' => 'admin.inc',
+  );
+
+  $items['admin/settings/uuid_resolver/%uuid_resolver_info'] = array(
+    'title' => 'Resolver',
+    'description' => 'Configure resolver settings.',
+    'page callback' => 'uuid_resolver_resolver_settings',
+    'page arguments' => array(3),
+    'access arguments' => $access,
+    'type' => MENU_CALLBACK,
+    'file' => 'admin.inc',
+  );
+
+  $items['admin/settings/uuid_resolver/settings'] = array(
+    'title' => 'Settings',
+    'description' => 'Configure settings for UUID path resolution.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('uuid_resolver_settings_form'),
+    'access arguments' => $access,
+    'type' => MENU_LOCAL_TASK,
+    'file' => 'admin.inc',
+  );
+
+  return $items;
+}
+
+/**
+ * Implements hook_menu_alter().
+ * Injects menu callbacks for each enabled resolver.
+ */
+function uuid_resolver_menu_alter(&$items) {
+  // Register path resolvers
+  $bins = _uuid_resolver_path_bins();
+  foreach ($bins as $path => $types) {
+    $path = trim($path, '/');
+    $argi = substr_count($path, '/') + 1;
+    $items[$path . '/%'] = array(
+      'title' => 'UUID resolver callback',
+      'page callback' => 'uuid_resolver_resolve',
+      'page arguments' => array($types, $argi),
+      'access callback' => TRUE,
+      'type' => MENU_CALLBACK,
+    );
+  }
+}
+
+/**
+ * Wildcard loader for 'uuid_resolver_info'.
+ * @return  Resolver info.
+ */
+function uuid_resolver_info_load($name) {
+  $resolvers = uuid_resolver_get_resolvers();
+  return $resolvers[$name];
+}
+
+/**
+ * Implements hook_theme().
+ */
+function uuid_resolver_theme() {
+  return array(
+    'uuid_resolver_overview_form' => array(
+      'file' => 'admin.inc',
+    ),
+  );
+}
+
+/**
+ * Get title of resolver.
+ * @param $resolver  Resolver info.
+ * @return  Title of resolver.
+ */
+function uuid_resolver_get_title($resolver) {
+  return $resolver['title'];
+}
+
+/**
+ * Get a well-formed HTTP response code from settings.
+ * @return  HTTP response code.
+ */
+function uuid_resolver_get_http_code() {
+  $code = (int) variable_get('uuid_resolver_http_code', 302);
+  if ($code < 100 || $code >= 600) {
+    // Use default for bad code
+    $code = UUID_RESOLVER_HTTP_CODE;
+  }
+  return $code;
+}
+
+/**
+ * Resolve UUID. This is the main handler for UUID resolution.
+ * @param $types  Array of object types to resolve.
+ * @param $uuid  UUID to resolve.
+ */
+function uuid_resolver_resolve($types, $uuid) {
+  if (uuid_is_valid($uuid)) {
+    $resolvers = uuid_resolver_get_resolvers();
+    foreach ($types as $type) {
+      $callback = $resolvers[$type]['callback'];
+      if (function_exists($callback)) {
+        $args = is_array($resolvers[$type]['callback arguments']) ?
+            $resolvers[$type]['callback arguments'] : array();
+        array_unshift($args, $uuid);
+        $path = call_user_func_array($callback, $args);
+        if (is_string($path) && !empty($path)) {
+          drupal_goto($path, NULL, NULL, uuid_resolver_get_http_code());
+        }
+      }
+    }
+  }
+
+  drupal_not_found();
+}
+
+/**
+ * Get whether a resolver exists.
+ * @param $resolver  Name of resolver.
+ * @return  TRUE if resolver exists, FALSE otherwise.
+ */
+function uuid_resolver_exists($resolver) {
+  return in_array((string) $resolver, array_keys(uuid_resolver_get_resolvers()), TRUE);
+}
+
+/**
+ * Get whether a resolver is enabled.
+ * @param $resolver  Name of resolver.
+ * @param $skip_check  If TRUE, this function will query the variable regardless of validity.
+ * @return  TRUE/FALSE indicating whether the resolver is enabled, or NULL if resolver does not exist.
+ */
+function uuid_resolver_is_enabled($resolver, $skip_check = FALSE) {
+  return $skip_check || uuid_resolver_exists($resolver) ?
+      variable_get('uuid_resolver_' . $resolver . '_enabled', FALSE) :
+      NULL;
+}
+
+/**
+ * Set whether a resolver is enabled.
+ * @param $resolver  Name of resolver.
+ * @param $enabled  TRUE/FALSE indicating whether the resolver is enabled, or NULL if resolver does not exist.
+ * @param $skip_check  If TRUE, this function will set the variable regardless of validity.
+ */
+function uuid_resolver_set_enabled($resolver, $enabled, $skip_check = FALSE) {
+  if (!$skip_check && !uuid_resolver_exists($resolver)) {
+    return;
+  }
+  variable_set('uuid_resolver_' . $resolver . '_enabled', $enabled);
+}
+
+/**
+ * Get the base path a resolver uses.
+ * @param $resolver  Name of resolver.
+ * @param $skip_check  If TRUE, this function will query the variable regardless of validity.
+ * @return  String base path for resolving UUIDs, or NULL if resolver does not exist.
+ */
+function uuid_resolver_get_base_path($resolver, $skip_check = FALSE) {
+  if (!$skip_check && !uuid_resolver_exists($resolver)) {
+    return NULL;
+  }
+  return variable_get('uuid_resolver_' . $resolver . '_base_path', 'uuid');
+}
+
+/**
+ * Set the base path a resolver uses.
+ * @param $resolver  Name of resolver.
+ * @param $path  String base path for resolving UUIDs.
+ * @param $skip_check  If TRUE, this function will set the variable regardless of validity.
+ */
+function uuid_resolver_set_base_path($resolver, $path, $skip_check = FALSE) {
+  if (!$skip_check && !uuid_resolver_exists($resolver)) {
+    return;
+  }
+  return variable_set('uuid_resolver_' . $resolver . '_base_path', $path);
+}
+
+/**
+ * Invoke all hook_uuid_resolver to build a list of resolvers.
+ * @param $full  If TRUE, resolver status and basic settings are attached, specifically 'enabled' and 'base_path'.
+ * @return  An array of resolvers keyed by name.
+ */
+function uuid_resolver_get_resolvers($full = FALSE) {
+  // Check cache
+  $resolvers = cache_get('uuid_resolver_info');
+  if (!is_array($resolvers)) {
+    // Rebuild cache
+    _uuid_resolver_load_includes();
+    $resolvers = module_invoke_all('uuid_resolver_info');
+    ksort($resolvers);
+    variable_set('uuid_resolver_available', array_keys($resolvers));
+
+    foreach ($resolvers as $name => $info) {
+      if (!isset($info['title']) || !isset($info['callback'])) {
+        unset($resolvers[$name]);
+        continue;
+      }
+      // Add name
+      $resolvers[$name] = array('name' => $name) + (array) $info;
+    }
+
+    // Cache and return
+    cache_set('uuid_resolver_info', $resolvers);
+  }
+  if ($full) {
+    // Load values
+    foreach ($resolvers as $name => &$info) {
+      $info['enabled'] = uuid_resolver_is_enabled($name, TRUE);
+      $info['base_path'] = uuid_resolver_get_base_path($name, TRUE);
+    }
+  }
+  return $resolvers;
+}
+
+/**
+ * Flush cached resolver elements.
+ */
+function uuid_resolver_cache_clear() {
+  cache_clear_all('uuid_resolver_info', 'cache', TRUE);
+  menu_rebuild();
+}
+
+/**
+ * Map base paths to the type of entity to resolve UUIDs for.
+ * @return  An array of groups of resolvers keyed by common base paths.
+ */
+function _uuid_resolver_path_bins() {
+  $bins = array();
+  foreach (uuid_resolver_get_resolvers() as $resolver => $info) {
+    if (uuid_resolver_is_enabled($resolver)) {
+      $bins[uuid_resolver_get_base_path($resolver)][] = $resolver;
+    }
+  }
+  return $bins;
+}
+
+/**
+ * Load include files implemented in this module.
+ */
+function _uuid_resolver_load_includes() {
+  static $loaded = FALSE;
+  if (!$loaded) {
+    // Load include files
+    $files = file_scan_directory(drupal_get_path('module', 'uuid_resolver') . '/includes', '\.inc$');
+    foreach ($files as $file) {
+      include_once($file->filename);
+    }
+
+    $loaded = TRUE;
+  }
+}
-- 
1.7.3.1.msysgit.0

