From ec4b0a57555fb77bb591bffc7e3e315c5b82e6f6 Mon Sep 17 00:00:00 2001 From: Craig Heydenburg Date: Wed, 24 Feb 2021 17:32:59 -0500 Subject: [PATCH] update Hooks to Symfony 5 (#4593) update Hooks to Symfony 5 --- Bridge/HookBundle/HookBundleInstaller.php | 99 +++++++++++++++++++ Command/SchemaToolInstallCommand.php | 57 +++++++++++ EventListener/HookBundleInstallerListener.php | 51 ++++++++++ 3 files changed, 207 insertions(+) create mode 100644 Bridge/HookBundle/HookBundleInstaller.php create mode 100644 Command/SchemaToolInstallCommand.php create mode 100644 EventListener/HookBundleInstallerListener.php diff --git a/Bridge/HookBundle/HookBundleInstaller.php b/Bridge/HookBundle/HookBundleInstaller.php new file mode 100644 index 0000000..7c33e57 --- /dev/null +++ b/Bridge/HookBundle/HookBundleInstaller.php @@ -0,0 +1,99 @@ +schemaTool = $schemaTool; + $this->em = $entityManager; + } + + public function install(): bool + { + $this->schemaTool->create(self::$entities); + + return true; + } + + public function uninstall(): bool + { + return false; + } + + public function upgrade(string $currentCoreVersion): bool + { + // special note, the $currentCoreVersion var will contain the version of the CORE (not this bundle) + + if (version_compare($currentCoreVersion, '2.0.0', '<')) { + // remove all old hook-related tables + $oldTables = [ + 'hook_area', + 'hook_provider', + 'hook_subscriber', + 'hook_binding', + 'hook_runtime' + ]; + foreach ($oldTables as $table) { + $sql = "DROP TABLE ${table};"; + $connection = $this->em->getConnection(); + $stmt = $connection->prepare($sql); + $stmt->execute(); + $stmt->closeCursor(); + } + $this->schemaTool->create(self::$entities); // create new versions of the tables for Core-2.0.0 + } + switch ($currentCoreVersion) { + case '2.0.0': + $this->schemaTool->update([ + HookRuntimeEntity::class + ]); + // no break + case '2.0.1': + // nothing + case '3.1.0'://current version + // allow old tables to remain on upgrade, just add the new one + $this->schemaTool->create(self::$entities); + } + + // Update successful + return true; + } +} diff --git a/Command/SchemaToolInstallCommand.php b/Command/SchemaToolInstallCommand.php new file mode 100644 index 0000000..60eafb5 --- /dev/null +++ b/Command/SchemaToolInstallCommand.php @@ -0,0 +1,57 @@ +schemaHelper = $schemaHelper; + } + + protected function configure() + { + $this + ->setDefinition([ + new InputArgument('entityClass', InputArgument::REQUIRED, 'The FqCN of the Entity Class'), + ]) + ->setDescription('Create a new table in the DB for the provided entity class.'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $entityClass = $input->getArgument('entityClass'); + if (!class_exists($entityClass)) { + throw new InvalidArgumentException('The entity class does not exist.'); + } + $this->schemaHelper->create([$entityClass]); + + return Command::SUCCESS; + } +} diff --git a/EventListener/HookBundleInstallerListener.php b/EventListener/HookBundleInstallerListener.php new file mode 100644 index 0000000..3b12ed0 --- /dev/null +++ b/EventListener/HookBundleInstallerListener.php @@ -0,0 +1,51 @@ +hookBundleInstaller = $hookBundleInstaller; + } + + public static function getSubscribedEvents() + { + return [ + CoreInstallationPreExtensionInstallation::class => 'installHookBundle', + CoreUpgradePreExtensionUpgrade::class => 'upgradeHookBundle' + ]; + } + + public function installHookBundle(CoreInstallationPreExtensionInstallation $event): void + { + if (!$this->hookBundleInstaller->install()) { + $event->stopPropagation(); + } + } + + public function upgradeHookBundle(CoreUpgradePreExtensionUpgrade $event): void + { + if (!$this->hookBundleInstaller->upgrade($event->getCurrentCoreVersion())) { + $event->stopPropagation(); + } + } +}