From bbc98c82c07d5f1e690fbd844a262f3f9a93cab4 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 8 Mar 2024 22:46:06 +0800 Subject: [PATCH] MDL-81180 core: Add support for array notation in hook callback --- admin/tool/mobile/db/hooks.php | 4 ++-- lib/classes/hook/manager.php | 7 +++++++ lib/tests/fixtures/hook/hooks1_valid.php | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/admin/tool/mobile/db/hooks.php b/admin/tool/mobile/db/hooks.php index 60e2d927e8c..aebed53b97b 100644 --- a/admin/tool/mobile/db/hooks.php +++ b/admin/tool/mobile/db/hooks.php @@ -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, ], ]; diff --git a/lib/classes/hook/manager.php b/lib/classes/hook/manager.php index e793c5810e6..68475fa6707 100644 --- a/lib/classes/hook/manager.php +++ b/lib/classes/hook/manager.php @@ -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; diff --git a/lib/tests/fixtures/hook/hooks1_valid.php b/lib/tests/fixtures/hook/hooks1_valid.php index 78266ac4890..4559902229f 100644 --- a/lib/tests/fixtures/hook/hooks1_valid.php +++ b/lib/tests/fixtures/hook/hooks1_valid.php @@ -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'], ], ];