Skip to content

Commit

Permalink
Refactor DiscoverEvents::within()
Browse files Browse the repository at this point in the history
The pairs of listeners and events can be mapped to groups of listeners keyed by the event  name.

By using `mapToDictionary` we can void zipping the pairs and then unzipping them while reducing.
  • Loading branch information
sebdesign committed Apr 5, 2019
1 parent abefa7b commit 1bf41e9
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions src/Illuminate/Foundation/Events/DiscoverEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,12 @@ class DiscoverEvents
*/
public static function within($listenerPath, $basePath)
{
$listenerEvents = collect(static::getListenerEvents((new Finder)
->files()
->in($listenerPath), $basePath));

return $listenerEvents->values()
->zip($listenerEvents->keys()->all())
->reduce(function ($carry, $listenerEventPair) {
$carry[$listenerEventPair[0]][] = $listenerEventPair[1];

return $carry;
}, []);
return collect(static::getListenerEvents((new Finder)
->files()
->in($listenerPath), $basePath))
->mapToDictionary(function ($event, $listener) {
return [$event => $listener];
})->all();
}

/**
Expand Down

0 comments on commit 1bf41e9

Please sign in to comment.