Skip to content

Commit

Permalink
MDL-74954 core: Detect changes to hook overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed May 23, 2023
1 parent 6823da5 commit 67ba0d3
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lib/classes/hook/manager.php
Expand Up @@ -286,7 +286,12 @@ private function init_standard_callbacks(): void {
$cache = \cache::make('core', 'hookcallbacks');
$callbacks = $cache->get('callbacks');
$deprecations = $cache->get('deprecations');
if (is_array($callbacks) && is_array($deprecations)) {
$overrideshash = $cache->get('overrideshash');

$usecache = is_array($callbacks);
$usecache = $usecache && is_array($deprecations);
$usecache = $usecache && $this->calculate_overrides_hash() === $overrideshash;
if ($usecache) {
$this->allcallbacks = $callbacks;
$this->alldeprecations = $deprecations;
return;
Expand Down Expand Up @@ -314,6 +319,7 @@ private function init_standard_callbacks(): void {
if ($cache) {
$cache->set('callbacks', $this->allcallbacks);
$cache->set('deprecations', $this->alldeprecations);
$cache->set('overrideshash', $this->calculate_overrides_hash());
}
}

Expand Down Expand Up @@ -393,6 +399,28 @@ private function load_callback_overrides(): void {
}
}

/**
* 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
*/
private function calculate_overrides_hash(): ?string {
global $CFG;

if (!property_exists($CFG, 'hooks_callback_overrides')) {
return null;
}

if (!is_iterable($CFG->hooks_callback_overrides)) {
return null;
}

return sha1(json_encode($CFG->hooks_callback_overrides));
}

/**
* Prioritise the callbacks.
*/
Expand Down

0 comments on commit 67ba0d3

Please sign in to comment.