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

[5.x] Support unions in addon event listener discovery #11015

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
75 changes: 45 additions & 30 deletions src/Providers/AddonServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Reflector;
use Illuminate\Support\ServiceProvider;
use Statamic\Actions\Action;
use Statamic\Dictionaries\Dictionary;
Expand Down Expand Up @@ -225,36 +226,11 @@ public function bootAddon()

public function bootEvents()
{
collect($this->autoloadFilesFromFolder('Listeners'))
->mapWithKeys(function ($class) {
$reflection = new \ReflectionClass($class);

if (
$reflection->hasMethod('handle')
&& isset($reflection->getMethod('handle')->getParameters()[0])
&& $reflection->getMethod('handle')->getParameters()[0]->hasType()
) {
$event = $reflection->getMethod('handle')->getParameters()[0]->getType()->getName();

return [$event => $class];
}

if (
$reflection->hasMethod('__invoke')
&& isset($reflection->getMethod('__invoke')->getParameters()[0])
&& $reflection->getMethod('__invoke')->getParameters()[0]->hasType()
) {
$event = $reflection->getMethod('__invoke')->getParameters()[0]->getType()->getName();

return [$event => $class];
}

return [];
})
->filter()
->merge(collect($this->listen)->flatMap(fn ($listeners, $event) => collect($listeners)->mapWithKeys(fn ($listener) => [$event => $listener])))
->unique()
->each(fn ($listener, $event) => Event::listen($event, $listener));
$this->getEventListeners()->each(function ($listeners, $event) {
foreach ($listeners as $listener) {
Event::listen($event, $listener);
}
});

$subscribers = collect($this->subscribe)
->merge($this->autoloadFilesFromFolder('Subscribers'))
Expand All @@ -267,6 +243,45 @@ public function bootEvents()
return $this;
}

private function getEventListeners()
{
$arr = [];

foreach ($this->discoverListenerEvents() as $listener => $events) {
foreach ($events as $event) {
$arr[$event][] = $listener;
}
}

foreach ($this->listen as $event => $listeners) {
foreach ($listeners as $listener) {
$arr[$event][] = $listener;
}
}

return collect($arr);
}

private function discoverListenerEvents()
{
return collect($this->autoloadFilesFromFolder('Listeners'))->mapWithKeys(function ($class) {
$listener = new \ReflectionClass($class);
$events = [];

foreach ($listener->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
if ((! Str::is('handle*', $method->name) && ! Str::is('__invoke', $method->name)) || ! isset($method->getParameters()[0])) {
continue;
}

$key = Str::is(['__invoke', 'handle'], $method->name) ? $class : $class.'@'.$method->name;

$events[$key] = Reflector::getParameterClassNames($method->getParameters()[0]);
}

return $events;
});
}

protected function bootTags()
{
$tags = collect($this->tags)
Expand Down
Loading