Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}