SerializesAndRestoresModelIdentifiers.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Illuminate\Queue;
  3. use Illuminate\Contracts\Queue\QueueableEntity;
  4. use Illuminate\Contracts\Database\ModelIdentifier;
  5. use Illuminate\Contracts\Queue\QueueableCollection;
  6. use Illuminate\Database\Eloquent\Collection as EloquentCollection;
  7. trait SerializesAndRestoresModelIdentifiers
  8. {
  9. /**
  10. * Get the property value prepared for serialization.
  11. *
  12. * @param mixed $value
  13. * @return mixed
  14. */
  15. protected function getSerializedPropertyValue($value)
  16. {
  17. if ($value instanceof QueueableCollection) {
  18. return new ModelIdentifier(
  19. $value->getQueueableClass(),
  20. $value->getQueueableIds(),
  21. $value->getQueueableRelations(),
  22. $value->getQueueableConnection()
  23. );
  24. }
  25. if ($value instanceof QueueableEntity) {
  26. return new ModelIdentifier(
  27. get_class($value),
  28. $value->getQueueableId(),
  29. $value->getQueueableRelations(),
  30. $value->getQueueableConnection()
  31. );
  32. }
  33. return $value;
  34. }
  35. /**
  36. * Get the restored property value after deserialization.
  37. *
  38. * @param mixed $value
  39. * @return mixed
  40. */
  41. protected function getRestoredPropertyValue($value)
  42. {
  43. if (! $value instanceof ModelIdentifier) {
  44. return $value;
  45. }
  46. return is_array($value->id)
  47. ? $this->restoreCollection($value)
  48. : $this->restoreModel($value);
  49. }
  50. /**
  51. * Restore a queueable collection instance.
  52. *
  53. * @param \Illuminate\Contracts\Database\ModelIdentifier $value
  54. * @return \Illuminate\Database\Eloquent\Collection
  55. */
  56. protected function restoreCollection($value)
  57. {
  58. if (! $value->class || count($value->id) === 0) {
  59. return new EloquentCollection;
  60. }
  61. return $this->getQueryForModelRestoration(
  62. (new $value->class)->setConnection($value->connection), $value->id
  63. )->useWritePdo()->get();
  64. }
  65. /**
  66. * Restore the model from the model identifier instance.
  67. *
  68. * @param \Illuminate\Contracts\Database\ModelIdentifier $value
  69. * @return \Illuminate\Database\Eloquent\Model
  70. */
  71. public function restoreModel($value)
  72. {
  73. return $this->getQueryForModelRestoration(
  74. (new $value->class)->setConnection($value->connection), $value->id
  75. )->useWritePdo()->firstOrFail()->load($value->relations ?? []);
  76. }
  77. /**
  78. * Get the query for model restoration.
  79. *
  80. * @param \Illuminate\Database\Eloquent\Model $model
  81. * @param array|int $ids
  82. * @return \Illuminate\Database\Eloquent\Builder
  83. */
  84. protected function getQueryForModelRestoration($model, $ids)
  85. {
  86. return $model->newQueryForRestoration($ids);
  87. }
  88. }