laravel-websockets/src/WebSocketsServiceProvider.php

149 lines
4.8 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
2020-08-13 11:51:18 +00:00
use BeyondCode\LaravelWebSockets\Apps\AppManager;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
2018-12-04 09:25:34 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
use BeyondCode\LaravelWebSockets\PubSub\Broadcasters\RedisPusherBroadcaster;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Server\Router;
use BeyondCode\LaravelWebSockets\Statistics\Drivers\StatisticsDriver;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
2020-08-13 07:20:31 +00:00
use Illuminate\Broadcasting\BroadcastManager;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
2020-08-13 11:02:58 +00:00
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use Pusher\Pusher;
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
{
2020-08-18 17:21:22 +00:00
/**
* Boot the service provider.
*
* @return void
*/
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');
2020-08-13 16:28:57 +00:00
$this->publishes([
__DIR__.'/../database/migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php' => database_path('migrations/0000_00_00_000000_create_websockets_statistics_entries_table.php'),
2020-08-13 16:28:57 +00:00
], 'migrations');
2018-12-03 10:58:42 +00:00
2020-08-18 17:21:22 +00:00
$this->registerDashboardRoutes()
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,
Console\RestartWebSocketServer::class,
2018-11-20 10:51:00 +00:00
]);
2019-07-28 18:50:10 +00:00
$this->configurePubSub();
}
2020-08-18 17:21:22 +00:00
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
$this->app->singleton('websockets.router', function () {
return new Router();
});
$this->app->singleton(ChannelManager::class, function () {
$channelManager = config('websockets.managers.channel', ArrayChannelManager::class);
return new $channelManager;
});
$this->app->singleton(AppManager::class, function () {
return $this->app->make(config('websockets.managers.app'));
});
$this->app->singleton(StatisticsDriver::class, function () {
$driver = config('websockets.statistics.driver');
return $this->app->make(
config('websockets.statistics')[$driver]['driver']
??
\BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver::class
);
});
2020-08-18 17:21:22 +00:00
}
/**
* Configure the PubSub replication.
*
* @return void
*/
2019-07-28 18:50:10 +00:00
protected function configurePubSub()
{
2020-08-13 19:05:57 +00:00
$this->app->make(BroadcastManager::class)->extend('websockets', 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
}
2020-08-18 17:21:22 +00:00
/**
* Register the dashboard routes.
*
* @return void
*/
protected function registerDashboardRoutes()
2018-11-26 22:38:27 +00:00
{
2020-08-23 15:34:29 +00:00
Route::group([
'prefix' => config('websockets.dashboard.path'),
'as' => 'laravel-websockets.',
'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
], function () {
Route::get('/', ShowDashboard::class)->name('dashboard');
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics'])->name('statistics');
Route::post('auth', AuthenticateDashboard::class)->name('auth');
Route::post('event', SendMessage::class)->name('send');
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
}
2020-08-18 17:21:22 +00:00
/**
* Register the dashboard gate.
*
* @return void
*/
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) {
2020-08-23 15:34:29 +00:00
return $this->app->environment(['local', 'testing']);
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
}
}