-
-
Notifications
You must be signed in to change notification settings - Fork 412
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #940 [make:subscriber] Improve MakeSubscriber to use KernelEv…
…ents constant instead hardcoded event (bdaler, jrushlow) This PR was merged into the 1.0-dev branch. Discussion ---------- [make:subscriber] Improve MakeSubscriber to use KernelEvents constant instead hardcoded event Use KernelEvents constant instead hardcoded event type in MakeSubscriber. #744 Commits ------- f724487 inline extra method 4556854 custom event class 187e0d9 fix assertion 3fa4360 add test case 416e5f4 slight refactoring 648db48 strict typing 37aec4e php-cs-fixer it up 54f3891 use generated use statements 9a397e6 Style fix. 0341a50 Use KernelEvents constant instead hardcoded event type in MakeSubscriber.
- Loading branch information
Showing
2 changed files
with
50 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 <[email protected]> | ||
|
@@ -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); | ||
} | ||
|
||
if (null !== $eventFullClassName) { | ||
$useStatements->addUseStatement($eventFullClassName); | ||
} | ||
|
@@ -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), | ||
] | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters