diff -u b/core/includes/bootstrap.inc b/core/includes/bootstrap.inc --- b/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -13,12 +13,12 @@ use Drupal\Core\Extension\ExtensionDiscovery; use Drupal\Core\Logger\RfcLogLevel; use Drupal\Core\Session\AccountInterface; -use Drupal\Core\DrupalStatic; use Drupal\Core\Site\Settings; use Drupal\Core\Utility\Error; use Symfony\Component\ClassLoader\ApcClassLoader; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Drupal\Component\Utility\StaticStorage; use Drupal\Core\Language\LanguageInterface; /** @@ -885,98 +885,6 @@ /** * Provides central static variable storage. * - * All functions requiring a static variable to persist or cache data within - * a single page request are encouraged to use this function unless it is - * absolutely certain that the static variable will not need to be reset during - * the page request. By centralizing static variable storage through this - * function, other functions can rely on a consistent API for resetting any - * other function's static variables. - * - * Example: - * @code - * function example_list($field = 'default') { - * $examples = &drupal_static(__FUNCTION__); - * if (!isset($examples)) { - * // If this function is being called for the first time after a reset, - * // query the database and execute any other code needed to retrieve - * // information. - * ... - * } - * if (!isset($examples[$field])) { - * // If this function is being called for the first time for a particular - * // index field, then execute code needed to index the information already - * // available in $examples by the desired field. - * ... - * } - * // Subsequent invocations of this function for a particular index field - * // skip the above two code blocks and quickly return the already indexed - * // information. - * return $examples[$field]; - * } - * function examples_admin_overview() { - * // When building the content for the overview page, make sure to get - * // completely fresh information. - * drupal_static_reset('example_list'); - * ... - * } - * @endcode - * - * In a few cases, a function can have certainty that there is no legitimate - * use-case for resetting that function's static variable. This is rare, - * because when writing a function, it's hard to forecast all the situations in - * which it will be used. A guideline is that if a function's static variable - * does not depend on any information outside of the function that might change - * during a single page request, then it's ok to use the "static" keyword - * instead of the drupal_static() function. - * - * Example: - * @code - * function mymodule_log_stream_handle($new_handle = NULL) { - * static $handle; - * if (isset($new_handle)) { - * $handle = $new_handle; - * } - * return $handle; - * } - * @endcode - * - * In a few cases, a function needs a resettable static variable, but the - * function is called many times (100+) during a single page request, so - * every microsecond of execution time that can be removed from the function - * counts. These functions can use a more cumbersome, but faster variant of - * calling drupal_static(). It works by storing the reference returned by - * drupal_static() in the calling function's own static variable, thereby - * removing the need to call drupal_static() for each iteration of the function. - * Conceptually, it replaces: - * @code - * $foo = &drupal_static(__FUNCTION__); - * @endcode - * with: - * @code - * // Unfortunately, this does not work. - * static $foo = &drupal_static(__FUNCTION__); - * @endcode - * However, the above line of code does not work, because PHP only allows static - * variables to be initialized by literal values, and does not allow static - * variables to be assigned to references. - * - http://php.net/manual/language.variables.scope.php#language.variables.scope.static - * - http://php.net/manual/language.variables.scope.php#language.variables.scope.references - * The example below shows the syntax needed to work around both limitations. - * For benchmarks and more information, see https://www.drupal.org/node/619666. - * - * Example: - * @code - * function example_default_format_type() { - * // Use the advanced drupal_static() pattern, since this is called very often. - * static $drupal_static_fast; - * if (!isset($drupal_static_fast)) { - * $drupal_static_fast['format_type'] = &drupal_static(__FUNCTION__); - * } - * $format_type = &$drupal_static_fast['format_type']; - * ... - * } - * @endcode - * * @param $name * Globally unique name for the variable. For a function with only one static, * variable, the function name (e.g. via the PHP magic __FUNCTION__ constant) @@ -984,10 +892,6 @@ * distinguishing suffix to the function name for each one. * @param $default_value * Optional default value. - * @param $reset - * TRUE to reset one or all variables(s). This parameter is only used - * internally and should not be passed in; use drupal_static_reset() instead. - * (This function's return value should not be used when TRUE is passed in.) * * @return * Returns a variable by reference. @@ -995,10 +899,10 @@ * @see drupal_static_reset() * * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. - * Use \Drupal\Core\DrupalStatic::get(). + * Use \Drupal\Component\Utility\StaticStorage::get(). */ function &drupal_static($name, $default_value = NULL) { - $value = &DrupalStatic::get($name, $default_value); + $value = &StaticStorage::get($name, $default_value); return $value; } @@ -1009,10 +913,10 @@ * Name of the static variable to reset. Omit to reset all variables. * * @deprecated in Drupal 8.x-dev, will be removed before Drupal 8.0. - * Use \Drupal\Core\DrupalStatic::reset(). + * Use \Drupal\Component\Utility\StaticStorage::reset(). */ function drupal_static_reset($name = NULL) { - DrupalStatic::reset($name); + StaticStorage::reset($name); } /** reverted: --- b/core/lib/Drupal/Core/DrupalStatic.php +++ /dev/null @@ -1,179 +0,0 @@ - $value) { - static::$data[$name] = $value; - } - } - elseif (isset(static::$data[$name]) || array_key_exists($name, static::$data)) { - // Reset pre-existing static variable to its default value. - static::$data[$name] = static::$default[$name]; - } - } - -} reverted: --- b/core/tests/Drupal/Tests/Core/DrupalStaticTest.php +++ /dev/null @@ -1,94 +0,0 @@ - 'Tests \Drupal\Core\DrupalStatic', - 'description' => '', - 'group' => 'Bootstrap', - ); - } - protected function tearDown() { - parent::tearDown(); - $empty = &DrupalStatic::get(NULL); - $empty = array(); - } - - /** - * @covers ::get - */ - public function testGet() { - $var1 = &DrupalStatic::get(__METHOD__, 'foo'); - $this->assertSame('foo', $var1); - } - - /** - * @covers ::get - */ - public function testGetRepeated() { - $var1 = &DrupalStatic::get(__METHOD__, 'foo'); - $this->assertSame('foo', $var1); - - $var1 = 'bar'; - $var2 = &DrupalStatic::get(__METHOD__, 'foo'); - $this->assertSame('bar', $var2); - } - - /** - * @covers ::get - */ - public function testGetNull() { - $var1 = &DrupalStatic::get(NULL); - $this->assertSame(array(), $var1); - - $var2 = &DrupalStatic::get(__METHOD__, 'foo'); - $expected = array( - __METHOD__ => $var2, - ); - $this->assertSame($expected, $var1); - } - - /** - * @covers ::reset - */ - public function testReset() { - $var1 = &DrupalStatic::get(__METHOD__, 'foo'); - $this->assertSame('foo', $var1); - - $var1 = 'bar'; - DrupalStatic::reset(__METHOD__); - $this->assertSame('foo', $var1); - } - - /** - * @covers ::reset - */ - public function testResetNull() { - $var1 = &DrupalStatic::get(__METHOD__, 'foo'); - $this->assertSame('foo', $var1); - $var1 = 'bar'; - - DrupalStatic::reset(); - $this->assertSame('foo', $var1); - } - -} only in patch2: unchanged: --- /dev/null +++ b/core/lib/Drupal/Component/Utility/StaticStorage.php @@ -0,0 +1,179 @@ + $value) { + static::$data[$name] = $value; + } + } + elseif (isset(static::$data[$name]) || array_key_exists($name, static::$data)) { + // Reset pre-existing static variable to its default value. + static::$data[$name] = static::$default[$name]; + } + } + +} only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Component/Utility/StaticStorageTest.php @@ -0,0 +1,94 @@ + 'Tests \Drupal\Component\Utility\StaticStorage', + 'description' => '', + 'group' => 'Bootstrap', + ); + } + protected function tearDown() { + parent::tearDown(); + $empty = &StaticStorage::get(NULL); + $empty = array(); + } + + /** + * @covers ::get + */ + public function testGet() { + $var1 = &StaticStorage::get(__METHOD__, 'foo'); + $this->assertSame('foo', $var1); + } + + /** + * @covers ::get + */ + public function testGetRepeated() { + $var1 = &StaticStorage::get(__METHOD__, 'foo'); + $this->assertSame('foo', $var1); + + $var1 = 'bar'; + $var2 = &StaticStorage::get(__METHOD__, 'foo'); + $this->assertSame('bar', $var2); + } + + /** + * @covers ::get + */ + public function testGetNull() { + $var1 = &StaticStorage::get(NULL); + $this->assertSame(array(), $var1); + + $var2 = &StaticStorage::get(__METHOD__, 'foo'); + $expected = array( + __METHOD__ => $var2, + ); + $this->assertSame($expected, $var1); + } + + /** + * @covers ::reset + */ + public function testReset() { + $var1 = &StaticStorage::get(__METHOD__, 'foo'); + $this->assertSame('foo', $var1); + + $var1 = 'bar'; + StaticStorage::reset(__METHOD__); + $this->assertSame('foo', $var1); + } + + /** + * @covers ::reset + */ + public function testResetNull() { + $var1 = &StaticStorage::get(__METHOD__, 'foo'); + $this->assertSame('foo', $var1); + $var1 = 'bar'; + + StaticStorage::reset(); + $this->assertSame('foo', $var1); + } + +}