MDL-81456 core: Use DI for all hook access

Using DI for all hook access means that it becomes significantly easier
to mock hooks and callbacks for unit testing without fundamentally
altering the structure of the code purely for the purposes of unit
testing.
This commit is contained in:
Andrew Nicols
2024-04-08 09:35:41 +08:00
parent 26649f5750
commit 2b49ad42f5
17 changed files with 89 additions and 42 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ class hook_list_table extends flexible_table {
*/
public function out(): void {
// All hook consumers referenced from the db/hooks.php files.
$hookmanager = \core\hook\manager::get_instance();
$hookmanager = \core\di::get(\core\hook\manager::class);
$allhooks = (array)$hookmanager->get_all_callbacks();
// Add any unused hooks.
+1 -1
View File
@@ -30,7 +30,7 @@ require_once($CFG->libdir . '/tablelib.php');
admin_externalpage_setup('hooksoverview');
require_capability('moodle/site:config', \core\context\system::instance());
$hookmanager = \core\hook\manager::get_instance();
$hookmanager = \core\di::get(\core\hook\manager::class);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('hooksoverview', 'core_admin'));
+1 -1
View File
@@ -334,7 +334,7 @@ class manager {
// Allow plugins to callback as soon possible after user has passed MFA.
$hook = new \tool_mfa\hook\after_user_passed_mfa();
\core\hook\manager::get_instance()->dispatch($hook);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
// Add/update record in DB for users last mfa auth.
self::update_pass_time();
+1 -1
View File
@@ -130,7 +130,7 @@ class user_bulk_action_form extends moodleform {
}
// Any plugin can append user bulk actions to this list by implementing a hook callback.
\core\hook\manager::get_instance()->dispatch($hook);
\core\di::get(\core\hook\manager::class)->dispatch($hook);
// This method may be called from 'Bulk actions' and 'Browse user list' pages. Some actions
// may be irrelevant in one of the contexts and they can be excluded by specifying the