Skip to content

Commit

Permalink
feat: add url for install psbo && update module
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john committed Nov 24, 2023
1 parent f5e60c9 commit 218a85a
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 4 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
54 changes: 52 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions config/helpers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
ps_eventbus.helper.module:
class: 'PrestaShop\Module\Ps_eventbus\Helper\ModuleHelper'
arguments:
- "@ps_eventbus"
public: true
36 changes: 36 additions & 0 deletions src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace PrestaShop\Module\Ps_eventbus\Helper;

use PrestaShopBundle\Service\Routing\Router;
use Ps_eventbus;

class ModuleHelper
{
/** @var Ps_eventbus */
private $module;

public function __construct(Ps_eventbus $module)
{
$this->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,
]);
}
}
57 changes: 57 additions & 0 deletions src/Module/Upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace PrestaShop\Module\Ps_eventbus\Module;

use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
use Prestashop\ModuleLibMboInstaller\Presenter as MBOPresenter;
use Prestashop\ModuleLibMboInstaller\Installer as MBOInstaller;
use Ps_eventbus;

class Upgrade
{
/**
* @var Ps_eventbus
*/
private $module;

/**
* Install constructor.
*
* @param Ps_eventbus $module
*
* @return void
*/
public function __construct(Ps_eventbus $module)
{
$this->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;
}
}
13 changes: 12 additions & 1 deletion src/Service/PresenterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand Down Expand Up @@ -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'),
];
}
}
Expand Down

0 comments on commit 218a85a

Please sign in to comment.