BroadcastsEvents.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. use Illuminate\Support\Arr;
  4. trait BroadcastsEvents
  5. {
  6. /**
  7. * Boot the event broadcasting trait.
  8. *
  9. * @return void
  10. */
  11. public static function bootBroadcastsEvents()
  12. {
  13. static::created(function ($model) {
  14. $model->broadcastCreated();
  15. });
  16. static::updated(function ($model) {
  17. $model->broadcastUpdated();
  18. });
  19. if (method_exists(static::class, 'bootSoftDeletes')) {
  20. static::softDeleted(function ($model) {
  21. $model->broadcastTrashed();
  22. });
  23. static::restored(function ($model) {
  24. $model->broadcastRestored();
  25. });
  26. }
  27. static::deleted(function ($model) {
  28. $model->broadcastDeleted();
  29. });
  30. }
  31. /**
  32. * Broadcast that the model was created.
  33. *
  34. * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels
  35. * @return \Illuminate\Broadcasting\PendingBroadcast
  36. */
  37. public function broadcastCreated($channels = null)
  38. {
  39. return $this->broadcastIfBroadcastChannelsExistForEvent(
  40. $this->newBroadcastableModelEvent('created'), 'created', $channels
  41. );
  42. }
  43. /**
  44. * Broadcast that the model was updated.
  45. *
  46. * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels
  47. * @return \Illuminate\Broadcasting\PendingBroadcast
  48. */
  49. public function broadcastUpdated($channels = null)
  50. {
  51. return $this->broadcastIfBroadcastChannelsExistForEvent(
  52. $this->newBroadcastableModelEvent('updated'), 'updated', $channels
  53. );
  54. }
  55. /**
  56. * Broadcast that the model was trashed.
  57. *
  58. * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels
  59. * @return \Illuminate\Broadcasting\PendingBroadcast
  60. */
  61. public function broadcastTrashed($channels = null)
  62. {
  63. return $this->broadcastIfBroadcastChannelsExistForEvent(
  64. $this->newBroadcastableModelEvent('trashed'), 'trashed', $channels
  65. );
  66. }
  67. /**
  68. * Broadcast that the model was restored.
  69. *
  70. * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels
  71. * @return \Illuminate\Broadcasting\PendingBroadcast
  72. */
  73. public function broadcastRestored($channels = null)
  74. {
  75. return $this->broadcastIfBroadcastChannelsExistForEvent(
  76. $this->newBroadcastableModelEvent('restored'), 'restored', $channels
  77. );
  78. }
  79. /**
  80. * Broadcast that the model was deleted.
  81. *
  82. * @param \Illuminate\Broadcasting\Channel|\Illuminate\Contracts\Broadcasting\HasBroadcastChannel|array|null $channels
  83. * @return \Illuminate\Broadcasting\PendingBroadcast
  84. */
  85. public function broadcastDeleted($channels = null)
  86. {
  87. return $this->broadcastIfBroadcastChannelsExistForEvent(
  88. $this->newBroadcastableModelEvent('deleted'), 'deleted', $channels
  89. );
  90. }
  91. /**
  92. * Broadcast the given event instance if channels are configured for the model event.
  93. *
  94. * @param mixed $instance
  95. * @param string $event
  96. * @param mixed $channels
  97. * @return \Illuminate\Broadcasting\PendingBroadcast|null
  98. */
  99. protected function broadcastIfBroadcastChannelsExistForEvent($instance, $event, $channels = null)
  100. {
  101. if (! static::$isBroadcasting) {
  102. return;
  103. }
  104. if (! empty($this->broadcastOn($event)) || ! empty($channels)) {
  105. return broadcast($instance->onChannels(Arr::wrap($channels)));
  106. }
  107. }
  108. /**
  109. * Create a new broadcastable model event event.
  110. *
  111. * @param string $event
  112. * @return mixed
  113. */
  114. public function newBroadcastableModelEvent($event)
  115. {
  116. return tap($this->newBroadcastableEvent($event), function ($event) {
  117. $event->connection = property_exists($this, 'broadcastConnection')
  118. ? $this->broadcastConnection
  119. : $this->broadcastConnection();
  120. $event->queue = property_exists($this, 'broadcastQueue')
  121. ? $this->broadcastQueue
  122. : $this->broadcastQueue();
  123. $event->afterCommit = property_exists($this, 'broadcastAfterCommit')
  124. ? $this->broadcastAfterCommit
  125. : $this->broadcastAfterCommit();
  126. });
  127. }
  128. /**
  129. * Create a new broadcastable model event for the model.
  130. *
  131. * @param string $event
  132. * @return \Illuminate\Database\Eloquent\BroadcastableModelEventOccurred
  133. */
  134. protected function newBroadcastableEvent(string $event)
  135. {
  136. return new BroadcastableModelEventOccurred($this, $event);
  137. }
  138. /**
  139. * Get the channels that model events should broadcast on.
  140. *
  141. * @param string $event
  142. * @return \Illuminate\Broadcasting\Channel|array
  143. */
  144. public function broadcastOn($event)
  145. {
  146. return [$this];
  147. }
  148. /**
  149. * Get the queue connection that should be used to broadcast model events.
  150. *
  151. * @return string|null
  152. */
  153. public function broadcastConnection()
  154. {
  155. //
  156. }
  157. /**
  158. * Get the queue that should be used to broadcast model events.
  159. *
  160. * @return string|null
  161. */
  162. public function broadcastQueue()
  163. {
  164. //
  165. }
  166. /**
  167. * Determine if the model event broadcast queued job should be dispatched after all transactions are committed.
  168. *
  169. * @return bool
  170. */
  171. public function broadcastAfterCommit()
  172. {
  173. return false;
  174. }
  175. }