- Patch #620688 by chx: fixed drupal_static_reset().

merge-requests/26/head
Dries Buytaert 2009-11-08 20:36:12 +00:00
parent 3213087441
commit 54925d947b
1 changed files with 21 additions and 15 deletions

View File

@ -2091,31 +2091,37 @@ function registry_rebuild() {
* TRUE to reset a specific named variable, or all variables if $name is NULL. * TRUE to reset a specific named variable, or all variables if $name is NULL.
* Resetting every variable should only be used, for example, for running * Resetting every variable should only be used, for example, for running
* unit tests with a clean environment. Should be used only though via * unit tests with a clean environment. Should be used only though via
* function drupal_static_reset(). * function drupal_static_reset() and the return value should not be used in
* this case.
* *
* @return * @return
* Returns a variable by reference if $reset is FALSE. * Returns a variable by reference.
*/ */
function &drupal_static($name, $default_value = NULL, $reset = FALSE) { function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
static $data = array(); static $data = array(), $default = array();
if (!isset($name)) {
// Reset a single value, or all values. // All variables are reset.
$data = $default;
// As the function returns a reference, the return should always be a
// variable.
return $data;
}
if ($reset) { if ($reset) {
if (isset($name)) { // The reset means the default is loaded.
unset($data[$name]); if (array_key_exists($name, $default)) {
$data[$name] = $default[$name];
} }
else { else {
$data = array(); // Reset was called before a default is set and yet a variable must be
// returned.
return $data;
} }
// We must return a reference to a variable.
$dummy = NULL;
return $dummy;
} }
elseif (!array_key_exists($name, $data)) {
if (!isset($data[$name])) { // Store the default value internally and also copy it to the reference to
$data[$name] = $default_value; // be returned.
$default[$name] = $data[$name] = $default_value;
} }
return $data[$name]; return $data[$name];
} }