From 1e85d97d4db1f0b6ae79934263877929f92781c4 Mon Sep 17 00:00:00 2001 From: Mh-Asmi Date: Tue, 6 Aug 2024 23:11:03 +0400 Subject: [PATCH] stop using SerializeModels --- src/Ushahidi/Core/Concerns/SiteAware.php | 49 +++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) 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); } }