laravel-websockets/src/WebSocketsServiceProvider.php

133 lines
5.0 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
use Pusher\Pusher;
use Psr\Log\LoggerInterface;
2018-12-04 21:22:33 +00:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\ServiceProvider;
use Illuminate\Broadcasting\BroadcastManager;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Server\Router;
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
use BeyondCode\LaravelWebSockets\PubSub\Drivers\EmptyClient;
use BeyondCode\LaravelWebSockets\PubSub\Drivers\RedisClient;
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
2018-11-26 22:24:24 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
2018-12-04 09:25:34 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
2018-12-03 22:19:46 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
2018-11-20 10:32:56 +00:00
2018-11-25 23:40:32 +00:00
class WebSocketsServiceProvider extends ServiceProvider
2018-11-20 10:32:56 +00:00
{
2019-07-28 18:50:10 +00:00
public function boot()
2018-11-20 10:32:56 +00:00
{
2018-11-22 22:55:39 +00:00
$this->publishes([
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
2018-11-22 22:55:39 +00:00
], 'config');
if (! class_exists('CreateWebSocketsStatisticsEntries')) {
2018-12-03 10:58:42 +00:00
$this->publishes([
__DIR__.'/../database/migrations/create_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_statistics_entries_table.php'),
2018-12-03 10:58:42 +00:00
], 'migrations');
}
2018-12-03 23:05:05 +00:00
$this
2018-12-04 09:06:26 +00:00
->registerRoutes()
2018-12-03 23:05:05 +00:00
->registerDashboardGate();
2018-11-23 23:06:28 +00:00
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
2018-11-23 23:06:28 +00:00
2018-11-20 10:51:00 +00:00
$this->commands([
Console\StartWebSocketServer::class,
2018-12-04 10:48:07 +00:00
Console\CleanStatistics::class,
2018-11-20 10:51:00 +00:00
]);
2019-07-28 18:50:10 +00:00
$this->configurePubSub();
}
protected function configurePubSub()
{
if (config('websockets.replication.enabled') !== true || config('websockets.replication.driver') !== 'redis') {
$this->app->singleton(ReplicationInterface::class, function () {
return new EmptyClient();
2019-07-28 18:50:10 +00:00
});
2019-07-28 18:50:10 +00:00
return;
}
$this->app->singleton(ReplicationInterface::class, function () {
return (new RedisClient())->boot($this->loop);
});
2019-07-28 19:33:30 +00:00
$this->app->get(BroadcastManager::class)->extend('redis-pusher', function ($app, array $config) {
$pusher = new Pusher(
$config['key'], $config['secret'],
$config['app_id'], $config['options'] ?? []
);
if ($config['log'] ?? false) {
$pusher->setLogger($this->app->make(LoggerInterface::class));
}
return new RedisPusherBroadcaster(
$pusher,
$config['app_id'],
$this->app->make('redis'),
$config['connection'] ?? null
);
});
2018-11-20 10:32:56 +00:00
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
2018-11-22 22:55:39 +00:00
2018-12-03 23:05:05 +00:00
$this->app->singleton('websockets.router', function () {
2018-11-20 10:32:56 +00:00
return new Router();
});
2018-11-21 23:25:24 +00:00
2018-12-03 23:05:05 +00:00
$this->app->singleton(ChannelManager::class, function () {
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager'))
2019-07-28 19:33:30 +00:00
? $this->app->get(config('websockets.channel_manager')) : new ArrayChannelManager();
2018-11-21 11:13:40 +00:00
});
2018-11-24 00:25:40 +00:00
2018-12-03 23:05:05 +00:00
$this->app->singleton(AppProvider::class, function () {
2019-07-28 19:33:30 +00:00
return $this->app->get(config('websockets.app_provider'));
2018-11-24 00:25:40 +00:00
});
2018-11-24 22:52:55 +00:00
}
2018-11-23 23:06:28 +00:00
2018-12-04 09:06:26 +00:00
protected function registerRoutes()
2018-11-26 22:38:27 +00:00
{
2018-12-04 21:22:33 +00:00
Route::prefix(config('websockets.path'))->group(function () {
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () {
2018-12-04 09:11:53 +00:00
Route::get('/', ShowDashboard::class);
2019-07-28 18:50:10 +00:00
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']);
2018-12-04 09:11:53 +00:00
Route::post('auth', AuthenticateDashboard::class);
Route::post('event', SendMessage::class);
});
2018-12-03 23:05:05 +00:00
2018-12-04 21:22:33 +00:00
Route::middleware(AuthorizeStatistics::class)->group(function () {
2018-12-04 09:25:34 +00:00
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']);
});
2018-11-26 22:38:27 +00:00
});
2018-12-03 23:05:05 +00:00
return $this;
2018-11-26 22:38:27 +00:00
}
2018-11-24 22:52:55 +00:00
protected function registerDashboardGate()
{
2018-11-25 23:57:22 +00:00
Gate::define('viewWebSocketsDashboard', function ($user = null) {
2019-07-28 19:33:30 +00:00
return $this->app->environment('local');
2018-11-23 23:06:28 +00:00
});
2018-12-03 23:05:05 +00:00
return $this;
2018-11-20 10:32:56 +00:00
}
}