Skip to content
Browse files

MDL-30592 moodlelib: new helper component_callback

I chose to be slighly inconsistent with plugin_callback in order to make a nicer API.

Conflicts:

	lib/moodlelib.php
  • Loading branch information...
1 parent 6508da4 commit 63d7b32d956ed0c42d6e7f96ec632da9cc27fc7c @timhunt timhunt committed Dec 5, 2011
Showing with 47 additions and 14 deletions.
  1. +47 −14 lib/moodlelib.php
View
61 lib/moodlelib.php
@@ -7310,32 +7310,65 @@ function get_list_of_plugins($directory='mod', $exclude='', $basedir='') {
return $plugins;
}
+/**
+* invoke plugin's callback functions
+*
+* @param string $type Plugin type e.g. 'mod'
+* @param string $name Plugin name
+* @param string $feature Feature name
+* @param string $action Feature's action
+* @param array $params parameters of callback function, should be an array
+* @param mixed $default default value if callback function hasn't been defined, or if it retursn null.
+* @return mixed
+*/
+function plugin_callback($type, $name, $feature, $action, $params = null, $default = null) {
+ return component_callback($type . '_' . $name, $feature . '_' . $action, (array) $params, $default);
+}
/**
* invoke plugin's callback functions
*
- * @param string $type Plugin type e.g. 'mod'
- * @param string $name Plugin name
- * @param string $feature Feature code (FEATURE_xx constant)
- * @param string $action Feature's action
- * @param string $options parameters of callback function, should be an array
- * @param mixed $default default value if callback function hasn't been defined
+ * @param string $component frankenstyle component name, e.g. 'mod_quiz'
+ * @param string $function the rest of the function name, e.g. 'cron' will end up calling 'mod_quiz_cron'
+ * @param array $params parameters of callback function
+ * @param mixed $default default value if callback function hasn't been defined, or if it retursn null.
* @return mixed
*/
-function plugin_callback($type, $name, $feature, $action, $options = null, $default=null) {
- global $CFG;
+function component_callback($component, $function, array $params = array(), $default = null) {
+ global $CFG; // this is needed for require_once() bellow
+
+ $cleancomponent = clean_param($component, PARAM_SAFEDIR);
+ if (empty($cleancomponent)) {
+ throw new coding_exception('Invalid component used in plugin_callback():' . $component);
+ }
+ $component = $cleancomponent;
- $name = clean_param($name, PARAM_SAFEDIR);
- $function = $name.'_'.$feature.'_'.$action;
- $file = get_component_directory($type . '_' . $name) . '/lib.php';
+ list($type, $name) = normalize_component($component);
+ $component = $type . '_' . $name;
+
+ $oldfunction = $name.'_'.$function;
+ $function = $component.'_'.$function;
+
+ $dir = get_component_directory($component);
+ if (empty($dir)) {
+ throw new coding_exception('Invalid component used in plugin_callback():' . $component);
+ }
// Load library and look for function
- if (file_exists($file)) {
- require_once($file);
+ if (file_exists($dir.'/lib.php')) {
+ require_once($dir.'/lib.php');
}
+
+ if (!function_exists($function) and function_exists($oldfunction)) {
+ if ($type !== 'mod' and $type !== 'core') {
+ debugging("Please use new function name $function instead of legacy $oldfunction");
+ }
+ $function = $oldfunction;
+ }
+
if (function_exists($function)) {
// Function exists, so just return function result
- $ret = call_user_func_array($function, (array)$options);
+ $ret = call_user_func_array($function, $params);
if (is_null($ret)) {
return $default;
} else {

0 comments on commit 63d7b32

Please sign in to comment.
Something went wrong with that request. Please try again.