Skip to content

Commit

Permalink
Support union types on event discovery (#38383)
Browse files Browse the repository at this point in the history
* support union types on event discovery

* add test

* add test

* fix tests

* extract method
  • Loading branch information
taylorotwell authored Aug 14, 2021
1 parent a668e6a commit 8c65b3d
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 5 deletions.
22 changes: 17 additions & 5 deletions src/Illuminate/Foundation/Events/DiscoverEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ class DiscoverEvents
*/
public static function within($listenerPath, $basePath)
{
return collect(static::getListenerEvents(
$listeners = collect(static::getListenerEvents(
(new Finder)->files()->in($listenerPath), $basePath
))->mapToDictionary(function ($event, $listener) {
return [$event => $listener];
})->all();
));

$discoveredEvents = [];

foreach ($listeners as $listener => $events) {
foreach ($events as $event) {
if (! isset($discoveredEvents[$event])) {
$discoveredEvents[$event] = [];
}

$discoveredEvents[$event][] = $listener;
}
}

return $discoveredEvents;
}

/**
Expand Down Expand Up @@ -59,7 +71,7 @@ protected static function getListenerEvents($listeners, $basePath)
}

$listenerEvents[$listener->name.'@'.$method->name] =
Reflector::getParameterClassName($method->getParameters()[0]);
Reflector::getParameterClassNames($method->getParameters()[0]);
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/Illuminate/Support/Reflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ReflectionClass;
use ReflectionMethod;
use ReflectionNamedType;
use ReflectionUnionType;

class Reflector
{
Expand Down Expand Up @@ -69,6 +70,45 @@ public static function getParameterClassName($parameter)
return;
}

return static::getTypeName($parameter, $type);
}

/**
* Get the class names of the given parameter's type, including union types.
*
* @param \ReflectionParameter $parameter
* @return array
*/
public static function getParameterClassNames($parameter)
{
$type = $parameter->getType();

if (! $type instanceof ReflectionUnionType) {
return [static::getParameterClassName($parameter)];
}

$unionTypes = [];

foreach ($type->getTypes() as $listedType) {
if (! $listedType instanceof ReflectionNamedType || $listedType->isBuiltin()) {
continue;
}

$unionTypes[] = static::getTypeName($parameter, $listedType);
}

return $unionTypes;
}

/**
* Get the given type's class name.
*
* @param \ReflectionParameter $parameter
* @param \ReflectionNamedType $type
* @return string
*/
protected static function getTypeName($parameter, $type)
{
$name = $type->getName();

if (! is_null($class = $parameter->getDeclaringClass())) {
Expand Down
21 changes: 21 additions & 0 deletions tests/Integration/Foundation/DiscoverEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\AbstractListener;
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener;
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\ListenerInterface;
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\UnionListeners\UnionListener;
use Orchestra\Testbench\TestCase;

class DiscoverEventsTest extends TestCase
Expand All @@ -30,4 +31,24 @@ class_alias(ListenerInterface::class, 'Tests\Integration\Foundation\Fixtures\Eve
],
], $events);
}

public function testUnionEventsCanBeDiscovered()
{
if (version_compare(phpversion(), '8.0.0', '<')) {
$this->markTestSkipped('Test uses union types.');
}

class_alias(UnionListener::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\UnionListeners\UnionListener');

$events = DiscoverEvents::within(__DIR__.'/Fixtures/EventDiscovery/UnionListeners', getcwd());

$this->assertEquals([
EventOne::class => [
UnionListener::class.'@handle',
],
EventTwo::class => [
UnionListener::class.'@handle',
],
], $events);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\UnionListeners;

use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventOne;
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventTwo;

class UnionListener
{
public function handle(EventOne|EventTwo $event)
{
//
}
}

0 comments on commit 8c65b3d

Please sign in to comment.