From: Tim Otten Date: Sat, 13 Feb 2021 02:11:39 +0000 (-0800) Subject: CRM_Core_Session - Add option 'useFakeSession()' for stateless requests X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bb1d19764d1d02f0c357d76d0c7a1ab79e0eaeec;p=civicrm-core.git CRM_Core_Session - Add option 'useFakeSession()' for stateless requests --- 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(); + } } }