Projet

Général

Profil

Paste
Télécharger (33,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / includes / plugins.inc @ 7e72b748

1
<?php
2

    
3
/**
4
 * @file
5
 * Contains routines to organize and load plugins. It allows a special
6
 * variation of the hook system so that plugins can be kept in separate
7
 * .inc files, and can be either loaded all at once or loaded only when
8
 * necessary.
9
 */
10

    
11
/**
12
 * Get an array of information about modules that support an API.
13
 *
14
 * This will ask each module if they support the given API, and if they do
15
 * it will return an array of information about the modules that do.
16
 *
17
 * This function invokes hook_ctools_api. This invocation is statically
18
 * cached, so feel free to call it as often per page run as you like, it
19
 * will cost very little.
20
 *
21
 * This function can be used as an alternative to module_implements and can
22
 * thus be used to find a precise list of modules that not only support
23
 * a given hook (aka 'api') but also restrict to only modules that use
24
 * the given version. This will allow multiple modules moving at different
25
 * paces to still be able to work together and, in the event of a mismatch,
26
 * either fall back to older behaviors or simply cease loading, which is
27
 * still better than a crash.
28
 *
29
 * @param $owner
30
 *   The name of the module that controls the API.
31
 * @param $api
32
 *   The name of the api. The api name forms the file name:
33
 *   $module.$api.inc
34
 * @param $minimum_version
35
 *   The lowest version API that is compatible with this one. If a module
36
 *   reports its API as older than this, its files will not be loaded. This
37
 *   should never change during operation.
38
 * @param $current_version
39
 *   The current version of the api. If a module reports its minimum API as
40
 *   higher than this, its files will not be loaded. This should never change
41
 *   during operation.
42
 *
43
 * @return
44
 *   An array of API information, keyed by module. Each module's information will
45
 *   contain:
46
 *   - 'version': The version of the API required by the module. The module
47
 *     should use the lowest number it can support so that the widest range
48
 *     of supported versions can be used.
49
 *   - 'path': If not provided, this will be the module's path. This is
50
 *     where the module will store any subsidiary files. This differs from
51
 *     plugin paths which are figured separately.
52
 *
53
 *   APIs can request any other information to be placed here that they might
54
 *   need. This should be in the documentation for that particular API.
55
 */
56
function ctools_plugin_api_info($owner, $api, $minimum_version, $current_version) {
57
  $cache = &drupal_static(__FUNCTION__, array());
58
  if (!isset($cache[$owner][$api])) {
59
    $cache[$owner][$api] = array();
60

    
61
    $hook = ctools_plugin_api_get_hook($owner, $api);
62

    
63
    foreach (module_implements($hook) as $module) {
64
      $function = $module . '_' . $hook;
65
      $info = $function($owner, $api);
66
      $version = NULL;
67
      // This is added to make hook_views_api() compatible with this, since
68
      // views used a different version key.
69
      if (isset($info['version'])) {
70
        $version = $info['version'];
71
      }
72
      elseif (isset($info['api'])) {
73
        $version = $info['api'];
74
      }
75

    
76
      if (!isset($version)) {
77
        continue;
78
      }
79

    
80
      // Only process if version is between minimum and current, inclusive.
81
      if (($version == $minimum_version) || ($version == $current_version)
82
        || (version_compare($version, $minimum_version, '>=')
83
        && version_compare($version, $current_version, '<='))) {
84
        if (!isset($info['path'])) {
85
          $info['path'] = drupal_get_path('module', $module);
86
        }
87
        $cache[$owner][$api][$module] = $info;
88
      }
89
    }
90

    
91
    // And allow themes to implement these as well.
92
    $themes = _ctools_list_themes();
93
    foreach ($themes as $name => $theme) {
94
      if (!empty($theme->info['api'][$owner][$api])) {
95
        $info = $theme->info['api'][$owner][$api];
96
        if (!isset($info['version'])) {
97
          continue;
98
        }
99

    
100
        // Only process if version is between minimum and current, inclusive.
101
        if (version_compare($info['version'], $minimum_version, '>=') && version_compare($info['version'], $current_version, '<=')) {
102
          if (!isset($info['path'])) {
103
            $info['path'] = '';
104
          }
105
          // Because themes can't easily specify full path, we add it here
106
          // even though we do not for modules:
107
          $info['path'] = drupal_get_path('theme', $name) . '/' . $info['path'];
108
          $cache[$owner][$api][$name] = $info;
109
        }
110
      }
111
    }
112

    
113
    // Allow other modules to hook in.
114
    drupal_alter($hook, $cache[$owner][$api], $owner, $api);
115
  }
116

    
117
  return $cache[$owner][$api];
118
}
119

    
120
/**
121
 * Load a group of API files.
122
 *
123
 * This will ask each module if they support the given API, and if they do
124
 * it will load the specified file name. The API and the file name
125
 * coincide by design.
126
 *
127
 * @param $owner
128
 *   The name of the module that controls the API.
129
 * @param $api
130
 *   The name of the api. The api name forms the file name:
131
 *   $module.$api.inc, though this can be overridden by the module's response.
132
 * @param $minimum_version
133
 *   The lowest version API that is compatible with this one. If a module
134
 *   reports its API as older than this, its files will not be loaded. This
135
 *   should never change during operation.
136
 * @param $current_version
137
 *   The current version of the api. If a module reports its minimum API as
138
 *   higher than this, its files will not be loaded. This should never change
139
 *   during operation.
140
 *
141
 * @return
142
 *   The API information, in case you need it.
143
 */
144
function ctools_plugin_api_include($owner, $api, $minimum_version, $current_version) {
145
  static $already_done = array();
146

    
147
  $info = ctools_plugin_api_info($owner, $api, $minimum_version, $current_version);
148
  foreach ($info as $module => $plugin_info) {
149
    if (!isset($already_done[$owner][$api][$module])) {
150
      if (isset($plugin_info["$api file"])) {
151
        $file = $plugin_info["$api file"];
152
      }
153
      elseif (isset($plugin_info['file'])) {
154
        $file = $plugin_info['file'];
155
      }
156
      else {
157
        $file = "$module.$api.inc";
158
      }
159

    
160
      if (file_exists(DRUPAL_ROOT . "/$plugin_info[path]/$file")) {
161
        require_once DRUPAL_ROOT . "/$plugin_info[path]/$file";
162
      }
163
      elseif (file_exists(DRUPAL_ROOT . "/$file")) {
164
        require_once DRUPAL_ROOT . "/$file";
165
      }
166
      $already_done[$owner][$api][$module] = TRUE;
167
    }
168
  }
169

    
170
  return $info;
171
}
172

    
173
/**
174
 * Find out what hook to use to determine if modules support an API.
175
 *
176
 * By default, most APIs will use hook_ctools_plugin_api, but some modules
177
 * want sole ownership. This technique lets modules define what hook
178
 * to use.
179
 */
180
function ctools_plugin_api_get_hook($owner, $api) {
181
  // Allow modules to use their own hook for this. The only easy way to do
182
  // this right now is with a magically named function.
183
  if (function_exists($function = $owner . '_' . $api . '_hook_name')) {
184
    $hook = $function();
185
  }
186
  elseif (function_exists($function = $owner . '_ctools_plugin_api_hook_name')) {
187
    $hook = $function();
188
  }
189

    
190
  // Do this last so that if the $function above failed to return, we have a
191
  // sane default.
192
  if (empty($hook)) {
193
    $hook = 'ctools_plugin_api';
194
  }
195

    
196
  return $hook;
197
}
198

    
199
/**
200
 * Fetch a group of plugins by name.
201
 *
202
 * @param string $module
203
 *   The name of the module that utilizes this plugin system. It will be used to
204
 *   get more data about the plugin as defined on hook_ctools_plugin_type().
205
 * @param string $type
206
 *   The type identifier of the plugin.
207
 * @param string $id
208
 *   If specified, return only information about plugin with this identifier.
209
 *   The system will do its utmost to load only plugins with this id.
210
 *
211
 * @return array
212
 *   An array of information arrays about the plugins received. The contents of
213
 *   the array are specific to the plugin.
214
 */
215
function ctools_get_plugins($module, $type, $id = NULL) {
216
  // Store local caches of plugins and plugin info so we don't have to do full
217
  // lookups every time.
218
  static $drupal_static_fast;
219
  if (!isset($drupal_static_fast)) {
220
    $drupal_static_fast['plugins'] = &drupal_static('ctools_plugins', array());
221
  }
222
  $plugins = &$drupal_static_fast['plugins'];
223

    
224
  $info = ctools_plugin_get_plugin_type_info();
225

    
226
  if (!isset($info[$module][$type])) {
227
    // If we don't find the plugin we attempt a cache rebuild before bailing out.
228
    $info = ctools_plugin_get_plugin_type_info(TRUE);
229
    // Bail out noisily if an invalid module/type combination is requested.
230
    if (!isset($info[$module][$type])) {
231
      watchdog('ctools', 'Invalid plugin module/type combination requested: module @module and type @type', array('@module' => $module, '@type' => $type), WATCHDOG_ERROR);
232
      return array();
233
    }
234
  }
235

    
236
  // Make sure our plugins array is populated.
237
  if (!isset($plugins[$module][$type])) {
238
    $plugins[$module][$type] = array();
239
  }
240

    
241
  // Attempt to shortcut this whole piece of code if we already have the
242
  // requested plugin:
243
  if ($id && array_key_exists($id, $plugins[$module][$type])) {
244
    return $plugins[$module][$type][$id];
245
  }
246

    
247
  // Store the status of plugin loading. If a module plugin type pair is true,
248
  // then it is fully loaded and no searching or setup needs to be done.
249
  $setup = &drupal_static('ctools_plugin_setup', array());
250

    
251
  // We assume we don't need to build a cache.
252
  $build_cache = FALSE;
253

    
254
  // If the plugin info says this can be cached, check cache first.
255
  if ($info[$module][$type]['cache'] && empty($setup[$module][$type])) {
256
    $cache = cache_get("plugins:$module:$type", $info[$module][$type]['cache table']);
257

    
258
    if (!empty($cache->data)) {
259
      // Cache load succeeded so use the cached plugin list.
260
      $plugins[$module][$type] = $cache->data;
261
      // Set $setup to true so we know things where loaded.
262
      $setup[$module][$type] = TRUE;
263
    }
264
    else {
265
      // Cache load failed so store that we need to build and write the cache.
266
      $build_cache = TRUE;
267
    }
268
  }
269

    
270
  // Always load all hooks if we need them. Note we only need them now if the
271
  // plugin asks for them. We can assume that if we have plugins we've already
272
  // called the global hook.
273
  if (!empty($info[$module][$type]['use hooks']) && empty($plugins[$module][$type])) {
274
    $plugins[$module][$type] = ctools_plugin_load_hooks($info[$module][$type]);
275
  }
276

    
277
  // Then see if we should load all files. We only do this if we want a list of
278
  // all plugins or there was a cache miss.
279
  if (empty($setup[$module][$type]) && ($build_cache || !$id)) {
280
    $setup[$module][$type] = TRUE;
281
    $plugins[$module][$type] = array_merge($plugins[$module][$type], ctools_plugin_load_includes($info[$module][$type]));
282
    // If the plugin can have child plugins, and we're loading all plugins,
283
    // go through the list of plugins we have and find child plugins.
284
    if (!$id && !empty($info[$module][$type]['child plugins'])) {
285
      // If a plugin supports children, go through each plugin and ask.
286
      $temp = array();
287
      foreach ($plugins[$module][$type] as $name => $plugin) {
288
        // The strpos ensures that we don't try to find children for plugins
289
        // that are already children.
290
        if (!empty($plugin['get children']) && function_exists($plugin['get children']) && strpos($name, ':') === FALSE) {
291
          $temp = array_merge($plugin['get children']($plugin, $name), $temp);
292
        }
293
        else {
294
          $temp[$name] = $plugin;
295
        }
296
      }
297
      $plugins[$module][$type] = $temp;
298
    }
299
  }
300

    
301
  // If we were told earlier that this is cacheable and the cache was empty,
302
  // give something back.
303
  if ($build_cache) {
304
    cache_set("plugins:$module:$type", $plugins[$module][$type], $info[$module][$type]['cache table']);
305
  }
306

    
307
  // If no id was requested, we are finished here:
308
  if (!$id) {
309
    // Use array_filter because looking for unknown plugins could cause NULL
310
    // entries to appear in the list later.
311
    return array_filter($plugins[$module][$type]);
312
  }
313

    
314
  // Check to see if we need to look for the file.
315
  if (!array_key_exists($id, $plugins[$module][$type])) {
316
    // If we can have child plugins, check to see if the plugin name is in the
317
    // format of parent:child and break it up if it is.
318
    if (!empty($info[$module][$type]['child plugins']) && strpos($id, ':') !== FALSE) {
319
      list($parent, $child) = explode(':', $id, 2);
320
    }
321
    else {
322
      $parent = $id;
323
    }
324

    
325
    if (!array_key_exists($parent, $plugins[$module][$type])) {
326
      $result = ctools_plugin_load_includes($info[$module][$type], $parent);
327
      // Set to either what was returned or NULL.
328
      $plugins[$module][$type][$parent] = isset($result[$parent]) ? $result[$parent] : NULL;
329
    }
330

    
331
    // If we are looking for a child, and have the parent, ask the parent for the child.
332
    if (!empty($child) && !empty($plugins[$module][$type][$parent]) && function_exists($plugins[$module][$type][$parent]['get child'])) {
333
      $plugins[$module][$type][$id] = $plugins[$module][$type][$parent]['get child']($plugins[$module][$type][$parent], $parent, $child);
334
    }
335
  }
336

    
337
  // At this point we should either have the plugin, or a NULL.
338
  return $plugins[$module][$type][$id];
339
}
340

    
341
/**
342
 * Return the full list of plugin type info for all plugin types registered in
343
 * the current system.
344
 *
345
 * This function manages its own cache getting/setting, and should always be
346
 * used as the way to initially populate the list of plugin types. Make sure you
347
 * call this function to properly populate the ctools_plugin_type_info static
348
 * variable.
349
 *
350
 * @return array
351
 *   A multilevel array of plugin type info, the outer array keyed on module
352
 *   name and each inner array keyed on plugin type name.
353
 */
354
function ctools_plugin_get_plugin_type_info($flush = FALSE) {
355
  static $drupal_static_fast;
356
  if (!isset($drupal_static_fast)) {
357
    $drupal_static_fast['info_loaded'] = &drupal_static('ctools_plugin_type_info_loaded', FALSE);
358
    $drupal_static_fast['all_type_info'] = &drupal_static('ctools_plugin_type_info', array());
359
  }
360
  $info_loaded = &$drupal_static_fast['info_loaded'];
361
  $all_type_info = &$drupal_static_fast['all_type_info'];
362

    
363
  // Only trigger info loading once.
364
  if ($info_loaded && !$flush) {
365
    return $all_type_info;
366
  }
367
  $info_loaded = TRUE;
368

    
369
  $cache = cache_get('ctools_plugin_type_info');
370
  if (!empty($cache->data) && !$flush) {
371
    // Plugin type info cache is warm, use it.
372
    $all_type_info = $cache->data;
373
  }
374
  else {
375
    // Cache expired, refill it.
376
    foreach (module_implements('ctools_plugin_type') as $module) {
377
      $module_infos = array();
378
      $function = $module . '_ctools_plugin_type';
379
      $module_infos = $function();
380

    
381
      foreach ($module_infos as $plugin_type_name => $plugin_type_info) {
382
        // Apply defaults. Array addition will not overwrite pre-existing keys.
383
        $plugin_type_info += array(
384
          'module' => $module,
385
          'type' => $plugin_type_name,
386
          'cache' => FALSE,
387
          'cache table' => 'cache',
388
          'classes' => array(),
389
          'use hooks' => FALSE,
390
          'defaults' => array(),
391
          'process' => '',
392
          'alterable' => TRUE,
393
          'extension' => 'inc',
394
          'info file' => FALSE,
395
          'hook' => $module . '_' . $plugin_type_name,
396
          'load themes' => FALSE,
397
        );
398
        $all_type_info[$module][$plugin_type_name] = $plugin_type_info;
399
      }
400
    }
401
    cache_set('ctools_plugin_type_info', $all_type_info);
402
  }
403

    
404
  return $all_type_info;
405
}
406

    
407
/**
408
 * Reset all static caches that affect the result of ctools_get_plugins().
409
 */
410
function ctools_get_plugins_reset() {
411
  drupal_static_reset('ctools_plugins');
412
  drupal_static_reset('ctools_plugin_setup');
413
  drupal_static_reset('ctools_plugin_load_includes');
414
  drupal_static_reset('ctools_plugin_api_info');
415
}
416

    
417
/**
418
 * Load plugins from a directory.
419
 *
420
 * @param array $info
421
 *   The plugin info as returned by ctools_plugin_get_info()
422
 * @param string $filename
423
 *   The file to load if we're looking for just one particular plugin.
424
 *
425
 * @return array
426
 *   A (possibly empty) array of information created for this plugin.
427
 */
428
function ctools_plugin_load_includes($info, $filename = NULL) {
429
  // Keep a static array so we don't hit file_scan_directory more than necessary.
430
  $all_files = &drupal_static(__FUNCTION__, array());
431

    
432
  // Store static of plugin arrays for reference because they can't be
433
  // reincluded, so there is no point in using drupal_static().
434
  static $plugin_arrays = array();
435

    
436
  if (!isset($all_files[$info['module']][$info['type']])) {
437
    $cache = cache_get("ctools_plugin_files:$info[module]:$info[type]");
438
    if ($cache) {
439
      $all_files[$info['module']][$info['type']] = $cache->data;
440
    }
441
    // Do not attempt any file scan even if the cached entry was empty.
442
    // A NULL entry here would mean the plugin just does not exists, and we
443
    // cannot afford to run file scan on production sites normal run.
444
    elseif (!isset($all_files[$info['module']][$info['type']])) {
445
      $all_files[$info['module']][$info['type']] = array();
446
      // Load all our plugins.
447
      $directories = ctools_plugin_get_directories($info);
448
      $extension = (empty($info['info file']) || ($info['extension'] != 'inc')) ? $info['extension'] : 'info';
449

    
450
      foreach ($directories as $module => $path) {
451
        $all_files[$info['module']][$info['type']][$module] = file_scan_directory($path, '/\.' . $extension . '$/', array('key' => 'name'));
452
      }
453

    
454
      cache_set("ctools_plugin_files:$info[module]:$info[type]", $all_files[$info['module']][$info['type']]);
455
    }
456
  }
457
  $file_list = $all_files[$info['module']][$info['type']];
458
  $plugins = array();
459

    
460
  // Iterate through all the plugin .inc files, load them and process the hook
461
  // that should now be available.
462
  foreach (array_filter($file_list) as $module => $files) {
463
    if ($filename) {
464
      $files = isset($files[$filename]) ? array($filename => $files[$filename]) : array();
465
    }
466
    foreach ($files as $file) {
467
      if (!empty($info['info file'])) {
468
        // Parse a .info file.
469
        $result = ctools_plugin_process_info($info, $module, $file);
470
      }
471
      else {
472
        // Parse a hook.
473
        // Ensure that we don't have something leftover from earlier.
474
        $plugin = NULL;
475

    
476
        if (isset($plugin_arrays[$file->uri])) {
477
          $identifier = $plugin_arrays[$file->uri];
478
        }
479
        else {
480
          include_once DRUPAL_ROOT . '/' . $file->uri;
481
          // .inc files have a special format for the hook identifier.
482
          // For example, 'foo.inc' in the module 'mogul' using the plugin
483
          // whose hook is named 'borg_type' should have a function named
484
          // (deep breath) mogul_foo_borg_type().
485
          // If, however, the .inc file set the quasi-global $plugin array, we
486
          // can use that and not even call a function. Set the $identifier
487
          // appropriately and ctools_plugin_process() will handle it.
488
          if (isset($plugin)) {
489
            $plugin_arrays[$file->uri] = $plugin;
490
            $identifier = $plugin;
491
          }
492
          else {
493
            $identifier = $module . '_' . $file->name;
494
          }
495
        }
496

    
497
        $result = ctools_plugin_process($info, $module, $identifier,
498
          dirname($file->uri), basename($file->uri), $file->name);
499
      }
500
      if (is_array($result)) {
501
        $plugins = array_merge($plugins, $result);
502
      }
503
    }
504
  }
505
  return $plugins;
506
}
507

    
508
/**
509
 * Get a list of directories to search for plugins of the given type.
510
 *
511
 * This utilizes hook_ctools_plugin_directory() to determine a complete list of
512
 * directories. Only modules that implement this hook and return a string
513
 * value will have their directories included.
514
 *
515
 * @param $info
516
 *   The $info array for the plugin as returned by ctools_plugin_get_info().
517
 *
518
 * @return array
519
 *   An array of directories to search.
520
 */
521
function ctools_plugin_get_directories($info) {
522
  $directories = array();
523

    
524
  foreach (module_implements('ctools_plugin_directory') as $module) {
525
    $function = $module . '_ctools_plugin_directory';
526
    $result = $function($info['module'], $info['type']);
527
    if ($result && is_string($result)) {
528
      $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
529
    }
530
  }
531

    
532
  if (!empty($info['load themes'])) {
533
    $themes = _ctools_list_themes();
534
    foreach ($themes as $name => $theme) {
535
      if (!empty($theme->info['plugins'][$info['module']][$info['type']])) {
536
        $directories[$name] = drupal_get_path('theme', $name) . '/' . $theme->info['plugins'][$info['module']][$info['type']];
537
      }
538
    }
539
  }
540
  return $directories;
541
}
542

    
543
/**
544
 * Helper to build a ctools-friendly list of themes capable of providing plugins.
545
 *
546
 * @return array
547
 *   A list of themes that can act as plugin providers, sorted parent-first with
548
 *   the active theme placed last.
549
 */
550
function _ctools_list_themes() {
551
  // @TODO: Use drupal_static() here?
552
  static $themes;
553
  if (is_null($themes)) {
554
    $current = variable_get('theme_default', FALSE);
555
    $themes = $active = array();
556
    $all_themes = list_themes();
557
    foreach ($all_themes as $name => $theme) {
558
      // Only search from active themes.
559
      if (empty($theme->status) && $theme->name != $current) {
560
        continue;
561
      }
562
      $active[$name] = $theme;
563
      // Prior to drupal 6.14, $theme->base_themes does not exist. Build it.
564
      if (!isset($theme->base_themes) && !empty($theme->base_theme)) {
565
        $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name);
566
      }
567
    }
568

    
569
    // Construct a parent-first list of all themes.
570
    foreach ($active as $name => $theme) {
571
      $base_themes = isset($theme->base_themes) ? $theme->base_themes : array();
572
      $themes = array_merge($themes, $base_themes, array($name => $theme->info['name']));
573
    }
574
    // Put the actual theme info objects into the array.
575
    foreach (array_keys($themes) as $name) {
576
      if (isset($all_themes[$name])) {
577
        $themes[$name] = $all_themes[$name];
578
      }
579
    }
580

    
581
    // Make sure the current default theme always gets the last word.
582
    if ($current_key = array_search($current, array_keys($themes))) {
583
      $themes += array_splice($themes, $current_key, 1);
584
    }
585
  }
586
  return $themes;
587
}
588

    
589
/**
590
 * Find all the base themes for the specified theme.
591
 *
592
 * Themes can inherit templates and function implementations from earlier themes.
593
 *
594
 * NOTE: this is a verbatim copy of system_find_base_themes(), which was not
595
 * implemented until 6.14. It is included here only as a fallback for outdated
596
 * versions of drupal core.
597
 *
598
 * @param $themes
599
 *   An array of available themes.
600
 * @param $key
601
 *   The name of the theme whose base we are looking for.
602
 * @param $used_keys
603
 *   A recursion parameter preventing endless loops.
604
 *
605
 * @return array
606
 *   Returns an array of all of the theme's ancestors; the first element's value
607
 *   will be NULL if an error occurred. (Note: this is NOT $arr[0]).
608
 */
