|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppProvider; |
|
6
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; |
|
7
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController; |
|
8
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; |
|
9
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; |
|
10
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard; |
|
11
|
|
|
use BeyondCode\LaravelWebSockets\Server\Router; |
|
12
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; |
|
13
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics; |
|
14
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
|
15
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; |
|
16
|
|
|
use Illuminate\Support\Facades\Gate; |
|
17
|
|
|
use Illuminate\Support\Facades\Route; |
|
18
|
|
|
use Illuminate\Support\ServiceProvider; |
|
19
|
|
|
use Illuminate\Support\Facades\Schema |
|
20
|
|
|
|
|
21
|
|
|
class WebSocketsServiceProvider extends ServiceProvider |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
public function boot() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->publishes([ |
|
26
|
|
|
__DIR__.'/../config/websockets.php' => base_path('config/websockets.php'), |
|
27
|
|
|
], 'config'); |
|
28
|
|
|
|
|
29
|
|
|
if (!Schema::hasTable('websockets_statistics_entries')) |
|
30
|
|
|
$this->publishes([ |
|
31
|
|
|
__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'), |
|
32
|
|
|
], 'migrations'); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$this |
|
36
|
|
|
->registerRoutes() |
|
37
|
|
|
->registerDashboardGate(); |
|
38
|
|
|
|
|
39
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); |
|
40
|
|
|
|
|
41
|
|
|
$this->commands([ |
|
42
|
|
|
Console\StartWebSocketServer::class, |
|
43
|
|
|
Console\CleanStatistics::class, |
|
44
|
|
|
]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function register() |
|
48
|
|
|
{ |
|
49
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets'); |
|
50
|
|
|
|
|
51
|
|
|
$this->app->singleton('websockets.router', function () { |
|
52
|
|
|
return new Router(); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
$this->app->singleton(ChannelManager::class, function () { |
|
56
|
|
|
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager')) |
|
57
|
|
|
? app(config('websockets.channel_manager')) : new ArrayChannelManager(); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$this->app->singleton(AppProvider::class, function () { |
|
61
|
|
|
return app(config('websockets.app_provider')); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
protected function registerRoutes() |
|
66
|
|
|
{ |
|
67
|
|
|
Route::prefix(config('websockets.path'))->group(function () { |
|
68
|
|
|
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () { |
|
69
|
|
|
Route::get('/', ShowDashboard::class); |
|
70
|
|
|
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']); |
|
71
|
|
|
Route::post('auth', AuthenticateDashboard::class); |
|
72
|
|
|
Route::post('event', SendMessage::class); |
|
73
|
|
|
}); |
|
74
|
|
|
|
|
75
|
|
|
Route::middleware(AuthorizeStatistics::class)->group(function () { |
|
76
|
|
|
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']); |
|
77
|
|
|
}); |
|
78
|
|
|
}); |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
protected function registerDashboardGate() |
|
84
|
|
|
{ |
|
85
|
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) { |
|
86
|
|
|
return app()->environment('local'); |
|
87
|
|
|
}); |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|