laravel-websockets/src/WebSocketsServiceProvider.php

71 lines
1.9 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
namespace BeyondCode\LaravelWebSockets;
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-11-24 00:25:40 +00:00
use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider;
2018-11-20 10:32:56 +00:00
use Illuminate\Support\ServiceProvider;
2018-11-21 11:13:40 +00:00
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\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-24 22:52:55 +00:00
Route::middlewareGroup('websockets', config('websockets.dashboard.middleware', []));
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-11-23 23:06:28 +00:00
$this->registerRoutes();
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
}
2018-11-23 23:06:28 +00:00
protected function registerRoutes()
{
Route::group($this->routeConfiguration(), function () {
$this->loadRoutesFrom(__DIR__.'/Http/routes.php');
});
}
protected function routeConfiguration()
{
return [
2018-11-24 22:52:55 +00:00
'prefix' => config('websockets.dashboard.path'),
2018-11-25 23:52:52 +00:00
'middleware' => 'websockets',
2018-11-23 23:06:28 +00:00
];
}
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
$this->app->singleton(ClientProvider::class, function() {
return app(config('websockets.client_provider'));
});
2018-11-24 22:52:55 +00:00
}
2018-11-23 23:06:28 +00:00
2018-11-24 22:52:55 +00:00
protected function registerDashboardGate()
{
Gate::define('viewWebSocketDashboard', function ($user = null) {
return app()->environment('local');
2018-11-23 23:06:28 +00:00
});
2018-11-20 10:32:56 +00:00
}
}