From 0a25a09f679604450742f427aecd70375ec87a52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Blanco?= Date: Tue, 20 Jul 2021 23:33:28 +0200 Subject: [PATCH 1/5] WIP --- .../Broadcasting/BroadcastEvent.php | 10 +++--- .../Broadcasting/PendingBroadcast.php | 13 ++++++++ tests/Broadcasting/BroadcastEventTest.php | 33 +++++++++++++++++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/Illuminate/Broadcasting/BroadcastEvent.php b/src/Illuminate/Broadcasting/BroadcastEvent.php index 372212f35f17..62d968c0c578 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,7 +66,9 @@ public function handle(Broadcaster $broadcaster) return; } - $broadcaster->broadcast( + $connection = $this->event->broadcaster ?? null; + + $manager->connection($connection)->broadcast( $channels, $name, $this->getPayloadFromEvent($this->event) ); diff --git a/src/Illuminate/Broadcasting/PendingBroadcast.php b/src/Illuminate/Broadcasting/PendingBroadcast.php index b7550290240d..450c0f9bdd8b 100644 --- a/src/Illuminate/Broadcasting/PendingBroadcast.php +++ b/src/Illuminate/Broadcasting/PendingBroadcast.php @@ -33,6 +33,19 @@ public function __construct(Dispatcher $events, $event) $this->events = $events; } + /** + * Broadcast the event using a specific broadcaster. + * + * @param string|null $connection + * @return $this + */ + public function withBroadcaster($connection = null) + { + $this->event->broadcaster = $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..8e1ec1aa8dde 100644 --- a/tests/Broadcasting/BroadcastEventTest.php +++ b/tests/Broadcasting/BroadcastEventTest.php @@ -4,6 +4,7 @@ use Illuminate\Broadcasting\BroadcastEvent; use Illuminate\Contracts\Broadcasting\Broadcaster; +use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Mockery as m; use PHPUnit\Framework\TestCase; @@ -22,9 +23,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 +40,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 +90,8 @@ public function broadcastWith() return ['name' => 'Taylor']; } } + +class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent +{ + public $broadcaster = 'log'; +} From 4c3bb32e70a409261a6c9cf0b14633154fe6fa97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Blanco?= Date: Wed, 21 Jul 2021 00:36:18 +0200 Subject: [PATCH 2/5] WIP --- .../Broadcasting/InteractsWithBroadcaster.php | 26 +++++++++++++++++++ .../Broadcasting/PendingBroadcast.php | 4 ++- tests/Broadcasting/BroadcastEventTest.php | 8 +++++- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/Illuminate/Broadcasting/InteractsWithBroadcaster.php diff --git a/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php b/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php new file mode 100644 index 000000000000..0f99b193673e --- /dev/null +++ b/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php @@ -0,0 +1,26 @@ +broadcaster = $connection; + + return $this; + } +} diff --git a/src/Illuminate/Broadcasting/PendingBroadcast.php b/src/Illuminate/Broadcasting/PendingBroadcast.php index 450c0f9bdd8b..e42badda1d31 100644 --- a/src/Illuminate/Broadcasting/PendingBroadcast.php +++ b/src/Illuminate/Broadcasting/PendingBroadcast.php @@ -41,7 +41,9 @@ public function __construct(Dispatcher $events, $event) */ public function withBroadcaster($connection = null) { - $this->event->broadcaster = $connection; + if (method_exists($this->event, 'broadcaster')) { + $this->event->broadcaster($connection); + } return $this; } diff --git a/tests/Broadcasting/BroadcastEventTest.php b/tests/Broadcasting/BroadcastEventTest.php index 8e1ec1aa8dde..400a7be8ea73 100644 --- a/tests/Broadcasting/BroadcastEventTest.php +++ b/tests/Broadcasting/BroadcastEventTest.php @@ -3,6 +3,7 @@ namespace Illuminate\Tests\Broadcasting; use Illuminate\Broadcasting\BroadcastEvent; +use Illuminate\Broadcasting\InteractsWithBroadcaster; use Illuminate\Contracts\Broadcasting\Broadcaster; use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Mockery as m; @@ -93,5 +94,10 @@ public function broadcastWith() class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent { - public $broadcaster = 'log'; + use InteractsWithBroadcaster; + + public function __construct() + { + $this->withBroadcaster('log'); + } } From f82a7948972ce71f192fbad29d52f6509566719b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Blanco?= Date: Wed, 21 Jul 2021 00:38:01 +0200 Subject: [PATCH 3/5] WIP --- src/Illuminate/Broadcasting/PendingBroadcast.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Broadcasting/PendingBroadcast.php b/src/Illuminate/Broadcasting/PendingBroadcast.php index e42badda1d31..c9b81acf28de 100644 --- a/src/Illuminate/Broadcasting/PendingBroadcast.php +++ b/src/Illuminate/Broadcasting/PendingBroadcast.php @@ -41,8 +41,8 @@ public function __construct(Dispatcher $events, $event) */ public function withBroadcaster($connection = null) { - if (method_exists($this->event, 'broadcaster')) { - $this->event->broadcaster($connection); + if (method_exists($this->event, 'withBroadcaster')) { + $this->event->withBroadcaster($connection); } return $this; From 15817a6c22cc092dd165c0cc73264fdbc8bd048f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Jul 2021 10:05:52 -0500 Subject: [PATCH 4/5] formatting --- .../Broadcasting/BroadcastEvent.php | 15 ++++--- .../Broadcasting/InteractsWithBroadcaster.php | 26 ------------ .../InteractsWithBroadcasting.php | 40 +++++++++++++++++++ .../Broadcasting/PendingBroadcast.php | 6 +-- tests/Broadcasting/BroadcastEventTest.php | 6 +-- 5 files changed, 56 insertions(+), 37 deletions(-) delete mode 100644 src/Illuminate/Broadcasting/InteractsWithBroadcaster.php create mode 100644 src/Illuminate/Broadcasting/InteractsWithBroadcasting.php diff --git a/src/Illuminate/Broadcasting/BroadcastEvent.php b/src/Illuminate/Broadcasting/BroadcastEvent.php index 62d968c0c578..07779fa70671 100644 --- a/src/Illuminate/Broadcasting/BroadcastEvent.php +++ b/src/Illuminate/Broadcasting/BroadcastEvent.php @@ -66,12 +66,17 @@ public function handle(BroadcastingFactory $manager) return; } - $connection = $this->event->broadcaster ?? null; + $connections = method_exists($this->event, 'broadcastConnections') + ? $this->event->broadcastConnections() + : [null]; - $manager->connection($connection)->broadcast( - $channels, $name, - $this->getPayloadFromEvent($this->event) - ); + $payload = $this->getPayloadFromEvent($this->event); + + foreach ($connections as $connection ) { + $manager->connection($connection)->broadcast( + $channels, $name, $payload + ); + } } /** diff --git a/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php b/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php deleted file mode 100644 index 0f99b193673e..000000000000 --- a/src/Illuminate/Broadcasting/InteractsWithBroadcaster.php +++ /dev/null @@ -1,26 +0,0 @@ -broadcaster = $connection; - - return $this; - } -} 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 c9b81acf28de..191b905f5938 100644 --- a/src/Illuminate/Broadcasting/PendingBroadcast.php +++ b/src/Illuminate/Broadcasting/PendingBroadcast.php @@ -39,10 +39,10 @@ public function __construct(Dispatcher $events, $event) * @param string|null $connection * @return $this */ - public function withBroadcaster($connection = null) + public function via($connection = null) { - if (method_exists($this->event, 'withBroadcaster')) { - $this->event->withBroadcaster($connection); + if (method_exists($this->event, 'broadcastVia')) { + $this->event->broadcastVia($connection); } return $this; diff --git a/tests/Broadcasting/BroadcastEventTest.php b/tests/Broadcasting/BroadcastEventTest.php index 400a7be8ea73..5b591406383a 100644 --- a/tests/Broadcasting/BroadcastEventTest.php +++ b/tests/Broadcasting/BroadcastEventTest.php @@ -3,7 +3,7 @@ namespace Illuminate\Tests\Broadcasting; use Illuminate\Broadcasting\BroadcastEvent; -use Illuminate\Broadcasting\InteractsWithBroadcaster; +use Illuminate\Broadcasting\InteractsWithBroadcasting; use Illuminate\Contracts\Broadcasting\Broadcaster; use Illuminate\Contracts\Broadcasting\Factory as BroadcastingFactory; use Mockery as m; @@ -94,10 +94,10 @@ public function broadcastWith() class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent { - use InteractsWithBroadcaster; + use InteractsWithBroadcasting; public function __construct() { - $this->withBroadcaster('log'); + $this->broadcastVia('log'); } } From e14ab9b00a5f60292e8062e1ad461e56e164e52f Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 21 Jul 2021 10:07:10 -0500 Subject: [PATCH 5/5] fix paren --- src/Illuminate/Broadcasting/BroadcastEvent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Broadcasting/BroadcastEvent.php b/src/Illuminate/Broadcasting/BroadcastEvent.php index 07779fa70671..bfa4fc680998 100644 --- a/src/Illuminate/Broadcasting/BroadcastEvent.php +++ b/src/Illuminate/Broadcasting/BroadcastEvent.php @@ -72,7 +72,7 @@ public function handle(BroadcastingFactory $manager) $payload = $this->getPayloadFromEvent($this->event); - foreach ($connections as $connection ) { + foreach ($connections as $connection) { $manager->connection($connection)->broadcast( $channels, $name, $payload );