From 93c0dc8ec40fbb35dc0b70b0b1f1b5a985e54ff9 Mon Sep 17 00:00:00 2001 From: Peter Burnett Date: Wed, 7 Apr 2021 10:33:06 +1000 Subject: [PATCH] MDL-70233 cache: Disabled factory now gets writer from parent --- cache/classes/factory.php | 32 ++++++++++++++++++++++++++++++++ cache/disabledlib.php | 8 ++++---- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/cache/classes/factory.php b/cache/classes/factory.php index 9791c4715d2..f535e72986d 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -658,4 +658,36 @@ class cache_factory { } return self::$displayhelper; } + + /** + * Gets the cache_config_writer to use when caching is disabled. + * This should only be called from cache_factory_disabled. + * + * @return cache_config_writer + */ + public static function get_disabled_writer(): cache_config_writer { + global $CFG; + + // Figure out if we are in a recursive loop using late static binding. + // This happens when get_disabled_writer is not overridden. We just want the default. + $loop = false; + if (!empty($CFG->alternative_cache_factory_class)) { + $loop = get_called_class() === $CFG->alternative_cache_factory_class; + } + + if (!$loop && !empty($CFG->alternative_cache_factory_class)) { + // Get the class to use from the alternative factory. + $factoryinstance = new $CFG->alternative_cache_factory_class(); + return $factoryinstance::get_disabled_writer(); + } else { + // We got here from cache_factory_disabled. + // We should use the default writer here. + // Make sure we have a default config if needed. + if (!cache_config::config_file_exists()) { + cache_config_writer::create_default_configuration(true); + } + + return new cache_config_writer(); + } + } } diff --git a/cache/disabledlib.php b/cache/disabledlib.php index 88af42d9f99..a86c704cd8d 100644 --- a/cache/disabledlib.php +++ b/cache/disabledlib.php @@ -313,13 +313,13 @@ class cache_factory_disabled extends cache_factory { self::set_state(self::STATE_INITIALISING); if ($class === 'cache_config_disabled') { $configuration = $class::create_default_configuration(); + $this->configs[$class] = new $class; } else { $configuration = false; - if (!cache_config::config_file_exists()) { - cache_config_writer::create_default_configuration(true); - } + // If we need a writer, we should get the classname from the generic factory. + // This is so alternative classes can be used if a different writer is required. + $this->configs[$class] = parent::get_disabled_writer(); } - $this->configs[$class] = new $class; $this->configs[$class]->load($configuration); } self::set_state(self::STATE_READY);