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