609
function ctools_find_base_themes($themes, $key, $used_keys = array()) {
610
  $base_key = $themes[$key]->info['base theme'];
611
  // Does the base theme exist?
612
  if (!isset($themes[$base_key])) {
613
    return array($base_key => NULL);
614
  }
615

    
616
  $current_base_theme = array($base_key => $themes[$base_key]->info['name']);
617

    
618
  // Is the base theme itself a child of another theme?
619
  if (isset($themes[$base_key]->info['base theme'])) {
620
    // Do we already know the base themes of this theme?
621
    if (isset($themes[$base_key]->base_themes)) {
622
      return $themes[$base_key]->base_themes + $current_base_theme;
623
    }
624
    // Prevent loops.
625
    if (!empty($used_keys[$base_key])) {
626
      return array($base_key => NULL);
627
    }
628
    $used_keys[$base_key] = TRUE;
629
    return ctools_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme;
630
  }
631
  // If we get here, then this is our parent theme.
632
  return $current_base_theme;
633
}
634

    
635
/**
636
 * Load plugin info for the provided hook; this is handled separately from
637
 * plugins from files.
638
 *
639
 * @param $info
640
 *   The info array about the plugin as created by ctools_plugin_get_info()
641
 *
642
 * @return
643
 *   An array of info supplied by any hook implementations.
644
 */
