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

[6.x] Allow model serialization for typed properties #30604

Merged
merged 5 commits into from
Nov 15, 2019
Merged
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
3 changes: 3 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
php:
preset: laravel
finder:
not-name:
- typed-properties.php
js:
finder:
not-name:
Expand Down
37 changes: 32 additions & 5 deletions src/Illuminate/Queue/SerializesModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

namespace Illuminate\Queue;

use Illuminate\Contracts\Database\ModelIdentifier;
use ReflectionClass;
use ReflectionProperty;

trait SerializesModels
{
use SerializesAndRestoresModelIdentifiers;

/**
* The list of serialized model identifiers.
*
* @var array
*/
protected $modelIdentifiers = [];

/**
* Prepare the instance for serialization.
*
Expand All @@ -19,9 +27,22 @@ public function __sleep()
$properties = (new ReflectionClass($this))->getProperties();

foreach ($properties as $property) {
$property->setValue($this, $this->getSerializedPropertyValue(
$this->getPropertyValue($property)
));
if ($property->getName() === 'modelIdentifiers') {
continue;
}

$serializedValue = $this->getSerializedPropertyValue(
$value = $this->getPropertyValue($property)
);

if ($serializedValue instanceof ModelIdentifier) {
$this->modelIdentifiers[$property->getName()] = $serializedValue;

// Empty instance of the model or collection to support typed properties...
$property->setValue($this, new $value);
} else {
$property->setValue($this, $value);
}
}

return array_values(array_filter(array_map(function ($p) {
Expand All @@ -37,12 +58,18 @@ public function __sleep()
public function __wakeup()
{
foreach ((new ReflectionClass($this))->getProperties() as $property) {
if ($property->isStatic()) {
if ($property->isStatic() || $property->getName() === 'modelIdentifiers') {
continue;
}

if (isset($this->modelIdentifiers[$property->getName()])) {
$value = $this->modelIdentifiers[$property->getName()];
} else {
$value = $this->getPropertyValue($property);
}

$property->setValue($this, $this->getRestoredPropertyValue(
$this->getPropertyValue($property)
$value
));
}
}
Expand Down
59 changes: 47 additions & 12 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ public function testItSerializeUserOnDefaultConnection()
$this->assertSame('testbench', $unSerialized->user->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user->email);

$serialized = serialize(new ModelSerializationTestClass(ModelSerializationTestUser::on('testbench')->get()));
$serialized = serialize(new CollectionSerializationTestClass(ModelSerializationTestUser::on('testbench')->get()));

$unSerialized = unserialize($serialized);

$this->assertSame('testbench', $unSerialized->user[0]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user[0]->email);
$this->assertSame('testbench', $unSerialized->user[1]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user[1]->email);
$this->assertSame('testbench', $unSerialized->users[0]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[0]->email);
$this->assertSame('testbench', $unSerialized->users[1]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[1]->email);
}

public function testItSerializeUserOnDifferentConnection()
Expand All @@ -117,14 +117,14 @@ public function testItSerializeUserOnDifferentConnection()
$this->assertSame('custom', $unSerialized->user->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user->email);

$serialized = serialize(new ModelSerializationTestClass(ModelSerializationTestUser::on('custom')->get()));
$serialized = serialize(new CollectionSerializationTestClass(ModelSerializationTestUser::on('custom')->get()));

$unSerialized = unserialize($serialized);

$this->assertSame('custom', $unSerialized->user[0]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user[0]->email);
$this->assertSame('custom', $unSerialized->user[1]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user[1]->email);
$this->assertSame('custom', $unSerialized->users[0]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[0]->email);
$this->assertSame('custom', $unSerialized->users[1]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[1]->email);
}

public function testItFailsIfModelsOnMultiConnections()
Expand All @@ -140,7 +140,7 @@ public function testItFailsIfModelsOnMultiConnections()
'email' => '[email protected]',
]);

$serialized = serialize(new ModelSerializationTestClass(
$serialized = serialize(new CollectionSerializationTestClass(
new Collection([$user, $user2])
));

Expand Down Expand Up @@ -214,7 +214,7 @@ public function testItCanUnserializeNestedRelationshipsWithoutPivot()

public function testItSerializesAnEmptyCollection()
{
$serialized = serialize(new ModelSerializationTestClass(
$serialized = serialize(new CollectionSerializationTestClass(
new Collection([])
));

Expand Down Expand Up @@ -269,6 +269,41 @@ public function testItCanUnserializeCustomCollection()

$this->assertInstanceOf(ModelSerializationTestCustomUserCollection::class, $unserialized->users);
}

public function testItSerializesTypedProperties()
{
if (version_compare(phpversion(), '7.4.0-dev', '<')) {
$this->markTestSkipped('Typed properties are only available from PHP 7.4 and up.');
}

require_once __DIR__.'/typed-properties.php';

$user = ModelSerializationTestUser::create([
'email' => '[email protected]',
]);

ModelSerializationTestUser::create([
'email' => '[email protected]',
]);

$serialized = serialize(new TypedPropertyTestClass($user, 5, ['James', 'Taylor', 'Mohamed']));

$unSerialized = unserialize($serialized);

$this->assertSame('testbench', $unSerialized->user->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->user->email);
$this->assertSame(5, $unSerialized->id);
$this->assertSame(['James', 'Taylor', 'Mohamed'], $unSerialized->names);

$serialized = serialize(new TypedPropertyCollectionTestClass(ModelSerializationTestUser::on('testbench')->get()));

$unSerialized = unserialize($serialized);

$this->assertSame('testbench', $unSerialized->users[0]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[0]->email);
$this->assertSame('testbench', $unSerialized->users[1]->getConnectionName());
$this->assertSame('[email protected]', $unSerialized->users[1]->email);
}
}

class ModelSerializationTestUser extends Model
Expand Down
36 changes: 36 additions & 0 deletions tests/Integration/Queue/typed-properties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Illuminate\Tests\Integration\Queue;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Queue\SerializesModels;

class TypedPropertyTestClass
{
use SerializesModels;

public ModelSerializationTestUser $user;

public int $id;

public array $names;

public function __construct(ModelSerializationTestUser $user, int $id, array $names)
{
$this->user = $user;
$this->id = $id;
$this->names = $names;
}
}

class TypedPropertyCollectionTestClass
{
use SerializesModels;

public Collection $users;

public function __construct(Collection $users)
{
$this->users = $users;
}
}