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

[8.x] Support union types on event discovery #38383

Merged
merged 5 commits into from
Aug 14, 2021
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
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)
{
//
}
}