Skip to content

Commit

Permalink
stop using SerializeModels
Browse files Browse the repository at this point in the history
  • Loading branch information
Mh-Asmi committed Aug 6, 2024
1 parent df8cd66 commit 0a1f23d
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions src/Ushahidi/Core/Concerns/SiteAware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,62 @@
use Ushahidi\Core\Facade\Site;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\SerializesAndRestoresModelIdentifiers;
use ReflectionClass;
use ReflectionProperty;

trait SiteAware
{
/**
* @var int The hostname ID of the previously active deployment
*/
protected $site;
use SerializesAndRestoresModelIdentifiers;

use SerializesModels {
__sleep as serializedSleep;
__wakeup as serializedWakeup;
}

public function __sleep()
{
if (!$this->site) {
$this->site = Site::instance()->getId();
Log::debug('Saving deployment id for job', [$this->site]);
}
$this->site = Site::instance()->getId();

$properties = (new ReflectionClass($this))->getProperties();

$attributes = $this->serializedSleep();
foreach ($properties as $property) {
$property->setValue($this, $this->getSerializedPropertyValue(
$this->getPropertyValue($property)
));
}

return $attributes;
return array_values(array_filter(array_map(function ($p) {
return $p->isStatic() ? null : $p->getName();
}, $properties)));
}

public function __wakeup()
{
if (isset($this->site) && $this->site) {
Log::debug('Restoring deployment id for job', [$this->site]);
Event::dispatch('site.restored', ['site' => $this->site]);
}
foreach ((new ReflectionClass($this))->getProperties() as $property) {
if ($property->isStatic()) {
continue;
}

$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);

$this->serializedWakeup();
return $property->getValue($this);
}
}

0 comments on commit 0a1f23d

Please sign in to comment.