1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Gate; |
6
|
|
|
use Illuminate\Support\Facades\Route; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use BeyondCode\LaravelWebSockets\Server\Router; |
9
|
|
|
use BeyondCode\LaravelWebSockets\Apps\AppProvider; |
10
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; |
11
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage; |
12
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard; |
13
|
|
|
use BeyondCode\LaravelWebSockets\Database\Http\Controllers\AppsController; |
14
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard; |
15
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers\DashboardApiController; |
16
|
|
|
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager; |
17
|
|
|
use BeyondCode\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard; |
18
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Middleware\Authorize as AuthorizeStatistics; |
19
|
|
|
use BeyondCode\LaravelWebSockets\Statistics\Http\Controllers\WebSocketStatisticsEntriesController; |
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
|
|
|
$this->publishMigrations(); |
30
|
|
|
|
31
|
|
|
$this |
32
|
|
|
->registerRoutes() |
33
|
|
|
->registerDashboardGate(); |
34
|
|
|
|
35
|
|
|
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'websockets'); |
36
|
|
|
|
37
|
|
|
$this->commands([ |
38
|
|
|
Console\StartWebSocketServer::class, |
39
|
|
|
Console\CleanStatistics::class, |
40
|
|
|
Database\Console\AppCreate::class, |
41
|
|
|
]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function register() |
45
|
|
|
{ |
46
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/websockets.php', 'websockets'); |
47
|
|
|
|
48
|
|
|
$this->app->singleton('websockets.router', function () { |
49
|
|
|
return new Router(); |
50
|
|
|
}); |
51
|
|
|
|
52
|
|
|
$this->app->singleton(ChannelManager::class, function () { |
53
|
|
|
return config('websockets.channel_manager') !== null && class_exists(config('websockets.channel_manager')) |
54
|
|
|
? app(config('websockets.channel_manager')) : new ArrayChannelManager(); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$this->app->singleton(AppProvider::class, function () { |
58
|
|
|
return app(config('websockets.app_provider')); |
59
|
|
|
}); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function publishMigrations() |
63
|
|
|
{ |
64
|
|
View Code Duplication |
if (! class_exists('CreateWebSocketsStatisticsEntries')) { |
|
|
|
|
65
|
|
|
$this->publishes([ |
66
|
|
|
__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'), |
67
|
|
|
], 'migrations'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if (! class_exists('CreateWebsocketsAppsTable')) { |
|
|
|
|
71
|
|
|
$this->publishes([ |
72
|
|
|
__DIR__.'/../database/migrations/create_websockets_apps_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_websockets_apps_table.php'), |
73
|
|
|
], 'migrations'); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function registerRoutes() |
78
|
|
|
{ |
79
|
|
|
Route::prefix(config('websockets.path'))->group(function () { |
80
|
|
|
Route::middleware(config('websockets.middleware', [AuthorizeDashboard::class]))->group(function () { |
81
|
|
|
Route::get('/', ShowDashboard::class); |
82
|
|
|
Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']); |
83
|
|
|
Route::post('auth', AuthenticateDashboard::class); |
84
|
|
|
Route::post('event', SendMessage::class); |
85
|
|
|
|
86
|
|
|
Route::get('/admin', AppsController::class.'@index')->name('websockets.admin.index'); |
87
|
|
|
Route::get('/admin/create', AppsController::class.'@create')->name('websockets.admin.create'); |
88
|
|
|
Route::post('/admin/store', AppsController::class.'@store')->name('websockets.admin.store'); |
89
|
|
|
Route::get('/admin/{app}/edit', AppsController::class.'@edit')->name('websockets.admin.edit'); |
90
|
|
|
Route::post('/admin/{app}/store', AppsController::class.'@update')->name('websockets.admin.update'); |
91
|
|
|
Route::post('/admin/{app}/destroy', AppsController::class.'@destroy')->name('websockets.admin.destroy'); |
92
|
|
|
}); |
93
|
|
|
|
94
|
|
|
Route::middleware(AuthorizeStatistics::class)->group(function () { |
95
|
|
|
Route::post('statistics', [WebSocketStatisticsEntriesController::class, 'store']); |
96
|
|
|
}); |
97
|
|
|
}); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
protected function registerDashboardGate() |
103
|
|
|
{ |
104
|
|
|
Gate::define('viewWebSocketsDashboard', function ($user = null) { |
|
|
|
|
105
|
|
|
return app()->environment('local'); |
106
|
|
|
}); |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.