laravel-websockets/src/WebSocketsServiceProvider.php

120 lines
3.9 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;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
2020-08-23 17:41:38 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
2018-12-04 09:25:34 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
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-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;
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
}
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 () {
2020-09-03 13:31:19 +00:00
$replicationDriver = config('websockets.replication.driver', 'local');
2020-08-18 17:21:22 +00:00
2020-09-03 13:31:19 +00:00
$class = config("websockets.replication.{$replicationDriver}.channel_manager", ArrayChannelManager::class);
return new $class;
2020-08-18 17:21:22 +00:00
});
$this->app->singleton(AppManager::class, function () {
return $this->app->make(config('websockets.managers.app'));
});
$this->app->singleton(StatisticsDriver::class, function () {
2020-09-04 09:00:04 +00:00
$driver = config('websockets.statistics.driver', 'local');
return $this->app->make(
config(
"websockets.statistics.{$driver}.driver",
\BeyondCode\LaravelWebSockets\Statistics\Drivers\DatabaseDriver::class
)
);
});
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');
2020-08-23 17:41:17 +00:00
Route::get('/api/{appId}/statistics', ShowStatistics::class)->name('statistics');
2020-08-23 16:12:22 +00:00
Route::post('/auth', AuthenticateDashboard::class)->name('auth');
2020-08-23 17:41:17 +00:00
Route::post('/event', SendMessage::class)->name('event');
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 16:12:22 +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
}
}