diff --git a/cache/classes/definition.php b/cache/classes/definition.php index 281fa30d9d1..4463e0a33b5 100644 --- a/cache/classes/definition.php +++ b/cache/classes/definition.php @@ -963,4 +963,31 @@ class cache_definition { public function has_required_identifiers() { return (count($this->requireidentifiers) > 0); } + + /** + * Returns the possible sharing options that can be used with this defintion. + * + * @return int + */ + public function get_sharing_options() { + return $this->sharingoptions; + } + + /** + * Returns the user entered sharing key for this definition. + * + * @return string + */ + public function get_user_input_sharing_key() { + return $this->userinputsharingkey; + } + + /** + * Returns the user selected sharing option for this definition. + * + * @return int + */ + public function get_selected_sharing_option() { + return $this->selectedsharingoption; + } } diff --git a/cache/classes/factory.php b/cache/classes/factory.php index f36f099142b..f850e690dd9 100644 --- a/cache/classes/factory.php +++ b/cache/classes/factory.php @@ -235,28 +235,10 @@ class cache_factory { */ public function create_cache(cache_definition $definition) { $class = $definition->get_cache_class(); - if ($this->is_initialising() || $this->stores_disabled()) { - // Do nothing we just want the dummy store. - $stores = array($this->create_dummy_store($definition)); - } else { - $stores = cache_helper::get_cache_stores($definition); - if (count($stores) === 0) { - // No suitable stores we found for the definition. We need to come up with a sensible default. - // If this has happened we can be sure that the user has mapped custom stores to either the - // mode of the definition. The first alternative to try is the system default for the mode. - // e.g. the default file store instance for application definitions. - $config = $this->create_config_instance(); - foreach ($config->get_stores($definition->get_mode()) as $name => $details) { - if (!empty($details['default'])) { - $stores[] = $this->create_store_from_config($name, $details, $definition); - break; - } - } - if (count($stores) === 0) { - // Hmm still no stores, better provide a dummy store to mimic functionality. The dev will be none the wiser. - $stores[] = $this->create_dummy_store($definition); - } - } + $stores = cache_helper::get_stores_suitable_for_definition($definition); + if (count($stores) === 0) { + // Hmm still no stores, better provide a dummy store to mimic functionality. The dev will be none the wiser. + $stores[] = $this->create_dummy_store($definition); } $loader = null; if ($definition->has_data_source()) { diff --git a/cache/classes/helper.php b/cache/classes/helper.php index 0d0529ebc64..90076aadbb9 100644 --- a/cache/classes/helper.php +++ b/cache/classes/helper.php @@ -143,7 +143,7 @@ class cache_helper { * * @param array $stores * @param cache_definition $definition - * @return array + * @return cache_store[] */ protected static function initialise_cachestore_instances(array $stores, cache_definition $definition) { $return = array(); @@ -702,4 +702,35 @@ class cache_helper { } return $stores; } + + /** + * Returns stores suitable for use with a given definition. + * + * @param cache_definition $definition + * @return cache_store[] + */ + public static function get_stores_suitable_for_definition(cache_definition $definition) { + $factory = cache_factory::instance(); + $stores = array(); + if ($factory->is_initialising() || $factory->stores_disabled()) { + // No suitable stores here. + return $stores; + } else { + $stores = self::get_cache_stores($definition); + if (count($stores) === 0) { + // No suitable stores we found for the definition. We need to come up with a sensible default. + // If this has happened we can be sure that the user has mapped custom stores to either the + // mode of the definition. The first alternative to try is the system default for the mode. + // e.g. the default file store instance for application definitions. + $config = $factory->create_config_instance(); + foreach ($config->get_stores($definition->get_mode()) as $name => $details) { + if (!empty($details['default'])) { + $stores[] = $factory->create_store_from_config($name, $details, $definition); + break; + } + } + } + } + return $stores; + } } diff --git a/cache/locallib.php b/cache/locallib.php index 2d83b9447cb..c7948cf6fd7 100644 --- a/cache/locallib.php +++ b/cache/locallib.php @@ -778,63 +778,36 @@ abstract class cache_administration_helper extends cache_helper { * @return array */ public static function get_definition_summaries() { - $instance = cache_config::instance(); - $definitions = $instance->get_definitions(); - + $factory = cache_factory::instance(); + $config = $factory->create_config_instance(); $storenames = array(); - foreach ($instance->get_all_stores() as $key => $store) { + foreach ($config->get_all_stores() as $key => $store) { if (!empty($store['default'])) { $storenames[$key] = new lang_string('store_'.$key, 'cache'); - } - } - - $modemappings = array(); - foreach ($instance->get_mode_mappings() as $mapping) { - $mode = $mapping['mode']; - if (!array_key_exists($mode, $modemappings)) { - $modemappings[$mode] = array(); - } - if (array_key_exists($mapping['store'], $storenames)) { - $modemappings[$mode][] = $storenames[$mapping['store']]; } else { - $modemappings[$mode][] = $mapping['store']; + $storenames[$store['name']] = $store['name']; } } - - $definitionmappings = array(); - foreach ($instance->get_definition_mappings() as $mapping) { - $definition = $mapping['definition']; - if (!array_key_exists($definition, $definitionmappings)) { - $definitionmappings[$definition] = array(); - } - if (array_key_exists($mapping['store'], $storenames)) { - $definitionmappings[$definition][] = $storenames[$mapping['store']]; - } else { - $definitionmappings[$definition][] = $mapping['store']; - } + /* @var cache_definition[] $definitions */ + $definitions = array(); + foreach ($config->get_definitions() as $key => $definition) { + $definitions[$key] = cache_definition::load($definition['component'].'/'.$definition['area'], $definition); } - - $return = array(); - foreach ($definitions as $id => $definition) { - $mappings = array(); - if (array_key_exists($id, $definitionmappings)) { - $mappings = $definitionmappings[$id]; - } else if (empty($definition['mappingsonly'])) { - $mappings = $modemappings[$definition['mode']]; + foreach (cache_helper::get_stores_suitable_for_definition($definition) as $store) { + $mappings[] = $storenames[$store->my_name()]; } - $return[$id] = array( 'id' => $id, - 'name' => cache_helper::get_definition_name($definition), - 'mode' => $definition['mode'], - 'component' => $definition['component'], - 'area' => $definition['area'], + 'name' => $definition->get_name(), + 'mode' => $definition->get_mode(), + 'component' => $definition->get_component(), + 'area' => $definition->get_area(), 'mappings' => $mappings, - 'sharingoptions' => self::get_definition_sharing_options($definition['sharingoptions'], false), - 'selectedsharingoption' => self::get_definition_sharing_options($definition['selectedsharingoption'], true), - 'userinputsharingkey' => $definition['userinputsharingkey'] + 'sharingoptions' => self::get_definition_sharing_options($definition->get_sharing_options(), false), + 'selectedsharingoption' => self::get_definition_sharing_options($definition->get_selected_sharing_option(), true), + 'userinputsharingkey' => $definition->get_user_input_sharing_key() ); } return $return;