Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case when SerializableClosure uses as class property #86

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.lock
/phpunit.xml
.phpunit.result.cache
.idea
28 changes: 24 additions & 4 deletions src/Serializers/Native.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,33 @@ public function __unserialize($data)

$this->closure = $this->closure->bindTo($this->code['this'], $this->code['scope']);

$this->bindObjectsIfNeeded();

$this->code = $this->code['function'];
}

private function bindObjectsIfNeeded()
{
if (! empty($this->code['objects'])) {
foreach ($this->code['objects'] as $item) {
$item['property']->setValue($item['instance'], $item['object']->getClosure());
}
$this->bindObjects();
}
}

$this->code = $this->code['function'];
private function bindObjects()
{
foreach ($this->code['objects'] as $item) {
$item['property']->setValue($item['instance'], $this->calculateObjectValue($item));
}
}

private function calculateObjectValue($item)
{
return $this->isSerializableClosure($item['object']) ? $item['object'] : $item['object']->getClosure();
}

private function isSerializableClosure($object): bool
{
return $object instanceof SerializableClosure || $object instanceof UnsignedSerializableClosure;
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Fixtures/ClassWithPublicProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures;

class ClassWithPublicProperty
{
public $closure;

public function __construct($closure)
{
$this->closure = $closure;
}
}
29 changes: 29 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Laravel\SerializableClosure\Serializers\Signed;
use Laravel\SerializableClosure\Support\ReflectionClosure;
use Laravel\SerializableClosure\UnsignedSerializableClosure;
use Tests\Fixtures\ClassWithPublicProperty;
use Tests\Fixtures\Model;

test('closure with simple const', function () {
Expand Down Expand Up @@ -485,6 +486,34 @@ function () {
new CarbonImmutable,
]);

test('SerializableClosure in the class property', function () {
SerializableClosure::setSecretKey('foo');

$innerClosure = new ClassWithPublicProperty(new SerializableClosure(function () {
}));
$outerClosure = new SerializableClosure(function () use ($innerClosure) {
return $innerClosure->closure;
});

$unSerializedOuterClosure = unserialize(serialize($outerClosure));

expect($outerClosure())->toBeInstanceOf(SerializableClosure::class);
expect($unSerializedOuterClosure())->toBeInstanceOf(SerializableClosure::class);
});

test('UnsignedSerializableClosure in the class property', function () {
$innerClosure = new ClassWithPublicProperty(SerializableClosure::unsigned(function () {
}));
$outerClosure = SerializableClosure::unsigned(function () use ($innerClosure) {
return $innerClosure->closure;
});

$unSerializedOuterClosure = unserialize(serialize($outerClosure));

expect($outerClosure())->toBeInstanceOf(UnsignedSerializableClosure::class);
expect($unSerializedOuterClosure())->toBeInstanceOf(UnsignedSerializableClosure::class);
});

function serializer_php_74_switch_statement_test_is_two($a)
{
return $a === 2;
Expand Down