MDL-61307 core: Add component_class_callback functionality

This commit is contained in:
Andrew Nicols 2018-03-09 09:48:28 +08:00
parent dcc16e155d
commit 431a3bb674
3 changed files with 218 additions and 0 deletions

View file

@ -7716,6 +7716,37 @@ function component_callback_exists($component, $function) {
return false;
}
/**
* Call the specified callback method on the provided class.
*
* If the callback returns null, then the default value is returned instead.
* If the class does not exist, then the default value is returned.
*
* @param string $classname The name of the class to call upon.
* @param string $methodname The name of the staticically defined method on the class.
* @param array $params The arguments to pass into the method.
* @param mixed $default The default value.
* @return mixed The return value.
*/
function component_class_callback($classname, $methodname, array $params, $default = null) {
if (!class_exists($classname)) {
return $default;
}
if (!method_exists($classname, $methodname)) {
return $default;
}
$fullfunction = $classname . '::' . $methodname;
$result = call_user_func_array($fullfunction, $params);
if (null === $result) {
return $default;
} else {
return $result;
}
}
/**
* Checks whether a plugin supports a specified feature.
*