645
function ctools_plugin_load_hooks($info) {
646
  $hooks = array();
647
  foreach (module_implements($info['hook']) as $module) {
648
    $result = ctools_plugin_process($info, $module, $module, drupal_get_path('module', $module));
649
    if (is_array($result)) {
650
      $hooks = array_merge($hooks, $result);
651
    }
652
  }
653
  return $hooks;
654
}
655

    
656
/**
657
 * Process a single hook implementation of a ctools plugin.
658
 *
659
 * @param array $info
660
 *   The $info array about the plugin as returned by ctools_plugin_get_info()
661
 * @param string $module
662
 *   The module that implements the plugin being processed.
663
 * @param string|array $identifier
664
 *   Used to create the base setting of return value. If:
665
 *    - $identifier is a string, a hook name is created from this and the 'hook'
666
 *      key of the $info array, and the return value of that hook function is
667
 *      used. The hook is called like this: $identifier_$hook($info);
668
 *    - $identifier is an array, this array is used directly.
669
 * @param string $path
670
 *   The path where files utilized by this plugin will be found.
671
 * @param string $file
672
 *   The file that was loaded for this plugin, if it exists.
673
 * @param string $base
674
 *   The base plugin name to use. If a file was loaded for the plugin, this
675
 *   is the plugin to assume must be present. This is used to automatically
676
 *   translate the array to make the syntax more friendly to plugin
677
 *   implementors.
678
 *
679
 * @return null|array
680
 *   NULL on failure, otherwise an array containing the results keyed by name.
681
 */
