-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[8.x] More Convenient Model Broadcasting #37491
Merged
Merged
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ce04ed4
Initial commit of model broadcasting conveniences
taylorotwell a26e908
allow null return from broadcaston
taylorotwell 93d39e0
add broadcast methods
taylorotwell a933120
Allow HasBroadcastChannel instances in routes
taylorotwell 3bd4cdd
Rename method
taylorotwell c1437a8
Do not broadcast if no channels for model event
taylorotwell af4dee8
add trait
taylorotwell a22f0dc
use model basename
taylorotwell b21e1af
allow manual override of channels
taylorotwell 91608aa
add test
taylorotwell 0400e11
add test
taylorotwell 2fb5317
allow specification of connection, queue, afterCommit
taylorotwell 81bf5b6
wip
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/Illuminate/Contracts/Broadcasting/HasBroadcastChannel.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Illuminate\Contracts\Broadcasting; | ||
|
||
interface HasBroadcastChannel | ||
{ | ||
/** | ||
* Get the broadcast channel route definition that is associated with the given entity. | ||
* | ||
* @return string | ||
*/ | ||
public function broadcastChannelRoute(); | ||
|
||
/** | ||
* Get the broadcast channel name that is associated with the given entity. | ||
* | ||
* @return string | ||
*/ | ||
public function broadcastChannel(); | ||
} |
62 changes: 62 additions & 0 deletions
62
src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent; | ||
|
||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class BroadcastableModelEventOccurred implements ShouldBroadcast | ||
{ | ||
use InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* The model instance corresponding to the event. | ||
* | ||
* @var \Illuminate\Database\Eloquent\Model | ||
*/ | ||
public $model; | ||
|
||
/** | ||
* The event name (created, updated, etc.). | ||
* | ||
* @var string | ||
*/ | ||
protected $event; | ||
|
||
/** | ||
* Create a new event instance. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @param string $event | ||
* @return void | ||
*/ | ||
public function __construct($model, $event) | ||
{ | ||
$this->model = $model; | ||
$this->event = $event; | ||
} | ||
|
||
/** | ||
* The channels the event should broadcast on. | ||
* | ||
* @return array | ||
*/ | ||
public function broadcastOn() | ||
{ | ||
return collect($this->model->broadcastOn($this->event) ?: [])->map(function ($channel) { | ||
return $channel instanceof Model ? new PrivateChannel($channel) : $channel; | ||
})->all(); | ||
} | ||
|
||
/** | ||
* The name the event should broadcast as. | ||
* | ||
* @return string | ||
*/ | ||
public function broadcastAs() | ||
{ | ||
return class_basename($this->model).ucfirst($this->event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
<?php | ||
|
||
namespace Illuminate\Database\Eloquent; | ||
|
||
trait BroadcastsEvents | ||
{ | ||
/** | ||
* Boot the event broadcasting trait. | ||
* | ||
* @return void | ||
*/ | ||
public static function bootBroadcastsEvents() | ||
{ | ||
static::created(function ($model) { | ||
$model->broadcastCreated(); | ||
}); | ||
|
||
static::updated(function ($model) { | ||
$model->broadcastUpdated(); | ||
}); | ||
|
||
if (method_exists(static::class, 'bootSoftDeletes')) { | ||
static::trashed(function ($model) { | ||
$model->broadcastTrashed(); | ||
}); | ||
|
||
static::restored(function ($model) { | ||
$model->broadcastRestored(); | ||
}); | ||
} | ||
|
||
static::deleted(function ($model) { | ||
$model->broadcastDeleted(); | ||
}); | ||
} | ||
|
||
/** | ||
* Broadcast that the model was created. | ||
* | ||
* @return \Illuminate\Broadcasting\PendingBroadcast | ||
*/ | ||
public function broadcastCreated() | ||
{ | ||
return $this->broadcastIfBroadcastChannelsExistForEvent( | ||
$this->newBroadcastableModelEvent('created'), 'created' | ||
); | ||
} | ||
|
||
/** | ||
* Broadcast that the model was updated. | ||
* | ||
* @return \Illuminate\Broadcasting\PendingBroadcast | ||
*/ | ||
public function broadcastUpdated() | ||
{ | ||
return $this->broadcastIfBroadcastChannelsExistForEvent( | ||
$this->newBroadcastableModelEvent('updated'), 'updated' | ||
); | ||
} | ||
|
||
/** | ||
* Broadcast that the model was trashed. | ||
* | ||
* @return \Illuminate\Broadcasting\PendingBroadcast | ||
*/ | ||
public function broadcastTrashed() | ||
{ | ||
return $this->broadcastIfBroadcastChannelsExistForEvent( | ||
$this->newBroadcastableModelEvent('trashed'), 'trashed' | ||
); | ||
} | ||
|
||
/** | ||
* Broadcast that the model was restored. | ||
* | ||
* @return \Illuminate\Broadcasting\PendingBroadcast | ||
*/ | ||
public function broadcastRestored() | ||
{ | ||
return $this->broadcastIfBroadcastChannelsExistForEvent( | ||
$this->newBroadcastableModelEvent('restored'), 'restored' | ||
); | ||
} | ||
|
||
/** | ||
* Broadcast that the model was deleted. | ||
* | ||
* @return \Illuminate\Broadcasting\PendingBroadcast | ||
*/ | ||
public function broadcastDeleted() | ||
{ | ||
return $this->broadcastIfBroadcastChannelsExistForEvent( | ||
$this->newBroadcastableModelEvent('deleted'), 'deleted' | ||
); | ||
} | ||
|
||
/** | ||
* Broadcast the given event instance if channels are configured for the model event. | ||
* | ||
* @param mixed $instance | ||
* @param string $event | ||
* @return \Illuminate\Broadcasting\PendingBroadcast|null | ||
*/ | ||
protected function broadcastIfBroadcastChannelsExistForEvent($instance, $event) | ||
{ | ||
if (! empty($this->broadcastOn($event))) { | ||
return broadcast($instance); | ||
} | ||
} | ||
|
||
/** | ||
* Get the channels that model events should broadcast on. | ||
* | ||
* @param string $event | ||
* @return \Illuminate\Broadcasting\Channel|array | ||
*/ | ||
public function broadcastOn($event) | ||
{ | ||
return [$this]; | ||
} | ||
|
||
/** | ||
* Create a new broadcastable model event event. | ||
* | ||
* @param string $event | ||
* @return mixed | ||
*/ | ||
public function newBroadcastableModelEvent($event) | ||
{ | ||
return new BroadcastableModelEventOccurred($this, $event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taylorotwell rather than having this (relatively) long method name, what would you think to having a method
shouldBroadcastEvent($event)
and then using this inbootBroadcastsEvents
? E.g.