MDL-81180 core: Add support for array notation in hook callback

This commit is contained in:
Andrew Nicols 2024-03-08 22:46:06 +08:00
parent 6231d9119d
commit bbc98c82c0
No known key found for this signature in database
GPG Key ID: 6D1E3157C8CFBF14
3 changed files with 10 additions and 3 deletions

View File

@ -26,8 +26,8 @@ defined('MOODLE_INTERNAL') || die();
$callbacks = [
[
'hook' => core\hook\output\standard_head_html_prepend::class,
'callback' => 'tool_mobile\local\hooks\output\standard_head_html_prepend::callback',
'hook' => \core\hook\output\standard_head_html_prepend::class,
'callback' => [\tool_mobile\local\hooks\output\standard_head_html_prepend::class, 'callback'],
'priority' => 0,
],
];

View File

@ -546,6 +546,13 @@ final class manager implements
return null;
}
$classmethod = $callback['callback'];
if (is_array($classmethod)) {
if (count($classmethod) !== 2) {
debugging("Hook callback definition contains invalid 'callback' array in '$component'", DEBUG_DEVELOPER);
return null;
}
$classmethod = implode('::', $classmethod);
}
if (!is_string($classmethod)) {
debugging("Hook callback definition contains invalid 'callback' string in '$component'", DEBUG_DEVELOPER);
return null;

View File

@ -28,6 +28,6 @@ defined('MOODLE_INTERNAL') || die();
$callbacks = [
[
'hook' => 'test_plugin\\hook\\hook',
'callback' => 'test_plugin\\callbacks::test1',
'callback' => [\test_plugin\callbacks::class, 'test1'],
],
];