CRM_Core_Session - Add option 'useFakeSession()' for stateless requests
authorTim Otten <totten@civicrm.org>
Sat, 13 Feb 2021 02:11:39 +0000 (18:11 -0800)
committerTim Otten <totten@civicrm.org>
Tue, 2 Mar 2021 19:37:53 +0000 (11:37 -0800)
CRM/Core/Session.php
CRM/Utils/System/Drupal.php

index 7ca6a46ead2587973f289598fc769743869e9f17..698b25eb9ddf22b8b0560e35b9657a17f0c4fbaf 100644 (file)
@@ -86,6 +86,39 @@ class CRM_Core_Session {
     return self::$_singleton;
   }
 
+  /**
+   * Replace the session object with a fake session.
+   */
+  public static function useFakeSession() {
+    self::$_singleton = new class() extends CRM_Core_Session {
+
+      public function initialize($isRead = FALSE) {
+        if ($isRead) {
+          return;
+        }
+
+        if (!isset($this->_session)) {
+          $this->_session = [];
+        }
+
+        if (!isset($this->_session[$this->_key]) || !is_array($this->_session[$this->_key])) {
+          $this->_session[$this->_key] = [];
+        }
+      }
+
+      public function isEmpty() {
+        return empty($this->_session);
+      }
+
+    };
+    self::$_singleton->_session = NULL;
+    // This is not a revocable proposition. Should survive, even with things 'System.flush'.
+    if (!defined('_CIVICRM_FAKE_SESSION')) {
+      define('_CIVICRM_FAKE_SESSION', TRUE);
+    }
+    return self::$_singleton;
+  }
+
   /**
    * Creates an array in the session.
    *
index fea28fb6a4f4c469d92541dc22399f9149f12f31..34742a094fd21ef9ae555037df0020a6942b7b48 100644 (file)
@@ -850,7 +850,9 @@ AND    u.status = 1
       if (!defined('MAINTENANCE_MODE') || MAINTENANCE_MODE != 'update') {
         module_invoke_all('exit');
       }
-      drupal_session_commit();
+      if (!defined('_CIVICRM_FAKE_SESSION')) {
+        drupal_session_commit();
+      }
     }
   }