diff --git a/src/Illuminate/Broadcasting/BroadcastEvent.php b/src/Illuminate/Broadcasting/BroadcastEvent.php index 372212f35f17..bfa4fc680998 100644 --- a/src/Illuminate/Broadcasting/BroadcastEvent.php +++ b/src/Illuminate/Broadcasting/BroadcastEvent.php @@ -3,7 +3,7 @@ namespace Illuminate\Broadcasting; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Broadcasting\Broadcaster; +use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Support\Arr; @@ -52,10 +52,10 @@ public function __construct($event) /** * Handle the queued job. * - * @param \Illuminate\Contracts\Broadcasting\Broadcaster $broadcaster + * @param \Illuminate\Contracts\Broadcasting\Factory $manager * @return void */ - public function handle(Broadcaster $broadcaster) + public function handle(BroadcastingFactory $manager) { $name = method_exists($this->event, 'broadcastAs') ? $this->event->broadcastAs() : get_class($this->event); @@ -66,10 +66,17 @@ public function handle(Broadcaster $broadcaster) return; } - $broadcaster->broadcast( - $channels, $name, - $this->getPayloadFromEvent($this->event) - ); + $connections = method_exists($this->event, 'broadcastConnections') + ? $this->event->broadcastConnections() + : [null]; + + $payload = $this->getPayloadFromEvent($this->event); + + foreach ($connections as $connection) { + $manager->connection($connection)->broadcast( + $channels, $name, $payload + ); + } } /** diff --git a/src/Illuminate/Broadcasting/InteractsWithBroadcasting.php b/src/Illuminate/Broadcasting/InteractsWithBroadcasting.php new file mode 100644 index 000000000000..fd27a8cabb67 --- /dev/null +++ b/src/Illuminate/Broadcasting/InteractsWithBroadcasting.php @@ -0,0 +1,40 @@ +broadcastConnection = is_null($connection) + ? [null] + : Arr::wrap($connection); + + return $this; + } + + /** + * Get the broadcaster connections the event should be broadcast on. + * + * @return array + */ + public function broadcastConnections() + { + return $this->broadcastConnection; + } +} diff --git a/src/Illuminate/Broadcasting/PendingBroadcast.php b/src/Illuminate/Broadcasting/PendingBroadcast.php index b7550290240d..191b905f5938 100644 --- a/src/Illuminate/Broadcasting/PendingBroadcast.php +++ b/src/Illuminate/Broadcasting/PendingBroadcast.php @@ -33,6 +33,21 @@ public function __construct(Dispatcher $events, $event) $this->events = $events; } + /** + * Broadcast the event using a specific broadcaster. + * + * @param string|null $connection + * @return $this + */ + public function via($connection = null) + { + if (method_exists($this->event, 'broadcastVia')) { + $this->event->broadcastVia($connection); + } + + return $this; + } + /** * Broadcast the event to everyone except the current user. * diff --git a/tests/Broadcasting/BroadcastEventTest.php b/tests/Broadcasting/BroadcastEventTest.php index 02d6b3983d42..5b591406383a 100644 --- a/tests/Broadcasting/BroadcastEventTest.php +++ b/tests/Broadcasting/BroadcastEventTest.php @@ -3,7 +3,9 @@ namespace Illuminate\Tests\Broadcasting; use Illuminate\Broadcasting\BroadcastEvent; +use Illuminate\Broadcasting\InteractsWithBroadcasting; use Illuminate\Contracts\Broadcasting\Broadcaster; +use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -22,9 +24,13 @@ public function testBasicEventBroadcastParameterFormatting() ['test-channel'], TestBroadcastEvent::class, ['firstName' => 'Taylor', 'lastName' => 'Otwell', 'collection' => ['foo' => 'bar']] ); + $manager = m::mock(BroadcastingFactory::class); + + $manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster); + $event = new TestBroadcastEvent; - (new BroadcastEvent($event))->handle($broadcaster); + (new BroadcastEvent($event))->handle($manager); } public function testManualParameterSpecification() @@ -35,9 +41,28 @@ public function testManualParameterSpecification() ['test-channel'], TestBroadcastEventWithManualData::class, ['name' => 'Taylor', 'socket' => null] ); + $manager = m::mock(BroadcastingFactory::class); + + $manager->shouldReceive('connection')->once()->with(null)->andReturn($broadcaster); + $event = new TestBroadcastEventWithManualData; - (new BroadcastEvent($event))->handle($broadcaster); + (new BroadcastEvent($event))->handle($manager); + } + + public function testSpecificBroadcasterGiven() + { + $broadcaster = m::mock(Broadcaster::class); + + $broadcaster->shouldReceive('broadcast')->once(); + + $manager = m::mock(BroadcastingFactory::class); + + $manager->shouldReceive('connection')->once()->with('log')->andReturn($broadcaster); + + $event = new TestBroadcastEventWithSpecificBroadcaster; + + (new BroadcastEvent($event))->handle($manager); } } @@ -66,3 +91,13 @@ public function broadcastWith() return ['name' => 'Taylor']; } } + +class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent +{ + use InteractsWithBroadcasting; + + public function __construct() + { + $this->broadcastVia('log'); + } +}