Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #175 from laravel-enso/feature/websocketsProvider
Feature/websockets provider
- Loading branch information
Showing
6 changed files
with
87 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Core\Facades; | ||
|
||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Websockets extends Facade | ||
{ | ||
public static function getFacadeAccessor() | ||
{ | ||
return 'websockets'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Core\Services; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class Websockets | ||
{ | ||
private Collection $channels; | ||
|
||
public function __construct() | ||
{ | ||
$this->channels = new Collection(); | ||
} | ||
|
||
public function register($channels) | ||
{ | ||
(new Collection($channels))->each(fn ($channel, $key) => $this->channels->put( | ||
$key, | ||
$channel | ||
)); | ||
} | ||
|
||
public function remove($aliases) | ||
{ | ||
(new Collection($aliases))->each(fn ($alias) => $this->channels->forget($alias)); | ||
} | ||
|
||
public function all() | ||
{ | ||
return $this->channels | ||
->map(fn ($channel) => is_string($channel) ? $channel : $channel->call($this, Auth::user())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace LaravelEnso\Core; | ||
|
||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\ServiceProvider; | ||
use LaravelEnso\Core\Facades\Websockets; | ||
use LaravelEnso\Core\Models\User; | ||
|
||
class WebsocketServiceProvider extends ServiceProvider | ||
{ | ||
protected array $register = []; | ||
|
||
public function boot() | ||
{ | ||
return static::class === self::class | ||
? $this->coreChannels() | ||
: Websockets::register($this->register); | ||
} | ||
|
||
private function coreChannels() | ||
{ | ||
Websockets::register([ | ||
'appUpdates' => 'app-updates', | ||
'private' => fn (User $user) => (new Collection( | ||
explode('\\', Config::get('auth.providers.users.model')) | ||
))->push($user->id)->implode('.'), | ||
]); | ||
} | ||
} |