682
function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) {
683
  if (is_array($identifier)) {
684
    $result = $identifier;
685
  }
686
  else {
687
    $function = $identifier . '_' . $info['hook'];
688
    if (!function_exists($function)) {
689
      return NULL;
690
    }
691
    $result = $function($info);
692
    if (!isset($result) || !is_array($result)) {
693
      return NULL;
694
    }
695
  }
696

    
697
  // Automatically convert to the proper format that lets plugin implementations
698
  // not nest arrays as deeply as they used to, but still support the older
699
  // format where they do:
700
  if ($base && (!isset($result[$base]) || !is_array($result[$base]))) {
701
    $result = array($base => $result);
702
  }
703

    
704
  return _ctools_process_data($result, $info, $module, $path, $file);
705
}
706

    
707
/**
708
 * Fill in default values and run hooks for data loaded for one or
709
 * more plugins.
710
 */
711
function _ctools_process_data($result, $plugin_type_info, $module, $path, $file) {
712
  // Fill in global defaults.
713
  foreach ($result as $name => $plugin) {
714
    $result[$name] += array(
715
      'module' => $module,
716
      'name' => $name,
717
      'path' => $path,
718
      'file' => $file,
719
      'plugin module' => $plugin_type_info['module'],
720
      'plugin type' => $plugin_type_info['type'],
721
    );
722

    
723
    // Fill in plugin-specific defaults, if they exist.
724
    if (!empty($plugin_type_info['defaults'])) {
725
      if (is_array($plugin_type_info['defaults'])) {
726
        $result[$name] += $plugin_type_info['defaults'];
727
      }
728
    }
729

    
730
    // Allow the plugin to be altered before processing.
731
    if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
732
      drupal_alter('ctools_plugin_pre', $result[$name], $plugin_type_info);
733
    }
734

    
735
    // Allow the plugin owner to do additional processing.
736
    if (!empty($plugin_type_info['process']) && $function = ctools_plugin_get_function($plugin_type_info, 'process')) {
737
      $function($result[$name], $plugin_type_info);
738
    }
739

    
740
    // Allow the plugin to be altered after processing.
741
    if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) {
742
      drupal_alter('ctools_plugin_post', $result[$name], $plugin_type_info);
743
    }
