Skip to content

Commit 0341a50

Browse files
bdalerjrushlow
authored andcommitted
Use KernelEvents constant instead hardcoded event type in MakeSubscriber.
1 parent 63c52a1 commit 0341a50

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/Maker/MakeSubscriber.php

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\Console\Input\InputArgument;
2424
use Symfony\Component\Console\Input\InputInterface;
2525
use Symfony\Component\Console\Question\Question;
26+
use Symfony\Component\HttpKernel\KernelEvents;
2627
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2728

2829
/**
@@ -96,8 +97,10 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
9697
'event/Subscriber.tpl.php',
9798
[
9899
'use_statements' => $useStatements,
99-
'event' => class_exists($event) ? sprintf('%s::class', $eventClassName) : sprintf('\'%s\'', $event),
100+
'event' => class_exists($event) ? sprintf('%s::class', $eventClassName) : $this->getEventConstant($event),
100101
'event_arg' => $eventClassName ? sprintf('%s $event', $eventClassName) : '$event',
102+
'import_constant_class' => $this->isNeedImportConstantsClass($event),
103+
'event_full_class_name' => $eventFullClassName,
101104
'method_name' => class_exists($event) ? Str::asEventMethod($eventClassName) : Str::asEventMethod($event),
102105
]
103106
);
@@ -115,4 +118,47 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
115118
public function configureDependencies(DependencyBuilder $dependencies): void
116119
{
117120
}
121+
122+
/**
123+
* @param $event
124+
* @return string
125+
*/
126+
private function getEventConstant($event): string
127+
{
128+
$constants = $this->getKernelEventsConstants();
129+
130+
foreach ($constants as $name => $value) {
131+
if ($value === $event) {
132+
return 'KernelEvents::' . $name;
133+
}
134+
}
135+
136+
return sprintf('\'%s\'', $event);
137+
}
138+
139+
/**
140+
* @param $event
141+
* @return bool
142+
*/
143+
private function isNeedImportConstantsClass($event): bool
144+
{
145+
$constants = $this->getKernelEventsConstants();
146+
147+
foreach ($constants as $name => $value) {
148+
if ($value === $event) {
149+
return true;
150+
}
151+
}
152+
153+
return false;
154+
}
155+
156+
/**
157+
* @return array
158+
*/
159+
private function getKernelEventsConstants(): array
160+
{
161+
$class = new \ReflectionClass(KernelEvents::class);
162+
return $class->getConstants();
163+
}
118164
}

0 commit comments

Comments
 (0)