- Patch #768090 by ridgerunner: changed drupal_static() performance improvement.

merge-requests/26/head
Dries Buytaert 2010-07-18 01:22:53 +00:00
parent 6195d47d15
commit d828e2170b
1 changed files with 21 additions and 20 deletions

View File

@ -2906,34 +2906,35 @@ function registry_update() {
*/ */
function &drupal_static($name, $default_value = NULL, $reset = FALSE) { function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
static $data = array(), $default = array(); static $data = array(), $default = array();
if (!isset($name)) { // First check if dealing with a previously defined static variable.
// All variables are reset. This needs to be done one at a time so that if (isset($data[$name]) || array_key_exists($name, $data)) {
// references returned by earlier invocations of drupal_static() also get // Non-NULL $name and both $data[$name] and $default[$name] statics exist.
// reset. if ($reset) {
foreach ($default as $name => $value) { // Reset pre-existing static variable to its default value.
$data[$name] = $value;
}
// As the function returns a reference, the return should always be a
// variable.
return $data;
}
if ($reset) {
// The reset means the default is loaded.
if (array_key_exists($name, $default)) {
$data[$name] = $default[$name]; $data[$name] = $default[$name];
} }
else { return $data[$name];
}
// Neither $data[$name] nor $default[$name] static variables exist.
if (isset($name)) {
if ($reset) {
// Reset was called before a default is set and yet a variable must be // Reset was called before a default is set and yet a variable must be
// returned. // returned.
return $data; return $data;
} }
} // First call with new non-NULL $name. Initialize a new static variable.
elseif (!array_key_exists($name, $data)) {
// Store the default value internally and also copy it to the reference to
// be returned.
$default[$name] = $data[$name] = $default_value; $default[$name] = $data[$name] = $default_value;
return $data[$name];
} }
return $data[$name]; // Reset all: ($name == NULL). This needs to be done one at a time so that
// references returned by earlier invocations of drupal_static() also get
// reset.
foreach ($default as $name => $value) {
$data[$name] = $value;
}
// As the function returns a reference, the return should always be a
// variable.
return $data;
} }
/** /**