Commit 0bd134c1 authored by catch's avatar catch
Browse files

task: #3037054 Deprecate drupal_static_reset() and drupal_static()

By: mile23
By: nicxvan
By: daffie
(cherry picked from commit 903f52dd)
parent 6f933572
Loading
Loading
Loading
Loading
Loading
+4 −1
Changes for core/includes/batch.inc: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -120,10 +120,13 @@ function _batch_page(Request $request) {
 *   TRUE if the batch information needs to be updated; FALSE otherwise.
 */
function _batch_needs_update($new_value = NULL) {
  $needs_update = &drupal_static(__FUNCTION__, FALSE);
  $cid = __FUNCTION__;
  $cached = \Drupal::cache('memory')->get($cid);
  $needs_update = $cached->data ?? NULL;

  if (isset($new_value)) {
    $needs_update = $new_value;
    \Drupal::cache('memory')->set($cid, $needs_update);
  }

  return $needs_update;
+40 −0
Changes for core/includes/bootstrap.inc: 40 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -377,9 +377,26 @@ function drupal_maintenance_theme(): void {
 * @return mixed
 *   Returns a variable by reference.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::cache('memory')->set() and \Drupal::cache('memory')->get()
 *   instead.
 *
 * @see https://www.drupal.org/node/2661204
 * @see drupal_static_reset()
 */
function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
  if ($name === 'clear_all_skip_deprecation') {
    $name = NULL;
  }
  else {
    $cid = $name ?? 'null';
    $cid = $cid . 'drupal_static_deprecation';
    if (!\Drupal::cache('memory')->get($cid)) {
      // The memory cache ensures the deprecation only triggers once.
      \Drupal::cache('memory')->set($cid, TRUE);
      @trigger_error("Calling drupal_static() with \"$name\" as argument is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::cache('memory')->set() and \Drupal::cache('memory')->get() instead. See https://www.drupal.org/node/2661204", E_USER_DEPRECATED);
    }
  }
  static $data = [], $defaults = [];
  if (isset($name)) {
    // Check if we're dealing with a previously defined static variable.
@@ -424,6 +441,11 @@ function &drupal_static($name, $default_value = NULL, $reset = FALSE) {
 *   Name of the static variable to reset. Omit to reset all variables.
 *   Resetting all variables should only be used, for example, for running
 *   unit tests with a clean environment.
 *
 * @deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use
 *   \Drupal::cache('memory')->delete() instead.
 *
 * @see https://www.drupal.org/node/2661204
 */
function drupal_static_reset($name = NULL): void {
  switch ($name) {
@@ -458,6 +480,24 @@ function drupal_static_reset($name = NULL): void {
      \Drupal::cache('memory')->delete('options.allowed_values');
      break;

    case 'clear_all_skip_deprecation':
      break;

    default:
      $cid = $name ?? 'null';
      $cid = $cid . 'drupal_static_reset_deprecation';
      if (!\Drupal::cache('memory')->get($cid)) {
        // The memory cache ensures the deprecation only triggers once.
        \Drupal::cache('memory')->set($cid, TRUE);
        if (!isset($name)) {
          @trigger_error("Calling drupal_static_reset() with \"NULL\" as argument is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::cache('memory')->delete() instead. See https://www.drupal.org/node/2661204", E_USER_DEPRECATED);
        }
        else {
          @trigger_error("Calling drupal_static_reset() with \"$name\" as argument is deprecated in drupal:11.5.0 and is removed from drupal:13.0.0. Use \Drupal::cache('memory')->delete() instead. See https://www.drupal.org/node/2661204", E_USER_DEPRECATED);
        }
      }
      break;

  }
  drupal_static($name, NULL, TRUE);
}
+2 −1
Changes for core/includes/common.inc: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -282,7 +282,8 @@ function drupal_flush_all_caches($kernel = NULL): void {
  \Drupal::service('asset.query_string')->reset();

  // Reset all static caches.
  drupal_static_reset();
  // @phpstan-ignore function.deprecated
  drupal_static_reset('clear_all_skip_deprecation');

  // Wipe the Twig PHP Storage cache.
  \Drupal::service('twig')->invalidate();
+2 −2
Changes for core/includes/form.inc: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -562,9 +562,9 @@ function batch_process($redirect = NULL, ?Url $url = NULL, $redirect_callback =
 * Retrieves the current batch.
 */
function &batch_get() {
  // Not drupal_static(), because Batch API operates at a lower level than most
  // Not a memory cache, because Batch API operates at a lower level than most
  // use-cases for resetting static variables, and we specifically do not want a
  // global drupal_static_reset() resetting the batch information. Functions
  // global memory cache resetting the batch information. Functions
  // that are part of the Batch API and need to reset the batch information may
  // call batch_get() and manipulate the result by reference. Functions that are
  // not part of the Batch API can also do this, but shouldn't.
+1 −1
Changes for core/lib/Drupal/Core/Asset/CssOptimizer.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ protected function processFile($css_asset) {
   *   Contents of the stylesheet, including any resolved @import commands.
   */
  protected function loadFile($file, $optimize = NULL, $reset_base_path = TRUE) {
    // These statics are not cache variables, so we don't use drupal_static().
    // These statics are not cache variables, so we don't use a memory cache.
    static $_optimize, $base_path;
    if ($reset_base_path) {
      $base_path = '';
Loading