From 02bc372a04e2c3b605246b4aaa7e7eec97977a50 Mon Sep 17 00:00:00 2001 From: Antonio Pauletich Date: Wed, 5 Dec 2018 02:07:14 +0100 Subject: [PATCH] Add illuminate/contracts dependency --- composer.json | 1 + src/Apps/ConfigAppProvider.php | 5 +++-- src/Console/CleanStatistics.php | 7 ++++--- src/Console/StartWebSocketServer.php | 17 +++++++++++------ .../Http/Controllers/DashboardApiController.php | 6 ++++-- src/Dashboard/Http/Controllers/SendMessage.php | 11 ++++++++++- src/Server/WebSocketServerFactory.php | 17 +++++++++++------ .../WebSocketStatisticsEntriesController.php | 5 +++-- src/WebSocketsServiceProvider.php | 7 +++++-- tests/ClientProviders/ConfigAppProviderTest.php | 2 +- 10 files changed, 53 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index 7f1fda42cb..6476be1285 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "guzzlehttp/psr7": "^1.5", "illuminate/broadcasting": "5.7.*", "illuminate/console": "5.7.*", + "illuminate/contracts": "5.7.*", "illuminate/http": "5.7.*", "illuminate/routing": "5.7.*", "illuminate/support": "5.7.*", diff --git a/src/Apps/ConfigAppProvider.php b/src/Apps/ConfigAppProvider.php index a0ded0f665..795c8cfb48 100644 --- a/src/Apps/ConfigAppProvider.php +++ b/src/Apps/ConfigAppProvider.php @@ -3,15 +3,16 @@ namespace BeyondCode\LaravelWebSockets\Apps; use Illuminate\Support\Collection; +use Illuminate\Contracts\Config\Repository; class ConfigAppProvider implements AppProvider { /** @var Collection */ protected $apps; - public function __construct() + public function __construct(Repository $config) { - $this->apps = collect(config('websockets.apps')); + $this->apps = collect($config->get('websockets.apps')); } /** @return array[\BeyondCode\LaravelWebSockets\AppProviders\App] */ diff --git a/src/Console/CleanStatistics.php b/src/Console/CleanStatistics.php index aba815f51a..dddfe6f87c 100644 --- a/src/Console/CleanStatistics.php +++ b/src/Console/CleanStatistics.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Contracts\Config\Repository; class CleanStatistics extends Command { @@ -13,17 +14,17 @@ class CleanStatistics extends Command protected $description = 'Clean up old statistics from the websocket log.'; - public function handle() + public function handle(Repository $config) { $this->comment('Cleaning WebSocket Statistics...'); $appId = $this->argument('appId'); - $maxAgeInDays = config('websockets.statistics.delete_statistics_older_than_days'); + $maxAgeInDays = $config->get('websockets.statistics.delete_statistics_older_than_days'); $cutOffDate = Carbon::now()->subDay($maxAgeInDays)->format('Y-m-d H:i:s'); - $webSocketsStatisticsEntryModelClass = config('websockets.statistics.model'); + $webSocketsStatisticsEntryModelClass = $config->get('websockets.statistics.model'); $amountDeleted = $webSocketsStatisticsEntryModelClass::where('created_at', '<', $cutOffDate) ->when(! is_null($appId), function (Builder $query) use ($appId) { diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index e7ae29a5f4..750e4759c8 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -5,6 +5,7 @@ use React\Socket\Connector; use Clue\React\Buzz\Browser; use Illuminate\Console\Command; +use Illuminate\Contracts\Config\Repository; use React\EventLoop\Factory as LoopFactory; use BeyondCode\LaravelWebSockets\Statistics\DnsResolver; use BeyondCode\LaravelWebSockets\Facades\StatisticsLogger; @@ -26,11 +27,15 @@ class StartWebSocketServer extends Command /** @var \React\EventLoop\LoopInterface */ protected $loop; - public function __construct() + /** @var \Illuminate\Contracts\Config\Repository */ + protected $config; + + public function __construct(Repository $config) { parent::__construct(); $this->loop = LoopFactory::create(); + $this->config = $config; } public function handle() @@ -56,7 +61,7 @@ protected function configureStatisticsLogger() return new HttpStatisticsLogger(app(ChannelManager::class), $browser); }); - $this->loop->addPeriodicTimer(config('websockets.statistics.interval_in_seconds'), function () { + $this->loop->addPeriodicTimer($this->config->get('websockets.statistics.interval_in_seconds'), function () { StatisticsLogger::save(); }); @@ -67,7 +72,7 @@ protected function configureHttpLogger() { app()->singleton(HttpLogger::class, function () { return (new HttpLogger($this->output)) - ->enable(config('app.debug')) + ->enable($this->config->get('app.debug')) ->verbose($this->output->isVerbose()); }); @@ -78,7 +83,7 @@ protected function configureMessageLogger() { app()->singleton(WebsocketsLogger::class, function () { return (new WebsocketsLogger($this->output)) - ->enable(config('app.debug')) + ->enable($this->config->get('app.debug')) ->verbose($this->output->isVerbose()); }); @@ -89,7 +94,7 @@ protected function configureConnectionLogger() { app()->bind(ConnectionLogger::class, function () { return (new ConnectionLogger($this->output)) - ->enable(config('app.debug')) + ->enable($this->config->get('app.debug')) ->verbose($this->output->isVerbose()); }); @@ -110,7 +115,7 @@ protected function startWebSocketServer() $routes = WebSocketsRouter::getRoutes(); /* 🛰 Start the server 🛰 */ - (new WebSocketServerFactory()) + (new WebSocketServerFactory($this->config)) ->setLoop($this->loop) ->useRoutes($routes) ->setHost($this->option('host')) diff --git a/src/Dashboard/Http/Controllers/DashboardApiController.php b/src/Dashboard/Http/Controllers/DashboardApiController.php index 1d77b9d874..5071c80886 100644 --- a/src/Dashboard/Http/Controllers/DashboardApiController.php +++ b/src/Dashboard/Http/Controllers/DashboardApiController.php @@ -2,11 +2,13 @@ namespace BeyondCode\LaravelWebSockets\Dashboard\Http\Controllers; +use Illuminate\Contracts\Config\Repository; + class DashboardApiController { - public function getStatistics($appId) + public function getStatistics($appId, Repository $config) { - $webSocketsStatisticsEntryModelClass = config('websockets.statistics.model'); + $webSocketsStatisticsEntryModelClass = $config->get('websockets.statistics.model'); $statistics = $webSocketsStatisticsEntryModelClass::where('app_id', $appId)->latest()->limit(120)->get(); $statisticData = $statistics->map(function ($statistic) { diff --git a/src/Dashboard/Http/Controllers/SendMessage.php b/src/Dashboard/Http/Controllers/SendMessage.php index 5c4461af62..5ff3fbee17 100644 --- a/src/Dashboard/Http/Controllers/SendMessage.php +++ b/src/Dashboard/Http/Controllers/SendMessage.php @@ -4,11 +4,20 @@ use Pusher\Pusher; use Illuminate\Http\Request; +use Illuminate\Contracts\Config\Repository; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use Illuminate\Broadcasting\Broadcasters\PusherBroadcaster; class SendMessage { + /** @var \Illuminate\Contracts\Config\Repository */ + protected $config; + + public function __construct(Repository $config) + { + $this->config = $config; + } + public function __invoke(Request $request) { $validated = $request->validate([ @@ -35,7 +44,7 @@ protected function getPusherBroadcaster(array $validated): PusherBroadcaster $validated['key'], $validated['secret'], $validated['appId'], - config('broadcasting.connections.pusher.options', []) + $this->config->get('broadcasting.connections.pusher.options', []) ); return new PusherBroadcaster($pusher); diff --git a/src/Server/WebSocketServerFactory.php b/src/Server/WebSocketServerFactory.php index e7bdd82a9b..210dc9334b 100644 --- a/src/Server/WebSocketServerFactory.php +++ b/src/Server/WebSocketServerFactory.php @@ -7,6 +7,7 @@ use Ratchet\Server\IoServer; use React\Socket\SecureServer; use React\EventLoop\LoopInterface; +use Illuminate\Contracts\Config\Repository; use React\EventLoop\Factory as LoopFactory; use Symfony\Component\Routing\RequestContext; use Symfony\Component\Routing\RouteCollection; @@ -28,12 +29,16 @@ class WebSocketServerFactory /** @var \Symfony\Component\Routing\RouteCollection */ protected $routes; - /** @var Symfony\Component\Console\Output\OutputInterface */ + /** @var \Symfony\Component\Console\Output\OutputInterface */ protected $consoleOutput; - public function __construct() + /** @var \Illuminate\Contracts\Config\Repository */ + protected $config; + + public function __construct(Repository $config) { $this->loop = LoopFactory::create(); + $this->config = $config; } public function useRoutes(RouteCollection $routes) @@ -75,17 +80,17 @@ public function createServer(): IoServer { $socket = new Server("{$this->host}:{$this->port}", $this->loop); - if (config('websockets.ssl.local_cert')) { - $socket = new SecureServer($socket, $this->loop, config('websockets.ssl')); + if ($this->config->get('websockets.ssl.local_cert')) { + $socket = new SecureServer($socket, $this->loop, $this->config->get('websockets.ssl')); } $urlMatcher = new UrlMatcher($this->routes, new RequestContext); $router = new Router($urlMatcher); - $app = new OriginCheck($router, config('websockets.allowed_origins', [])); + $app = new OriginCheck($router, $this->config->get('websockets.allowed_origins', [])); - $httpServer = new HttpServer($app, config('websockets.max_request_size_in_kb') * 1024); + $httpServer = new HttpServer($app, $this->config->get('websockets.max_request_size_in_kb') * 1024); if (HttpLogger::isEnabled()) { $httpServer = HttpLogger::decorate($httpServer); diff --git a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php index 39ea041748..c4a16bdeaf 100644 --- a/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php +++ b/src/Statistics/Http/Controllers/WebSocketStatisticsEntriesController.php @@ -3,12 +3,13 @@ namespace BeyondCode\LaravelWebSockets\Statistics\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Contracts\Config\Repository; use BeyondCode\LaravelWebSockets\Statistics\Rules\AppId; use BeyondCode\LaravelWebSockets\Statistics\Events\StatisticsUpdated; class WebSocketStatisticsEntriesController { - public function store(Request $request) + public function store(Request $request, Repository $config) { $validatedAttributes = $request->validate([ 'app_id' => ['required', new AppId()], @@ -17,7 +18,7 @@ public function store(Request $request) 'api_message_count' => 'required|integer', ]); - $webSocketsStatisticsEntryModelClass = config('websockets.statistics.model'); + $webSocketsStatisticsEntryModelClass = $config->get('websockets.statistics.model'); $statisticModel = $webSocketsStatisticsEntryModelClass::create($validatedAttributes); diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 3c821260a4..b9e83bde62 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use Illuminate\Support\ServiceProvider; +use Illuminate\Contracts\Config\Repository; use BeyondCode\LaravelWebSockets\Server\Router; use BeyondCode\LaravelWebSockets\Apps\AppProvider; use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager; @@ -55,13 +56,15 @@ public function register() }); $this->app->singleton(AppProvider::class, function () { - return app(config('websockets.app_provider')); + return $this->app->make( + $this->app->make(Repository::class)->get('websockets.app_provider') + ); }); } protected function registerRoutes() { - Route::prefix(config('websockets.path'))->group(function () { + Route::prefix($this->app->make(Repository::class)->get('websockets.path'))->group(function () { Route::middleware(AuthorizeDashboard::class)->group(function () { Route::get('/', ShowDashboard::class); Route::get('/api/{appId}/statistics', [DashboardApiController::class, 'getStatistics']); diff --git a/tests/ClientProviders/ConfigAppProviderTest.php b/tests/ClientProviders/ConfigAppProviderTest.php index f8909dbf20..c28f38f001 100644 --- a/tests/ClientProviders/ConfigAppProviderTest.php +++ b/tests/ClientProviders/ConfigAppProviderTest.php @@ -14,7 +14,7 @@ public function setUp() { parent::setUp(); - $this->configAppProvider = new ConfigAppProvider(); + $this->configAppProvider = $this->app->make(ConfigAppProvider::class); } /** @test */