Index: skinr.handlers.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/skinr/skinr.handlers.inc,v
retrieving revision 1.9
diff -u -p -F '^f' -r1.9 skinr.handlers.inc
--- skinr.handlers.inc	20 Dec 2010 21:42:29 -0000	1.9
+++ skinr.handlers.inc	10 Jan 2011 04:15:39 -0000
@@ -140,26 +140,28 @@ function skinr_submit_handler(&$form, $f
   }
 }
 
-// Declare API compatibility on behalf of core modules:
-
 /**
- * Implements hook_skinr_api().
- *
- * This one is used as the base to reduce errors when updating.
+ * Implements hook_skinr_api_VERSION().
  */
-function skinr_skinr_api() {
-  return array(
-    'api' => 1,
-    'path' => drupal_get_path('module', 'skinr') . '/modules',
-  );
+function skinr_skinr_api_2() {
 }
 
-function block_skinr_api() { return skinr_skinr_api(); }
+function block_skinr_api_2() {
+  return skinr_skinr_api_2();
+}
 
-function comment_skinr_api() { return skinr_skinr_api(); }
+function comment_skinr_api_2() {
+  return skinr_skinr_api_2();
+}
 
-function node_skinr_api() { return skinr_skinr_api(); }
+function node_skinr_api_2() {
+  return skinr_skinr_api_2();
+}
 
-function panels_skinr_api() { return skinr_skinr_api(); }
+function panels_skinr_api_2() {
+  return skinr_skinr_api_2();
+}
 
-function views_skinr_api() { return skinr_skinr_api(); }
+function views_skinr_api_2() {
+  return skinr_skinr_api_2();
+}
Index: skinr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/skinr/skinr.module,v
retrieving revision 1.37
diff -u -p -F '^f' -r1.37 skinr.module
--- skinr.module	20 Dec 2010 21:42:29 -0000	1.37
+++ skinr.module	10 Jan 2011 04:15:39 -0000
@@ -7,6 +7,11 @@
  */
 
 /**
+ * The Skinr API version.
+ */
+define('SKINR_VERSION', 2);
+
+/**
  * Implements hook_help().
  */
 function skinr_help($path, $arg) {
@@ -23,6 +28,19 @@ function skinr_help($path, $arg) {
 }
 
 /**
+ * Implements hook_hook_info().
+ */
+function skinr_hook_info() {
+  $hooks = array(
+    'skinr_api_2',
+  );
+  $hooks = array_fill_keys($hooks, array(
+    'group' => 'skinr',
+  ));
+  return $hooks;
+}
+
+/**
  * Implements hook_module_implements_alter().
  */
 function skinr_module_implements_alter(&$implementations, $hook) {
@@ -36,10 +54,12 @@ function skinr_module_implements_alter(&
 
 /**
  * Implements hook_init().
+ *
+ * @todo Kill me. Entirely.
  */
 function skinr_init() {
   module_load_include('inc', 'skinr', 'skinr.handlers');
-  skinr_module_load_all_includes();
+  skinr_load_includes();
 }
 
 /**
@@ -64,8 +84,10 @@ function skinr_preprocess(&$variables, $
       $preprocess_settings = $module_settings['preprocess'][$original_hook];
       $sids = skinr_handler('preprocess_index_handler', 'preprocess', $preprocess_settings['index_handler'], $variables);
 
+      // @todo Kill skinr_skin_extract(), and only store what has been
+      //   configured, enabled, and should be actively applied, in order to use
+      //   drupal_process_attached().
       if ($extracted = skinr_skin_extract($module, $sids, $current_theme)) {
-        // @todo How can this be done better with drupal_process_attached()?
         foreach ($extracted['css'] as $file) {
           if ($file['enabled']) {
             drupal_add_css($file['path'], $file['file_options']);
@@ -417,37 +439,75 @@ function skinr_rule_visible($rid) {
   return FALSE;
 }
 
-// ------------------------------------------------------------------
-// Include file helpers.
-
 /**
- * Includes $module.skinr.inc files.
- */
-function skinr_module_load_all_includes() {
-  foreach (skinr_get_module_apis() as $module => $info) {
-    $file = DRUPAL_ROOT . "/$info[path]/$module.skinr.inc";
-    if (is_file($file)) {
-      require_once $file;
-    }
-  }
-}
-
-/**
- * Get a list of modules that support skinr.
+ * Returns a list of extensions that implement this API version of Skinr.
+ *
+ * @return
+ *   An associative array whose keys are system names of extensions and whose
+ *   values are again associative arrays containing:
+ *   - type: Either 'module' or 'theme'.
+ *   - name: The system name of the extension.
+ *   - path: The path to the extension.
+ *   - directory: (optional) The sub-directory holding Skinr plugin files.
+ *   - ...: Any other properties defined by the module or theme.
  */
-function skinr_get_module_apis() {
+function skinr_implements() {
   $cache = &drupal_static(__FUNCTION__);
 
   if (!isset($cache)) {
     $cache = array();
-    foreach (module_implements('skinr_api') as $module) {
-      $function = $module . '_skinr_api';
+    // Collect hook_skinr_api_VERSION() module implementations. This will also
+    // auto-load $module.skinr.inc files, which may contain skin/group hook
+    // implementations (when not using the plugin system).
+    foreach (module_implements('skinr_api_' . SKINR_VERSION) as $module) {
+      // Ensure that $module and the extension type is registered.
+      $cache[$module] = array(
+        'type' => 'module',
+        'name' => $module,
+      );
+      // Check whether the hook returns any information.
+      $function = $module . '_skinr_api_' . SKINR_VERSION;
       $info = $function();
-      if (isset($info['api']) && $info['api'] == 1.000) {
-        if (!isset($info['path'])) {
-          $info['path'] = drupal_get_path('module', $module);
+      if (isset($info) && is_array($info)) {
+        $cache[$module] += $info;
+      }
+      // Populate defaults.
+      $cache[$module] += array(
+        'path' => drupal_get_path('module', $module),
+        'directory' => NULL,
+      );
+    }
+    // Collect the equivalent of hook_skinr_api_VERSION() implementations in
+    // themes. The theme system only initializes one theme (and optionally its
+    // base themes) for the current request, and the phptemplate engine only
+    // loads template.php during theme initialization. Furthermore, template.php
+    // is a custom concept of the phptemplate engine and does not exist for
+    // other theme engines. Since we are interested in all existing
+    // implementations of all enabled themes, the equivalent of the module hook
+    // is a theme .info file property 'skinr' that has the sub-keys 'api' and
+    // optionally 'directory' defined.
+    foreach (list_themes() as $name => $theme) {
+      if (!empty($theme->status) && isset($theme->info['skinr']['api']) && $theme->info['skinr']['api'] == SKINR_VERSION) {
+        // Ensure that the theme name and the extension type is registered.
+        $cache[$name] = array(
+          'type' => 'theme',
+          'name' => $name,
+        );
+        // Add any additional information that has been registered.
+        $cache[$name] += $theme->info['skinr'];
+        // Populate defaults.
+        $cache[$name] += array(
+          'path' => drupal_get_path('theme', $name),
+          // Since themes cannot do anything else than registering skins and
+          // groups, we default to the sub-directory 'skins'.
+          'directory' => 'skins',
+        );
+        // Lastly, for API consistency with modules, check whether the theme
+        // contains a $theme.skinr.inc file and auto-load it, if any.
+        $file = DRUPAL_ROOT . '/' . $cache[$name]['path'] . '/' . $name . '.skinr.inc';
+        if (file_exists($file)) {
+          require_once $file;
         }
-        $cache[$module] = $info;
       }
     }
   }
@@ -455,6 +515,48 @@ function skinr_get_module_apis() {
   return $cache;
 }
 
+/**
+ * Includes $extension.skinr.inc files of extensions compatible with this version of Skinr.
+ *
+ * @todo Shoot me. Twice.
+ */
+function skinr_load_includes() {
+  foreach (skinr_implements() as $extension) {
+    $file = DRUPAL_ROOT . '/' . $extension['path'] . '/' . $extension['name'] . '.skinr.inc';
+    if (file_exists($file)) {
+      require_once $file;
+    }
+  }
+}
+
+/**
+ * Includes Skinr plugin files for an extension, if any.
+ *
+ * @param $extension
+ *   The API information for an extension, as returned by skinr_implements().
+ */
+function skinr_load_plugins($extension) {
+  static $loaded = array();
+
+  // If plugins have already been loaded for this extension, return them.
+  if (isset($loaded[$extension['name']])) {
+    return $loaded[$extension['name']];
+  }
+  $loaded[$extension['name']] = array();
+
+  // If the extension defines a plugin directory, scan its plugins.
+  if (isset($extension['directory'])) {
+    $dir = DRUPAL_ROOT . '/' . $extension['path'] . '/' . $extension['directory'];
+    $mask = '@^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.inc$@';
+    $loaded[$extension['name']] = file_scan_directory($dir, $mask, array(
+      'key' => 'name',
+      'recurse' => FALSE,
+      'callback' => 'include_once',
+    ));
+  }
+  return $loaded[$extension['name']];
+}
+
 // -----------------------------------------------------------------------
 // Skinr data handling functions.
 
@@ -702,24 +804,12 @@ function skinr_current_theme($exclude_ad
 }
 
 /**
- * Prepare defaults for groups.
- *
- * @return
- *   An array of default group settings.
- */
-function skinr_group_info_default() {
-  return array(
-    'title' => '',
-    'description' => '',
-    'weight' => NULL,
-  );
-}
-
-/**
  * Prepare defaults for skins.
  *
  * @return
  *   An array of default skin settings.
+ *
+ * @todo Merge into skinr_get_skin_info() and remove this function.
  */
 function skinr_skin_info_default() {
   return array(
@@ -758,24 +848,6 @@ function skinr_skin_info_status_default(
 }
 
 /**
- * Get a list of plugin hooks.
- *
- * @return
- *   An array of hook details keyed by hook name. The hook details contain the
- *   following keys:
- *   - 'type': Available options are 'module' or 'theme'.
- *   - 'name': The name of the owner module or theme of this plugin.
- *   - 'plugin': The name of the skins plugin.
- *   - 'path': This plugin's path.
- */
-function skinr_plugin_hooks() {
-  // @todo Load the include files and get an array of hooks; we need to
-  //   know which module and which plugin comprises the hook.
-  $hooks = array();
-  return $hooks;
-}
-
-/**
  * Helper function to prepend a path to an array of stylesheet or script filenames.
  *
  * If the url is absolute (e.g. the url start with 'http://' or 'https://')
@@ -813,11 +885,10 @@ function _skinr_add_path_to_files(&$file
  * @param $skin_infos
  *   A skins array as returned from a skins plugin.
  * @param $source
- *   An array with the following keys:
- *   - 'type': Available options are 'module' or 'theme'.
- *   - 'name': The name of the owner module or theme of this plugin.
- *   - 'plugin': The name of the skins plugin.
- *   - 'path': This plugin's path.
+ *   An associative array containing information about the source of the skin.
+ *   See skinr_implements() for details.
+ *
+ * @todo Merge into skinr_get_skin_info() and remove this function.
  */
 function skinr_skin_info_process(&$skin_infos, $source) {
   foreach ($skin_infos as $skin_name => $skin_info) {
@@ -833,9 +904,6 @@ function skinr_skin_info_process(&$skin_
     // Merge in default statuses for all themes.
     $skin_infos[$skin_name]['status'] = array_merge(skinr_skin_info_status_default($skin_infos[$skin_name]['default_status']), $skin_infos[$skin_name]['status']);
 
-    // @todo Apply overridden statuses. This will need to be loaded from
-    //   database.
-
     // Add path to stylesheets.
     if (isset($skin_infos[$skin_name]['attached']['css'])) {
       _skinr_add_path_to_files($skin_infos[$skin_name]['attached']['css'], $source['path']);
@@ -867,82 +935,111 @@ function skinr_skin_info_process(&$skin_
 }
 
 /**
- * Retrieves a list of all available skins.
+ * Retrieves all skins registered by modules and themes.
  *
  * @return
- *    An array of skins.
+ *   An array of skins.
  */
 function skinr_get_skin_info() {
-  $skin_infos = &drupal_static(__FUNCTION__);
+  $skin_info = &drupal_static(__FUNCTION__);
 
-  if (!isset($skin_infos)) {
-    // Set skins to an array to avoid errors down the line if no skins
-    // are found.
-    $skin_infos = array();
-
-    // Load skins from hook_skinr_skins().
-    $hooks = skinr_plugin_hooks();
-    foreach ($hooks as $hook => $source) {
-      $function = $hook . '_skinr_skin_info';
-      $result = $function();
-      if (isset($result)) {
-        if (!is_array($result)) {
-          $result = array($result);
+  if (!isset($skin_info)) {
+    if ($cached = cache_get('skinr_skin_info')) {
+      $skin_info = $cached->data;
+      return $skin_info;
+    }
+    $skin_info = array();
+
+    foreach (skinr_implements() as $name => $extension) {
+      $hooks = array(
+        "{$name}_skinr_skin_info" => $extension,
+      );
+      // Load the extension's plugins, if any.
+      if ($files = skinr_load_plugins($extension)) {
+        // The source path for a plugin is the directory it is contained in.
+        $dir = $extension['path'] . '/' . $extension['directory'];
+        foreach ($files as $plugin => $file) {
+          $hooks["{$name}_skinr_skin_{$plugin}_info"] = array(
+            'path' => $dir,
+          ) + $extension;
         }
+      }
+      foreach ($hooks as $function => $source) {
+        if (function_exists($function)) {
+          $extension_info = $function();
+          if (isset($extension_info) && is_array($extension_info)) {
+            // Prepare the skin information.
+            skinr_skin_info_process($extension_info, $source);
 
-        // Make any programatic changes.
-        skinr_skin_info_process($result, $source);
-
-        $skin_infos = array_merge_recursive($skin_infos, $result);
+            $skin_info += $extension_info;
+          }
+        }
       }
     }
 
-    // Allow modules to alter skin infos through hook_skinr_skin_info_alter().
-    drupal_alter('skinr_skin_info', $skin_infos);
+    // Allow modules to alter registered skin information.
+    drupal_alter('skinr_skin_info', $skin_info);
+
+    cache_set('skinr_skin_info', $skin_info);
   }
 
-  return $skin_infos;
+  return $skin_info;
 }
 
 /**
- * Retrieves a list of all available groups.
+ * Retrieves all skin groups registered by modules and themes.
  *
  * @return
- *    An array of groups.
+ *   An array of groups.
  */
 function skinr_get_group_info() {
-  $group_infos = &drupal_static(__FUNCTION__);
+  $group_info = &drupal_static(__FUNCTION__);
 
-  if (!isset($group_infos)) {
-    // Set groups to an array to avoid errors down the line if no groups
-    // are found.
-    $group_infos = array();
-
-    // Load skins from hook_skinr_groups().
-    $hooks = skinr_plugin_hooks();
-    foreach ($hooks as $hook => $source) {
-      $function = $hook . '_skinr_group_info';
-      $result = $function();
-      if (isset($result)) {
-        if (!is_array($result)) {
-          $result = array($result);
+  if (!isset($group_info)) {
+    if ($cached = cache_get('skinr_group_info')) {
+      $skin_info = $cached->data;
+      return $group_info;
+    }
+    $group_info = array();
+
+    foreach (skinr_implements() as $name => $extension) {
+      $hooks = array(
+        "{$name}_skinr_group_info" => $extension,
+      );
+      // Load the extension's plugins, if any.
+      if ($files = skinr_load_plugins($extension)) {
+        // The source path for a plugin is the directory it is contained in.
+        $dir = $extension['path'] . '/' . $extension['directory'];
+        foreach ($files as $plugin => $file) {
+          $hooks["{$name}_skinr_group_{$plugin}_info"] = array(
+            'path' => $dir,
+          ) + $extension;
         }
+      }
+      foreach ($hooks as $function => $source) {
+        if (function_exists($function)) {
+          $extension_info = $function();
+          if (isset($extension_info) && is_array($extension_info)) {
+            // Prepare the skin group information.
+            $extension_info += array(
+              'title' => '',
+              'description' => '',
+              'weight' => 0,
+            );
 
-        // Make any programatic changes.
-        // Merge in defaults.
-        foreach ($result as $group_name => $group_info) {
-          $result[$group_name] = array_merge(skinr_group_info_default(), $group_info);
+            $group_info += $extension_info;
+          }
         }
-
-        $group_infos = array_merge_recursive($group_infos, $result);
       }
     }
 
     // Allow modules to alter groups through hook_skinr_group_info_alter().
-    drupal_alter('skinr_group_info', $group_infos);
+    drupal_alter('skinr_group_info', $group_info);
+
+    cache_set('skinr_group_info', $group_info);
   }
 
-  return $group_infos;
+  return $group_info;
 }
 
 /**
