Skip to content

Commit

Permalink
MDL-43033 cache: fixed display of definition stores in admin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Dec 1, 2013
1 parent d31fd61 commit e28a953
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 67 deletions.
27 changes: 27 additions & 0 deletions cache/classes/definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,4 +963,31 @@ protected function get_cache_identifier() {
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;
}
}
26 changes: 4 additions & 22 deletions cache/classes/factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,28 +235,10 @@ public function create_cache_from_params($mode, $component, $area, array $identi
*/
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()) {
Expand Down
33 changes: 32 additions & 1 deletion cache/classes/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static function get_cache_stores(cache_definition $definition) {
*
* @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();
Expand Down Expand Up @@ -702,4 +702,35 @@ public static function get_stores_suitable_for_mode_default() {
}
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;
}
}
61 changes: 17 additions & 44 deletions cache/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,63 +778,36 @@ public static function get_store_plugin_summaries() {
* @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;
Expand Down

0 comments on commit e28a953

Please sign in to comment.