Avoid calls to app()

This commit is contained in:
Francis Lavoie 2019-07-29 17:20:22 -04:00
parent 00e8f3e1a8
commit 990a075b20
No known key found for this signature in database
GPG Key ID: B9E0E04A76AF4692
3 changed files with 24 additions and 17 deletions

View File

@ -5,13 +5,23 @@ namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use React\Promise\PromiseInterface;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface; use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
class FetchChannelsController extends Controller 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) public function __invoke(Request $request)
{ {
$attributes = []; $attributes = [];
@ -39,10 +49,8 @@ class FetchChannelsController extends Controller
return $channel->getChannelName(); return $channel->getChannelName();
})->toArray(); })->toArray();
/** @var PromiseInterface $memberCounts */
// We ask the replication backend to get us the member count per channel // We ask the replication backend to get us the member count per channel
$memberCounts = app(ReplicationInterface::class) $memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames);
->channelMemberCounts($request->appId, $channelNames);
// We return a promise since the backend runs async. We get $counts back // 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. // as a key-value array of channel names and their member count.

View File

@ -15,7 +15,7 @@ class Channel
protected $channelName; protected $channelName;
/** @var ReplicationInterface */ /** @var ReplicationInterface */
protected $pubSub; protected $replication;
/** @var \Ratchet\ConnectionInterface[] */ /** @var \Ratchet\ConnectionInterface[] */
protected $subscribedConnections = []; protected $subscribedConnections = [];
@ -23,7 +23,7 @@ class Channel
public function __construct(string $channelName) public function __construct(string $channelName)
{ {
$this->channelName = $channelName; $this->channelName = $channelName;
$this->pubSub = app(ReplicationInterface::class); $this->replication = app(ReplicationInterface::class);
} }
public function getChannelName(): string public function getChannelName(): string
@ -68,7 +68,7 @@ class Channel
$this->saveConnection($connection); $this->saveConnection($connection);
// Subscribe to broadcasted messages from the pub/sub backend // 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([ $connection->send(json_encode([
'event' => 'pusher_internal:subscription_succeeded', 'event' => 'pusher_internal:subscription_succeeded',
@ -81,7 +81,7 @@ class Channel
unset($this->subscribedConnections[$connection->socketId]); unset($this->subscribedConnections[$connection->socketId]);
// Unsubscribe from the pub/sub backend // 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()) { if (! $this->hasConnections()) {
DashboardLogger::vacated($connection, $this->channelName); DashboardLogger::vacated($connection, $this->channelName);
@ -111,7 +111,7 @@ class Channel
public function broadcastToOthers(ConnectionInterface $connection, $payload) public function broadcastToOthers(ConnectionInterface $connection, $payload)
{ {
// Also broadcast via the other websocket servers // 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); $this->broadcastToEveryoneExcept($payload, $connection->socketId);
} }

View File

@ -5,7 +5,6 @@ namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
use stdClass; use stdClass;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
class PresenceChannel extends Channel class PresenceChannel extends Channel
@ -24,12 +23,12 @@ class PresenceChannel extends Channel
/** /**
* @param string $appId * @param string $appId
* @return array|PromiseInterface * @return PromiseInterface
*/ */
public function getUsers(string $appId) public function getUsers(string $appId)
{ {
// Get the members list from the replication backend // Get the members list from the replication backend
return app(ReplicationInterface::class) return $this->replication
->channelMembers($appId, $this->channelName); ->channelMembers($appId, $this->channelName);
} }
@ -50,7 +49,7 @@ class PresenceChannel extends Channel
$this->users[$connection->socketId] = $channelData; $this->users[$connection->socketId] = $channelData;
// Add the connection as a member of the channel // Add the connection as a member of the channel
app(ReplicationInterface::class) $this->replication
->joinChannel( ->joinChannel(
$connection->app->id, $connection->app->id,
$this->channelName, $this->channelName,
@ -60,7 +59,7 @@ class PresenceChannel extends Channel
// We need to pull the channel data from the replication backend, // We need to pull the channel data from the replication backend,
// otherwise we won't be sending the full details of the channel // otherwise we won't be sending the full details of the channel
app(ReplicationInterface::class) $this->replication
->channelMembers($connection->app->id, $this->channelName) ->channelMembers($connection->app->id, $this->channelName)
->then(function ($users) use ($connection) { ->then(function ($users) use ($connection) {
// Send the success event // Send the success event
@ -87,7 +86,7 @@ class PresenceChannel extends Channel
} }
// Remove the connection as a member of the channel // Remove the connection as a member of the channel
app(ReplicationInterface::class) $this->replication
->leaveChannel( ->leaveChannel(
$connection->app->id, $connection->app->id,
$this->channelName, $this->channelName,
@ -107,11 +106,11 @@ class PresenceChannel extends Channel
/** /**
* @param string|null $appId * @param string|null $appId
* @return PromiseInterface|array * @return PromiseInterface
*/ */
public function toArray(string $appId = null) public function toArray(string $appId = null)
{ {
return app(ReplicationInterface::class) return $this->replication
->channelMembers($appId, $this->channelName) ->channelMembers($appId, $this->channelName)
->then(function ($users) { ->then(function ($users) {
return array_merge(parent::toArray(), [ return array_merge(parent::toArray(), [