From c203d24469a1bf1c3f60431cbec8674bb6482931 Mon Sep 17 00:00:00 2001 From: Francis Lavoie Date: Sun, 24 Mar 2019 00:56:47 -0400 Subject: [PATCH] Clean up some typos, add some type hints, StyleCI fixes --- .gitignore | 3 +- config/websockets.php | 1 - src/Apps/ConfigAppProvider.php | 10 ++-- src/Console/StartWebSocketServer.php | 3 +- src/Facades/StatisticsLogger.php | 5 +- src/Facades/WebSocketsRouter.php | 5 +- src/HttpApi/Controllers/Controller.php | 51 +++++++++++-------- src/Server/Router.php | 2 +- src/WebSockets/Channels/Channel.php | 7 ++- .../ChannelManagers/ArrayChannelManager.php | 2 +- 10 files changed, 55 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index e45efd8..4071d4e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ composer.lock docs vendor coverage -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.idea/ diff --git a/config/websockets.php b/config/websockets.php index f5b43e3..3826580 100644 --- a/config/websockets.php +++ b/config/websockets.php @@ -137,7 +137,6 @@ return [ ], ], - /* * Channel Manager * This class handles how channel persistence is handled. diff --git a/src/Apps/ConfigAppProvider.php b/src/Apps/ConfigAppProvider.php index 0476aba..b9b7ab7 100644 --- a/src/Apps/ConfigAppProvider.php +++ b/src/Apps/ConfigAppProvider.php @@ -19,7 +19,7 @@ class ConfigAppProvider implements AppProvider { return $this->apps ->map(function (array $appAttributes) { - return $this->instanciate($appAttributes); + return $this->instantiate($appAttributes); }) ->toArray(); } @@ -30,7 +30,7 @@ class ConfigAppProvider implements AppProvider ->apps ->firstWhere('id', $appId); - return $this->instanciate($appAttributes); + return $this->instantiate($appAttributes); } public function findByKey(string $appKey): ?App @@ -39,7 +39,7 @@ class ConfigAppProvider implements AppProvider ->apps ->firstWhere('key', $appKey); - return $this->instanciate($appAttributes); + return $this->instantiate($appAttributes); } public function findBySecret(string $appSecret): ?App @@ -48,10 +48,10 @@ class ConfigAppProvider implements AppProvider ->apps ->firstWhere('secret', $appSecret); - return $this->instanciate($appAttributes); + return $this->instantiate($appAttributes); } - protected function instanciate(?array $appAttributes): ?App + protected function instantiate(?array $appAttributes): ?App { if (! $appAttributes) { return null; diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index e014e29..8a882a5 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -11,9 +11,10 @@ use React\EventLoop\Factory as LoopFactory; use React\Dns\Resolver\Factory as DnsFactory; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\PubSub\PubSubInterface; -use BeyondCode\LaravelWebSockets\PubSub\Redis\RedisClient; +use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; +use BeyondCode\LaravelWebSockets\PubSub\Redis\RedisClient; use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; diff --git a/src/Facades/StatisticsLogger.php b/src/Facades/StatisticsLogger.php index 858d63f..095d796 100644 --- a/src/Facades/StatisticsLogger.php +++ b/src/Facades/StatisticsLogger.php @@ -5,7 +5,10 @@ namespace BeyondCode\LaravelWebSockets\Facades; use Illuminate\Support\Facades\Facade; use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; -/** @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger */ +/** + * @see \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger + * @mixin \BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger + */ class StatisticsLogger extends Facade { protected static function getFacadeAccessor() diff --git a/src/Facades/WebSocketsRouter.php b/src/Facades/WebSocketsRouter.php index 2c7b75a..925f685 100644 --- a/src/Facades/WebSocketsRouter.php +++ b/src/Facades/WebSocketsRouter.php @@ -4,7 +4,10 @@ namespace BeyondCode\LaravelWebSockets\Facades; use Illuminate\Support\Facades\Facade; -/** @see \BeyondCode\LaravelWebSockets\Server\Router */ +/** + * @see \BeyondCode\LaravelWebSockets\Server\Router + * @mixin \BeyondCode\LaravelWebSockets\Server\Router + */ class WebSocketsRouter extends Facade { protected static function getFacadeAccessor() diff --git a/src/HttpApi/Controllers/Controller.php b/src/HttpApi/Controllers/Controller.php index 975e8ef..48ecb5d 100644 --- a/src/HttpApi/Controllers/Controller.php +++ b/src/HttpApi/Controllers/Controller.php @@ -46,7 +46,11 @@ abstract class Controller implements HttpServerInterface $this->requestBuffer = (string) $request->getBody(); - $this->checkContentLength($connection); + if (! $this->checkContentLength()) { + return; + } + + $this->handleRequest($connection); } protected function findContentLength(array $headers): int @@ -60,31 +64,38 @@ abstract class Controller implements HttpServerInterface { $this->requestBuffer .= $msg; - $this->checkContentLength($from); + if (! $this->checkContentLength()) { + return; + } + + $this->handleRequest($from); } - protected function checkContentLength(ConnectionInterface $connection) + protected function checkContentLength() { - if (strlen($this->requestBuffer) === $this->contentLength) { - $serverRequest = (new ServerRequest( - $this->request->getMethod(), - $this->request->getUri(), - $this->request->getHeaders(), - $this->requestBuffer, - $this->request->getProtocolVersion() - ))->withQueryParams(QueryParameters::create($this->request)->all()); + return strlen($this->requestBuffer) !== $this->contentLength; + } - $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest)); + protected function handleRequest(ConnectionInterface $connection) + { + $serverRequest = (new ServerRequest( + $this->request->getMethod(), + $this->request->getUri(), + $this->request->getHeaders(), + $this->requestBuffer, + $this->request->getProtocolVersion() + ))->withQueryParams(QueryParameters::create($this->request)->all()); - $this - ->ensureValidAppId($laravelRequest->appId) - ->ensureValidSignature($laravelRequest); + $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest)); - $response = $this($laravelRequest); + $this + ->ensureValidAppId($laravelRequest->appId) + ->ensureValidSignature($laravelRequest); - $connection->send(JsonResponse::create($response)); - $connection->close(); - } + $response = $this($laravelRequest); + + $connection->send(JsonResponse::create($response)); + $connection->close(); } public function onClose(ConnectionInterface $connection) @@ -122,7 +133,7 @@ abstract class Controller implements HttpServerInterface /* * The `auth_signature` & `body_md5` parameters are not included when calculating the `auth_signature` value. * - * The `appId`, `appKey` & `channelName` parameters are actually route paramaters and are never supplied by the client. + * The `appId`, `appKey` & `channelName` parameters are actually route parameters and are never supplied by the client. */ $params = Arr::except($request->query(), ['auth_signature', 'body_md5', 'appId', 'appKey', 'channelName']); diff --git a/src/Server/Router.php b/src/Server/Router.php index 3ce6685..1950acb 100644 --- a/src/Server/Router.php +++ b/src/Server/Router.php @@ -94,7 +94,7 @@ class Router * If the given action is a class that handles WebSockets, then it's not a regular * controller but a WebSocketHandler that needs to converted to a WsServer. * - * If the given action is a regular controller we'll just instanciate it. + * If the given action is a regular controller we'll just instantiate it. */ $action = is_subclass_of($action, MessageComponentInterface::class) ? $this->createWebSocketsServer($action) diff --git a/src/WebSockets/Channels/Channel.php b/src/WebSockets/Channels/Channel.php index a6136d4..f050fb2 100644 --- a/src/WebSockets/Channels/Channel.php +++ b/src/WebSockets/Channels/Channel.php @@ -95,11 +95,14 @@ class Channel public function broadcastToEveryoneExcept($payload, ?string $socketId = null, ?string $appId = null) { if (config('websockets.replication.enabled') === true) { - app()->get(PubSubInterface::class)->publish($appId, $payload); + // Also broadcast via the other websocket instances + app()->get(PubSubInterface::class) + ->publish($appId, $payload); } if (is_null($socketId)) { - return $this->broadcast($payload); + $this->broadcast($payload); + return; } foreach ($this->subscribedConnections as $connection) { diff --git a/src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php b/src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php index 9664c65..3465160 100644 --- a/src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php +++ b/src/WebSockets/Channels/ChannelManagers/ArrayChannelManager.php @@ -15,7 +15,7 @@ class ArrayChannelManager implements ChannelManager /** @var string */ protected $appId; - /** @var array */ + /** @var Channel[][] */ protected $channels = []; public function findOrCreate(string $appId, string $channelName): Channel