10000 [12.x] perf: support iterables for event discovery paths by calebdw · Pull Request #55699 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] perf: support iterables for event discovery paths #55699

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

Merged
merged 1 commit into from
May 10, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, string>|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);
}

Expand Down
15 changes: 10 additions & 5 deletions src/Illuminate/Foundation/Events/Discover 10000 Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Foundation\Events;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Reflector;
use Illuminate\Support\Str;
Expand All @@ -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<int, string>|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
));
Expand All @@ -51,7 +56,7 @@ public static function within($listenerPath, $basePath)
/**
* Get all of the listeners and their corresponding events.
*
* @param iterable $listeners
* @param iterable<string, SplFileInfo> $listeners
* @param string $basePath
* @return array
*/
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +43,7 @@ class EventServiceProvider extends ServiceProvider
/**
* The configured event discovery paths.
*
* @var array|null
* @var iterable<int, string>|null
*/
protected static $eventDiscoveryPaths;

Expand Down Expand Up @@ -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())
);
}, []);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array_merge_recursive can be totally removed? Does this not break anything?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes because the DiscoverEvents::within() does the same thing with its double foreach loops:

public static function within($listenerPath, $basePath)
{
$listeners = new Collection(static::getListenerEvents(
Finder::create()->files()->in($listenerPath), $basePath
));
$discoveredEvents = [];
foreach ($listeners as $listener => $events) {
foreach ($events as $event) {
if (! isset($discoveredEvents[$event])) {
$discoveredEvents[$event] = [];
}
$discoveredEvents[$event][] = $listener;
}
}
return $discoveredEvents;
}

The Finder::in() method can accept an array and we're able to process all the listeners at once instead of repeated calls for every directory and then having to merge all of the arrays into one large array

->pipe(fn ($directories) => DiscoverEvents::within(
$directories->all(),
$this->eventDiscoveryBasePath(),
));
}

/**
* Get the listener directories that should be used to discover events.
*
* @return array
* @return iterable<int, string>
*/
protected function discoverEventsWithin()
{
Expand All @@ -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<int, string> $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<int, string> $paths
* @return void
*/
public static function setEventDiscoveryPaths(array $paths)
public static function setEventDiscoveryPaths(iterable $paths)
{
static::$eventDiscoveryPaths = $paths;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Integration/Foundation/DiscoverEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
0