diff --git a/src/Ushahidi/Core/Concerns/SiteAware.php b/src/Ushahidi/Core/Concerns/SiteAware.php index 1f3b04ddd5..e4b3509994 100644 --- a/src/Ushahidi/Core/Concerns/SiteAware.php +++ b/src/Ushahidi/Core/Concerns/SiteAware.php @@ -5,7 +5,9 @@ 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 { @@ -13,31 +15,52 @@ 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); } }