Skip to content

Commit

Permalink
make event:list command colorful
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Apr 20, 2022
1 parent 84c5e35 commit 2a13769
Showing 1 changed file with 40 additions and 30 deletions.
70 changes: 40 additions & 30 deletions src/Illuminate/Foundation/Console/EventListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function handle()
$this->printSeparator($width);
}

$this->line('');
$this->line(PHP_EOL);
}

/**
Expand Down Expand Up @@ -94,17 +94,17 @@ protected function getListenersOnDispatcher()
$events = [];

foreach ($this->getRawListeners() as $event => $rawListeners) {
foreach ($rawListeners as $rawListener) {
if (is_string($rawListener)) {
$events[$event][] = [$rawListener, 'handle'];
} elseif ($rawListener instanceof Closure) {
$events[$event][] = $this->stringifyClosure($rawListener);
} elseif (is_array($rawListener) && count($rawListener) === 2) {
if (is_object($rawListener[0])) {
$rawListener[0] = get_class($rawListener[0]);
foreach ($rawListeners as $listener) {
if (is_string($listener)) {
$events[$event][] = [$listener, 'handle'];
} elseif ($listener instanceof Closure) {
$events[$event][] = $this->stringifyClosure($listener);
} elseif (is_array($listener) && count($listener) === 2) {
if (is_object($listener[0])) {
$listener[0] = get_class($listener[0]);
}

$events[$event][] = $rawListener;
$events[$event][] = $listener;
}
}
}
Expand All @@ -127,6 +127,19 @@ protected function stringifyClosure(Closure $rawListener)
return $path.':'.$reflection->getStartLine();
}

/**
* Remove commandline styling tags from the string.
*
* @param $string
* @return string
*/
public function getPrintable($string)
{
$string = str_replace('</>', '', $string);

return preg_replace('/<fg=(.*?)>/', '', $string);
}

/**
* Filter the given events using the provided event name filter.
*
Expand Down Expand Up @@ -186,37 +199,42 @@ protected function printsListeners($listeners, int $width)
{
$colorings = [
'@' => '<fg=white>@</>',
'Closure at: ' => '<fg=blue>Closure at: </>',
' - ' => '<fg=white> - </>',
'(ShouldQueue)' => '<fg=blue>(ShouldQueue)</>',
'(ShouldBroadcast)' => '<fg=blue>(ShouldBroadcast)</>',
'.php:' => '.php<fg=white>:</>',
' ... ' => '<fg=gray>...</>',
' Closure' => '<fg=blue> Closure</>',
' Class ' => '<fg=bright-cyan> Class </>',
'...ShouldQueue' => '<fg=gray>...</><fg=red>ShouldQueue</>',
'...ShouldBroadcast' => '<fg=gray>...</><fg=red>ShouldBroadcast</>',
];

foreach ($listeners as $listener) {
if (is_string($listener)) {
$listener = 'Closure at: '.$listener;
$listener = ' Closure '.$this->getDots($width, $listener).' '.$listener;
}

$shouldQueue = '';
$shouldBroadcast = '';
if (is_array($listener)) {
if ($this->implements($listener[0], ShouldQueue::class)) {
$listener[1] .= ' (ShouldQueue)';
$shouldQueue = '...ShouldQueue';
}

if ($this->implements($listener[0], ShouldBroadcast::class)) {
$listener[1] .= ' (ShouldBroadcast)';
$shouldBroadcast = '...ShouldBroadcast';
}

$listener = $listener[0].'@'.$listener[1];
$listener = ' Class '.$shouldQueue.$shouldBroadcast.$this->getDots($width, $listener.$shouldQueue.$shouldBroadcast).' '.$listener;
}

$listener = ' - '.$listener;
$listener = ' '.$listener;
if (strlen($this->getPrintable($listener)) >= $width) {
$listener = Str::limit($this->getPrintable($listener), $width - 3);
}

Str::of($listener)
->replace(array_keys($colorings), array_values($colorings))
->pipe(fn ($line) => $this->line($line, 'options=bold;fg=bright-blue'));
->pipe(fn ($line) => $this->line($line, 'fg=bright-blue'));
}
}

Expand All @@ -228,7 +246,7 @@ protected function printsListeners($listeners, int $width)
*/
protected function printEvent(string $event)
{
$this->line(' '.$event, 'fg=yellow');
$this->line(' '.$event, 'fg=bright-yellow');
}

/**
Expand All @@ -243,16 +261,8 @@ protected function implements(string $class, string $interface)
return in_array($interface, class_implements($class));
}

/**
* Remove commandline styling tags from the string.
*
* @param $line
* @return string
*/
protected function getPrintable($line)
protected function getDots(int $width, string $listener): string
{
$line = str_replace('</>', '', $line);

return preg_replace('/<fg=(.*?)>/', '', $line);
return '<fg=gray>'.str_repeat('.', max($width - strlen($listener) - 16, 3)).'</>';
}
}

0 comments on commit 2a13769

Please sign in to comment.