diff --git a/README.md b/README.md index d0e9547..1589783 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,16 @@ Checks the difference of that definition with the database and creates a migrati Asks for the block you want to extend and creates the twig extension file for you +### Generate Event Subscriber + +```shell +./bin/console frosh:make:event-subscriber +``` + +Asks for the name of event subscriber. +Please note to register service in service.xml + + ### SQL Logger for Console Debugging Prints executed SQL to the console, in such a way that they can be easily copied to other SQL tools for further diff --git a/src/Command/CreateEventSubscriber.php b/src/Command/CreateEventSubscriber.php new file mode 100644 index 0000000..0d0b7d2 --- /dev/null +++ b/src/Command/CreateEventSubscriber.php @@ -0,0 +1,130 @@ +blockCollector = $blockCollector; + $this->pluginInfos = $pluginInfos; + $this->cacheClearer = $cacheClearer; + } + + protected function configure() + { + $this + ->setDescription('Generates a event subscriber for you') + ->addArgument('pluginName', InputArgument::REQUIRED, 'Plugin Name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $pluginPath = $this->determinePluginPath($input->getArgument('pluginName')); + + $io = new SymfonyStyle($input, $output); + $question = new Question('Event subscriber name'); + $question->setAutocompleterValues(array($input->getArgument('pluginName'))); + $chosenEventSubscriberName = $io->askQuestion($question); + + if ($chosenEventSubscriberName === null) { + throw new \RuntimeException('Event subscriber name is required'); + } + + $fs = new Filesystem(); + + $subscriberFolderPath = $pluginPath . '/Service/'; + $subscriberPath = $subscriberFolderPath . $chosenEventSubscriberName . '.php'; + + if (!file_exists($subscriberFolderPath)) { + $fs->mkdir($subscriberFolderPath); + } + + if (!file_exists(dirname($subscriberPath))) { + $fs->mkdir(dirname($subscriberPath)); + } + if (!file_exists($subscriberPath)) { + $tpl = << 'eventFunction', + ]; + } + public static function eventFunction(): array + { + return array(); + } + + +} + +TPL; + $content = str_replace( + [ + '###PLUGINNAME###', + '###SERVICENAME###' + ], + [ + $input->getArgument('pluginName'), + $chosenEventSubscriberName + ], + $tpl + ); + + $fs->dumpFile($subscriberPath, $content); + + $io->success(sprintf('Created file at "%s"', $subscriberPath)); + } + + $this->cacheClearer->clear(); + $io->info('Cleared cache'); + + + return 0; + } + + private function determinePluginPath(string $name): string + { + foreach ($this->pluginInfos as $pluginInfo) { + if ($pluginInfo['name'] !== $name) { + continue; + } + + $reflectionClass = new \ReflectionClass($pluginInfo['baseClass']); + + return dirname($reflectionClass->getFileName()); + } + + throw new \RuntimeException(sprintf('Cannot find plugin by name "%s"', $name)); + } +}