744
  }
745
  return $result;
746
}
747

    
748
/**
749
 * Process an info file for plugin information, rather than a hook.
750
 *
751
 * @param array $info
752
 *   The $info array about the plugin as returned by ctools_plugin_get_info()
753
 * @param string $module
754
 *   The module that implements the plugin being processed.
755
 * @param object $file
756
 *   An object containing 'uri' and 'name' properties. 'uri' is the name of the
757
 *   'info' file to process. 'name' is the plugin key-name.
758
 *
759
 * @return null|array
760
 *   NULL on failure, otherwise an array containing the results keyed by name.
761
 */
762
function ctools_plugin_process_info($info, $module, $file) {
763
  $result = drupal_parse_info_file($file->uri);
764
  if ($result) {
765
    $result = array($file->name => $result);
766
    return _ctools_process_data($result, $info, $module, dirname($file->uri), basename($file->uri));
767
  }
768
}
769

    
770
/**
771
 * Ask a module for info about a particular plugin type.
772
 */
773
function ctools_plugin_get_info($module, $type) {
774
  $all_info = ctools_plugin_get_plugin_type_info();
775
  return isset($all_info[$module][$type]) ? $all_info[$module][$type] : array();
776
}
777

    
778
/**
779
 * Get a function from a plugin, if it exists. If the plugin is not already
780
 * loaded, try ctools_plugin_load_function() instead.
781
 *
782
 * @param $plugin_definition
783
 *   The loaded plugin type.
784
 * @param $function_name
785
 *   The identifier of the function. For example, 'settings form'.
786
 *
787
 * @return string
788
 *   The actual name of the function to call, or NULL if the function
789
 *   does not exist.
790
 */
