From 65a3ef83a7050c88ff85116ab7cf0c1face13b8d Mon Sep 17 00:00:00 2001 From: Craig Heydenburg Date: Thu, 18 Jun 2015 14:13:56 -0400 Subject: [PATCH] refactor ExtensionsModule Api methods to account for new ExtensionInstallerInterface and abstract some of the functionality to avoid code duplication. refs #2500 --- .../Module/ExtensionsModule/Api/AdminApi.php | 71 +++++++++---------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/system/Zikula/Module/ExtensionsModule/Api/AdminApi.php b/src/system/Zikula/Module/ExtensionsModule/Api/AdminApi.php index dd268e8369..c30b20f845 100644 --- a/src/system/Zikula/Module/ExtensionsModule/Api/AdminApi.php +++ b/src/system/Zikula/Module/ExtensionsModule/Api/AdminApi.php @@ -36,6 +36,7 @@ use Zikula\Core\Doctrine\Entity\ExtensionDependencyEntity; use Zikula\Bundle\CoreBundle\Bundle\Scanner; use Symfony\Component\Security\Core\Exception\AccessDeniedException; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; /** * Administrative API functions for the Extensions module. @@ -377,18 +378,7 @@ public function remove($args) // Module deletion function. Only execute if the module is initialised. if ($modinfo['state'] != ModUtil::STATE_UNINITIALISED) { - if (null === $module) { - $className = ucwords($modinfo['name']).'\\'.ucwords($modinfo['name']).'Installer'; - $classNameOld = ucwords($modinfo['name']) . '_Installer'; - $className = class_exists($className) ? $className : $classNameOld; - } else { - $className = $module->getInstallerClass(); - } - $reflectionInstaller = new ReflectionClass($className); - if (!$reflectionInstaller->isSubclassOf('Zikula_AbstractInstaller')) { - throw new \RuntimeException($this->__f("%s must be an instance of Zikula_AbstractInstaller", $className)); - } - $installer = $reflectionInstaller->newInstanceArgs(array($this->serviceManager, $module)); + $installer = $this->getInstaller($module, $modinfo); // perform the actual deletion of the module $func = array($installer, 'uninstall'); @@ -968,18 +958,7 @@ public function initialise($args) include_once $bootstrap; } - if (null === $module) { - $className = ucwords($modinfo['name']).'\\'.ucwords($modinfo['name']).'Installer'; - $classNameOld = ucwords($modinfo['name']) . '_Installer'; - $className = class_exists($className) ? $className : $classNameOld; - } else { - $className = $module->getInstallerClass(); - } - $reflectionInstaller = new ReflectionClass($className); - if (!$reflectionInstaller->isSubclassOf('Zikula_AbstractInstaller')) { - throw new \RuntimeException($this->__f("%s must be an instance of Zikula_AbstractInstaller", $className)); - } - $installer = $reflectionInstaller->newInstanceArgs(array($this->serviceManager, $module)); + $installer = $this->getInstaller($module, $modinfo); // perform the actual install of the module // system or module @@ -1065,18 +1044,7 @@ public function upgrade($args) include_once $bootstrap; } - if (null === $module) { - $className = ucwords($modinfo['name']).'\\'.ucwords($modinfo['name']).'Installer'; - $classNameOld = ucwords($modinfo['name']) . '_Installer'; - $className = class_exists($className) ? $className : $classNameOld; - } else { - $className = $module->getInstallerClass(); - } - $reflectionInstaller = new ReflectionClass($className); - if (!$reflectionInstaller->isSubclassOf('Zikula_AbstractInstaller')) { - throw new \RuntimeException($this->__f("%s must be an instance of Zikula_AbstractInstaller", $className)); - } - $installer = $reflectionInstaller->newInstanceArgs(array($this->serviceManager, $module)); + $installer = $this->getInstaller($module, $modinfo); // perform the actual upgrade of the module $func = array($installer, 'upgrade'); @@ -1503,4 +1471,35 @@ private function isCoreCompatible($min = null, $max = null) } return true; } + + /** + * Get an instance of the module installer class + * @param \Zikula\Core\AbstractModule|null $module + * @param array $modInfo + * @return \Zikula_AbstractInstaller|\Zikula\Core\ExtensionInstallerInterface + */ + private function getInstaller($module, array $modInfo) + { + if (null === $module) { + $className = ucwords($modInfo['name']) . '\\' . ucwords($modInfo['name']) . 'Installer'; + $classNameOld = ucwords($modInfo['name']) . '_Installer'; + $className = class_exists($className) ? $className : $classNameOld; + } else { + $className = $module->getInstallerClass(); + } + $reflectionInstaller = new ReflectionClass($className); + if ($reflectionInstaller->isSubclassOf('Zikula_AbstractInstaller')) { + $installer = $reflectionInstaller->newInstanceArgs(array($this->serviceManager, $module)); + } elseif ($reflectionInstaller->isSubclassOf('\Zikula\Core\ExtensionInstallerInterface')) { + $installer = $reflectionInstaller->newInstance(); + $installer->setBundle($module); + if ($installer instanceof ContainerAwareInterface) { + $installer->setContainer($this->getContainer()); + } + } else { + throw new \RuntimeException($this->__f("%s must be an instance of Zikula_AbstractInstaller or implement ExtensionInstallerInterface", $className)); + } + + return $installer; + } }