From bb053c7f4bb0ccf26fc86a78066de090d773edd5 Mon Sep 17 00:00:00 2001 From: Bastien Ho Date: Fri, 13 Oct 2023 11:23:35 +0200 Subject: [PATCH 1/4] Implements __serialize and __unserialize required for Serializable since PHP 8.1.0 --- CRM/Core/Payment/OmnipayMultiProcessor.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CRM/Core/Payment/OmnipayMultiProcessor.php b/CRM/Core/Payment/OmnipayMultiProcessor.php index d636d91db..2f96de852 100644 --- a/CRM/Core/Payment/OmnipayMultiProcessor.php +++ b/CRM/Core/Payment/OmnipayMultiProcessor.php @@ -95,6 +95,15 @@ public function serialize(): string { return serialize(get_object_vars($this)); } + /** + * For PHP8.1 + * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize + */ + public function __serialize(): array { + //$this->cleanupClassForSerialization(TRUE); + return get_object_vars($this); + } + /** * Unserialize * @@ -109,6 +118,16 @@ public function unserialize($data) { } } + /** + * For PHP8.1 + * https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize + */ + public function __unserialize(array $data): void { + foreach ($data as $key => $value) { + $this->$key = $value; + } + } + /** * Omnipay gateway. * From 64387b65df4f5514162fcea2663ca822d57b744b Mon Sep 17 00:00:00 2001 From: Bastien Ho Date: Tue, 17 Oct 2023 08:57:22 +0200 Subject: [PATCH 2/4] Adds cleanupObjectForSerialization utility --- CRM/Core/Payment/PaymentExtended.php | 31 +++++++++++++++++++--------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/CRM/Core/Payment/PaymentExtended.php b/CRM/Core/Payment/PaymentExtended.php index 0093023cc..2872655a4 100644 --- a/CRM/Core/Payment/PaymentExtended.php +++ b/CRM/Core/Payment/PaymentExtended.php @@ -381,6 +381,25 @@ public function serialize() { return serialize($this); } + /** + * Unset various objects that will fail to serialize when the form is stored to session. + * + * @param object $object (by reference) + * @param bool $isIncludeGateWay Should we also unset the gateway. + */ + protected function cleanupObjectForSerialization(&$object, $isIncludeGateWay = FALSE) { + if (\Civi::settings()->get('omnipay_developer_mode') && !empty($object->history)) { + $object->logHttpTraffic(FALSE); + } + $object->history = []; + $object->client = NULL; + $object->lock = NULL; + $object->guzzleClient = NULL; + if ($isIncludeGateWay) { + $object->gateway = NULL; + } + } + /** * Unset various objects that will fail to serialize when the form is stored to session. * @@ -388,17 +407,9 @@ public function serialize() { * Should we also unset the gateway. * (possibly the default here should be TRUE but we want to be sure we are not * unsetting it when it is still being used.) + * For retro-compatibilty, this still transforms the current entity. */ protected function cleanupClassForSerialization($isIncludeGateWay = FALSE) { - if (\Civi::settings()->get('omnipay_developer_mode') && !empty($this->history)) { - $this->logHttpTraffic(FALSE); - } - $this->history = []; - $this->client = NULL; - $this->lock = NULL; - $this->guzzleClient = NULL; - if ($isIncludeGateWay) { - $this->gateway = NULL; - } + $this->cleanupObjectForSerialization($this, $isIncludeGateWay); } } From 098c32154f5b844d2564ed814ac6e769d458c4b6 Mon Sep 17 00:00:00 2001 From: Bastien Ho Date: Tue, 17 Oct 2023 08:59:41 +0200 Subject: [PATCH 3/4] Factorize *serialize functions --- CRM/Core/Payment/OmnipayMultiProcessor.php | 25 +++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/CRM/Core/Payment/OmnipayMultiProcessor.php b/CRM/Core/Payment/OmnipayMultiProcessor.php index 090d8611e..1213dde84 100644 --- a/CRM/Core/Payment/OmnipayMultiProcessor.php +++ b/CRM/Core/Payment/OmnipayMultiProcessor.php @@ -82,7 +82,18 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt * @var \Civi\Payment\PropertyBag */ protected $propertyBag; - + + /** + * For PHP8.1 + * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize + * @return array + */ + public function __serialize(): array { + $data = (object) get_object_vars($this); + $this->cleanupObjectForSerialization($data, TRUE); + return (array) $data; + } + /** * Serialize, first removing gateway * @@ -91,17 +102,7 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt * @return string */ public function serialize(): string { - $this->cleanupClassForSerialization(TRUE); - return serialize(get_object_vars($this)); - } - - /** - * For PHP8.1 - * https://www.php.net/manual/en/language.oop5.magic.php#object.serialize - */ - public function __serialize(): array { - //$this->cleanupClassForSerialization(TRUE); - return get_object_vars($this); + return serialize($this->serialize()); } /** From be6a2a2cfc4259bdfef13de8f11d030c95f56cd6 Mon Sep 17 00:00:00 2001 From: Bastien Ho Date: Tue, 17 Oct 2023 09:05:08 +0200 Subject: [PATCH 4/4] Factorize *unserialize functions --- CRM/Core/Payment/OmnipayMultiProcessor.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CRM/Core/Payment/OmnipayMultiProcessor.php b/CRM/Core/Payment/OmnipayMultiProcessor.php index 1213dde84..7d55c2826 100644 --- a/CRM/Core/Payment/OmnipayMultiProcessor.php +++ b/CRM/Core/Payment/OmnipayMultiProcessor.php @@ -114,14 +114,14 @@ public function serialize(): string { */ public function unserialize($data) { $values = unserialize($data); - foreach ($values as $key => $value) { - $this->$key = $value; - } + $this->__unserialize($values); } /** * For PHP8.1 * https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize + * + * @param array $data */ public function __unserialize(array $data): void { foreach ($data as $key => $value) {