Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add a "channel:list" command #46248

Merged
merged 2 commits into from
Feb 24, 2023
Merged
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
10 changes: 10 additions & 0 deletions src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,14 @@ protected function channelNameMatchesPattern($channel, $pattern)
{
return preg_match('/^'.preg_replace('/\{(.*?)\}/', '([^\.]+)', $pattern).'$/', $channel);
}

/**
* Get all of the registered channels.
*
* @return \Illuminate\Support\Collection
*/
public function getChannels()
{
return collect($this->channels);
}
}
151 changes: 151 additions & 0 deletions src/Illuminate/Foundation/Console/ChannelListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Illuminate\Foundation\Console;

use Closure;
use Illuminate\Console\Command;
use Illuminate\Contracts\Broadcasting\Broadcaster;
use Illuminate\Support\Collection;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Terminal;

#[AsCommand(name: 'channel:list')]
class ChannelListCommand extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'channel:list';

/**
* The console command description.
*
* @var string
*/
protected $description = 'List all registered private broadcast channels';

/**
* The terminal width resolver callback.
*
* @var \Closure|null
*/
protected static $terminalWidthResolver;

/**
* Execute the console command.
*
* @param \Illuminate\Contracts\Broadcasting\Broadcaster
* @return void
*/
public function handle(Broadcaster $broadcaster)
{
$channels = $broadcaster->getChannels();

if (! $this->laravel->providerIsLoaded('App\Providers\BroadcastServiceProvider') &&
file_exists($this->laravel->path('Providers/BroadcastServiceProvider.php'))) {
$this->components->warn('The [App\Providers\BroadcastServiceProvider] has not been loaded. Your private channels may not be loaded.');
}

if (! $channels->count()) {
return $this->components->error("Your application doesn't have any private broadcasting channels.");
}

$this->displayChannels($channels);
}

/**
* Display the channel information on the console.
*
* @param Collection $channels
* @return void
*/
protected function displayChannels($channels)
{
$this->output->writeln($this->forCli($channels));
}

/**
* Convert the given channels to regular CLI output.
*
* @param \Illuminate\Support\Collection $channels
* @return array
*/
protected function forCli($channels)
{
$maxChannelName = $channels->keys()->max(function ($channelName) {
return mb_strlen($channelName);
});

$terminalWidth = $this->getTerminalWidth();

$channelCount = $this->determineChannelCountOutput($channels, $terminalWidth);

return $channels->map(function ($channel, $channelName) use ($maxChannelName, $terminalWidth) {
$resolver = $channel instanceof Closure ? 'Closure' : $channel;

$spaces = str_repeat(' ', max($maxChannelName + 6 - mb_strlen($channelName), 0));

$dots = str_repeat('.', max(
$terminalWidth - mb_strlen($channelName.$spaces.$resolver) - 6, 0
));

$dots = empty($dots) ? $dots : " $dots";

return sprintf(
' <fg=blue;options=bold>%s</> %s<fg=white>%s</><fg=#6C7280>%s</>',
$channelName,
$spaces,
$resolver,
$dots,
);
})
->filter()
->sort()
->prepend('')
->push('')->push($channelCount)->push('')
->toArray();
}

/**
* Determine and return the output for displaying the number of registered chanels in the CLI output.
*
* @param \Illuminate\Support\Collection $channels
* @param int $terminalWidth
* @return string
*/
protected function determineChannelCountOutput($channels, $terminalWidth)
{
$channelCountText = 'Showing ['.$channels->count().'] private channels';

$offset = $terminalWidth - mb_strlen($channelCountText) - 2;

$spaces = str_repeat(' ', $offset);

return $spaces.'<fg=blue;options=bold>Showing ['.$channels->count().'] private channels</>';
}

/**
* Get the terminal width.
*
* @return int
*/
public static function getTerminalWidth()
{
return is_null(static::$terminalWidthResolver)
? (new Terminal)->getWidth()
: call_user_func(static::$terminalWidthResolver);
}

/**
* Set a callback that should be used when resolving the terminal width.
*
* @param \Closure|null $resolver
* @return void
*/
public static function resolveTerminalWidthUsing($resolver)
{
static::$terminalWidthResolver = $resolver;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Illuminate\Database\Console\WipeCommand;
use Illuminate\Foundation\Console\AboutCommand;
use Illuminate\Foundation\Console\CastMakeCommand;
use Illuminate\Foundation\Console\ChannelListCommand;
use Illuminate\Foundation\Console\ChannelMakeCommand;
use Illuminate\Foundation\Console\ClearCompiledCommand;
use Illuminate\Foundation\Console\ComponentMakeCommand;
Expand Down Expand Up @@ -165,6 +166,7 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
protected $devCommands = [
'CacheTable' => CacheTableCommand::class,
'CastMake' => CastMakeCommand::class,
'ChannelList' => ChannelListCommand::class,
'ChannelMake' => ChannelMakeCommand::class,
'ComponentMake' => ComponentMakeCommand::class,
'ConsoleMake' => ConsoleMakeCommand::class,
Expand Down