Skip to content

Commit

Permalink
refactor ExtensionsModule Api methods to account for new ExtensionIns…
Browse files Browse the repository at this point in the history
…tallerInterface and abstract some of the functionality to avoid code duplication. refs #2500
  • Loading branch information
craigh committed Jul 28, 2015
1 parent 567d56f commit bf57902
Showing 1 changed file with 35 additions and 36 deletions.
71 changes: 35 additions & 36 deletions src/system/Zikula/Module/ExtensionsModule/Api/AdminApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
}
}

0 comments on commit bf57902

Please sign in to comment.