791
function ctools_plugin_get_function($plugin_definition, $function_name) {
792
  // If cached the .inc file may not have been loaded. require_once is quite safe
793
  // and fast so it's okay to keep calling it.
794
  if (isset($plugin_definition['file'])) {
795
    // Plugins that are loaded from info files have the info file as
796
    // $plugin['file'].  Don't try to run those.
797
    $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
798
    if (empty($info['info file'])) {
799
      require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
800
    }
801
  }
802

    
803
  if (!isset($plugin_definition[$function_name])) {
804
    return NULL;
805
  }
806

    
807
  if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) {
808
    $function = $plugin_definition[$function_name]['function'];
809
    if (isset($plugin_definition[$function_name]['file'])) {
810
      $file = $plugin_definition[$function_name]['file'];
811
      if (isset($plugin_definition[$function_name]['path'])) {
812
        $file = $plugin_definition[$function_name]['path'] . '/' . $file;
813
      }
814
      require_once DRUPAL_ROOT . '/' . $file;
815
    }
816
  }
817
  else {
818
    $function = $plugin_definition[$function_name];
819
  }
820

    
821
  if (function_exists($function)) {
822
    return $function;
823
  }
824
}
825

    
826
/**
827
 * Load a plugin and get a function name from it, returning success only
828
 * if the function exists.
829
 *
830
 * @param $module
831
 *   The module that owns the plugin type.
832
 * @param $type
833
 *   The type of plugin.
834
 * @param $id
835
 *   The id of the specific plugin to load.
836
 * @param $function_name
837
 *   The identifier of the function. For example, 'settings form'.
838
 *
839
 * @return string
840
 *   The actual name of the function to call, or NULL if the function
841
 *   does not exist.
842
 */
