Add support for Laravel 5.8 (#122)

* Fix Laravel 5.8 compatibility

* Use Arr:: and Str:: instead of global helpers

* Remove testbench-core dependency, release was tagged
This commit is contained in:
Francis Lavoie 2019-02-27 09:27:21 -05:00 committed by Marcel Pociot
parent edcf205dba
commit 3ec6c0ade3
12 changed files with 30 additions and 21 deletions

View File

@ -27,18 +27,18 @@
"cboden/ratchet": "^0.4.1", "cboden/ratchet": "^0.4.1",
"clue/buzz-react": "^2.5", "clue/buzz-react": "^2.5",
"guzzlehttp/psr7": "^1.5", "guzzlehttp/psr7": "^1.5",
"illuminate/broadcasting": "5.7.*", "illuminate/broadcasting": "5.7.* || 5.8.*",
"illuminate/console": "5.7.*", "illuminate/console": "5.7.* || 5.8.*",
"illuminate/http": "5.7.*", "illuminate/http": "5.7.* || 5.8.*",
"illuminate/routing": "5.7.*", "illuminate/routing": "5.7.* || 5.8.*",
"illuminate/support": "5.7.*", "illuminate/support": "5.7.* || 5.8.*",
"pusher/pusher-php-server": "~3.0", "pusher/pusher-php-server": "~3.0",
"symfony/http-kernel": "~4.0", "symfony/http-kernel": "~4.0",
"symfony/psr-http-message-bridge": "^1.1" "symfony/psr-http-message-bridge": "^1.1"
}, },
"require-dev": { "require-dev": {
"mockery/mockery": "^1.2", "mockery/mockery": "^1.2",
"orchestra/testbench": "3.7.*", "orchestra/testbench": "3.7.* || 3.8.*",
"phpunit/phpunit": "^7.0" "phpunit/phpunit": "^7.0"
}, },
"autoload": { "autoload": {

View File

@ -4,6 +4,7 @@ namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
use Exception; use Exception;
use Pusher\Pusher; use Pusher\Pusher;
use Illuminate\Support\Arr;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use GuzzleHttp\Psr7\Response; use GuzzleHttp\Psr7\Response;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
@ -123,7 +124,7 @@ abstract class Controller implements HttpServerInterface
* *
* The `appId`, `appKey` & `channelName` parameters are actually route paramaters and are never supplied by the client. * The `appId`, `appKey` & `channelName` parameters are actually route paramaters and are never supplied by the client.
*/ */
$params = array_except($request->query(), ['auth_signature', 'body_md5', 'appId', 'appKey', 'channelName']); $params = Arr::except($request->query(), ['auth_signature', 'body_md5', 'appId', 'appKey', 'channelName']);
if ($request->getContent() !== '') { if ($request->getContent() !== '') {
$params['body_md5'] = md5($request->getContent()); $params['body_md5'] = md5($request->getContent());

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers; namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel; use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
@ -16,7 +17,7 @@ class FetchChannelsController extends Controller
if ($request->has('filter_by_prefix')) { if ($request->has('filter_by_prefix')) {
$channels = $channels->filter(function ($channel, $channelName) use ($request) { $channels = $channels->filter(function ($channel, $channelName) use ($request) {
return starts_with($channelName, $request->filter_by_prefix); return Str::startsWith($channelName, $request->filter_by_prefix);
}); });
} }

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets\Statistics\Events; namespace BeyondCode\LaravelWebSockets\Statistics\Events;
use Illuminate\Support\Str;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel; use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
@ -33,7 +34,7 @@ class StatisticsUpdated implements ShouldBroadcast
public function broadcastOn() public function broadcastOn()
{ {
$channelName = str_after(DashboardLogger::LOG_CHANNEL_PREFIX.'statistics', 'private-'); $channelName = Str::after(DashboardLogger::LOG_CHANNEL_PREFIX.'statistics', 'private-');
return new PrivateChannel($channelName); return new PrivateChannel($channelName);
} }

View File

@ -3,6 +3,7 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels; namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
use stdClass; use stdClass;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature; use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
@ -38,7 +39,7 @@ class Channel
$signature .= ":{$payload->channel_data}"; $signature .= ":{$payload->channel_data}";
} }
if (str_after($payload->auth, ':') !== hash_hmac('sha256', $signature, $connection->app->secret)) { if (Str::after($payload->auth, ':') !== hash_hmac('sha256', $signature, $connection->app->secret)) {
throw new InvalidSignature(); throw new InvalidSignature();
} }
} }

View File

