- Patch #768090 by ridgerunner: changed drupal_static() performance improvement.
parent
6195d47d15
commit
d828e2170b
|
@ -2906,34 +2906,35 @@ function registry_update() {
|
|||
*/
|
||||
function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
|
||||
static $data = array(), $default = array();
|
||||
if (!isset($name)) {
|
||||
// All variables are reset. 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;
|
||||
}
|
||||
if ($reset) {
|
||||
// The reset means the default is loaded.
|
||||
if (array_key_exists($name, $default)) {
|
||||
// First check if dealing with a previously defined static variable.
|
||||
if (isset($data[$name]) || array_key_exists($name, $data)) {
|
||||
// Non-NULL $name and both $data[$name] and $default[$name] statics exist.
|
||||
if ($reset) {
|
||||
// Reset pre-existing static variable to its default value.
|
||||
$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
|
||||
// returned.
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
elseif (!array_key_exists($name, $data)) {
|
||||
// Store the default value internally and also copy it to the reference to
|
||||
// be returned.
|
||||
// First call with new non-NULL $name. Initialize a new static variable.
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue