Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Add illuminate/contracts dependency #7

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"guzzlehttp/psr7": "^1.5",
"illuminate/broadcasting": "5.7.*",
"illuminate/console": "5.7.*",
"illuminate/contracts": "5.7.*",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"illuminate/contracts": "5.7.*",

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what the "best practises" are for this, but I think it's alright to rely on their dependency of illuminate/contracts.

We should not care about other packages dependencies. Each package should declare what dependencies it relies upon. If the dependencies this package depends upon suddenly drop their dependency on illuminate/contracts (which has a really low probability but still...) this package would also break which of course shouldn't happen.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That does make sense. Thank you for taking the time to clarify. ✌️

"illuminate/http": "5.7.*",
"illuminate/routing": "5.7.*",
"illuminate/support": "5.7.*",
Expand Down
5 changes: 3 additions & 2 deletions src/Apps/ConfigAppProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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] */
Expand Down
7 changes: 4 additions & 3 deletions src/Console/CleanStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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) {
Expand Down
17 changes: 11 additions & 6 deletions src/Console/StartWebSocketServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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();
});

Expand All @@ -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());
});

Expand All @@ -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());
});

Expand All @@ -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());
});

Expand All @@ -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'))
Expand Down
6 changes: 4 additions & 2 deletions src/Dashboard/Http/Controllers/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 10 additions & 1 deletion src/Dashboard/Http/Controllers/SendMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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);
Expand Down
17 changes: 11 additions & 6 deletions src/Server/WebSocketServerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand All @@ -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);

Expand Down
7 changes: 5 additions & 2 deletions src/WebSocketsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientProviders/ConfigAppProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function setUp()
{
parent::setUp();

$this->configAppProvider = new ConfigAppProvider();
$this->configAppProvider = $this->app->make(ConfigAppProvider::class);
}

/** @test */
Expand Down