Skip to content

[make:listener] Match event name against active events class/id #1579

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 2 commits into from
Aug 29, 2024
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
33 changes: 33 additions & 0 deletions src/Maker/MakeListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
Expand Down Expand Up @@ -102,6 +103,38 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
$event = $io->askQuestion($question);
$input->setArgument('event', $event);
}

$event = $input->getArgument('event');
if (null === $this->getEventConstant($event) && null === $this->eventRegistry->getEventClassName($event)) {
$eventList = $this->eventRegistry->getAllActiveEvents();
$eventFQCNList = array_filter(array_map($this->eventRegistry->getEventClassName(...), $eventList), fn ($eventFQCN) => \is_string($eventFQCN));
$eventIdAndFQCNList = array_unique(array_merge($eventList, $eventFQCNList));
$suggestionList = [];

foreach ($eventIdAndFQCNList as $eventSuggestion) {
if (levenshtein($event, Str::getShortClassName($eventSuggestion)) < 3) {
$suggestionList[] = $eventSuggestion;
}
}

if (!$suggestionList) {
return;
}

if (1 === \count($suggestionList)) {
$question = new ConfirmationQuestion(\sprintf('<fg=green>Did you mean</> <fg=yellow>"%s"</> <fg=green>?</>', $suggestionList[0]), false);
$input->setArgument('event', $io->askQuestion($question) ? $suggestionList[0] : $event);

return;
}

$io->writeln(' <fg=yellow>Did you mean one of these events?</>');
$io->listing($suggestionList);
$question = new Question(\sprintf(' <fg=green>%s</>', $command->getDefinition()->getArgument('event')->getDescription()), $event);
$question->setAutocompleterValues(array_merge($suggestionList, [$event]));

$input->setArgument('event', $io->askQuestion($question));
}
}

public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
Expand Down
76 changes: 76 additions & 0 deletions tests/Maker/MakeListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,82 @@ public function getTestDetails(): \Generator
);
}),
];

yield 'it_makes_listener_for_known_event_by_id' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
// listener name
'FooListener',
// event name
'kernel.request',
// accept the suggestion
'y',
]
);
self::assertFileEquals(
self::EXPECTED_LISTENER_PATH.'FooListener.php',
$runner->getPath('src/EventListener/FooListener.php')
);
}),
];

yield 'it_makes_listener_for_known_event_by_short_class_name' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
// listener name
'BarListener',
// event name
'RequestEvent',
// accept the suggestion
'y',
]
);
self::assertFileEquals(
self::EXPECTED_LISTENER_PATH.'BarListener.php',
$runner->getPath('src/EventListener/BarListener.php')
);
}),
];

yield 'it_makes_listener_for_known_event_by_id_with_2_letters_typo' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
// listener name
'FooListener',
// event name
'kernem.reques',
// accept the suggestion
'y',
]
);
self::assertFileEquals(
self::EXPECTED_LISTENER_PATH.'FooListener.php',
$runner->getPath('src/EventListener/FooListener.php')
);
}),
];

yield 'it_makes_listener_for_known_event_by_short_class_name_with_2_letters_typo' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
// listener name
'BarListener',
// event name
'RequstEveny',
// accept the suggestion
'y',
]
);
self::assertFileEquals(
self::EXPECTED_LISTENER_PATH.'BarListener.php',
$runner->getPath('src/EventListener/BarListener.php')
);
}),
];
}

protected function getMakerClass(): string
Expand Down
15 changes: 15 additions & 0 deletions tests/fixtures/make-listener/tests/EventListener/BarListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;

final class BarListener
{
#[AsEventListener(event: RequestEvent::class)]
public function onRequestEvent(RequestEvent $event): void
{
// ...
}
}
Loading