You are here

function ctools_plugin_load_includes in Chaos Tool Suite (ctools) 7

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

Load plugins from a directory.

Parameters

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

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

Return value

array A (possibly empty) 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 428
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 file_scan_directory more than necessary.
  $all_files =& drupal_static(__FUNCTION__, array());

  // Store static of plugin arrays for reference because they can't be
  // reincluded, so there is no point in using drupal_static().
  static $plugin_arrays = array();
  if (!isset($all_files[$info['module']][$info['type']])) {
    $cache = cache_get("ctools_plugin_files:{$info['module']}:{$info['type']}");
    if ($cache) {
      $all_files[$info['module']][$info['type']] = $cache->data;
    }
    elseif (!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'] != 'inc' ? $info['extension'] : 'info';
      foreach ($directories as $module => $path) {
        $all_files[$info['module']][$info['type']][$module] = file_scan_directory($path, '/\\.' . $extension . '$/', array(
          'key' => 'name',
        ));
      }
      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.
        // Ensure that we don't have something leftover from earlier.
        $plugin = NULL;
        if (isset($plugin_arrays[$file->uri])) {
          $identifier = $plugin_arrays[$file->uri];
        }
        else {
          include_once DRUPAL_ROOT . '/' . $file->uri;

          // .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->uri] = $plugin;
            $identifier = $plugin;
          }
          else {
            $identifier = $module . '_' . $file->name;
          }
        }
        $result = ctools_plugin_process($info, $module, $identifier, dirname($file->uri), basename($file->uri), $file->name);
      }
      if (is_array($result)) {
        $plugins = array_merge($plugins, $result);
      }
    }
  }
  return $plugins;
}