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 @ c304a780

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
  static $themes;
552
  if (is_null($themes)) {
553
    $current = variable_get('theme_default', FALSE);
554
    $themes = $active = array();
555
    $all_themes = list_themes();
556
    foreach ($all_themes as $name => $theme) {
557
      // Only search from active themes.
558
      if (empty($theme->status) && $theme->name != $current) {
559
        continue;
560
      }
561
      $active[$name] = $theme;
562
      // Prior to drupal 6.14, $theme->base_themes does not exist. Build it.
563
      if (!isset($theme->base_themes) && !empty($theme->base_theme)) {
564
        $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name);
565
      }
566
    }
567

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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