Skip to content

Commit

Permalink
[8.x] Ability to specify the broadcaster to use when broadcasting an …
Browse files Browse the repository at this point in the history
…event (#38086)

* WIP

* WIP

* WIP

* formatting

* fix paren

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
clemblanco and taylorotwell authored Jul 21, 2021
1 parent a17fd16 commit f4bb259
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 9 deletions.
21 changes: 14 additions & 7 deletions src/Illuminate/Broadcasting/BroadcastEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
);
}
}

/**
Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Broadcasting/InteractsWithBroadcasting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Illuminate\Broadcasting;

use Illuminate\Support\Arr;

trait InteractsWithBroadcasting
{
/**
* The broadcaster connection to use to broadcast the event.
*
* @var array
*/
protected $broadcastConnection = [null];

/**
* Broadcast the event using a specific broadcaster.
*
* @param array|string|null $connection
* @return $this
*/
public function broadcastVia($connection = null)
{
$this->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;
}
}
15 changes: 15 additions & 0 deletions src/Illuminate/Broadcasting/PendingBroadcast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
39 changes: 37 additions & 2 deletions tests/Broadcasting/BroadcastEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -66,3 +91,13 @@ public function broadcastWith()
return ['name' => 'Taylor'];
}
}

class TestBroadcastEventWithSpecificBroadcaster extends TestBroadcastEvent
{
use InteractsWithBroadcasting;

public function __construct()
{
$this->broadcastVia('log');
}
}

0 comments on commit f4bb259

Please sign in to comment.