laravel-websockets/src/WebSocketsServiceProvider.php

83 lines
2.9 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
2018-11-26 22:24:24 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
2018-11-26 22:28:39 +00:00
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize;
2018-11-27 14:55:30 +00:00
use BeyondCode\LaravelWebSockets\Server\Router;
2018-12-03 12:33:00 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebsocketStatisticsEntriesController;
2018-12-03 13:45:50 +00:00
use BeyondCode\LaravelWebSockets\Statistics\Logger\HttpStatisticsLogger;
2018-11-24 22:52:55 +00:00
use Illuminate\Support\Facades\Gate;
2018-11-23 23:06:28 +00:00
use Illuminate\Support\Facades\Route;
2018-12-01 12:57:02 +00:00
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
2018-11-20 10:32:56 +00:00
use Illuminate\Support\ServiceProvider;
2018-11-27 15:35:28 +00:00
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
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([
2018-11-24 00:25:40 +00:00
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'),
2018-11-22 22:55:39 +00:00
], 'config');
2018-12-03 10:58:42 +00:00
if (! class_exists('CreateWebSocketsStatisticsEntries')) {
$this->publishes([
__DIR__.'/../database/migrations/cretae_websockets_statistics_entries_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_cretae_websockets_statistics_entries_table.php'),
], 'migrations');
}
2018-11-26 22:24:24 +00:00
$this->registerRouteMacro();
2018-11-23 23:06:28 +00:00
2018-11-24 22:52:55 +00:00
$this->registerDashboardGate();
2018-11-23 23:06:28 +00:00
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets');
2018-11-20 10:51:00 +00:00
$this->commands([
Console\StartWebSocketServer::class,
]);
2018-11-20 10:32:56 +00:00
}
public function register()
{
2018-11-24 00:25:40 +00:00
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets');
2018-11-22 22:55:39 +00:00
2018-11-20 10:32:56 +00:00
$this->app->singleton('websockets.router', function() {
return new Router();
});
2018-11-21 23:25:24 +00:00
2018-11-21 11:13:40 +00:00
$this->app->singleton(ChannelManager::class, function() {
return new ChannelManager();
});
2018-11-24 00:25:40 +00:00
2018-12-01 12:57:02 +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-11-26 22:38:27 +00:00
protected function registerRouteMacro()
{
2018-12-03 12:33:00 +00:00
Route::macro('webSockets', function($prefix = 'websockets') {
2018-12-03 12:49:56 +00:00
Route::prefix($prefix)->middleware(Authorize::class)->group(function() {
2018-11-26 22:38:27 +00:00
Route::get('/', ShowDashboard::class);
Route::post('auth', AuthenticateDashboard::class);
Route::post('event', SendMessage::class);
});
2018-12-03 12:33:00 +00:00
//TODO: add middleware
2018-12-03 12:49:56 +00:00
Route::prefix($prefix)->group(function() {
2018-12-03 12:33:00 +00:00
Route::post('statistics', [WebsocketStatisticsEntriesController::class, 'store']);
});
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-11-20 10:32:56 +00:00
}
}