mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 12:40:01 +01:00
MDL-39450 course formats: add static cache to get_format_or_default
This function gets called hundreds of times on every page, often with exactly the same input, so I added a static cache.
This commit is contained in:
parent
95190fda69
commit
6382060daf
@ -72,6 +72,8 @@ abstract class format_base {
|
|||||||
protected $formatoptions = array();
|
protected $formatoptions = array();
|
||||||
/** @var array cached instances */
|
/** @var array cached instances */
|
||||||
private static $instances = array();
|
private static $instances = array();
|
||||||
|
/** @var array plugin name => class name. */
|
||||||
|
private static $classesforformat = array('site' => 'site');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new instance of class
|
* Creates a new instance of class
|
||||||
@ -94,24 +96,28 @@ abstract class format_base {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected static final function get_format_or_default($format) {
|
protected static final function get_format_or_default($format) {
|
||||||
if ($format === 'site') {
|
if (array_key_exists($format, self::$classesforformat)) {
|
||||||
return $format;
|
return self::$classesforformat[$format];
|
||||||
}
|
}
|
||||||
|
|
||||||
$plugins = get_sorted_course_formats();
|
$plugins = get_sorted_course_formats();
|
||||||
if (in_array($format, $plugins)) {
|
foreach ($plugins as $plugin) {
|
||||||
return $format;
|
self::$classesforformat[$plugin] = $plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (array_key_exists($format, self::$classesforformat)) {
|
||||||
|
return self::$classesforformat[$format];
|
||||||
|
}
|
||||||
|
|
||||||
// Else return default format
|
// Else return default format
|
||||||
$defaultformat = get_config('moodlecourse', 'format');
|
$defaultformat = get_config('moodlecourse', 'format');
|
||||||
if (!in_array($defaultformat, $plugins)) {
|
if (!in_array($defaultformat, $plugins)) {
|
||||||
// when default format is not set correctly, use the first available format
|
// when default format is not set correctly, use the first available format
|
||||||
$defaultformat = reset($plugins);
|
$defaultformat = reset($plugins);
|
||||||
}
|
}
|
||||||
static $warningprinted = array();
|
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
|
||||||
if (empty($warningprinted[$format])) {
|
|
||||||
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
|
self::$classesforformat[$format] = $defaultformat;
|
||||||
$warningprinted[$format] = true;
|
|
||||||
}
|
|
||||||
return $defaultformat;
|
return $defaultformat;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user