@ -2,6 +2,8 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers; namespace BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel; use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@ -34,11 +36,11 @@ class ArrayChannelManager implements ChannelManager
protected function determineChannelClass(string $channelName): string protected function determineChannelClass(string $channelName): string
{ {
if (starts_with($channelName, 'private-')) { if (Str::startsWith($channelName, 'private-')) {
return PrivateChannel::class; return PrivateChannel::class;
} }
if (starts_with($channelName, 'presence-')) { if (Str::startsWith($channelName, 'presence-')) {
return PresenceChannel::class; return PresenceChannel::class;
} }
@ -67,18 +69,18 @@ class ArrayChannelManager implements ChannelManager
/* /*
* Remove the connection from all channels. * Remove the connection from all channels.
*/ */
collect(array_get($this->channels, $connection->app->id, []))->each->unsubscribe($connection); collect(Arr::get($this->channels, $connection->app->id, []))->each->unsubscribe($connection);
/* /*
* Unset all channels that have no connections so we don't leak memory. * Unset all channels that have no connections so we don't leak memory.
*/ */
collect(array_get($this->channels, $connection->app->id, [])) collect(Arr::get($this->channels, $connection->app->id, []))
->reject->hasConnections() ->reject->hasConnections()
->each(function (Channel $channel, string $channelName) use ($connection) { ->each(function (Channel $channel, string $channelName) use ($connection) {
unset($this->channels[$connection->app->id][$channelName]); unset($this->channels[$connection->app->id][$channelName]);
}); });
if (count(array_get($this->channels, $connection->app->id, [])) === 0) { if (count(Arr::get($this->channels, $connection->app->id, [])) === 0) {
unset($this->channels[$connection->app->id]); unset($this->channels[$connection->app->id]);
} }
} }

View File

@ -3,6 +3,7 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Messages; namespace BeyondCode\LaravelWebSockets\WebSockets\Messages;
use stdClass; use stdClass;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@ -28,7 +29,7 @@ class PusherChannelProtocolMessage implements PusherMessage
public function respond() public function respond()
{ {
$eventName = camel_case(str_after($this->payload->event, ':')); $eventName = Str::camel(Str::after($this->payload->event, ':'));
if (method_exists($this, $eventName)) { if (method_exists($this, $eventName)) {
call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass()); call_user_func([$this, $eventName], $this->connection, $this->payload->data ?? new stdClass());

View File

@ -3,6 +3,7 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Messages; namespace BeyondCode\LaravelWebSockets\WebSockets\Messages;
use stdClass; use stdClass;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger; use BeyondCode\LaravelWebSockets\Dashboard\DashboardLogger;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@ -29,7 +30,7 @@ class PusherClientMessage implements PusherMessage
public function respond() public function respond()
{ {
if (! starts_with($this->payload->event, 'client-')) { if (! Str::startsWith($this->payload->event, 'client-')) {
return; return;
} }

View File

@ -2,6 +2,7 @@
namespace BeyondCode\LaravelWebSockets\WebSockets\Messages; namespace BeyondCode\LaravelWebSockets\WebSockets\Messages;
use Illuminate\Support\Str;
use Ratchet\ConnectionInterface; use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface; use Ratchet\RFC6455\Messaging\MessageInterface;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
@ -15,7 +16,7 @@ class PusherMessageFactory
{ {
$payload = json_decode($message->getPayload()); $payload = json_decode($message->getPayload());
return starts_with($payload->event, 'pusher:') return Str::startsWith($payload->event, 'pusher:')
? new PusherChannelProtocolMessage($payload, $connection, $channelManager) ? new PusherChannelProtocolMessage($payload, $connection, $channelManager)
: new PusherClientMessage($payload, $connection, $channelManager); : new PusherClientMessage($payload, $connection, $channelManager);
} }

View File

@ -10,7 +10,7 @@ class ConfigAppProviderTest extends TestCase
/** @var \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider */ /** @var \BeyondCode\LaravelWebSockets\Apps\ConfigAppProvider */
protected $configAppProvider; protected $configAppProvider;
public function setUp() public function setUp(): void
{ {
parent::setUp(); parent::setUp();

View File

@ -10,7 +10,7 @@ use BeyondCode\LaravelWebSockets\Statistics\Models\WebSocketsStatisticsEntry;
class CleanStatisticsTest extends TestCase class CleanStatisticsTest extends TestCase
{ {
public function setUp() public function setUp(): void
{ {
parent::setUp(); parent::setUp();

View File

@ -19,7 +19,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase
/** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */ /** @var \BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager */
protected $channelManager; protected $channelManager;
public function setUp() public function setUp(): void
{ {
parent::setUp(); parent::setUp();