laravel-websockets/src/WebSocketsServiceProvider.php

162 lines
4.6 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
2020-09-11 13:39:56 +00:00
use BeyondCode\LaravelWebSockets\Contracts\StatisticsCollector;
2020-09-11 06:07:57 +00:00
use BeyondCode\LaravelWebSockets\Contracts\StatisticsStore;
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;
2020-09-10 19:59:49 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
use BeyondCode\LaravelWebSockets\Server\Router;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
2020-09-10 19:59:49 +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([
2020-09-10 19:59:26 +00:00
__DIR__.'/../config/websockets.php' => config_path('websockets.php'),
2018-11-22 22:55:39 +00:00
], 'config');
2020-09-10 19:59:26 +00:00
$this->mergeConfigFrom(
__DIR__.'/../config/websockets.php', 'websockets'
);
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-09-10 20:20:19 +00:00
__DIR__.'/../database/migrations/0000_00_00_000000_rename_statistics_counters.php' => database_path('migrations/0000_00_00_000000_rename_statistics_counters.php'),
2020-08-13 16:28:57 +00:00
], 'migrations');
2018-12-03 10:58:42 +00:00
2020-09-11 06:07:57 +00:00
$this->registerStatistics();
2020-09-10 19:59:26 +00:00
$this->registerDashboard();
2018-11-23 23:06:28 +00:00
2020-09-10 19:59:26 +00:00
$this->registerCommands();
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()
{
2020-09-10 19:59:26 +00:00
$this->registerRouter();
$this->registerManagers();
}
2020-08-18 17:21:22 +00:00
2020-09-11 06:07:57 +00:00
/**
* Register the statistics-related contracts.
*
* @return void
*/
protected function registerStatistics()
{
$this->app->singleton(StatisticsStore::class, function () {
$class = config('websockets.statistics.store');
return new $class;
});
2020-09-11 13:39:56 +00:00
$this->app->singleton(StatisticsCollector::class, function () {
$replicationMode = config('websockets.replication.mode', 'local');
$class = config("websockets.replication.modes.{$replicationMode}.collector");
return new $class;
});
2020-09-11 06:07:57 +00:00
}
2020-09-10 19:59:26 +00:00
/**
* Regsiter the dashboard components.
*
* @return void
*/
protected function registerDashboard()
{
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
2020-08-18 17:21:22 +00:00
2020-09-10 19:59:26 +00:00
$this->registerDashboardRoutes();
$this->registerDashboardGate();
}
2020-08-18 17:21:22 +00:00
2020-09-10 19:59:26 +00:00
/**
* Register the package commands.
*
* @return void
*/
protected function registerCommands()
{
$this->commands([
Console\Commands\StartServer::class,
Console\Commands\RestartServer::class,
Console\Commands\CleanStatistics::class,
2020-09-11 13:39:56 +00:00
Console\Commands\FlushCollectedStatistics::class,
2020-09-10 19:59:26 +00:00
]);
}
2020-09-03 13:31:19 +00:00
2020-09-10 19:59:26 +00:00
/**
* Register the routing.
*
* @return void
*/
protected function registerRouter()
{
$this->app->singleton('websockets.router', function () {
return new Router;
2020-08-18 17:21:22 +00:00
});
2020-09-10 19:59:26 +00:00
}
2020-08-18 17:21:22 +00:00
2020-09-10 19:59:26 +00:00
/**
* Register the managers for the app.
*
* @return void
*/
protected function registerManagers()
{
$this->app->singleton(Contracts\AppManager::class, function () {
2020-08-18 17:21:22 +00:00
return $this->app->make(config('websockets.managers.app'));
});
}
/**
* 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
});
}
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-11-20 10:32:56 +00:00
}
}