Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / includes / plugins.inc @ 1e39edcb

1
<?php
2

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

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

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

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

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

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

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

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

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

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

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

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

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

    
171
  return $info;
172
}
173

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

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

    
197
  return $hook;
198
}
199

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

    
225
  $info = ctools_plugin_get_plugin_type_info();
226

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

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

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

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

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

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

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

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

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

    
302

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

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

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

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

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

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

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

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

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

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

    
406
  return $all_type_info;
407
}
408

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

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

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

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

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

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

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

    
476
        if (isset($plugin_arrays[$file->uri])) {
477
          $identifier = $plugin_arrays[$file->uri];
478
        }
479
        else {
480

    
481
          include_once DRUPAL_ROOT . '/' . $file->uri;
482
          // .inc files have a special format for the hook identifier.
483
          // For example, 'foo.inc' in the module 'mogul' using the plugin
484
          // whose hook is named 'borg_type' should have a function named (deep breath)
485
          // mogul_foo_borg_type()
486

    
487
          // If, however, the .inc file set the quasi-global $plugin array, we
488
          // can use that and not even call a function. Set the $identifier
489
          // appropriately and ctools_plugin_process() will handle it.
490
          if (isset($plugin)) {
491
            $plugin_arrays[$file->uri] = $plugin;
492
            $identifier = $plugin;
493
          }
494
          else {
495
            $identifier = $module . '_' . $file->name;
496
          }
497
        }
498

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

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

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

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

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

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

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

    
590

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

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

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

    
636

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

    
658
/**
659
 * Process a single hook implementation of a ctools plugin.
660
 *
661
 * @param $info
662
 *   The $info array about the plugin as returned by ctools_plugin_get_info()
663
 * @param $module
664
 *   The module that implements the plugin being processed.
665
 * @param $identifier
666
 *   The plugin identifier, which is used to create the name of the hook
667
 *   function being called.
668
 * @param $path
669
 *   The path where files utilized by this plugin will be found.
670
 * @param $file
671
 *   The file that was loaded for this plugin, if it exists.
672
 * @param $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
function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) {
679
  if (is_array($identifier)) {
680
    $result = $identifier;
681
  }
682
  else {
683
    $function = $identifier . '_' . $info['hook'];
684
    if (!function_exists($function)) {
685
      return NULL;
686
    }
687
    $result = $function($info);
688
    if (!isset($result) || !is_array($result)) {
689
      return NULL;
690
    }
691
  }
692

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

    
700
  return _ctools_process_data($result, $info, $module, $path, $file);
701
}
702

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

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

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

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

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

    
744

    
745
/**
746
 * Process an info file for plugin information, rather than a hook.
747
 */
748
function ctools_plugin_process_info($info, $module, $file) {
749
  $result = drupal_parse_info_file($file->uri);
750
  if ($result) {
751
    $result = array($file->name => $result);
752
    return _ctools_process_data($result, $info, $module, dirname($file->uri), basename($file->uri));
753
  }
754
}
755

    
756
/**
757
 * Ask a module for info about a particular plugin type.
758
 */
759
function ctools_plugin_get_info($module, $type) {
760
  $all_info = ctools_plugin_get_plugin_type_info();
761
  return isset($all_info[$module][$type]) ? $all_info[$module][$type] : array();
762
}
763

    
764
/**
765
 * Get a function from a plugin, if it exists. If the plugin is not already
766
 * loaded, try ctools_plugin_load_function() instead.
767
 *
768
 * @param $plugin_definition
769
 *   The loaded plugin type.
770
 * @param $function_name
771
 *   The identifier of the function. For example, 'settings form'.
772
 *
773
 * @return
774
 *   The actual name of the function to call, or NULL if the function
775
 *   does not exist.
776
 */
777
function ctools_plugin_get_function($plugin_definition, $function_name) {
778
  // If cached the .inc file may not have been loaded. require_once is quite safe
779
  // and fast so it's okay to keep calling it.
780
  if (isset($plugin_definition['file'])) {
781
    // Plugins that are loaded from info files have the info file as
782
    // $plugin['file'].  Don't try to run those.
783
    $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
784
    if (empty($info['info file'])) {
785
      require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
786
    }
787
  }
788

    
789
  if (!isset($plugin_definition[$function_name])) {
790
    return;
791
  }
792

    
793
  if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) {
794
    $function = $plugin_definition[$function_name]['function'];
795
    if (isset($plugin_definition[$function_name]['file'])) {
796
      $file = $plugin_definition[$function_name]['file'];
797
      if (isset($plugin_definition[$function_name]['path'])) {
798
        $file = $plugin_definition[$function_name]['path'] . '/' . $file;
799
      }
800
      require_once DRUPAL_ROOT . '/' . $file;
801
    }
802
  }
