From 07749cf32f6076d60b7976dc17fdf4a97f1f52af Mon Sep 17 00:00:00 2001 From: Caleb White Date: Fri, 9 May 2025 13:26:22 -0500 Subject: [PATCH] feat: support iterables for event discovery paths --- .../Configuration/ApplicationBuilder.php | 6 ++-- .../Foundation/Events/DiscoverEvents.php | 15 ++++++--- .../Providers/EventServiceProvider.php | 33 +++++++++---------- .../Foundation/DiscoverEventsTest.php | 27 +++++++++++++++ 4 files changed, 56 insertions(+), 25 deletions(-) diff --git a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php index f8506bc07dd4..e0ffdd534495 100644 --- a/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php +++ b/src/Illuminate/Foundation/Configuration/ApplicationBuilder.php @@ -92,12 +92,12 @@ public function withProviders(array $providers = [], bool $withBootstrapProvider /** * Register the core event service provider for the application. * - * @param array|bool $discover + * @param iterable|bool $discover * @return $this */ - public function withEvents(array|bool $discover = []) + public function withEvents(iterable|bool $discover = true) { - if (is_array($discover) && count($discover) > 0) { + if (is_iterable($discover)) { AppEventServiceProvider::setEventDiscoveryPaths($discover); } diff --git a/src/Illuminate/Foundation/Events/DiscoverEvents.php b/src/Illuminate/Foundation/Events/DiscoverEvents.php index e2933f937872..35f244837bce 100644 --- a/src/Illuminate/Foundation/Events/DiscoverEvents.php +++ b/src/Illuminate/Foundation/Events/DiscoverEvents.php @@ -2,6 +2,7 @@ namespace Illuminate\Foundation\Events; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Reflector; use Illuminate\Support\Str; @@ -16,19 +17,23 @@ class DiscoverEvents /** * The callback to be used to guess class names. * - * @var callable(SplFileInfo, string): string|null + * @var (callable(SplFileInfo, string): class-string)|null */ public static $guessClassNamesUsingCallback; /** * Get all of the events and listeners by searching the given listener directory. * - * @param string $listenerPath + * @param array|string $listenerPath * @param string $basePath * @return array */ public static function within($listenerPath, $basePath) { + if (Arr::wrap($listenerPath) === []) { + return []; + } + $listeners = new Collection(static::getListenerEvents( Finder::create()->files()->in($listenerPath), $basePath )); @@ -51,7 +56,7 @@ public static function within($listenerPath, $basePath) /** * Get all of the listeners and their corresponding events. * - * @param iterable $listeners + * @param iterable $listeners * @param string $basePath * @return array */ @@ -91,7 +96,7 @@ protected static function getListenerEvents($listeners, $basePath) * * @param \SplFileInfo $file * @param string $basePath - * @return string + * @return class-string */ protected static function classFromFile(SplFileInfo $file, $basePath) { @@ -111,7 +116,7 @@ protected static function classFromFile(SplFileInfo $file, $basePath) /** * Specify a callback to be used to guess class names. * - * @param callable(SplFileInfo, string): string $callback + * @param callable(SplFileInfo, string): class-string $callback * @return void */ public static function guessClassNamesUsing(callable $callback) diff --git a/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php b/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php index d5074505302d..ac7b3ec7838b 100644 --- a/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php +++ b/src/Illuminate/Foundation/Support/Providers/EventServiceProvider.php @@ -6,8 +6,8 @@ use Illuminate\Auth\Listeners\SendEmailVerificationNotification; use Illuminate\Foundation\Events\DiscoverEvents; use Illuminate\Support\Arr; -use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; +use Illuminate\Support\LazyCollection; use Illuminate\Support\ServiceProvider; class EventServiceProvider extends ServiceProvider @@ -43,7 +43,7 @@ class EventServiceProvider extends ServiceProvider /** * The configured event discovery paths. * - * @var array|null + * @var iterable|null */ protected static $eventDiscoveryPaths; @@ -145,25 +145,23 @@ public function shouldDiscoverEvents() */ public function discoverEvents() { - return (new Collection($this->discoverEventsWithin())) + return (new LazyCollection($this->discoverEventsWithin())) ->flatMap(function ($directory) { return glob($directory, GLOB_ONLYDIR); }) ->reject(function ($directory) { return ! is_dir($directory); }) - ->reduce(function ($discovered, $directory) { - return array_merge_recursive( - $discovered, - DiscoverEvents::within($directory, $this->eventDiscoveryBasePath()) - ); - }, []); + ->pipe(fn ($directories) => DiscoverEvents::within( + $directories->all(), + $this->eventDiscoveryBasePath(), + )); } /** * Get the listener directories that should be used to discover events. * - * @return array + * @return iterable */ protected function discoverEventsWithin() { @@ -175,23 +173,24 @@ protected function discoverEventsWithin() /** * Add the given event discovery paths to the application's event discovery paths. * - * @param string|array $paths + * @param string|iterable $paths * @return void */ - public static function addEventDiscoveryPaths(array|string $paths) + public static function addEventDiscoveryPaths(iterable|string $paths) { - static::$eventDiscoveryPaths = array_values(array_unique( - array_merge(static::$eventDiscoveryPaths, Arr::wrap($paths)) - )); + static::$eventDiscoveryPaths = (new LazyCollection(static::$eventDiscoveryPaths)) + ->merge(is_string($paths) ? [$paths] : $paths) + ->unique() + ->values(); } /** * Set the globally configured event discovery paths. * - * @param array $paths + * @param iterable $paths * @return void */ - public static function setEventDiscoveryPaths(array $paths) + public static function setEventDiscoveryPaths(iterable $paths) { static::$eventDiscoveryPaths = $paths; } diff --git a/tests/Integration/Foundation/DiscoverEventsTest.php b/tests/Integration/Foundation/DiscoverEventsTest.php index 116f2374afba..417358539e24 100644 --- a/tests/Integration/Foundation/DiscoverEventsTest.php +++ b/tests/Integration/Foundation/DiscoverEventsTest.php @@ -57,6 +57,33 @@ class_alias(UnionListener::class, 'Tests\Integration\Foundation\Fixtures\EventDi ], $events); } + public function testMultipleDirectoriesCanBeDiscovered(): void + { + $events = DiscoverEvents::within([ + __DIR__.'/Fixtures/EventDiscovery/Listeners', + __DIR__.'/Fixtures/EventDiscovery/UnionListeners', + ], getcwd()); + + $this->assertEquals([ + EventOne::class => [ + Listener::class.'@handle', + Listener::class.'@handleEventOne', + UnionListener::class.'@handle', + ], + EventTwo::class => [ + Listener::class.'@handleEventTwo', + UnionListener::class.'@handle', + ], + ], $events); + } + + public function testNoExceptionForEmptyDirectories(): void + { + $events = DiscoverEvents::within([], getcwd()); + + $this->assertEquals([], $events); + } + public function testEventsCanBeDiscoveredUsingCustomClassNameGuessing() { DiscoverEvents::guessClassNamesUsing(function (SplFileInfo $file, $basePath) {