* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ declare(strict_types=1); namespace PrestaShop\Module\Mbo\Service; use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder; class ModuleInstaller { private $moduleName; private $moduleVersion; private $absoluteCompare; private $moduleManager; /** * @param string $moduleName * @param string|null $moduleVersion * @param bool $absoluteCompare */ public function __construct(string $moduleName, string $moduleVersion = null, bool $absoluteCompare = false) { $this->moduleName = $moduleName; $this->moduleVersion = $moduleVersion; $this->absoluteCompare = $absoluteCompare; $moduleManagerBuilder = ModuleManagerBuilder::getInstance(); $this->moduleManager = $moduleManagerBuilder->build(); } /** * Install ps_accounts module if not installed * Method to call in every psx modules during the installation process * * @return bool * * @throws \Exception */ public function install() { if ($this->isModuleInstalled() && $this->isModuleVersionSatisfied()) { return true; } return $this->moduleManager->install($this->moduleName); } /** * @return bool */ public function enable() { if (!$this->moduleManager->isEnabled($this->moduleName)) { return $this->moduleManager->enable($this->moduleName); } return true; } /** * @return bool */ public function isShopVersion17() { return version_compare(_PS_VERSION_, '1.7.0.0', '>='); } /** * @return boolean */ public function isModuleInstalled() { if (false === $this->isShopVersion17()) { return \Module::isInstalled($this->moduleName); } return $this->moduleManager->isInstalled($this->moduleName); } /** * @return boolean */ public function isModuleVersionSatisfied() { if (!$this->moduleVersion) return true; $module = \Module::getInstanceByName($this->moduleName); return version_compare( $module->version, $this->moduleVersion, $this->absoluteCompare ? '>' : '>=' ); } }