59 lines
1.3 KiB
PHP
59 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Illuminate\Queue;
|
|
|
|
use ReflectionClass;
|
|
use ReflectionProperty;
|
|
|
|
trait SerializesModels
|
|
{
|
|
use SerializesAndRestoresModelIdentifiers;
|
|
|
|
/**
|
|
* Prepare the instance for serialization.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function __sleep()
|
|
{
|
|
$properties = (new ReflectionClass($this))->getProperties();
|
|
|
|
foreach ($properties as $property) {
|
|
$property->setValue($this, $this->getSerializedPropertyValue(
|
|
$this->getPropertyValue($property)
|
|
));
|
|
}
|
|
|
|
return array_map(function ($p) {
|
|
return $p->getName();
|
|
}, $properties);
|
|
}
|
|
|
|
/**
|
|
* Restore the model after serialization.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __wakeup()
|
|
{
|
|
foreach ((new ReflectionClass($this))->getProperties() as $property) {
|
|
$property->setValue($this, $this->getRestoredPropertyValue(
|
|
$this->getPropertyValue($property)
|
|
));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the property value for the given property.
|
|
*
|
|
* @param \ReflectionProperty $property
|
|
* @return mixed
|
|
*/
|
|
protected function getPropertyValue(ReflectionProperty $property)
|
|
{
|
|
$property->setAccessible(true);
|
|
|
|
return $property->getValue($this);
|
|
}
|
|
}
|