Skip to content

Commit

Permalink
tweak event:list command
Browse files Browse the repository at this point in the history
  • Loading branch information
imanghafoori1 committed Apr 21, 2022
1 parent 31e8e14 commit 650fe8d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
31 changes: 28 additions & 3 deletions src/Illuminate/Foundation/Console/EventListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Closure;
use Illuminate\Console\Command;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use ReflectionFunction;
use Symfony\Component\Console\Attribute\AsCommand;

Expand Down Expand Up @@ -93,15 +95,15 @@ protected function getListenersOnDispatcher()
foreach ($this->getRawListeners() as $event => $rawListeners) {
foreach ($rawListeners as $rawListener) {
if (is_string($rawListener)) {
$events[$event][] = $rawListener;
$events[$event][] = $this->appendInterfaces(explode('@', $rawListener));
} 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]);
}

$events[$event][] = implode('@', $rawListener);
$events[$event][] = $this->appendInterfaces($rawListener);
}
}
}
Expand All @@ -119,7 +121,7 @@ protected function stringifyClosure(Closure $rawListener)
{
$reflection = new ReflectionFunction($rawListener);

$path = str_replace(base_path(), '', $reflection->getFileName() ?: '');
$path = str_replace([base_path(), DIRECTORY_SEPARATOR], ['', '/'], $reflection->getFileName() ?: '');

return 'Closure at: '.$path.':'.$reflection->getStartLine();
}
Expand Down Expand Up @@ -183,4 +185,27 @@ public static function resolveEventsUsing($resolver)
{
static::$eventsResolver = $resolver;
}

/**
* Adds the implemented interfaces.
*
* @param array $listener
* @return array
*/
protected function appendInterfaces(array $listener)
{
$interfaces = class_implements($listener[0]);

$listener = implode('@', $listener);

if (in_array(ShouldQueue::class, $interfaces)) {
$listener .= ' <fg=bright-blue>(ShouldQueue)</>';
}

if (in_array(ShouldBroadcast::class, $interfaces)) {
$listener .= ' <fg=bright-blue>(ShouldBroadcast)</>';
}

return $listener;
}
}
14 changes: 12 additions & 2 deletions tests/Integration/Console/Events/EventListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Console\Events;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Events\Dispatcher;
use Illuminate\Foundation\Console\EventListCommand;
use Orchestra\Testbench\TestCase;
Expand All @@ -27,9 +28,10 @@ public function testDisplayEvents()
{
$this->dispatcher->subscribe(ExampleSubscriber::class);
$this->dispatcher->listen(ExampleEvent::class, ExampleListener::class);
$this->dispatcher->listen(ExampleEvent::class, ExampleQueueListener::class);
$this->dispatcher->listen(ExampleEvent::class, fn () => '');
$closureLineNumber = __LINE__ - 1;
$closureFilePath = __FILE__;
$unixFilePath = str_replace('\\', '/', __FILE__);

$this->artisan(EventListCommand::class)
->assertSuccessful()
Expand All @@ -38,7 +40,8 @@ public function testDisplayEvents()
->expectsOutput(' ⇂ Illuminate\Tests\Integration\Console\Events\ExampleSubscriber@b')
->expectsOutput(' Illuminate\Tests\Integration\Console\Events\ExampleEvent')
->expectsOutput(' ⇂ Illuminate\Tests\Integration\Console\Events\ExampleListener')
->expectsOutput(' ⇂ Closure at: '.$closureFilePath.':'.$closureLineNumber);
->expectsOutput(' ⇂ Illuminate\Tests\Integration\Console\Events\ExampleQueueListener (ShouldQueue)')
->expectsOutput(' ⇂ Closure at: '.$unixFilePath.':'.$closureLineNumber);
}

public function testDisplayFilteredEvent()
Expand Down Expand Up @@ -91,3 +94,10 @@ public function handle()
{
}
}

class ExampleQueueListener implements ShouldQueue
{
public function handle()
{
}
}

0 comments on commit 650fe8d

Please sign in to comment.