laravel-websockets/src/WebSocketsServiceProvider.php

101 lines
3.9 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
2018-12-04 21:22:33 +00:00
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
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;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Server\Router;
2018-12-03 22:19:46 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController;
2020-03-04 09:58:39 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager;
2020-08-13 15:32:31 +00:00
use Illuminate\Database\QueryException;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Schema;
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
{
public function boot()
{
2018-11-22 22:55:39 +00:00
$this->publishes([
2020-08-13 15:37:38 +00:00
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
2018-11-22 22:55:39 +00:00
], 'config');
2020-08-13 15:32:31 +00:00
try {
2020-08-13 15:37:38 +00:00
if (! Schema::hasTable('websockets_statistics_entries')) {
2020-08-13 15:32:31 +00:00
$this->publishes([
2020-08-13 15:37:38 +00:00
__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'),
], 'migrations');
2020-08-13 15:32:31 +00:00
}
$this
2020-08-13 15:37:38 +00:00
->registerRoutes()
->registerDashboardGate();
2020-08-13 15:32:31 +00:00
2020-08-13 15:37:38 +00:00
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
2020-08-13 15:32:31 +00:00
$this->commands([
Console\StartWebSocketServer::class,
Console\CleanStatistics::class,
Console\RestartWebSocketServer::class,
]);
} catch (QueryException $e) {
// Exception raised by composer update
// Usually happens when doing on CI where no DB exists at start
// Either way if DB connection not obtained
// Catching and doing nothing is ignore for composer update
2018-12-03 10:58:42 +00:00
}
2018-11-20 10:32:56 +00:00
}
public function register()
{
2020-08-13 15:37:38 +00:00
$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'))
2020-08-13 15:37:38 +00:00
? app(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 () {
2018-12-01 13:17:32 +00:00
return app(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);
2020-08-13 15:37:38 +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) {
2018-11-24 22:52:55 +00:00
return 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
}
}