mirror of
https://github.com/moodle/moodle.git
synced 2025-03-14 04:30:15 +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();
|
||||
/** @var array cached instances */
|
||||
private static $instances = array();
|
||||
/** @var array plugin name => class name. */
|
||||
private static $classesforformat = array('site' => 'site');
|
||||
|
||||
/**
|
||||
* Creates a new instance of class
|
||||
@ -94,24 +96,28 @@ abstract class format_base {
|
||||
* @return string
|
||||
*/
|
||||
protected static final function get_format_or_default($format) {
|
||||
if ($format === 'site') {
|
||||
return $format;
|
||||
if (array_key_exists($format, self::$classesforformat)) {
|
||||
return self::$classesforformat[$format];
|
||||
}
|
||||
|
||||
$plugins = get_sorted_course_formats();
|
||||
if (in_array($format, $plugins)) {
|
||||
return $format;
|
||||
foreach ($plugins as $plugin) {
|
||||
self::$classesforformat[$plugin] = $plugin;
|
||||
}
|
||||
|
||||
if (array_key_exists($format, self::$classesforformat)) {
|
||||
return self::$classesforformat[$format];
|
||||
}
|
||||
|
||||
// Else return default format
|
||||
$defaultformat = get_config('moodlecourse', 'format');
|
||||
if (!in_array($defaultformat, $plugins)) {
|
||||
// when default format is not set correctly, use the first available format
|
||||
$defaultformat = reset($plugins);
|
||||
}
|
||||
static $warningprinted = array();
|
||||
if (empty($warningprinted[$format])) {
|
||||
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
|
||||
$warningprinted[$format] = true;
|
||||
}
|
||||
debugging('Format plugin format_'.$format.' is not found. Using default format_'.$defaultformat, DEBUG_DEVELOPER);
|
||||
|
||||
self::$classesforformat[$format] = $defaultformat;
|
||||
return $defaultformat;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user