MINOR Caching DataObject->fieldLabels() in memory, to avoid _t() being called excessively through fieldLabel()

This commit is contained in:
Ingo Schommer 2012-04-13 18:38:23 +02:00
parent 3002598a5e
commit b81863cfd9

View File

@ -148,6 +148,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
protected static $_cache_get_one;
protected static $_cache_get_class_ancestry;
protected static $_cache_composite_fields = array();
protected static $_cache_field_labels = array();
/**
* Non-static relationship cache, indexed by component name.
@ -2672,6 +2673,7 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
DataObject::$_cache_get_one = array();
DataObject::$_cache_composite_fields = array();
DataObject::$_cache_get_class_ancestry = array();
DataObject::$_cache_field_labels = array();
}
/**
@ -2953,6 +2955,9 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
* @return array|string Array of all element labels if no argument given, otherwise the label of the field
*/
public function fieldLabels($includerelations = true) {
$cacheKey = $this->class . '_' . $includerelations;
if(!isset(self::$_cache_field_labels[$cacheKey])) {
$customLabels = $this->stat('field_labels');
$autoLabels = array();
@ -2970,16 +2975,19 @@ class DataObject extends ViewableData implements DataObjectInterface, i18nEntity
$types['many_many'] = (array)singleton($ancestorClass)->uninherited('many_many', true);
}
foreach($types as $type => $attrs) {
foreach($attrs as $name => $spec)
foreach($attrs as $name => $spec) {
// var_dump("{$ancestorClass}.{$type}_{$name}");
$autoLabels[$name] = _t("{$ancestorClass}.{$type}_{$name}",FormField::name_to_label($name));
}
}
}
$labels = array_merge((array)$autoLabels, (array)$customLabels);
$this->extend('updateFieldLabels', $labels);
self::$_cache_field_labels[$cacheKey] = $labels;
}
return $labels;
return self::$_cache_field_labels[$cacheKey];
}
/**