843
function ctools_plugin_load_function($module, $type, $id, $function_name) {
844
  $plugin = ctools_get_plugins($module, $type, $id);
845
  return ctools_plugin_get_function($plugin, $function_name);
846
}
847

    
848
/**
849
 * Get a class from a plugin, if it exists. If the plugin is not already
850
 * loaded, try ctools_plugin_load_class() instead.
851
 *
852
 * @param $plugin_definition
853
 *   The loaded plugin type.
854
 * @param $class_name
855
 *   The identifier of the class. For example, 'handler'.
856
 *
857
 * @return string
858
 *   The actual name of the class to call, or NULL if the class does not exist.
859
 */
860
function ctools_plugin_get_class($plugin_definition, $class_name) {
861
  // If cached the .inc file may not have been loaded. require_once is quite safe
862
  // and fast so it's okay to keep calling it.
863
  if (isset($plugin_definition['file'])) {
864
    // Plugins that are loaded from info files have the info file as
865
    // $plugin['file'].  Don't try to run those.
866
    $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
867
    if (empty($info['info file'])) {
868
      require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
869
    }
870
  }
871

    
872
  $return = FALSE;
873
  if (!isset($plugin_definition[$class_name])) {
874
    return;
875
  }
876
  elseif (is_string($plugin_definition[$class_name])) {
877
    // Plugin uses the string form shorthand.
878
    $return = $plugin_definition[$class_name];
879
  }
880
  elseif (isset($plugin_definition[$class_name]['class'])) {
881
    // Plugin uses the verbose array form.
882
    $return = $plugin_definition[$class_name]['class'];
883
  }
884
  // @todo consider adding an else {watchdog(...)} here
885

    
886
  return ($return && class_exists($return)) ? $return : NULL;
887
}
888

    
889
/**
890
 * Load a plugin and get a class name from it, returning success only if the
891
 * class exists.
892
 *
893
 * @param $module
894
 *   The module that owns the plugin type.
895
 * @param $type
896
 *   The type of plugin.
897
 * @param $id
898
 *   The id of the specific plugin to load.
899
 * @param $class_name
900
 *   The identifier of the class. For example, 'handler'.
901
 *
902
 * @return string
903
 *   The actual name of the class to call, or NULL if the class does not exist.
904
 */
905
function ctools_plugin_load_class($module, $type, $id, $class_name) {
906
  $plugin = ctools_get_plugins($module, $type, $id);
907
  return ctools_plugin_get_class($plugin, $class_name);
908
}
909

    
910
/**
911
 * Sort callback for sorting plugins naturally.
912
 *
913
 * Sort first by weight, then by title.
914
 */
915
function ctools_plugin_sort($a, $b) {
916
  if (is_object($a)) {
917
    $a = (array) $a;
918
  }
919
  if (is_object($b)) {
920
    $b = (array) $b;
921
  }
922

    
923
  if (empty($a['weight'])) {
924
    $a['weight'] = 0;
925
  }
926

    
927
  if (empty($b['weight'])) {
928
    $b['weight'] = 0;
929
  }
930

    
931
  if ($a['weight'] == $b['weight']) {
932
    return strnatcmp(strtolower($a['title']), strtolower($b['title']));
933
  }
934
  return ($a['weight'] < $b['weight']) ? -1 : 1;
935
}