laravel-websockets/src/WebSocketsServiceProvider.php

261 lines
7.8 KiB
PHP
Raw Normal View History

2018-11-20 10:32:56 +00:00
<?php
2025-01-16 07:54:02 +00:00
namespace BlaxSoftware\LaravelWebSockets;
2018-11-20 10:32:56 +00:00
2025-01-16 07:54:02 +00:00
use BlaxSoftware\LaravelWebSockets\Contracts\StatisticsCollector;
use BlaxSoftware\LaravelWebSockets\Contracts\StatisticsStore;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\AuthenticateDashboard;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\SendMessage;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\ShowApps;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\ShowDashboard;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\ShowStatistics;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Controllers\StoreApp;
use BlaxSoftware\LaravelWebSockets\Dashboard\Http\Middleware\Authorize as AuthorizeDashboard;
use BlaxSoftware\LaravelWebSockets\Queue\AsyncRedisConnector;
use BlaxSoftware\LaravelWebSockets\Server\Router;
use Clue\React\SQLite\DatabaseInterface;
use Clue\React\SQLite\Factory as SQLiteFactory;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Facades\Gate;
2020-09-25 19:16:06 +00:00
use Illuminate\Support\Facades\Queue;
2020-03-04 09:58:39 +00:00
use Illuminate\Support\Facades\Route;
2020-09-10 19:59:49 +00:00
use Illuminate\Support\ServiceProvider;
use React\EventLoop\Factory;
use React\EventLoop\LoopInterface;
use React\MySQL\ConnectionInterface;
use React\MySQL\Factory as MySQLFactory;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
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
$this->registerEventLoop();
$this->registerSQLiteDatabase();
$this->registerMySqlDatabase();
2020-09-25 19:16:06 +00:00
$this->registerAsyncRedisQueueDriver();
$this->registerWebSocketHandler();
2020-09-25 19:16:06 +00:00
$this->registerRouter();
$this->registerManagers();
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-25 19:16:06 +00:00
//
}
protected function registerEventLoop()
{
$this->app->singleton(LoopInterface::class, function () {
return Factory::create();
});
}
2020-09-25 19:16:06 +00:00
/**
* Register the async, non-blocking Redis queue driver.
*
* @return void
*/
protected function registerAsyncRedisQueueDriver()
{
Queue::extend('async-redis', function () {
return new AsyncRedisConnector($this->app['redis']);
2020-09-25 19:16:06 +00:00
});
2020-09-10 19:59:26 +00:00
}
2020-08-18 17:21:22 +00:00
protected function registerSQLiteDatabase()
{
$this->app->singleton(DatabaseInterface::class, function () {
$factory = new SQLiteFactory($this->app->make(LoopInterface::class));
$database = $factory->openLazy(
config('websockets.managers.sqlite.database', ':memory:')
);
$migrations = (new Finder())
->files()
->ignoreDotFiles(true)
->in(__DIR__.'/../database/migrations/sqlite')
->name('*.sql');
/** @var SplFileInfo $migration */
foreach ($migrations as $migration) {
$database->exec($migration->getContents());
}
return $database;
});
}
protected function registerMySqlDatabase()
{
$this->app->singleton(ConnectionInterface::class, function () {
$factory = new MySQLFactory($this->app->make(LoopInterface::class));
$connectionKey = 'database.connections.'.config('websockets.managers.mysql.connection');
$auth = trim(config($connectionKey.'.username').':'.config($connectionKey.'.password'), ':');
$connection = trim(config($connectionKey.'.host').':'.config($connectionKey.'.port'), ':');
$database = config($connectionKey.'.database');
$database = $factory->createLazyConnection(trim("{$auth}@{$connection}/{$database}", '@'));
return $database;
});
}
2020-09-11 06:07:57 +00:00
/**
* Register the statistics-related contracts.
*
* @return void
*/
protected function registerStatistics()
{
$this->app->singleton(StatisticsStore::class, function ($app) {
$config = $app['config']['websockets'];
$class = $config['statistics']['store'];
2020-09-11 06:07:57 +00:00
return new $class;
});
2020-09-11 13:39:56 +00:00
$this->app->singleton(StatisticsCollector::class, function ($app) {
$config = $app['config']['websockets'];
$replicationMode = $config['replication']['mode'] ?? 'local';
2020-09-11 13:39:56 +00:00
$class = $config['replication']['modes'][$replicationMode]['collector'];
2020-09-11 13:39:56 +00:00
return new $class;
});
2020-09-11 06:07:57 +00:00
}
2020-09-10 19:59:26 +00:00
/**
* Register the dashboard components.
2020-09-10 19:59:26 +00:00
*
* @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 ($app) {
$config = $app['config']['websockets'];
return $this->app->make($config['managers']['app']);
2020-08-18 17:21:22 +00:00
});
}
/**
* 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([
'domain' => config('websockets.dashboard.domain'),
2020-08-23 15:34:29 +00:00
'prefix' => config('websockets.dashboard.path'),
'as' => 'laravel-websockets.',
'middleware' => config('websockets.dashboard.middleware', [AuthorizeDashboard::class]),
], function () {
Route::get('/', ShowDashboard::class)->name('dashboard');
Route::get('/apps', ShowApps::class)->name('apps');
Route::post('/apps', StoreApp::class)->name('apps.store');
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
}
protected function registerWebSocketHandler()
{
$this->app->singleton('websockets.handler', function () {
return app(config('websockets.handlers.websocket'));
});
}
2018-11-20 10:32:56 +00:00
}