Skip to content

Commit

Permalink
allow applying WithoutRelations to class
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmastech committed Aug 14, 2023
1 parent 8bea688 commit 1a028a7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Attributes/WithoutRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
#[Attribute(Attribute::TARGET_PROPERTY|Attribute::TARGET_CLASS)]
class WithoutRelations
{
//
Expand Down
11 changes: 6 additions & 5 deletions src/Illuminate/Queue/SerializesModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function __serialize()
{
$values = [];

$properties = (new ReflectionClass($this))->getProperties();
$reflectionClass = new ReflectionClass($this);
$properties = $reflectionClass->getProperties();
$classExplicitlyBarsRelations = ! empty($reflectionClass->getAttributes(WithoutRelations::class));

$class = get_class($this);

Expand All @@ -46,10 +48,9 @@ public function __serialize()
$name = "\0*\0{$name}";
}

$values[$name] = $this->getSerializedPropertyValue(
$value,
empty($property->getAttributes(WithoutRelations::class))
);
$withoutRelations = $classExplicitlyBarsRelations || ! empty($property->getAttributes(WithoutRelations::class));

$values[$name] = $this->getSerializedPropertyValue($value, ! $withoutRelations);
}

return $values;
Expand Down
30 changes: 30 additions & 0 deletions tests/Integration/Queue/ModelSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,24 @@ public function test_it_respects_without_relations_attribute()
);
}

public function test_it_respects_without_relations_attribute_applied_to_class()
{
$user = User::create([
'email' => 'taylor@laravel.com',
])->load(['roles']);

$serialized = serialize(new ModelSerializationAttributeTargetsClassTestClass($user));

$this->assertSame(
'O:83:"Illuminate\Tests\Integration\Queue\ModelSerializationAttributeTargetsClassTestClass":1:{s:4:"user";O:45:"Illuminate\Contracts\Database\ModelIdentifier":5:{s:5:"class";s:39:"Illuminate\Tests\Integration\Queue\User";s:2:"id";i:1;s:9:"relations";a:0:{}s:10:"connection";s:7:"testing";s:15:"collectionClass";N;}}', $serialized
);

/** @var ModelSerializationAttributeTargetsClassTestClass $unserialized */
$unserialized = unserialize($serialized);

$this->assertFalse($unserialized->user->relationLoaded('roles'));
}

public function test_serialization_types_empty_custom_eloquent_collection()
{
$class = new ModelSerializationTypedCustomCollectionTestClass(
Expand Down Expand Up @@ -526,6 +544,18 @@ public function __construct(User $user)
}
}

#[WithoutRelations]
class ModelSerializationAttributeTargetsClassTestClass
{
use SerializesModels;

public User $user;

public function __construct(User $user) {
$this->user = $user;
}
}

class ModelRelationSerializationTestClass
{
use SerializesModels;
Expand Down

0 comments on commit 1a028a7

Please sign in to comment.