plugins.inc 33 KB

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