MDL-53655 tool_lp: Allow closures in default values of persistents

This commit is contained in:
Frederic Massart
2016-04-18 10:59:01 +08:00
parent f1979b60a1
commit 3baf704ddb
2 changed files with 25 additions and 3 deletions
@@ -51,7 +51,9 @@ class course_competency_settings extends persistent {
),
'pushratingstouserplans' => array(
'type' => PARAM_BOOL,
'default' => get_config('tool_lp', 'pushcourseratingstouserplans')
'default' => function() {
return get_config('tool_lp', 'pushcourseratingstouserplans');
}
),
);
}
+22 -2
View File
@@ -140,7 +140,14 @@ abstract class persistent {
*
* Each property MUST be listed here.
*
* Example:
* The result of this method is cached internally for the whole request.
*
* The 'default' value can be a Closure when its value may change during a single request.
* For example if the default value is based on a $CFG property, then it should be wrapped in a closure
* to avoid running into scenarios where the true value of $CFG is not reflected in the definition.
* Do not abuse closures as they obviously add some overhead.
*
* Examples:
*
* array(
* 'property_name' => array(
@@ -152,6 +159,15 @@ abstract class persistent {
* )
* )
*
* array(
* 'dynamic_property_name' => array(
* 'default' => function() {
* return $CFG->something;
* },
* 'type' => PARAM_INT,
* )
* )
*
* @return array Where keys are the property names.
*/
protected static function define_properties() {
@@ -254,7 +270,11 @@ abstract class persistent {
if (!isset($properties[$property]['default'])) {
return null;
}
return $properties[$property]['default'];
$value = $properties[$property]['default'];
if ($value instanceof \Closure) {
return $value();
}
return $value;
}
/**