From 990a075b201096c7b9b82e320e62a965868e7549 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Mon, 29 Jul 2019 17:20:22 -0400 Subject: [PATCH] Avoid calls to app() --- .../Controllers/FetchChannelsController.php | 16 ++++++++++++---- src/WebSockets/Channels/Channel.php | 10 +++++----- src/WebSockets/Channels/PresenceChannel.php | 15 +++++++-------- 3 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/HttpApi/Controllers/FetchChannelsController.php b/src/HttpApi/Controllers/FetchChannelsController.php index 96f7141..ed44872 100644 --- a/src/HttpApi/Controllers/FetchChannelsController.php +++ b/src/HttpApi/Controllers/FetchChannelsController.php @@ -5,13 +5,23 @@ namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers; use Illuminate\Support\Str; use Illuminate\Http\Request; use Illuminate\Support\Collection; -use React\Promise\PromiseInterface; use Symfony\Component\HttpKernel\Exception\HttpException; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; +use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; class FetchChannelsController extends Controller { + /** @var ReplicationInterface */ + protected $replication; + + public function __construct(ChannelManager $channelManager, ReplicationInterface $replication) + { + parent::__construct($channelManager); + + $this->replication = $replication; + } + public function __invoke(Request $request) { $attributes = []; @@ -39,10 +49,8 @@ class FetchChannelsController extends Controller return $channel->getChannelName(); })->toArray(); - /** @var PromiseInterface $memberCounts */ // We ask the replication backend to get us the member count per channel - $memberCounts = app(ReplicationInterface::class) - ->channelMemberCounts($request->appId, $channelNames); + $memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames); // We return a promise since the backend runs async. We get $counts back // as a key-value array of channel names and their member count. diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index d072cbd..8cf1469 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -15,7 +15,7 @@ class Channel protected $channelName; /** @var ReplicationInterface */ - protected $pubSub; + protected $replication; /** @var \Ratchet\ConnectionInterface[] */ protected $subscribedConnections = []; @@ -23,7 +23,7 @@ class Channel public function __construct(string $channelName) { $this->channelName = $channelName; - $this->pubSub = app(ReplicationInterface::class); + $this->replication = app(ReplicationInterface::class); } public function getChannelName(): string @@ -68,7 +68,7 @@ class Channel $this->saveConnection($connection); // Subscribe to broadcasted messages from the pub/sub backend - $this->pubSub->subscribe($connection->app->id, $this->channelName); + $this->replication->subscribe($connection->app->id, $this->channelName); $connection->send(json_encode([ 'event' => 'pusher_internal:subscription_succeeded', @@ -81,7 +81,7 @@ class Channel unset($this->subscribedConnections[$connection->socketId]); // Unsubscribe from the pub/sub backend - $this->pubSub->unsubscribe($connection->app->id, $this->channelName); + $this->replication->unsubscribe($connection->app->id, $this->channelName); if (! $this->hasConnections()) { DashboardLogger::vacated($connection, $this->channelName); @@ -111,7 +111,7 @@ class Channel public function broadcastToOthers(ConnectionInterface $connection, $payload) { // Also broadcast via the other websocket servers - $this->pubSub->publish($connection->app->id, $this->channelName, $payload); + $this->replication->publish($connection->app->id, $this->channelName, $payload); $this->broadcastToEveryoneExcept($payload, $connection->socketId); } diff --git a/src/WebSockets/Channels/PresenceChannel.php b/src/WebSockets/Channels/PresenceChannel.php index 895e96a..2578c70 100644 --- a/src/WebSockets/Channels/PresenceChannel.php +++ b/src/WebSockets/Channels/PresenceChannel.php @@ -5,7 +5,6 @@ namespace BeyondCode\LaravelWebSockets\WebSockets\Channels; use stdClass; use Ratchet\ConnectionInterface; use React\Promise\PromiseInterface; -use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; class PresenceChannel extends Channel @@ -24,12 +23,12 @@ class PresenceChannel extends Channel /** * @param string $appId - * @return array|PromiseInterface + * @return PromiseInterface */ public function getUsers(string $appId) { // Get the members list from the replication backend - return app(ReplicationInterface::class) + return $this->replication ->channelMembers($appId, $this->channelName); } @@ -50,7 +49,7 @@ class PresenceChannel extends Channel $this->users[$connection->socketId] = $channelData; // Add the connection as a member of the channel - app(ReplicationInterface::class) + $this->replication ->joinChannel( $connection->app->id, $this->channelName, @@ -60,7 +59,7 @@ class PresenceChannel extends Channel // We need to pull the channel data from the replication backend, // otherwise we won't be sending the full details of the channel - app(ReplicationInterface::class) + $this->replication ->channelMembers($connection->app->id, $this->channelName) ->then(function ($users) use ($connection) { // Send the success event @@ -87,7 +86,7 @@ class PresenceChannel extends Channel } // Remove the connection as a member of the channel - app(ReplicationInterface::class) + $this->replication ->leaveChannel( $connection->app->id, $this->channelName, @@ -107,11 +106,11 @@ class PresenceChannel extends Channel /** * @param string|null $appId - * @return PromiseInterface|array + * @return PromiseInterface */ public function toArray(string $appId = null) { - return app(ReplicationInterface::class) + return $this->replication ->channelMembers($appId, $this->channelName) ->then(function ($users) { return array_merge(parent::toArray(), [