Skip to content

Commit 1eed738

Browse files
authored
Add ability to customize class resolution in event discovery (#48031)
1 parent 7ee6d0d commit 1eed738

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Illuminate/Foundation/Events/DiscoverEvents.php

+22
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
class DiscoverEvents
1414
{
15+
/**
16+
* The callback to be used to guess class names.
17+
*
18+
* @var callable(SplFileInfo, string): string|null
19+
*/
20+
public static $guessClassNamesUsingCallback;
21+
1522
/**
1623
* Get all of the events and listeners by searching the given listener directory.
1724
*
@@ -87,6 +94,10 @@ protected static function getListenerEvents($listeners, $basePath)
8794
*/
8895
protected static function classFromFile(SplFileInfo $file, $basePath)
8996
{
97+
if (static::$guessClassNamesUsingCallback) {
98+
return call_user_func(static::$guessClassNamesUsingCallback, $file, $basePath);
99+
}
100+
90101
$class = trim(Str::replaceFirst($basePath, '', $file->getRealPath()), DIRECTORY_SEPARATOR);
91102

92103
return str_replace(
@@ -95,4 +106,15 @@ protected static function classFromFile(SplFileInfo $file, $basePath)
95106
ucfirst(Str::replaceLast('.php', '', $class))
96107
);
97108
}
109+
110+
/**
111+
* Specify a callback to be used to guess class names.
112+
*
113+
* @param callable(SplFileInfo, string): string $callback
114+
* @return void
115+
*/
116+
public static function guessClassNamesUsing(callable $callback)
117+
{
118+
static::$guessClassNamesUsingCallback = $callback;
119+
}
98120
}

tests/Integration/Foundation/DiscoverEventsTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,25 @@
33
namespace Illuminate\Tests\Integration\Foundation;
44

55
use Illuminate\Foundation\Events\DiscoverEvents;
6+
use Illuminate\Support\Str;
67
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventOne;
78
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Events\EventTwo;
89
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\AbstractListener;
910
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener;
1011
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\ListenerInterface;
1112
use Illuminate\Tests\Integration\Foundation\Fixtures\EventDiscovery\UnionListeners\UnionListener;
1213
use Orchestra\Testbench\TestCase;
14+
use SplFileInfo;
1315

1416
class DiscoverEventsTest extends TestCase
1517
{
18+
protected function tearDown(): void
19+
{
20+
DiscoverEvents::$guessClassNamesUsingCallback = null;
21+
22+
parent::tearDown();
23+
}
24+
1625
public function testEventsCanBeDiscovered()
1726
{
1827
class_alias(Listener::class, 'Tests\Integration\Foundation\Fixtures\EventDiscovery\Listeners\Listener');
@@ -47,4 +56,29 @@ class_alias(UnionListener::class, 'Tests\Integration\Foundation\Fixtures\EventDi
4756
],
4857
], $events);
4958
}
59+
60+
public function testEventsCanBeDiscoveredUsingCustomClassNameGuessing()
61+
{
62+
DiscoverEvents::guessClassNamesUsing(function (SplFileInfo $file, $basePath) {
63+
return Str::of($file->getRealPath())
64+
->after($basePath.DIRECTORY_SEPARATOR)
65+
->before('.php')
66+
->replace(DIRECTORY_SEPARATOR, '\\')
67+
->ucfirst()
68+
->prepend('Illuminate\\')
69+
->toString();
70+
});
71+
72+
$events = DiscoverEvents::within(__DIR__.'/Fixtures/EventDiscovery/Listeners', getcwd());
73+
74+
$this->assertEquals([
75+
EventOne::class => [
76+
Listener::class.'@handle',
77+
Listener::class.'@handleEventOne',
78+
],
79+
EventTwo::class => [
80+
Listener::class.'@handleEventTwo',
81+
],
82+
], $events);
83+
}
5084
}

0 commit comments

Comments
 (0)