Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

(no description)

File Size: 622 lines (22 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 3 classes


Class: manager  - X-Ref

Hook manager implementing "Dispatcher" and "Event Provider" from PSR-14.

Due to class/method naming restrictions and collision with
Moodle events the definitions from PSR-14 should be interpreted as:

1. Event --> Hook
2. Listener --> Hook callback
3. Emitter --> Hook emitter
4. Dispatcher --> Hook dispatcher - implemented in manager::dispatch()
5. Listener Provider --> Hook callback provider - implemented in manager::get_callbacks_for_hook()

Note that technically any object can be a hook, but it is recommended
to put all hook classes into \component_name\hook namespaces and
each hook should implement \core\hook\described_hook interface.

__construct()   X-Ref
Constructor can be used only from factory methods.


get_instance()   X-Ref
Factory method, returns instance of manager that serves
as hook dispatcher and callback provider.

return: self

phpunit_get_instance(array $componentfiles)   X-Ref
Factory method for testing of hook manager in PHPUnit tests.

param: array $componentfiles list of hook callback files for each component.
return: self

phpunit_redirect_hook(string $hookname, callable $callback)   X-Ref
Override hook callbacks for testing purposes.

param: string $hookname
param: callable $callback
return: void

phpunit_stop_redirections()   X-Ref
Cancel all redirections of hook callbacks.

return: void

get_callbacks_for_hook(string $hookclassname)   X-Ref
Returns list of callbacks for given hook name.

NOTE: this is the "Listener Provider" described in PSR-14,
instead of instance parameter it uses real PHP class names.

param: string $hookclassname PHP class name of hook
return: array list of callback definitions

get_all_callbacks()   X-Ref
Returns list of all callbacks found in db/hooks.php files.

return: iterable

getListenersForEvent(object $event)   X-Ref
Get the list of listeners for the specified event.

param: object $event The object being listened to (aka hook).
return: iterable<callable>

is_callback_valid(string $component, string $callback)   X-Ref
Verify that callback is valid.

param: string $component
param: string $callback
return: bool

Class: name  - X-Ref

get_hooks_with_callbacks()   X-Ref
Returns the list of Hook class names that have registered callbacks.

return: array

dispatch(object $event)   X-Ref
Provide all relevant listeners with an event to process.

param: object $event The object to process (aka hook).
return: object The Event that was passed, now modified by listeners.

init_standard_callbacks()   X-Ref
Initialise list of all callbacks for each hook.

return: void

load_callbacks(array $componentfiles)   X-Ref
Load callbacks from component db/hooks.php files.

param: array $componentfiles list of all components with their callback files
return: void

load_callback_overrides()   X-Ref
In extremely special cases admins may decide to override callbacks via config.php setting.


calculate_overrides_hash()   X-Ref
Calculate a hash of the overrides.
This is used to inform if the overrides have changed, which invalidates the cache.

Overrides are only configured in config.php where there is no other mechanism to invalidate the cache.

return: null|string

prioritise_callbacks()   X-Ref
Prioritise the callbacks.


fetch_deprecated_callbacks()   X-Ref
Fetch the list of callbacks that this hook replaces.


add_component_callbacks(string $component, string $hookfile)   X-Ref
Add hook callbacks from file.

param: string $component component where hook callbacks are defined
param: string $hookfile file with list of all callbacks for component
return: void

normalise_callback(string $component, array $callback)   X-Ref
Normalise the callback class::method value.

param: string $component
param: array $callback
return: null|string

Class: method  - X-Ref

is_deprecated_plugin_callback(string $plugincallback)   X-Ref
Is the plugin callback from lib.php deprecated by any hook?

param: string $plugincallback short callback name without the component prefix
return: bool

is_deprecating_hook_present(string $component, string $plugincallback)   X-Ref
Is there a hook callback in component that deprecates given lib.php plugin callback?

NOTE: if there is both hook and deprecated callback then we ignore the old callback
to allow compatibility of contrib plugins with multiple Moodle branches.

param: string $component
param: string $plugincallback short callback name without the component prefix
return: bool

discover_known_hooks()   X-Ref
Returns list of hooks discovered through hook namespaces or discovery agents.

The hooks overview page includes also all other classes that are
referenced in callback registrations in db/hooks.php files, those
are not included here.

return: array hook class names