Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 990a075

Browse files
committed
Avoid calls to app()
1 parent 00e8f3e commit 990a075

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

src/HttpApi/Controllers/FetchChannelsController.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
use Illuminate\Support\Str;
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Collection;
8-
use React\Promise\PromiseInterface;
98
use Symfony\Component\HttpKernel\Exception\HttpException;
109
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
10+
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
1111
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
1212

1313
class FetchChannelsController extends Controller
1414
{
15+
/** @var ReplicationInterface */
16+
protected $replication;
17+
18+
public function __construct(ChannelManager $channelManager, ReplicationInterface $replication)
19+
{
20+
parent::__construct($channelManager);
21+
22+
$this->replication = $replication;
23+
}
24+
1525
public function __invoke(Request $request)
1626
{
1727
$attributes = [];
@@ -39,10 +49,8 @@ public function __invoke(Request $request)
3949
return $channel->getChannelName();
4050
})->toArray();
4151

42-
/** @var PromiseInterface $memberCounts */
4352
// We ask the replication backend to get us the member count per channel
44-
$memberCounts = app(ReplicationInterface::class)
45-
->channelMemberCounts($request->appId, $channelNames);
53+
$memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames);
4654

4755
// We return a promise since the backend runs async. We get $counts back
4856
// as a key-value array of channel names and their member count.

src/WebSockets/Channels/Channel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Channel
1515
protected $channelName;
1616

1717
/** @var ReplicationInterface */
18-
protected $pubSub;
18+
protected $replication;
1919

2020
/** @var \Ratchet\ConnectionInterface[] */
2121
protected $subscribedConnections = [];
2222

2323
public function __construct(string $channelName)
2424
{
2525
$this->channelName = $channelName;
26-
$this->pubSub = app(ReplicationInterface::class);
26+
$this->replication = app(ReplicationInterface::class);
2727
}
2828

2929
public function getChannelName(): string
@@ -68,7 +68,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
6868
$this->saveConnection($connection);
6969

7070
// Subscribe to broadcasted messages from the pub/sub backend
71-
$this->pubSub->subscribe($connection->app->id, $this->channelName);
71+
$this->replication->subscribe($connection->app->id, $this->channelName);
7272

7373
$connection->send(json_encode([
7474
'event' => 'pusher_internal:subscription_succeeded',
@@ -81,7 +81,7 @@ public function unsubscribe(ConnectionInterface $connection)
8181
unset($this->subscribedConnections[$connection->socketId]);
8282

8383
// Unsubscribe from the pub/sub backend
84-
$this->pubSub->unsubscribe($connection->app->id, $this->channelName);
84+
$this->replication->unsubscribe($connection->app->id, $this->channelName);
8585

8686
if (! $this->hasConnections()) {
8787
DashboardLogger::vacated($connection, $this->channelName);
@@ -111,7 +111,7 @@ public function broadcast($payload)
111111
public function broadcastToOthers(ConnectionInterface $connection, $payload)
112112
{
113113
// Also broadcast via the other websocket servers
114-
$this->pubSub->publish($connection->app->id, $this->channelName, $payload);
114+
$this->replication->publish($connection->app->id, $this->channelName, $payload);
115115

116116
$this->broadcastToEveryoneExcept($payload, $connection->socketId);
117117
}

src/WebSockets/Channels/PresenceChannel.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use stdClass;
66
use Ratchet\ConnectionInterface;
77
use React\Promise\PromiseInterface;
8-
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
98
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
109

1110
class PresenceChannel extends Channel
@@ -24,12 +23,12 @@ class PresenceChannel extends Channel
2423

2524
/**
2625
* @param string $appId
27-
* @return array|PromiseInterface
26+
* @return PromiseInterface
2827
*/
2928
public function getUsers(string $appId)
3029
{
3130
// Get the members list from the replication backend
32-
return app(ReplicationInterface::class)
31+
return $this->replication
3332
->channelMembers($appId, $this->channelName);
3433
}
3534

@@ -50,7 +49,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
5049
$this->users[$connection->socketId] = $channelData;
5150

5251
// Add the connection as a member of the channel
53-
app(ReplicationInterface::class)
52+
$this->replication
5453
->joinChannel(
5554
$connection->app->id,
5655
$this->channelName,
@@ -60,7 +59,7 @@ public function subscribe(ConnectionInterface $connection, stdClass $payload)
6059

6160
// We need to pull the channel data from the replication backend,
6261
// otherwise we won't be sending the full details of the channel
63-
app(ReplicationInterface::class)
62+
$this->replication
6463
->channelMembers($connection->app->id, $this->channelName)
6564
->then(function ($users) use ($connection) {
6665
// Send the success event
@@ -87,7 +86,7 @@ public function unsubscribe(ConnectionInterface $connection)
8786
}
8887

8988
// Remove the connection as a member of the channel
90-
app(ReplicationInterface::class)
89+
$this->replication
9190
->leaveChannel(
9291
$connection->app->id,
9392
$this->channelName,
@@ -107,11 +106,11 @@ public function unsubscribe(ConnectionInterface $connection)
107106

108107
/**
109108
* @param string|null $appId
110-
* @return PromiseInterface|array
109+
* @return PromiseInterface
111110
*/
112111
public function toArray(string $appId = null)
113112
{
114-
return app(ReplicationInterface::class)
113+
return $this->replication
115114
->channelMembers($appId, $this->channelName)
116115
->then(function ($users) {
117116
return array_merge(parent::toArray(), [

0 commit comments

Comments
 (0)