Clean up some typos, add some type hints, StyleCI fixes

This commit is contained in:
Francis Lavoie 2019-03-24 00:56:47 -04:00 committed by Francis Lavoie
parent b584d0cacb
commit c203d24469
No known key found for this signature in database
GPG Key ID: B9E0E04A76AF4692
10 changed files with 55 additions and 34 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ composer.lock
docs docs
vendor vendor
coverage coverage
.phpunit.result.cache .phpunit.result.cache
.idea/

View File

@ -137,7 +137,6 @@ return [
], ],
], ],
/* /*
* Channel Manager * Channel Manager
* This class handles how channel persistence is handled. * This class handles how channel persistence is handled.

View File

@ -19,7 +19,7 @@ class ConfigAppProvider implements AppProvider
{ {
return $this->apps return $this->apps
->map(function (array $appAttributes) { ->map(function (array $appAttributes) {
return $this->instanciate($appAttributes); return $this->instantiate($appAttributes);
}) })
->toArray(); ->toArray();
} }
@ -30,7 +30,7 @@ class ConfigAppProvider implements AppProvider
->apps ->apps
->firstWhere('id', $appId); ->firstWhere('id', $appId);
return $this->instanciate($appAttributes); return $this->instantiate($appAttributes);
} }
public function findByKey(string $appKey): ?App public function findByKey(string $appKey): ?App
@ -39,7 +39,7 @@ class ConfigAppProvider implements AppProvider
->apps ->apps
->firstWhere('key', $appKey); ->firstWhere('key', $appKey);
return $this->instanciate($appAttributes); return $this->instantiate($appAttributes);
} }
public function findBySecret(string $appSecret): ?App public function findBySecret(string $appSecret): ?App
@ -48,10 +48,10 @@ class ConfigAppProvider implements AppProvider
->apps ->apps
->firstWhere('secret', $appSecret); ->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) { if (! $appAttributes) {
return null; return null;

View File

@ -11,9 +11,10 @@ use React\EventLoop\Factory as LoopFactory;
use React\Dns\Resolver\Factory as DnsFactory; use React\Dns\Resolver\Factory as DnsFactory;
use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver;
use BeyondCode\LaravelWebSockets\PubSub\PubSubInterface; 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\StatisticsLogger;
use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter; use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
use BeyondCode\LaravelWebSockets\PubSub\Redis\RedisClient;
use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger; use BeyondCode\LaravelWebSockets\Server\Logger\HttpLogger;
use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory; use BeyondCode\LaravelWebSockets\Server\WebSocketServerFactory;
use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger; use BeyondCode\LaravelWebSockets\Server\Logger\ConnectionLogger;

View File

@ -5,7 +5,10 @@ namespace BeyondCode\LaravelWebSockets\Facades;
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
use BeyondCode\LaravelWebSockets\Statistics\Logger\StatisticsLogger as StatisticsLoggerInterface; 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 class StatisticsLogger extends Facade
{ {
protected static function getFacadeAccessor() protected static function getFacadeAccessor()

View File

@ -4,7 +4,10 @@ namespace BeyondCode\LaravelWebSockets\Facades;
use Illuminate\Support\Facades\Facade; use Illuminate\Support\Facades\Facade;
/** @see \BeyondCode\LaravelWebSockets\Server\Router */ /**
* @see \BeyondCode\LaravelWebSockets\Server\Router
* @mixin \BeyondCode\LaravelWebSockets\Server\Router
*/
class WebSocketsRouter extends Facade class WebSocketsRouter extends Facade
{ {
protected static function getFacadeAccessor() protected static function getFacadeAccessor()

View File

@ -46,7 +46,11 @@ abstract class Controller implements HttpServerInterface
$this->requestBuffer = (string) $request->getBody(); $this->requestBuffer = (string) $request->getBody();
$this->checkContentLength($connection); if (! $this->checkContentLength()) {
return;
}
$this->handleRequest($connection);
} }
protected function findContentLength(array $headers): int protected function findContentLength(array $headers): int
@ -60,31 +64,38 @@ abstract class Controller implements HttpServerInterface
{ {
$this->requestBuffer .= $msg; $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) { return 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());
$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 $laravelRequest = Request::createFromBase((new HttpFoundationFactory)->createRequest($serverRequest));
->ensureValidAppId($laravelRequest->appId)
->ensureValidSignature($laravelRequest);
$response = $this($laravelRequest); $this
->ensureValidAppId($laravelRequest->appId)
->ensureValidSignature($laravelRequest);
$connection->send(JsonResponse::create($response)); $response = $this($laravelRequest);
$connection->close();
} $connection->send(JsonResponse::create($response));
$connection->close();
} }
public function onClose(ConnectionInterface $connection) 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 `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']); $params = Arr::except($request->query(), ['auth_signature', 'body_md5', 'appId', 'appKey', 'channelName']);

View File

@ -94,7 +94,7 @@ class Router
* If the given action is a class that handles WebSockets, then it's not a regular * 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. * 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) $action = is_subclass_of($action, MessageComponentInterface::class)
? $this->createWebSocketsServer($action) ? $this->createWebSocketsServer($action)

View File

@ -95,11 +95,14 @@ class Channel
public function broadcastToEveryoneExcept($payload, ?string $socketId = null, ?string $appId = null) public function broadcastToEveryoneExcept($payload, ?string $socketId = null, ?string $appId = null)
{ {
if (config('websockets.replication.enabled') === true) { 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)) { if (is_null($socketId)) {
return $this->broadcast($payload); $this->broadcast($payload);
return;
} }
foreach ($this->subscribedConnections as $connection) { foreach ($this->subscribedConnections as $connection) {

View File

@ -15,7 +15,7 @@ class ArrayChannelManager implements ChannelManager
/** @var string */ /** @var string */
protected $appId; protected $appId;
/** @var array */ /** @var Channel[][] */
protected $channels = []; protected $channels = [];
public function findOrCreate(string $appId, string $channelName): Channel public function findOrCreate(string $appId, string $channelName): Channel