From 8804c950bb16d68496e5634b4a6eacb8abe24de5 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 29 Sep 2021 12:59:32 +0100 Subject: [PATCH 1/3] Fixes rebound closures serialization --- src/Serializers/Native.php | 20 ++++++++++++++++++++ tests/SerializerTest.php | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index bbea9285..5647c149 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -200,6 +200,26 @@ public function __unserialize($data) $this->code = $this->code['function']; } + /** + * Wraps a closure and sets the serialization context (if any). + * + * @param \Closure $closure + * @return static + */ + public static function from($closure) + { + if (static::$context === null) { + $instance = new static($closure); + } elseif (isset(static::$context->scope[$closure])) { + $instance = static::$context->scope[$closure]; + } else { + $instance = new static($closure); + static::$context->scope[$closure] = $instance; + } + + return $instance; + } + /** * Ensures the given closures are serializable. * diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index e40d8962..1efa4541 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -367,6 +367,24 @@ function () { expect($r)->toEqual('Hi'); })->with('serializers'); + +test('rebound closure', function () { + $closure = Closure::bind( + function () { + return $this->hello(); + }, + new A3(function () { + return 'Hi'; + }), + A3::class + ); + + $u = s($closure); + $r = $u(); + + expect($r)->toEqual('Hi'); +})->with('serializers'); + class A { protected static function aStaticProtected() @@ -422,6 +440,21 @@ public function getEquality() } } +class A3 +{ + private $closure; + + public function __construct($closure) + { + $this->closure = $closure; + } + + public function hello() + { + return ($this->closure)(); + } +} + class ObjSelf { public $o; From f1c7194a8a4b10fc146e1ddbe75f15d5cbfad16d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 29 Sep 2021 13:14:11 +0000 Subject: [PATCH 2/3] Apply fixes from StyleCI --- tests/SerializerTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 1efa4541..378e6cb4 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -367,7 +367,6 @@ function () { expect($r)->toEqual('Hi'); })->with('serializers'); - test('rebound closure', function () { $closure = Closure::bind( function () { From da2e37827f7ce6dbb988a49f07f2489504800b48 Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Wed, 29 Sep 2021 14:16:16 +0100 Subject: [PATCH 3/3] Applies minor refactors --- src/Serializers/Native.php | 22 +--------------------- tests/SerializerTest.php | 1 - 2 files changed, 1 insertion(+), 22 deletions(-) diff --git a/src/Serializers/Native.php b/src/Serializers/Native.php index 5647c149..95916681 100644 --- a/src/Serializers/Native.php +++ b/src/Serializers/Native.php @@ -200,26 +200,6 @@ public function __unserialize($data) $this->code = $this->code['function']; } - /** - * Wraps a closure and sets the serialization context (if any). - * - * @param \Closure $closure - * @return static - */ - public static function from($closure) - { - if (static::$context === null) { - $instance = new static($closure); - } elseif (isset(static::$context->scope[$closure])) { - $instance = static::$context->scope[$closure]; - } else { - $instance = new static($closure); - static::$context->scope[$closure] = $instance; - } - - return $instance; - } - /** * Ensures the given closures are serializable. * @@ -230,7 +210,7 @@ public static function from($closure) public static function wrapClosures(&$data, $storage) { if ($data instanceof Closure) { - $data = static::from($data); + $data = new static($data); } elseif (is_array($data)) { if (isset($data[self::ARRAY_RECURSIVE_KEY])) { return; diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index 1efa4541..378e6cb4 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -367,7 +367,6 @@ function () { expect($r)->toEqual('Hi'); })->with('serializers'); - test('rebound closure', function () { $closure = Closure::bind( function () {