BroadcastableModelEventOccurred.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use Illuminate\Broadcasting\InteractsWithSockets;
  4. use Illuminate\Broadcasting\PrivateChannel;
  5. use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
  6. use Illuminate\Queue\SerializesModels;
  7. class BroadcastableModelEventOccurred implements ShouldBroadcast
  8. {
  9. use InteractsWithSockets, SerializesModels;
  10. /**
  11. * The model instance corresponding to the event.
  12. *
  13. * @var \Illuminate\Database\Eloquent\Model
  14. */
  15. public $model;
  16. /**
  17. * The event name (created, updated, etc.).
  18. *
  19. * @var string
  20. */
  21. protected $event;
  22. /**
  23. * The channels that the event should be broadcast on.
  24. *
  25. * @var array
  26. */
  27. protected $channels = [];
  28. /**
  29. * The queue connection that should be used to queue the broadcast job.
  30. *
  31. * @var string
  32. */
  33. public $connection;
  34. /**
  35. * The queue that should be used to queue the broadcast job.
  36. *
  37. * @var string
  38. */
  39. public $queue;
  40. /**
  41. * Indicates whether the job should be dispatched after all database transactions have committed.
  42. *
  43. * @var bool|null
  44. */
  45. public $afterCommit;
  46. /**
  47. * Create a new event instance.
  48. *
  49. * @param \Illuminate\Database\Eloquent\Model $model
  50. * @param string $event
  51. * @return void
  52. */
  53. public function __construct($model, $event)
  54. {
  55. $this->model = $model;
  56. $this->event = $event;
  57. }
  58. /**
  59. * The channels the event should broadcast on.
  60. *
  61. * @return array
  62. */
  63. public function broadcastOn()
  64. {
  65. $channels = empty($this->channels)
  66. ? ($this->model->broadcastOn($this->event) ?: [])
  67. : $this->channels;
  68. return collect($channels)->map(function ($channel) {
  69. return $channel instanceof Model ? new PrivateChannel($channel) : $channel;
  70. })->all();
  71. }
  72. /**
  73. * The name the event should broadcast as.
  74. *
  75. * @return string
  76. */
  77. public function broadcastAs()
  78. {
  79. $default = class_basename($this->model).ucfirst($this->event);
  80. return method_exists($this->model, 'broadcastAs')
  81. ? ($this->model->broadcastAs($this->event) ?: $default)
  82. : $default;
  83. }
  84. /**
  85. * Get the data that should be sent with the broadcasted event.
  86. *
  87. * @return array|null
  88. */
  89. public function broadcastWith()
  90. {
  91. return method_exists($this->model, 'broadcastWith')
  92. ? $this->model->broadcastWith($this->event)
  93. : null;
  94. }
  95. /**
  96. * Manually specify the channels the event should broadcast on.
  97. *
  98. * @param array $channels
  99. * @return $this
  100. */
  101. public function onChannels(array $channels)
  102. {
  103. $this->channels = $channels;
  104. return $this;
  105. }
  106. /**
  107. * Determine if the event should be broadcast synchronously.
  108. *
  109. * @return bool
  110. */
  111. public function shouldBroadcastNow()
  112. {
  113. return $this->event === 'deleted' &&
  114. ! method_exists($this->model, 'bootSoftDeletes');
  115. }
  116. /**
  117. * Get the event name.
  118. *
  119. * @return string
  120. */
  121. public function event()
  122. {
  123. return $this->event;
  124. }
  125. }