Skip to content

[make:subscriber] Improve MakeSubscriber to use KernelEvents constant instead hardcoded event #940

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 10 commits into from
Jul 13, 2022
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
22 changes: 21 additions & 1 deletion src/Maker/MakeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -87,6 +88,14 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
EventSubscriberInterface::class,
]);

// Determine if we use a KernelEvents::CONSTANT or custom even name
if (null !== ($eventConstant = $this->getEventConstant($event))) {
$useStatements->addUseStatement(KernelEvents::class);
$eventName = $eventConstant;
} else {
$eventName = class_exists($event) ? sprintf('%s::class', $eventClassName) : sprintf('\'%s\'', $event);
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we could move $eventFullClassName and $eventClassName into this else. And also move the $useStatements->addUseStatement($eventFullClassName); into this as well. It's all just a bit spread out right now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we can without adding more noise.. we have to handle a custom event like \Symfony\MakerBundle\Generator::class and assert that we subscribe to Generator::class => onGenerator. Side note - just added a testcase for this situation


if (null !== $eventFullClassName) {
$useStatements->addUseStatement($eventFullClassName);
}
Expand All @@ -96,7 +105,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
'event/Subscriber.tpl.php',
[
'use_statements' => $useStatements,
'event' => class_exists($event) ? sprintf('%s::class', $eventClassName) : sprintf('\'%s\'', $event),
'event' => $eventName,
'event_arg' => $eventClassName ? sprintf('%s $event', $eventClassName) : '$event',
'method_name' => class_exists($event) ? Str::asEventMethod($eventClassName) : Str::asEventMethod($event),
]
Expand All @@ -115,4 +124,15 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
public function configureDependencies(DependencyBuilder $dependencies): void
{
}

private function getEventConstant(string $event): ?string
{
$constants = (new \ReflectionClass(KernelEvents::class))->getConstants();

if (false !== ($name = array_search($event, $constants, true))) {
return sprintf('KernelEvents::%s', $name);
}

return null;
}
}
30 changes: 29 additions & 1 deletion tests/Maker/MakeSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected function getMakerClass(): string
return MakeSubscriber::class;
}

public function getTestDetails()
public function getTestDetails(): \Generator
{
yield 'it_makes_subscriber_for_known_event' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
Expand All @@ -34,6 +34,29 @@ public function getTestDetails()
'kernel.request',
]
);

self::assertStringContainsString(
'KernelEvents::REQUEST => \'onKernelRequest\'',
file_get_contents($runner->getPath('src/EventSubscriber/FooBarSubscriber.php'))
);
}),
];

yield 'it_makes_subscriber_for_custom_event_class' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->runMaker(
[
// subscriber name
'FooBar',
// event name
\Symfony\Bundle\MakerBundle\Generator::class,
]
);

self::assertStringContainsString(
'Generator::class => \'onGenerator\'',
file_get_contents($runner->getPath('src/EventSubscriber/FooBarSubscriber.php'))
);
}),
];

Expand All @@ -47,6 +70,11 @@ public function getTestDetails()
'foo.unknown_event',
]
);

self::assertStringContainsString(
'\'foo.unknown_event\' => \'onFooUnknownEvent\',',
file_get_contents($runner->getPath('src/EventSubscriber/FooBarSubscriber.php'))
);
}),
];
}
Expand Down