|
5 | 5 | use Ushahidi\Core\Facade\Site; |
6 | 6 | use Illuminate\Support\Facades\Log; |
7 | 7 | use Illuminate\Support\Facades\Event; |
8 | | -use Illuminate\Queue\SerializesModels; |
| 8 | +use Illuminate\Queue\SerializesAndRestoresModelIdentifiers; |
| 9 | +use ReflectionClass; |
| 10 | +use ReflectionProperty; |
9 | 11 |
|
10 | 12 | trait SiteAware |
11 | 13 | { |
12 | 14 | /** |
13 | 15 | * @var int The hostname ID of the previously active deployment |
14 | 16 | */ |
15 | 17 | protected $site; |
| 18 | + use SerializesAndRestoresModelIdentifiers; |
16 | 19 |
|
17 | | - use SerializesModels { |
18 | | - __sleep as serializedSleep; |
19 | | - __wakeup as serializedWakeup; |
20 | | - } |
21 | 20 |
|
22 | 21 | public function __sleep() |
23 | 22 | { |
24 | | - if (!$this->site) { |
25 | | - $this->site = Site::instance()->getId(); |
26 | | - Log::debug('Saving deployment id for job', [$this->site]); |
27 | | - } |
| 23 | + $this->site = Site::instance()->getId(); |
| 24 | + |
| 25 | + $properties = (new ReflectionClass($this))->getProperties(); |
28 | 26 |
|
29 | | - $attributes = $this->serializedSleep(); |
| 27 | + foreach ($properties as $property) { |
| 28 | + $property->setValue($this, $this->getSerializedPropertyValue( |
| 29 | + $this->getPropertyValue($property) |
| 30 | + )); |
| 31 | + } |
30 | 32 |
|
31 | | - return $attributes; |
| 33 | + return array_values(array_filter(array_map(function ($p) { |
| 34 | + return $p->isStatic() ? null : $p->getName(); |
| 35 | + }, $properties))); |
32 | 36 | } |
33 | 37 |
|
34 | 38 | public function __wakeup() |
35 | 39 | { |
36 | 40 | if (isset($this->site) && $this->site) { |
37 | | - Log::debug('Restoring deployment id for job', [$this->site]); |
38 | 41 | Event::dispatch('site.restored', ['site' => $this->site]); |
39 | 42 | } |
| 43 | + foreach ((new ReflectionClass($this))->getProperties() as $property) { |
| 44 | + if ($property->isStatic()) { |
| 45 | + continue; |
| 46 | + } |
| 47 | + |
| 48 | + $property->setValue($this, $this->getRestoredPropertyValue( |
| 49 | + $this->getPropertyValue($property) |
| 50 | + )); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Get the property value for the given property. |
| 56 | + * |
| 57 | + * @param \ReflectionProperty $property |
| 58 | + * @return mixed |
| 59 | + */ |
| 60 | + protected function getPropertyValue(ReflectionProperty $property) |
| 61 | + { |
| 62 | + $property->setAccessible(true); |
40 | 63 |
|
41 | | - $this->serializedWakeup(); |
| 64 | + return $property->getValue($this); |
42 | 65 | } |
43 | 66 | } |
0 commit comments