From bb1d19764d1d02f0c357d76d0c7a1ab79e0eaeec Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 12 Feb 2021 18:11:39 -0800 Subject: [PATCH] CRM_Core_Session - Add option 'useFakeSession()' for stateless requests --- CRM/Core/Session.php | 33 +++++++++++++++++++++++++++++++++ CRM/Utils/System/Drupal.php | 4 +++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CRM/Core/Session.php b/CRM/Core/Session.php index 7ca6a46ead..698b25eb9d 100644 --- a/CRM/Core/Session.php +++ b/CRM/Core/Session.php @@ -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. * diff --git a/CRM/Utils/System/Drupal.php b/CRM/Utils/System/Drupal.php index fea28fb6a4..34742a094f 100644 --- a/CRM/Utils/System/Drupal.php +++ b/CRM/Utils/System/Drupal.php @@ -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(); + } } } -- 2.25.1