From ad8b6b02b7f91a49f0800d8016059e69f26f7899 Mon Sep 17 00:00:00 2001 From: Philipp Rien <53903379+HashtagAssist@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:17:45 +0200 Subject: [PATCH 1/5] Create CreateService.php Create a console command for creating service: ddev exec bin/console frosh:make:service --- src/Command/CreateService.php | 130 ++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/Command/CreateService.php diff --git a/src/Command/CreateService.php b/src/Command/CreateService.php new file mode 100644 index 0000000..458ee28 --- /dev/null +++ b/src/Command/CreateService.php @@ -0,0 +1,130 @@ +blockCollector = $blockCollector; + $this->pluginInfos = $pluginInfos; + $this->cacheClearer = $cacheClearer; + } + + protected function configure() + { + $this + ->setDescription('Generates a service 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('Service Name'); + $question->setAutocompleterValues(array($input->getArgument('pluginName'))); + $chosenFile = $io->askQuestion($question); + + if ($chosenFile === null) { + throw new \RuntimeException('Service Name is required'); + } + + $fs = new Filesystem(); + + $serviceFolderPath = $pluginPath . '/Service/'; + $servicePath = $serviceFolderPath . $chosenFile . '.php'; + + if (!file_exists($serviceFolderPath)) { + $fs->mkdir($serviceFolderPath); + } + + if (!file_exists(dirname($servicePath))) { + $fs->mkdir(dirname($servicePath)); + } + if (!file_exists($servicePath)) { + $tpl = << 'eventFunction', + ]; + } + public static function eventFunction(): array + { + return array(); + } + + +} + +TPL; + $content = str_replace( + [ + '###PLUGINNAME###', + '###SERVICENAME###' + ], + [ + $input->getArgument('pluginName'), + $chosenFile + ], + $tpl + ); + + $fs->dumpFile($servicePath, $content); + + $io->success(sprintf('Created file at "%s"', $servicePath)); + } + + $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)); + } +} From db59e6c46eaea54cd75d97a8ac5df07b296ffcd6 Mon Sep 17 00:00:00 2001 From: Philipp Rien <53903379+HashtagAssist@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:20:34 +0200 Subject: [PATCH 2/5] Change var name CreateService.php chosenFile -> chosenServiceName --- src/Command/CreateService.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Command/CreateService.php b/src/Command/CreateService.php index 458ee28..b9b8b38 100644 --- a/src/Command/CreateService.php +++ b/src/Command/CreateService.php @@ -42,16 +42,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $io = new SymfonyStyle($input, $output); $question = new Question('Service Name'); $question->setAutocompleterValues(array($input->getArgument('pluginName'))); - $chosenFile = $io->askQuestion($question); + $chosenServiceName = $io->askQuestion($question); - if ($chosenFile === null) { + if ($chosenServiceName === null) { throw new \RuntimeException('Service Name is required'); } $fs = new Filesystem(); $serviceFolderPath = $pluginPath . '/Service/'; - $servicePath = $serviceFolderPath . $chosenFile . '.php'; + $servicePath = $serviceFolderPath . $chosenServiceName . '.php'; if (!file_exists($serviceFolderPath)) { $fs->mkdir($serviceFolderPath); @@ -96,7 +96,7 @@ public static function eventFunction(): array ], [ $input->getArgument('pluginName'), - $chosenFile + $chosenServiceName ], $tpl ); From 578d3a9b7e92e67e20c04aaf770f90689f376e56 Mon Sep 17 00:00:00 2001 From: Philipp Rien <53903379+HashtagAssist@users.noreply.github.com> Date: Tue, 19 Sep 2023 08:42:25 +0200 Subject: [PATCH 3/5] Update and rename CreateService.php to CreateEventSubscriber.php --- ...eService.php => CreateEventSubscriber.php} | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) rename src/Command/{CreateService.php => CreateEventSubscriber.php} (74%) diff --git a/src/Command/CreateService.php b/src/Command/CreateEventSubscriber.php similarity index 74% rename from src/Command/CreateService.php rename to src/Command/CreateEventSubscriber.php index b9b8b38..0d0b7d2 100644 --- a/src/Command/CreateService.php +++ b/src/Command/CreateEventSubscriber.php @@ -13,9 +13,9 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; -class CreateService extends Command +class CreateEventSubscriber extends Command { - public static $defaultName = 'frosh:create:service'; + public static $defaultName = 'frosh:create:eventSubscriber'; private BlockCollector $blockCollector; private array $pluginInfos; private CacheClearer $cacheClearer; @@ -31,7 +31,7 @@ public function __construct(BlockCollector $blockCollector, array $pluginInfos, protected function configure() { $this - ->setDescription('Generates a service for you') + ->setDescription('Generates a event subscriber for you') ->addArgument('pluginName', InputArgument::REQUIRED, 'Plugin Name'); } @@ -40,27 +40,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int $pluginPath = $this->determinePluginPath($input->getArgument('pluginName')); $io = new SymfonyStyle($input, $output); - $question = new Question('Service Name'); + $question = new Question('Event subscriber name'); $question->setAutocompleterValues(array($input->getArgument('pluginName'))); - $chosenServiceName = $io->askQuestion($question); + $chosenEventSubscriberName = $io->askQuestion($question); - if ($chosenServiceName === null) { - throw new \RuntimeException('Service Name is required'); + if ($chosenEventSubscriberName === null) { + throw new \RuntimeException('Event subscriber name is required'); } $fs = new Filesystem(); - $serviceFolderPath = $pluginPath . '/Service/'; - $servicePath = $serviceFolderPath . $chosenServiceName . '.php'; + $subscriberFolderPath = $pluginPath . '/Service/'; + $subscriberPath = $subscriberFolderPath . $chosenEventSubscriberName . '.php'; - if (!file_exists($serviceFolderPath)) { - $fs->mkdir($serviceFolderPath); + if (!file_exists($subscriberFolderPath)) { + $fs->mkdir($subscriberFolderPath); } - if (!file_exists(dirname($servicePath))) { - $fs->mkdir(dirname($servicePath)); + if (!file_exists(dirname($subscriberPath))) { + $fs->mkdir(dirname($subscriberPath)); } - if (!file_exists($servicePath)) { + if (!file_exists($subscriberPath)) { $tpl = <<getArgument('pluginName'), - $chosenServiceName + $chosenEventSubscriberName ], $tpl ); - $fs->dumpFile($servicePath, $content); + $fs->dumpFile($subscriberPath, $content); - $io->success(sprintf('Created file at "%s"', $servicePath)); + $io->success(sprintf('Created file at "%s"', $subscriberPath)); } $this->cacheClearer->clear(); From acb2a269bfb8acee1a9c5ef85a6cde2b1e6855f8 Mon Sep 17 00:00:00 2001 From: Philipp Rien <53903379+HashtagAssist@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:31:14 +0200 Subject: [PATCH 4/5] Update README.md --- README.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/README.md b/README.md index d0e9547..6a72fba 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 +ddev exec 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 From c6c6e85de0e981c76bdd1893f4e685123ef52d8a Mon Sep 17 00:00:00 2001 From: Philipp Rien <53903379+HashtagAssist@users.noreply.github.com> Date: Tue, 19 Sep 2023 09:34:58 +0200 Subject: [PATCH 5/5] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6a72fba..1589783 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Asks for the block you want to extend and creates the twig extension file for yo ### Generate Event Subscriber ```shell -ddev exec bin/console frosh:make:event-subscriber +./bin/console frosh:make:event-subscriber ``` Asks for the name of event subscriber.