1 |
cb15593b
|
Cajova-Houba
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace Illuminate\Queue;
|
4 |
|
|
|
5 |
|
|
use ReflectionClass;
|
6 |
|
|
use ReflectionProperty;
|
7 |
|
|
use Illuminate\Contracts\Queue\QueueableEntity;
|
8 |
|
|
use Illuminate\Contracts\Database\ModelIdentifier;
|
9 |
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
10 |
|
|
|
11 |
|
|
trait SerializesModels
|
12 |
|
|
{
|
13 |
|
|
/**
|
14 |
|
|
* Prepare the instance for serialization.
|
15 |
|
|
*
|
16 |
|
|
* @return array
|
17 |
|
|
*/
|
18 |
|
|
public function __sleep()
|
19 |
|
|
{
|
20 |
|
|
$properties = (new ReflectionClass($this))->getProperties();
|
21 |
|
|
|
22 |
|
|
foreach ($properties as $property) {
|
23 |
|
|
$property->setValue($this, $this->getSerializedPropertyValue(
|
24 |
|
|
$this->getPropertyValue($property)
|
25 |
|
|
));
|
26 |
|
|
}
|
27 |
|
|
|
28 |
|
|
return array_map(function ($p) {
|
29 |
|
|
return $p->getName();
|
30 |
|
|
}, $properties);
|
31 |
|
|
}
|
32 |
|
|
|
33 |
|
|
/**
|
34 |
|
|
* Restore the model after serialization.
|
35 |
|
|
*
|
36 |
|
|
* @return void
|
37 |
|
|
*/
|
38 |
|
|
public function __wakeup()
|
39 |
|
|
{
|
40 |
|
|
foreach ((new ReflectionClass($this))->getProperties() as $property) {
|
41 |
|
|
$property->setValue($this, $this->getRestoredPropertyValue(
|
42 |
|
|
$this->getPropertyValue($property)
|
43 |
|
|
));
|
44 |
|
|
}
|
45 |
|
|
}
|
46 |
|
|
|
47 |
|
|
/**
|
48 |
|
|
* Get the property value prepared for serialization.
|
49 |
|
|
*
|
50 |
|
|
* @param mixed $value
|
51 |
|
|
* @return mixed
|
52 |
|
|
*/
|
53 |
|
|
protected function getSerializedPropertyValue($value)
|
54 |
|
|
{
|
55 |
|
|
if ($value instanceof QueueableEntity) {
|
56 |
|
|
return new ModelIdentifier(get_class($value), $value->getQueueableId());
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
return $value;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
/**
|
63 |
|
|
* Get the restored property value after deserialization.
|
64 |
|
|
*
|
65 |
|
|
* @param mixed $value
|
66 |
|
|
* @return mixed
|
67 |
|
|
*/
|
68 |
|
|
protected function getRestoredPropertyValue($value)
|
69 |
|
|
{
|
70 |
|
|
if (! $value instanceof ModelIdentifier) {
|
71 |
|
|
return $value;
|
72 |
|
|
}
|
73 |
|
|
|
74 |
|
|
return is_array($value->id)
|
75 |
|
|
? $this->restoreCollection($value)
|
76 |
|
|
: (new $value->class)->newQuery()->useWritePdo()->findOrFail($value->id);
|
77 |
|
|
}
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Restore a queueable collection instance.
|
81 |
|
|
*
|
82 |
|
|
* @param \Illuminate\Contracts\Database\ModelIdentifier $value
|
83 |
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
84 |
|
|
*/
|
85 |
|
|
protected function restoreCollection($value)
|
86 |
|
|
{
|
87 |
|
|
if (! $value->class || count($value->id) === 0) {
|
88 |
|
|
return new EloquentCollection;
|
89 |
|
|
}
|
90 |
|
|
|
91 |
|
|
$model = new $value->class;
|
92 |
|
|
|
93 |
|
|
return $model->newQuery()->useWritePdo()
|
94 |
|
|
->whereIn($model->getKeyName(), $value->id)->get();
|
95 |
|
|
}
|
96 |
|
|
|
97 |
|
|
/**
|
98 |
|
|
* Get the property value for the given property.
|
99 |
|
|
*
|
100 |
|
|
* @param \ReflectionProperty $property
|
101 |
|
|
* @return mixed
|
102 |
|
|
*/
|
103 |
|
|
protected function getPropertyValue(ReflectionProperty $property)
|
104 |
|
|
{
|
105 |
|
|
$property->setAccessible(true);
|
106 |
|
|
|
107 |
|
|
return $property->getValue($this);
|
108 |
|
|
}
|
109 |
|
|
}
|