803
  else {
804
    $function = $plugin_definition[$function_name];
805
  }
806

    
807
  if (function_exists($function)) {
808
    return $function;
809
  }
810
}
811

    
812
/**
813
 * Load a plugin and get a function name from it, returning success only
814
 * if the function exists.
815
 *
816
 * @param $module
817
 *   The module that owns the plugin type.
818
 * @param $type
819
 *   The type of plugin.
820
 * @param $id
821
 *   The id of the specific plugin to load.
822
 * @param $function_name
823
 *   The identifier of the function. For example, 'settings form'.
824
 *
825
 * @return
826
 *   The actual name of the function to call, or NULL if the function
827
 *   does not exist.
828
 */
829
function ctools_plugin_load_function($module, $type, $id, $function_name) {
830
  $plugin = ctools_get_plugins($module, $type, $id);
831
  return ctools_plugin_get_function($plugin, $function_name);
832
}
833

    
834
/**
835
 * Get a class from a plugin, if it exists. If the plugin is not already
836
 * loaded, try ctools_plugin_load_class() instead.
837
 *
838
 * @param $plugin_definition
839
 *   The loaded plugin type.
840
 * @param $class_name
841
 *   The identifier of the class. For example, 'handler'.
842
 *
843
 * @return
844
 *   The actual name of the class to call, or NULL if the class does not exist.
845
 */
846
function ctools_plugin_get_class($plugin_definition, $class_name) {
847
  // If cached the .inc file may not have been loaded. require_once is quite safe
848
  // and fast so it's okay to keep calling it.
849
  if (isset($plugin_definition['file'])) {
850
    // Plugins that are loaded from info files have the info file as
851
    // $plugin['file'].  Don't try to run those.
852
    $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']);
853
    if (empty($info['info file'])) {
854
      require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file'];
855
    }
856
  }
857

    
858
  $return = FALSE;
859
  if (!isset($plugin_definition[$class_name])) {
860
    return;
861
  }
862
  else if (is_string($plugin_definition[$class_name])) {
863
    // Plugin uses the string form shorthand.
864
    $return = $plugin_definition[$class_name];
865
  }
866
  else if (isset($plugin_definition[$class_name]['class'])) {
867
    // Plugin uses the verbose array form.
868
    $return = $plugin_definition[$class_name]['class'];
869
  }
870
  // @todo consider adding an else {watchdog(...)} here
871

    
872
  return ($return && class_exists($return)) ? $return : NULL;
873
}
874

    
875
/**
876
 * Load a plugin and get a class name from it, returning success only if the
877
 * class exists.
878
 *
879
 * @param $module
880
 *   The module that owns the plugin type.
881
 * @param $type
882
 *   The type of plugin.
883
 * @param $id
884
 *   The id of the specific plugin to load.
885
 * @param $class_name
886
 *   The identifier of the class. For example, 'handler'.
887
 *
888
 * @return
889
 *   The actual name of the class to call, or NULL if the class does not exist.
890
 */
891
function ctools_plugin_load_class($module, $type, $id, $class_name) {
892
  $plugin = ctools_get_plugins($module, $type, $id);
893
  return ctools_plugin_get_class($plugin, $class_name);
894
}
895

    
896
/**
897
 * Sort callback for sorting plugins naturally.
898
 *
899
 * Sort first by weight, then by title.
900
 */
901
function ctools_plugin_sort($a, $b) {
902
  if (is_object($a)) {
903
    $a = (array) $a;
904
  }
905
  if (is_object($b)) {
906
    $b = (array) $b;
907
  }
908

    
909
  if (empty($a['weight'])) {
910
    $a['weight'] = 0;
911
  }
912

    
913
  if (empty($b['weight'])) {
914
    $b['weight'] = 0;
915
  }
916

    
917
  if ($a['weight'] == $b['weight']) {
918
    return strnatcmp(strtolower($a['title']), strtolower($b['title']));
919
  }
920
  return ($a['weight'] < $b['weight']) ? -1 : 1;
921
}