[177a560] | 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 = &ctools_static(__FUNCTION__, array()); |
---|
| 59 | if (!isset($cache[$owner][$api])) { |
---|
| 60 | $cache[$owner][$api] = array(); |
---|
| 61 | foreach (module_implements('ctools_plugin_api') as $module) { |
---|
| 62 | $function = $module . '_ctools_plugin_api'; |
---|
| 63 | $info = $function($owner, $api); |
---|
| 64 | if (!isset($info['version'])) { |
---|
| 65 | continue; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // Only process if version is between minimum and current, inclusive. |
---|
| 69 | if ($info['version'] >= $minimum_version && $info['version'] <= $current_version) { |
---|
| 70 | if (!isset($info['path'])) { |
---|
| 71 | $info['path'] = drupal_get_path('module', $module); |
---|
| 72 | } |
---|
| 73 | $cache[$owner][$api][$module] = $info; |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | // And allow themes to implement these as well. |
---|
| 78 | $themes = _ctools_list_themes(); |
---|
| 79 | foreach ($themes as $name => $theme) { |
---|
| 80 | if (!empty($theme->info['api'][$owner][$api])) { |
---|
| 81 | $info = $theme->info['api'][$owner][$api]; |
---|
| 82 | if (!isset($info['version'])) { |
---|
| 83 | continue; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | // Only process if version is between minimum and current, inclusive. |
---|
| 87 | if ($info['version'] >= $minimum_version && $info['version'] <= $current_version) { |
---|
| 88 | if (!isset($info['path'])) { |
---|
| 89 | $info['path'] = ''; |
---|
| 90 | } |
---|
| 91 | // Because themes can't easily specify full path, we add it here |
---|
| 92 | // even though we do not for modules: |
---|
| 93 | $info['path'] = drupal_get_path('theme', $name) . '/' . $info['path']; |
---|
| 94 | $cache[$owner][$api][$name] = $info; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | return $cache[$owner][$api]; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | /** |
---|
| 104 | * Load a group of API files. |
---|
| 105 | * |
---|
| 106 | * This will ask each module if they support the given API, and if they do |
---|
| 107 | * it will load the specified file name. The API and the file name |
---|
| 108 | * coincide by design. |
---|
| 109 | * |
---|
| 110 | * @param $owner |
---|
| 111 | * The name of the module that controls the API. |
---|
| 112 | * @param $api |
---|
| 113 | * The name of the api. The api name forms the file name: |
---|
| 114 | * $module.$api.inc, though this can be overridden by the module's response. |
---|
| 115 | * @param $minimum_version |
---|
| 116 | * The lowest version API that is compatible with this one. If a module |
---|
| 117 | * reports its API as older than this, its files will not be loaded. This |
---|
| 118 | * should never change during operation. |
---|
| 119 | * @param $current_version |
---|
| 120 | * The current version of the api. If a module reports its minimum API as |
---|
| 121 | * higher than this, its files will not be loaded. This should never change |
---|
| 122 | * during operation. |
---|
| 123 | * |
---|
| 124 | * @return |
---|
| 125 | * The API information, in case you need it. |
---|
| 126 | */ |
---|
| 127 | function ctools_plugin_api_include($owner, $api, $minimum_version, $current_version) { |
---|
| 128 | static $already_done = array(); |
---|
| 129 | |
---|
| 130 | $info = ctools_plugin_api_info($owner, $api, $minimum_version, $current_version); |
---|
| 131 | if (!isset($already_done[$owner][$api])) { |
---|
| 132 | foreach ($info as $module => $plugin_info) { |
---|
| 133 | if (!isset($plugin_info['file'])) { |
---|
| 134 | $plugin_info['file'] = "$module.$api.inc"; |
---|
| 135 | } |
---|
| 136 | if (file_exists("./$plugin_info[path]/$plugin_info[file]")) { |
---|
| 137 | $plugin_info[$module]['included'] = TRUE; |
---|
| 138 | require_once "./$plugin_info[path]/$plugin_info[file]"; |
---|
| 139 | } |
---|
| 140 | $info[$module] = $plugin_info; |
---|
| 141 | } |
---|
| 142 | $already_done[$owner][$api] = TRUE; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | return $info; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Fetch a group of plugins by name. |
---|
| 150 | * |
---|
| 151 | * @param $module |
---|
| 152 | * The name of the module that utilizes this plugin system. It will be |
---|
| 153 | * used to call hook_ctools_plugin_$plugin() to get more data about the plugin. |
---|
| 154 | * @param $type |
---|
| 155 | * The type identifier of the plugin. |
---|
| 156 | * @param $id |
---|
| 157 | * If specified, return only information about plugin with this identifier. |
---|
| 158 | * The system will do its utmost to load only plugins with this id. |
---|
| 159 | * |
---|
| 160 | * @return |
---|
| 161 | * An array of information arrays about the plugins received. The contents |
---|
| 162 | * of the array are specific to the plugin. |
---|
| 163 | */ |
---|
| 164 | function ctools_get_plugins($module, $type, $id = NULL) { |
---|
| 165 | // Store local caches of plugins and plugin info so we don't have to do full |
---|
| 166 | // lookups everytime. |
---|
| 167 | $info = &ctools_static('ctools_plugin_info', array()); |
---|
| 168 | $plugins = &ctools_static('ctools_plugins', array()); |
---|
| 169 | |
---|
| 170 | // Attempt to shortcut this whole piece of code if we already have |
---|
| 171 | // the requested plugin: |
---|
| 172 | if ($id && isset($plugins[$module][$type]) && array_key_exists($id, $plugins[$module][$type])) { |
---|
| 173 | return $plugins[$module][$type][$id]; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | // Store the status of plugin loading. If a module plugin type pair is true, |
---|
| 177 | // then it is fully loaded and no searching or setup needs to be done. |
---|
| 178 | $setup = &ctools_static('ctools_plugin_setup', array()); |
---|
| 179 | |
---|
| 180 | // Request metadata/defaults for this plugin from the declaring module. This |
---|
| 181 | // is done once per page request, upon a request being made for that plugin. |
---|
| 182 | if (!isset($info[$module][$type])) { |
---|
| 183 | $info[$module][$type] = ctools_plugin_get_info($module, $type); |
---|
| 184 | // Also, initialize the local plugin cache. |
---|
| 185 | $plugins[$module][$type] = array(); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // We assume we don't need to build a cache. |
---|
| 189 | $build_cache = FALSE; |
---|
| 190 | |
---|
| 191 | // If the plugin info says this can be cached, check cache first. |
---|
| 192 | if ($info[$module][$type]['cache'] && empty($setup[$module][$type])) { |
---|
| 193 | $cache = cache_get("plugins:$module:$type", $info[$module][$type]['cache table']); |
---|
| 194 | |
---|
| 195 | if (!empty($cache->data)) { |
---|
| 196 | // Cache load succeeded so use the cached plugin list. |
---|
| 197 | $plugins[$module][$type] = $cache->data; |
---|
| 198 | // Set $setup to true so we know things where loaded. |
---|
| 199 | $setup[$module][$type] = TRUE; |
---|
| 200 | } |
---|
| 201 | else { |
---|
| 202 | // Cache load failed so store that we need to build and write the cache. |
---|
| 203 | $build_cache = TRUE; |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | // Always load all hooks if we need them. Note we only need them now if the |
---|
| 208 | // plugin asks for them. We can assume that if we have plugins we've already |
---|
| 209 | // called the global hook. |
---|
| 210 | if (!empty($info[$module][$type]['use hooks']) && empty($plugins[$module][$type])) { |
---|
| 211 | $plugins[$module][$type] = ctools_plugin_load_hooks($info[$module][$type]); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | // Then see if we should load all files. We only do this if we |
---|
| 215 | // want a list of all plugins or there was a cache miss. |
---|
| 216 | if (empty($setup[$module][$type]) && ($build_cache || !$id)) { |
---|
| 217 | $setup[$module][$type] = TRUE; |
---|
| 218 | $plugins[$module][$type] = array_merge($plugins[$module][$type], ctools_plugin_load_includes($info[$module][$type])); |
---|
| 219 | // If the plugin can have child plugins, and we're loading all plugins, |
---|
| 220 | // go through the list of plugins we have and find child plugins. |
---|
| 221 | if (!$id && !empty($info[$module][$type]['child plugins'])) { |
---|
| 222 | // If a plugin supports children, go through each plugin and ask. |
---|
| 223 | $temp = array(); |
---|
| 224 | foreach ($plugins[$module][$type] as $name => $plugin) { |
---|
| 225 | if (!empty($plugin['get children']) && function_exists($plugin['get children'])) { |
---|
| 226 | $temp = array_merge($plugin['get children']($plugin, $name), $temp); |
---|
| 227 | } |
---|
| 228 | else { |
---|
| 229 | $temp[$name] = $plugin; |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | $plugins[$module][$type] = $temp; |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | // If we were told earlier that this is cacheable and the cache was |
---|
| 238 | // empty, give something back. |
---|
| 239 | if ($build_cache) { |
---|
| 240 | cache_set("plugins:$module:$type", $plugins[$module][$type], $info[$module][$type]['cache table']); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // If no id was requested, we are finished here: |
---|
| 244 | if (!$id) { |
---|
| 245 | // Use array_filter because looking for unknown plugins could cause NULL |
---|
| 246 | // entries to appear in the list later. |
---|
| 247 | return array_filter($plugins[$module][$type]); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | // Check to see if we need to look for the file |
---|
| 251 | if (!array_key_exists($id, $plugins[$module][$type])) { |
---|
| 252 | // If we can have child plugins, check to see if the plugin name is in the |
---|
| 253 | // format of parent:child and break it up if it is. |
---|
| 254 | if (!empty($info[$module][$type]['child plugins']) && strpos($id, ':') !== FALSE) { |
---|
| 255 | list($parent, $child) = explode(':', $id, 2); |
---|
| 256 | } |
---|
| 257 | else { |
---|
| 258 | $parent = $id; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | if (!array_key_exists($parent, $plugins[$module][$type])) { |
---|
| 262 | $result = ctools_plugin_load_includes($info[$module][$type], $parent); |
---|
| 263 | // Set to either what was returned or NULL. |
---|
| 264 | $plugins[$module][$type][$parent] = isset($result[$parent]) ? $result[$parent] : NULL; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | // If we are looking for a child, and have the parent, ask the parent for the child. |
---|
| 268 | if (!empty($child) && !empty($plugins[$module][$type][$parent]) && function_exists($plugins[$module][$type][$parent]['get child'])) { |
---|
| 269 | $plugins[$module][$type][$id] = $plugins[$module][$type][$parent]['get child']($plugins[$module][$type][$parent], $parent, $child); |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | // At this point we should either have the plugin, or a NULL. |
---|
| 274 | return $plugins[$module][$type][$id]; |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
[2efe680] | 278 | * Reset all static caches that affect the result of ctools_get_plugins(). |
---|
| 279 | */ |
---|
| 280 | function ctools_get_plugins_reset() { |
---|
| 281 | ctools_static_reset('ctools_plugin_load_includes'); |
---|
| 282 | ctools_static_reset('ctools_plugin_api_info'); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | /** |
---|
[177a560] | 286 | * Load plugins from a directory. |
---|
| 287 | * |
---|
| 288 | * @param $info |
---|
| 289 | * The plugin info as returned by ctools_plugin_get_info() |
---|
| 290 | * @param $file |
---|
| 291 | * The file to load if we're looking for just one particular plugin. |
---|
| 292 | * |
---|
| 293 | * @return |
---|
| 294 | * An array of information created for this plugin. |
---|
| 295 | */ |
---|
| 296 | function ctools_plugin_load_includes($info, $filename = NULL) { |
---|
| 297 | // Keep a static array so we don't hit drupal_system_listing more than necessary. |
---|
| 298 | $all_files = &ctools_static(__FUNCTION__, array()); |
---|
| 299 | |
---|
[2efe680] | 300 | // store static of plugin arrays for reference because they can't be reincluded. |
---|
| 301 | static $plugin_arrays = array(); |
---|
| 302 | |
---|
[177a560] | 303 | // If we're being asked for all plugins of a type, skip any caching |
---|
| 304 | // we may have done because this is an admin task and it's ok to |
---|
| 305 | // spend the extra time. |
---|
| 306 | if (!isset($filename)) { |
---|
| 307 | $all_files[$info['module']][$info['type']] = NULL; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | if (!isset($all_files[$info['module']][$info['type']])) { |
---|
| 311 | // If a filename was set, we will try to load our list of files from |
---|
| 312 | // cache. This is considered normal operation and we try to reduce |
---|
| 313 | // the time spent finding files. |
---|
| 314 | if (isset($filename)) { |
---|
| 315 | $cache = cache_get("ctools_plugin_files:$info[module]:$info[type]"); |
---|
| 316 | if ($cache) { |
---|
| 317 | $all_files[$info['module']][$info['type']] = $cache->data; |
---|
| 318 | } |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | if (!isset($all_files[$info['module']][$info['type']])) { |
---|
| 322 | $all_files[$info['module']][$info['type']] = array(); |
---|
| 323 | // Load all our plugins. |
---|
| 324 | $directories = ctools_plugin_get_directories($info); |
---|
| 325 | $extension = empty($info['info file']) ? $info['extension'] : 'info'; |
---|
| 326 | |
---|
| 327 | foreach ($directories as $module => $path) { |
---|
| 328 | $all_files[$info['module']][$info['type']][$module] = drupal_system_listing('\.' . $extension . '$', $path, 'name', 0); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | cache_set("ctools_plugin_files:$info[module]:$info[type]", $all_files[$info['module']][$info['type']]); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | $file_list = $all_files[$info['module']][$info['type']]; |
---|
| 335 | $plugins = array(); |
---|
| 336 | |
---|
| 337 | // Iterate through all the plugin .inc files, load them and process the hook |
---|
| 338 | // that should now be available. |
---|
| 339 | foreach (array_filter($file_list) as $module => $files) { |
---|
| 340 | if ($filename) { |
---|
| 341 | $files = isset($files[$filename]) ? array($filename => $files[$filename]) : array(); |
---|
| 342 | } |
---|
| 343 | foreach ($files as $file) { |
---|
| 344 | if (!empty($info['info file'])) { |
---|
| 345 | // Parse a .info file |
---|
| 346 | $result = ctools_plugin_process_info($info, $module, $file); |
---|
| 347 | } |
---|
| 348 | else { |
---|
| 349 | // Parse a hook. |
---|
| 350 | $plugin = NULL; // ensure that we don't have something leftover from earlier. |
---|
[2efe680] | 351 | |
---|
| 352 | if (isset($plugin_arrays[$file->filename])) { |
---|
| 353 | $identifier = $plugin_arrays[$file->filename]; |
---|
| 354 | } |
---|
| 355 | else { |
---|
| 356 | |
---|
| 357 | require_once './' . $file->filename; |
---|
| 358 | // .inc files have a special format for the hook identifier. |
---|
| 359 | // For example, 'foo.inc' in the module 'mogul' using the plugin |
---|
| 360 | // whose hook is named 'borg_type' should have a function named (deep breath) |
---|
| 361 | // mogul_foo_borg_type() |
---|
| 362 | |
---|
| 363 | // If, however, the .inc file set the quasi-global $plugin array, we |
---|
| 364 | // can use that and not even call a function. Set the $identifier |
---|
| 365 | // appropriately and ctools_plugin_process() will handle it. |
---|
| 366 | if (isset($plugin)) { |
---|
| 367 | $plugin_arrays[$file->filename] = $plugin; |
---|
| 368 | $identifier = $plugin; |
---|
| 369 | } |
---|
| 370 | else { |
---|
| 371 | $identifier = $module . '_' . $file->name; |
---|
| 372 | } |
---|
| 373 | } |
---|
[177a560] | 374 | $result = ctools_plugin_process($info, $module, $identifier, dirname($file->filename), basename($file->filename), $file->name); |
---|
| 375 | } |
---|
| 376 | if (is_array($result)) { |
---|
| 377 | $plugins = array_merge($plugins, $result); |
---|
| 378 | } |
---|
| 379 | } |
---|
| 380 | } |
---|
| 381 | return $plugins; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | * Get a list of directories to search for plugins of the given type. |
---|
| 386 | * |
---|
| 387 | * This utilizes hook_ctools_plugin_directory() to determine a complete list of |
---|
| 388 | * directories. Only modules that implement this hook and return a string |
---|
| 389 | * value will have their directories included. |
---|
| 390 | * |
---|
| 391 | * @param $info |
---|
| 392 | * The $info array for the plugin as returned by ctools_plugin_get_info(). |
---|
| 393 | * |
---|
| 394 | * @return array $directories |
---|
| 395 | * An array of directories to search. |
---|
| 396 | */ |
---|
| 397 | function ctools_plugin_get_directories($info) { |
---|
| 398 | $directories = array(); |
---|
| 399 | |
---|
| 400 | foreach (module_implements('ctools_plugin_directory') as $module) { |
---|
| 401 | $function = $module . '_ctools_plugin_directory'; |
---|
| 402 | $result = $function($info['module'], $info['type']); |
---|
| 403 | if ($result && is_string($result)) { |
---|
| 404 | $directories[$module] = drupal_get_path('module', $module) . '/' . $result; |
---|
| 405 | } |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | if (!empty($info['load themes'])) { |
---|
| 409 | $themes = _ctools_list_themes(); |
---|
| 410 | foreach ($themes as $name => $theme) { |
---|
| 411 | if (!empty($theme->info['plugins'][$info['module']][$info['type']])) { |
---|
| 412 | $directories[$name] = drupal_get_path('theme', $name) . '/' . $theme->info['plugins'][$info['module']][$info['type']]; |
---|
| 413 | } |
---|
| 414 | } |
---|
| 415 | } |
---|
| 416 | return $directories; |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | /** |
---|
| 420 | * Helper function to build a ctools-friendly list of themes capable of |
---|
| 421 | * providing plugins. |
---|
| 422 | * |
---|
| 423 | * @return array $themes |
---|
| 424 | * A list of themes that can act as plugin providers, sorted parent-first with |
---|
| 425 | * the active theme placed last. |
---|
| 426 | */ |
---|
| 427 | function _ctools_list_themes() { |
---|
| 428 | static $themes; |
---|
| 429 | if (is_null($themes)) { |
---|
| 430 | $current = variable_get('theme_default', FALSE); |
---|
| 431 | $themes = $active = array(); |
---|
| 432 | $all_themes = list_themes(); |
---|
| 433 | foreach ($all_themes as $name => $theme) { |
---|
| 434 | // Only search from active themes |
---|
| 435 | if (empty($theme->status) && $theme->name != $current) { |
---|
| 436 | continue; |
---|
| 437 | } |
---|
| 438 | $active[$name] = $theme; |
---|
| 439 | // Prior to drupal 6.14, $theme->base_themes does not exist. Build it. |
---|
| 440 | if (!isset($theme->base_themes) && !empty($theme->base_theme)) { |
---|
| 441 | $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name); |
---|
| 442 | } |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | // Construct a parent-first list of all themes |
---|
| 446 | foreach ($active as $name => $theme) { |
---|
| 447 | $base_themes = isset($theme->base_themes) ? $theme->base_themes : array(); |
---|
| 448 | $themes = array_merge($themes, $base_themes, array($name => $theme->info['name'])); |
---|
| 449 | } |
---|
| 450 | // Put the actual theme info objects into the array |
---|
| 451 | foreach (array_keys($themes) as $name) { |
---|
| 452 | $themes[$name] = $all_themes[$name]; |
---|
| 453 | } |
---|
| 454 | |
---|
| 455 | // Make sure the current default theme always gets the last word |
---|
| 456 | if ($current_key = array_search($current, array_keys($themes))) { |
---|
| 457 | $themes += array_splice($themes, $current_key, 1); |
---|
| 458 | } |
---|
| 459 | } |
---|
| 460 | return $themes; |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | |
---|
| 464 | /** |
---|
| 465 | * Find all the base themes for the specified theme. |
---|
| 466 | * |
---|
| 467 | * Themes can inherit templates and function implementations from earlier themes. |
---|
| 468 | * |
---|
| 469 | * NOTE: this is a verbatim copy of system_find_base_themes(), which was not |
---|
| 470 | * implemented until 6.14. It is included here only as a fallback for outdated |
---|
| 471 | * versions of drupal core. |
---|
| 472 | * |
---|
| 473 | * @param $themes |
---|
| 474 | * An array of available themes. |
---|
| 475 | * @param $key |
---|
| 476 | * The name of the theme whose base we are looking for. |
---|
| 477 | * @param $used_keys |
---|
| 478 | * A recursion parameter preventing endless loops. |
---|
| 479 | * @return |
---|
| 480 | * Returns an array of all of the theme's ancestors; the first element's value |
---|
| 481 | * will be NULL if an error occurred. |
---|
| 482 | */ |
---|
| 483 | function ctools_find_base_themes($themes, $key, $used_keys = array()) { |
---|
| 484 | $base_key = $themes[$key]->info['base theme']; |
---|
| 485 | // Does the base theme exist? |
---|
| 486 | if (!isset($themes[$base_key])) { |
---|
| 487 | return array($base_key => NULL); |
---|
| 488 | } |
---|
| 489 | |
---|
| 490 | $current_base_theme = array($base_key => $themes[$base_key]->info['name']); |
---|
| 491 | |
---|
| 492 | // Is the base theme itself a child of another theme? |
---|
| 493 | if (isset($themes[$base_key]->info['base theme'])) { |
---|
| 494 | // Do we already know the base themes of this theme? |
---|
| 495 | if (isset($themes[$base_key]->base_themes)) { |
---|
| 496 | return $themes[$base_key]->base_themes + $current_base_theme; |
---|
| 497 | } |
---|
| 498 | // Prevent loops. |
---|
| 499 | if (!empty($used_keys[$base_key])) { |
---|
| 500 | return array($base_key => NULL); |
---|
| 501 | } |
---|
| 502 | $used_keys[$base_key] = TRUE; |
---|
| 503 | return ctools_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme; |
---|
| 504 | } |
---|
| 505 | // If we get here, then this is our parent theme. |
---|
| 506 | return $current_base_theme; |
---|
| 507 | } |
---|
| 508 | |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | * Load plugin info for the provided hook; this is handled separately from |
---|
| 512 | * plugins from files. |
---|
| 513 | * |
---|
| 514 | * @param $info |
---|
| 515 | * The info array about the plugin as created by ctools_plugin_get_info() |
---|
| 516 | * |
---|
| 517 | * @return |
---|
| 518 | * An array of info supplied by any hook implementations. |
---|
| 519 | */ |
---|
| 520 | function ctools_plugin_load_hooks($info) { |
---|
| 521 | $hooks = array(); |
---|
| 522 | foreach (module_implements($info['hook']) as $module) { |
---|
| 523 | $result = ctools_plugin_process($info, $module, $module, drupal_get_path('module', $module)); |
---|
| 524 | if (is_array($result)) { |
---|
| 525 | $hooks = array_merge($hooks, $result); |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | return $hooks; |
---|
| 529 | } |
---|
| 530 | |
---|
| 531 | /** |
---|
| 532 | * Process a single hook implementation of a ctools plugin. |
---|
| 533 | * |
---|
| 534 | * @param $info |
---|
| 535 | * The $info array about the plugin as returned by ctools_plugin_get_info() |
---|
| 536 | * @param $module |
---|
| 537 | * The module that implements the plugin being processed. |
---|
| 538 | * @param $identifier |
---|
| 539 | * The plugin identifier, which is used to create the name of the hook |
---|
| 540 | * function being called. |
---|
| 541 | * @param $path |
---|
| 542 | * The path where files utilized by this plugin will be found. |
---|
| 543 | * @param $file |
---|
| 544 | * The file that was loaded for this plugin, if it exists. |
---|
| 545 | * @param $base |
---|
| 546 | * The base plugin name to use. If a file was loaded for the plugin, this |
---|
| 547 | * is the plugin to assume must be present. This is used to automatically |
---|
| 548 | * translate the array to make the syntax more friendly to plugin |
---|
| 549 | * implementors. |
---|
| 550 | */ |
---|
| 551 | function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) { |
---|
| 552 | if (is_array($identifier)) { |
---|
| 553 | $result = $identifier; |
---|
| 554 | } |
---|
| 555 | else { |
---|
| 556 | $function = $identifier . '_' . $info['hook']; |
---|
| 557 | if (!function_exists($function)) { |
---|
| 558 | return NULL; |
---|
| 559 | } |
---|
| 560 | $result = $function(); |
---|
| 561 | if (!isset($result) || !is_array($result)) { |
---|
| 562 | return NULL; |
---|
| 563 | } |
---|
| 564 | } |
---|
| 565 | |
---|
| 566 | // Automatically convert to the proper format that lets plugin implementations |
---|
| 567 | // not nest arrays as deeply as they used to, but still support the older |
---|
| 568 | // format where they do: |
---|
| 569 | if ($base && (!isset($result[$base]) || !is_array($result[$base]))) { |
---|
| 570 | $result = array($base => $result); |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | return _ctools_process_data($result, $info, $module, $path, $file); |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | /** |
---|
| 577 | * Fill in default values and run hooks for data loaded for one or |
---|
| 578 | * more plugins. |
---|
| 579 | */ |
---|
| 580 | function _ctools_process_data($result, $info, $module, $path, $file) { |
---|
| 581 | // Fill in global defaults. |
---|
| 582 | foreach ($result as $name => $plugin) { |
---|
| 583 | $result[$name] += array( |
---|
| 584 | 'module' => $module, |
---|
| 585 | 'name' => $name, |
---|
| 586 | 'path' => $path, |
---|
| 587 | 'file' => $file, |
---|
| 588 | 'plugin module' => $info['module'], |
---|
| 589 | 'plugin type' => $info['type'], |
---|
| 590 | ); |
---|
| 591 | |
---|
| 592 | // Fill in plugin-specific defaults, if they exist. |
---|
| 593 | if (!empty($info['defaults'])) { |
---|
| 594 | if (is_array($info['defaults'])) { |
---|
| 595 | $result[$name] += $info['defaults']; |
---|
| 596 | } |
---|
| 597 | // FIXME This callback-based approach for the 'defaults' key is entirely |
---|
| 598 | // redundant with the 'process' callback. Consequently, that approach is |
---|
| 599 | // DEPRECATED in favor using a 'process' callback. In the next major |
---|
| 600 | // version, 'defaults' callbacks will be removed entirely; only the array |
---|
| 601 | // addition 'defaults' approach will be allowed. |
---|
| 602 | else if (function_exists($info['defaults'])) { |
---|
| 603 | $info['defaults']($info, $result[$name]); |
---|
| 604 | } |
---|
| 605 | } |
---|
| 606 | |
---|
| 607 | // Allow the plugin owner to do additional processing. |
---|
| 608 | if (!empty($info['process']) && function_exists($info['process'])) { |
---|
| 609 | $info['process']($result[$name], $info); |
---|
| 610 | } |
---|
| 611 | } |
---|
| 612 | return $result; |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | |
---|
| 616 | /** |
---|
| 617 | * Process an info file for plugin information, rather than a hook. |
---|
| 618 | */ |
---|
| 619 | function ctools_plugin_process_info($info, $module, $file) { |
---|
| 620 | $result = drupal_parse_info_file($file->filename); |
---|
| 621 | if ($result) { |
---|
| 622 | $result = array($file->name => $result); |
---|
| 623 | return _ctools_process_data($result, $info, $module, dirname($file->filename), basename($file->filename)); |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | /** |
---|
| 628 | * Ask a module for info about a particular plugin type. |
---|
| 629 | */ |
---|
| 630 | function ctools_plugin_get_info($module, $type) { |
---|
| 631 | $info = array(); |
---|
| 632 | $function = $module . '_ctools_plugin_' . $type; |
---|
| 633 | if (function_exists($function)) { |
---|
| 634 | $info = $function(); |
---|
| 635 | } |
---|
| 636 | |
---|
| 637 | // Apply defaults. Array addition will not overwrite pre-existing keys. |
---|
| 638 | $info += array( |
---|
| 639 | 'module' => $module, |
---|
| 640 | 'type' => $type, |
---|
| 641 | 'cache' => FALSE, |
---|
| 642 | 'cache table' => 'cache', |
---|
| 643 | 'use hooks' => TRUE, // TODO will default to FALSE in next major version |
---|
| 644 | 'defaults' => array(), |
---|
| 645 | 'process' => '', |
---|
| 646 | 'extension' => 'inc', |
---|
| 647 | 'info file' => FALSE, |
---|
| 648 | 'hook' => $module . '_' . $type, |
---|
| 649 | 'load themes' => FALSE, |
---|
| 650 | ); |
---|
| 651 | |
---|
| 652 | return $info; |
---|
| 653 | } |
---|
| 654 | |
---|
| 655 | /** |
---|
| 656 | * Get a function from a plugin, if it exists. If the plugin is not already |
---|
| 657 | * loaded, try ctools_plugin_load_function() instead. |
---|
| 658 | * |
---|
| 659 | * @param $plugin_definition |
---|
| 660 | * The loaded plugin type. |
---|
| 661 | * @param $function_name |
---|
| 662 | * The identifier of the function. For example, 'settings form'. |
---|
| 663 | * |
---|
| 664 | * @return |
---|
| 665 | * The actual name of the function to call, or NULL if the function |
---|
| 666 | * does not exist. |
---|
| 667 | */ |
---|
| 668 | function ctools_plugin_get_function($plugin_definition, $function_name) { |
---|
| 669 | // If cached the .inc file may not have been loaded. require_once is quite safe |
---|
| 670 | // and fast so it's okay to keep calling it. |
---|
| 671 | if (isset($plugin_definition['file'])) { |
---|
| 672 | // Plugins that are loaded from info files have the info file as |
---|
| 673 | // $plugin['file']. Don't try to run those. |
---|
| 674 | $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']); |
---|
| 675 | if (empty($info['info file'])) { |
---|
| 676 | require_once './' . $plugin_definition['path'] . '/' . $plugin_definition['file']; |
---|
| 677 | } |
---|
| 678 | } |
---|
| 679 | |
---|
| 680 | if (!isset($plugin_definition[$function_name])) { |
---|
| 681 | return; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) { |
---|
| 685 | $function = $plugin_definition[$function_name]['function']; |
---|
| 686 | if (isset($plugin_definition[$function_name]['file'])) { |
---|
| 687 | $file = $plugin_definition[$function_name]['file']; |
---|
| 688 | if (isset($plugin_definition[$function_name]['path'])) { |
---|
| 689 | $file = $plugin_definition[$function_name]['path'] . '/' . $file; |
---|
| 690 | } |
---|
| 691 | require_once './' . $file; |
---|
| 692 | } |
---|
| 693 | } |
---|
| 694 | else { |
---|
| 695 | $function = $plugin_definition[$function_name]; |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | if (function_exists($function)) { |
---|
| 699 | return $function; |
---|
| 700 | } |
---|
| 701 | } |
---|
| 702 | |
---|
| 703 | /** |
---|
| 704 | * Load a plugin and get a function name from it, returning success only |
---|
| 705 | * if the function exists. |
---|
| 706 | * |
---|
| 707 | * @param $module |
---|
| 708 | * The module that owns the plugin type. |
---|
| 709 | * @param $type |
---|
| 710 | * The type of plugin. |
---|
| 711 | * @param $id |
---|
| 712 | * The id of the specific plugin to load. |
---|
| 713 | * @param $function_name |
---|
| 714 | * The identifier of the function. For example, 'settings form'. |
---|
| 715 | * |
---|
| 716 | * @return |
---|
| 717 | * The actual name of the function to call, or NULL if the function |
---|
| 718 | * does not exist. |
---|
| 719 | */ |
---|
| 720 | function ctools_plugin_load_function($module, $type, $id, $function_name) { |
---|
| 721 | $plugin = ctools_get_plugins($module, $type, $id); |
---|
| 722 | return ctools_plugin_get_function($plugin, $function_name); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | /** |
---|
| 726 | * Get a class from a plugin, if it exists. If the plugin is not already |
---|
| 727 | * loaded, try ctools_plugin_load_class() instead. |
---|
| 728 | * |
---|
| 729 | * @param $plugin_definition |
---|
| 730 | * The loaded plugin type. |
---|
| 731 | * @param $class_name |
---|
| 732 | * The identifier of the class. For example, 'handler'. |
---|
| 733 | * @param $abstract |
---|
| 734 | * If true, will return abstract classes. Otherwise, parents will be included but nothing will be returned. |
---|
| 735 | * |
---|
| 736 | * @return |
---|
| 737 | * The actual name of the class to call, or NULL if the class does not exist. |
---|
| 738 | */ |
---|
| 739 | function ctools_plugin_get_class($plugin_definition, $class_name, $abstract = FALSE) { |
---|
| 740 | // If cached the .inc file may not have been loaded. require_once is quite safe |
---|
| 741 | // and fast so it's okay to keep calling it. |
---|
| 742 | if (isset($plugin_definition['file'])) { |
---|
| 743 | // Plugins that are loaded from info files have the info file as |
---|
| 744 | // $plugin['file']. Don't try to run those. |
---|
| 745 | $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']); |
---|
| 746 | if (empty($info['info file'])) { |
---|
| 747 | require_once './' . $plugin_definition['path'] . '/' . $plugin_definition['file']; |
---|
| 748 | } |
---|
| 749 | } |
---|
| 750 | |
---|
| 751 | if (!isset($plugin_definition[$class_name])) { |
---|
| 752 | return; |
---|
| 753 | } |
---|
| 754 | |
---|
| 755 | if (is_array($plugin_definition[$class_name]) && isset($plugin_definition[$class_name]['class'])) { |
---|
| 756 | if (isset($plugin_definition[$class_name]['parent'])) { |
---|
| 757 | // Make sure parents are included. |
---|
| 758 | // TODO parent-loading needs to be better documented; the 'parent' designated |
---|
| 759 | // on the plugin actually corresponds not to the name of the parent CLASS, |
---|
| 760 | // but the name of the parent PLUGIN (and it then loads loads whatever |
---|
| 761 | // class is in the same $class_name slot). Initially unintuitive. |
---|
| 762 | ctools_plugin_load_class($plugin_definition['plugin module'], $plugin_definition['plugin type'], $plugin_definition[$class_name]['parent'], $class_name); |
---|
| 763 | } |
---|
| 764 | $class = $plugin_definition[$class_name]['class']; |
---|
| 765 | if (isset($plugin_definition[$class_name]['file'])) { |
---|
| 766 | $file = $plugin_definition[$class_name]['file']; |
---|
| 767 | if (isset($plugin_definition[$class_name]['path'])) { |
---|
| 768 | $file = $plugin_definition[$class_name]['path'] . '/' . $file; |
---|
| 769 | } |
---|
| 770 | require_once './' . $file; |
---|
| 771 | } |
---|
| 772 | } |
---|
| 773 | else { |
---|
| 774 | $class = $plugin_definition[$class_name]; |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | // If we didn't explicitly include a file above, try autoloading a file |
---|
| 778 | // based on the class' name. |
---|
| 779 | if (!isset($file) && file_exists($plugin_definition['path'] . "/$class.class.php")) { |
---|
| 780 | require_once $plugin_definition['path'] . "/$class.class.php"; |
---|
| 781 | } |
---|
| 782 | |
---|
| 783 | if (class_exists($class) && |
---|
| 784 | (empty($plugin_definition[$class_name]['abstract']) || $abstract)) { |
---|
| 785 | return $class; |
---|
| 786 | } |
---|
| 787 | } |
---|
| 788 | |
---|
| 789 | /** |
---|
| 790 | * Load a plugin and get a class name from it, returning success only if the |
---|
| 791 | * class exists. |
---|
| 792 | * |
---|
| 793 | * @param $module |
---|
| 794 | * The module that owns the plugin type. |
---|
| 795 | * @param $type |
---|
| 796 | * The type of plugin. |
---|
| 797 | * @param $id |
---|
| 798 | * The id of the specific plugin to load. |
---|
| 799 | * @param $class_name |
---|
| 800 | * The identifier of the class. For example, 'handler'. |
---|
| 801 | * @param $abstract |
---|
| 802 | * If true, will tell ctools_plugin_get_class to allow the return of abstract classes. |
---|
| 803 | * |
---|
| 804 | * @return |
---|
| 805 | * The actual name of the class to call, or NULL if the class does not exist. |
---|
| 806 | */ |
---|
| 807 | function ctools_plugin_load_class($module, $type, $id, $class_name, $abstract = FALSE) { |
---|
| 808 | $plugin = ctools_get_plugins($module, $type, $id); |
---|
| 809 | return ctools_plugin_get_class($plugin, $class_name, $abstract); |
---|
| 810 | } |
---|
| 811 | |
---|
| 812 | /** |
---|
| 813 | * Sort callback for sorting plugins naturally. |
---|
| 814 | * |
---|
| 815 | * Sort first by weight, then by title. |
---|
| 816 | */ |
---|
| 817 | function ctools_plugin_sort($a, $b) { |
---|
| 818 | if (is_object($a)) { |
---|
| 819 | $a = (array) $a; |
---|
| 820 | } |
---|
| 821 | if (is_object($b)) { |
---|
| 822 | $b = (array) $b; |
---|
| 823 | } |
---|
| 824 | |
---|
| 825 | if (empty($a['weight'])) { |
---|
| 826 | $a['weight'] = 0; |
---|
| 827 | } |
---|
| 828 | |
---|
| 829 | if (empty($b['weight'])) { |
---|
| 830 | $b['weight'] = 0; |
---|
| 831 | } |
---|
| 832 | |
---|
| 833 | if ($a['weight'] == $b['weight']) { |
---|
| 834 | return strnatcmp(strtolower($a['title']), strtolower($b['title'])); |
---|
| 835 | } |
---|
| 836 | return ($a['weight'] < $b['weight']) ? -1 : 1; |
---|
| 837 | } |
---|