You are here

function ctools_plugin_load_includes in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 includes/plugins.inc \ctools_plugin_load_includes()

Load plugins from a directory.

Parameters

$info: The plugin info as returned by ctools_plugin_get_info()

$file: The file to load if we're looking for just one particular plugin.

Return value

An array of information created for this plugin.

1 call to ctools_plugin_load_includes()
ctools_get_plugins in includes/plugins.inc
Fetch a group of plugins by name.
1 string reference to 'ctools_plugin_load_includes'
ctools_get_plugins_reset in includes/plugins.inc
Reset all static caches that affect the result of ctools_get_plugins().

File

includes/plugins.inc, line 296
Contains routines to organize and load plugins. It allows a special variation of the hook system so that plugins can be kept in separate .inc files, and can be either loaded all at once or loaded only when necessary.

Code

function ctools_plugin_load_includes($info, $filename = NULL) {

  // Keep a static array so we don't hit drupal_system_listing more than necessary.
  $all_files =& ctools_static(__FUNCTION__, array());

  // store static of plugin arrays for reference because they can't be reincluded.
  static $plugin_arrays = array();

  // If we're being asked for all plugins of a type, skip any caching
  // we may have done because this is an admin task and it's ok to
  // spend the extra time.
  if (!isset($filename)) {
    $all_files[$info['module']][$info['type']] = NULL;
  }
  if (!isset($all_files[$info['module']][$info['type']])) {

    // If a filename was set, we will try to load our list of files from
    // cache. This is considered normal operation and we try to reduce
    // the time spent finding files.
    if (isset($filename)) {
      $cache = cache_get("ctools_plugin_files:{$info['module']}:{$info['type']}");
      if ($cache) {
        $all_files[$info['module']][$info['type']] = $cache->data;
      }
    }
    if (!isset($all_files[$info['module']][$info['type']])) {
      $all_files[$info['module']][$info['type']] = array();

      // Load all our plugins.
      $directories = ctools_plugin_get_directories($info);
      $extension = empty($info['info file']) ? $info['extension'] : 'info';
      foreach ($directories as $module => $path) {
        $all_files[$info['module']][$info['type']][$module] = drupal_system_listing('\\.' . $extension . '$', $path, 'name', 0);
      }
      cache_set("ctools_plugin_files:{$info['module']}:{$info['type']}", $all_files[$info['module']][$info['type']]);
    }
  }
  $file_list = $all_files[$info['module']][$info['type']];
  $plugins = array();

  // Iterate through all the plugin .inc files, load them and process the hook
  // that should now be available.
  foreach (array_filter($file_list) as $module => $files) {
    if ($filename) {
      $files = isset($files[$filename]) ? array(
        $filename => $files[$filename],
      ) : array();
    }
    foreach ($files as $file) {
      if (!empty($info['info file'])) {

        // Parse a .info file
        $result = ctools_plugin_process_info($info, $module, $file);
      }
      else {

        // Parse a hook.
        $plugin = NULL;

        // ensure that we don't have something leftover from earlier.
        if (isset($plugin_arrays[$file->filename])) {
          $identifier = $plugin_arrays[$file->filename];
        }
        else {
          require_once './' . $file->filename;

          // .inc files have a special format for the hook identifier.
          // For example, 'foo.inc' in the module 'mogul' using the plugin
          // whose hook is named 'borg_type' should have a function named (deep breath)
          // mogul_foo_borg_type()
          // If, however, the .inc file set the quasi-global $plugin array, we
          // can use that and not even call a function. Set the $identifier
          // appropriately and ctools_plugin_process() will handle it.
          if (isset($plugin)) {
            $plugin_arrays[$file->filename] = $plugin;
            $identifier = $plugin;
          }
          else {
            $identifier = $module . '_' . $file->name;
          }
        }
        $result = ctools_plugin_process($info, $module, $identifier, dirname($file->filename), basename($file->filename), $file->name);
      }
      if (is_array($result)) {
        $plugins = array_merge($plugins, $result);
      }
    }
  }
  return $plugins;
}