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
vendor
coverage
.phpunit.result.cache
.phpunit.result.cache
.idea/

View File

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

View File

@ -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;

View File

@ -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;

View File

@ -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()

View File

@ -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()

View File

@ -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']);

View File

@ -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)

View File

@ -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) {

View File

@ -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