From 218a85acd03068eac2d88b7278ffdc4aa0c3ad87 Mon Sep 17 00:00:00 2001 From: "John.R" Date: Fri, 24 Nov 2023 16:43:42 +0100 Subject: [PATCH] feat: add url for install psbo && update module --- composer.json | 3 +- composer.lock | 54 ++++++++++++++++++++++++++++-- config/admin/services.yml | 8 +++++ config/helpers.yml | 6 ++++ src/Helper/ModuleHelper.php | 36 ++++++++++++++++++++ src/Module/Upgrade.php | 57 ++++++++++++++++++++++++++++++++ src/Service/PresenterService.php | 13 +++++++- 7 files changed, 173 insertions(+), 4 deletions(-) create mode 100644 config/helpers.yml create mode 100644 src/Helper/ModuleHelper.php create mode 100644 src/Module/Upgrade.php diff --git a/composer.json b/composer.json index cac65eeb0..6499682e4 100755 --- a/composer.json +++ b/composer.json @@ -45,6 +45,7 @@ "psr/http-message": "<1.1", "symfony/config": "^3.4", "symfony/dependency-injection": "^3.4", - "symfony/yaml": "^3.4" + "symfony/yaml": "^3.4", + "prestashop/module-lib-mbo-installer": "^0.1.0" } } diff --git a/composer.lock b/composer.lock index 40b19fecb..d0cd01d56 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "0ce2d7d45e4d80f3a1ef10bbceb81b2d", + "content-hash": "109a4677b8bb8529bbd0de8d822d1775", "packages": [ { "name": "clue/stream-filter", @@ -570,6 +570,56 @@ "description": "Plug modules to the Guzzle client available on a running shop", "time": "2023-01-11T15:09:56+00:00" }, + { + "name": "prestashop/module-lib-mbo-installer", + "version": "v0.1", + "source": { + "type": "git", + "url": "https://github.com/PrestaShopCorp/module-lib-mbo-installer.git", + "reference": "c9e598307ef17975e10c98c98538c2d3512b962b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PrestaShopCorp/module-lib-mbo-installer/zipball/c9e598307ef17975e10c98c98538c2d3512b962b", + "reference": "c9e598307ef17975e10c98c98538c2d3512b962b", + "shasum": "" + }, + "require": { + "php": ">=5.6", + "prestashop/module-lib-guzzle-adapter": "^0.6" + }, + "require-dev": { + "phpstan/phpstan": "^1.9", + "phpunit/phpunit": "^9.5|^8.5|^5.7", + "prestashop/php-dev-tools": "^4.2|^3.16" + }, + "type": "library", + "autoload": { + "psr-4": { + "Prestashop\\ModuleLibMboInstaller\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "AFL-3.0" + ], + "authors": [ + { + "name": "PrestaShop SA", + "email": "contact@prestashop.com" + }, + { + "name": "Mikatux", + "email": "mickael@mayeur.eu" + } + ], + "description": "A helper to ease the download PS MBO from the Addons Marketplace", + "support": { + "issues": "https://github.com/PrestaShopCorp/module-lib-mbo-installer/issues", + "source": "https://github.com/PrestaShopCorp/module-lib-mbo-installer/tree/v0.1" + }, + "time": "2023-02-06T10:42:50+00:00" + }, { "name": "prestashop/module-lib-service-container", "version": "v2.0", @@ -1282,5 +1332,5 @@ "platform-overrides": { "php": "7.1" }, - "plugin-api-version": "1.1.0" + "plugin-api-version": "2.3.0" } diff --git a/config/admin/services.yml b/config/admin/services.yml index 863e67eb5..fe7cc7d0c 100755 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -2,3 +2,11 @@ imports: - { resource: ../common.yml } - { resource: ../front/repository.yml } - { resource: ../front/services.yml } + - { resource: ../helpers.yml } + +services: + ps_eventbus.service.presenter: + class: 'PrestaShop\Module\Ps_eventbus\Service\PresenterService' + arguments: + - "@ps_eventbus.helper.module" + public: true diff --git a/config/helpers.yml b/config/helpers.yml new file mode 100644 index 000000000..68be0c0c9 --- /dev/null +++ b/config/helpers.yml @@ -0,0 +1,6 @@ +services: + ps_eventbus.helper.module: + class: 'PrestaShop\Module\Ps_eventbus\Helper\ModuleHelper' + arguments: + - "@ps_eventbus" + public: true diff --git a/src/Helper/ModuleHelper.php b/src/Helper/ModuleHelper.php new file mode 100644 index 000000000..2de3a390a --- /dev/null +++ b/src/Helper/ModuleHelper.php @@ -0,0 +1,36 @@ +module = $module; + } + + /** + * returns the update link of the module if it is not enabled. If enabled, returns an empty string + * + * @param string $moduleName + * + * @return string + */ + public function getUpdateLink(string $moduleName) + { + /** @var Router $router * */ + $router = $this->module->getService('router'); + + return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) . + $router->generate('admin_module_manage_action', [ + 'action' => 'upgrade', + 'module_name' => $moduleName, + ]); + } +} diff --git a/src/Module/Upgrade.php b/src/Module/Upgrade.php new file mode 100644 index 000000000..7291321da --- /dev/null +++ b/src/Module/Upgrade.php @@ -0,0 +1,57 @@ +module = $module; + } + + /** + * Upgrade ps_eventbus module + * + * @return bool + */ + public function upgradePsEventbus() + { + $mboStatus = (new MBOPresenter)->present(); + + if ($mboStatus['isInstalled'] == false && $mboStatus['isEnabled']) { + try { + $mboInstaller = new MBOInstaller(_PS_VERSION_); + $mboInstaller->installModule(); + } catch (\Exception $e) { + throw new \Exception('Error while installing MBO module'); + } + } + + if (true === \Module::needUpgrade($this->module)) { + /** @var ModuleManagerBuilder $moduleManagerBuilder */ + $moduleManagerBuilder = ModuleManagerBuilder::getInstance(); + $moduleManager = $moduleManagerBuilder->build(); + + return $moduleManager->upgrade((string) $this->module->name); + } + + return true; + } +} diff --git a/src/Service/PresenterService.php b/src/Service/PresenterService.php index 1185a55ba..15fa0e4e1 100755 --- a/src/Service/PresenterService.php +++ b/src/Service/PresenterService.php @@ -3,7 +3,10 @@ namespace PrestaShop\Module\PsEventbus\Service; use PrestaShop\AccountsAuth\Service\PsAccountsService; +use PrestaShop\Module\Ps_eventbus\Helper\ModuleHelper; use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder; +use Ps_eventbus; +use Symfony\Component\HttpFoundation\Response; class PresenterService { @@ -12,8 +15,15 @@ class PresenterService */ private $psAccountsService; - public function __construct() + /** + * @var ModuleHelper + */ + private $moduleHelper; + + public function __construct(ModuleHelper $moduleHelper) { + $this->moduleHelper = $moduleHelper; + $moduleManager = ModuleManagerBuilder::getInstance(); if (!$moduleManager) { return; @@ -108,6 +118,7 @@ public function expose(\ModuleCore $module, $requiredConsents = [], $optionalCon 'lang' => $language->iso_code, ], 'psEventbusModule' => $this->convertObjectToArray(\Module::getInstanceByName('ps_eventbus')), + 'updatePsEvenbusUrl' => $this->moduleHelper->getUpdateLink('ps_eventbus'), ]; } }