Skip to content

Commit

Permalink
add channels file support
Browse files Browse the repository at this point in the history
  • Loading branch information
mozex committed Apr 19, 2024
1 parent 458783c commit f03fb63
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
14 changes: 13 additions & 1 deletion src/ModulesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,18 @@ protected function bootRoutes(): void
return;
}

[$commands, $routes] = AssetType::Routes->scout()->collect()
[$commands, $rest] = AssetType::Routes->scout()->collect()
->partition(
fn (array $asset) => collect(AssetType::Routes->config()['commands_filenames'])
->contains(File::name($asset['path']))
);

[$channels, $routes] = $rest
->partition(
fn (array $asset) => collect(AssetType::Routes->config()['channels_filenames'])
->contains(File::name($asset['path']))
);

$this->callAfterResolving(Kernel::class, function (Kernel $kernel) use ($commands) {
// Compatibility with Laravel 10
if (method_exists($kernel, 'addCommandRoutePaths')) {
Expand All @@ -298,6 +304,12 @@ protected function bootRoutes(): void
}
});

$this->app->booted(function () use ($channels) {
$channels->each(function (array $asset): void {
require $asset['path'];
});
});

if ($this->app->routesAreCached()) {
return;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Feature/ChannelsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

use Illuminate\Support\Facades\Broadcast;
use Mozex\Modules\Scouts\RoutesScout;

it('can register channels', function (bool $cache) {
$discoverer = RoutesScout::create();

if ($cache) {
$discoverer->cache();
}

$channels = collect(Broadcast::getChannels())->keys();

expect($channels)
->toContain('App.Models.User.{id}')
->toContain('Chat.{userId}');

if ($cache) {
$discoverer->clear();
}
})->with([
'without cache' => false,
'with cache' => true,
]);
6 changes: 5 additions & 1 deletion tests/Feature/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,13 @@
->and($collection->pluck('path'))
->toContain(realpath(Modules::modulesPath('First/Routes/web.php')))
->toContain(realpath(Modules::modulesPath('First/Routes/api.php')))
->toContain(realpath(Modules::modulesPath('First/Routes/console.php')))
->toContain(realpath(Modules::modulesPath('First/Routes/channels.php')))
->toContain(realpath(Modules::modulesPath('Second/Routes/web.php')))
->toContain(realpath(Modules::modulesPath('Second/Routes/undefined.php')))
->toContain(realpath(Modules::modulesPath('Second/Routes/custom.php')));
->toContain(realpath(Modules::modulesPath('Second/Routes/custom.php')))
->toContain(realpath(Modules::modulesPath('Second/Routes/console.php')))
->toContain(realpath(Modules::modulesPath('Second/Routes/channels.php')));

if ($cache) {
$discoverer->clear();
Expand Down
2 changes: 1 addition & 1 deletion workbench/Modules/First/Routes/channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
use Illuminate\Support\Facades\Broadcast;
use Modules\First\Models\User;

Broadcast::channel('Chat.{userId}', fn(User $user, int $userId) => $user->id === $userId);
Broadcast::channel('Chat.{userId}', fn (User $user, int $userId) => $user->id === $userId);
2 changes: 0 additions & 2 deletions workbench/Modules/Second/Routes/channels.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php

use Illuminate\Support\Facades\Broadcast;
use Modules\First\Models\User;

Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});

0 comments on commit f03fb63

Please sign in to comment.