Skip to content

Commit

Permalink
Add new features on Boilerplate to install and update modules (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume60240 authored May 13, 2024
2 parents 54a4235 + f04f132 commit 5584284
Show file tree
Hide file tree
Showing 15 changed files with 1,908 additions and 461 deletions.
2 changes: 2 additions & 0 deletions .zip-contents
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ logo.png
config.xml
LICENCE
README.md
src
.env
116 changes: 116 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,122 @@ if (!$moduleManager->isInstalled("ps_eventbus")) {
}
```

## Use the lib mbo_installer for managing dependencies

Remember that a dependency can be uninstall, disable or not up to date. To help the store to use a full functional package it's recommended to verify that yourself.

- **First had mbo_installer on composer.json**

```json
"require": {
//.... Your dependencies here
"prestashop/module-lib-mbo-installer": "^0.1.0"
},
```

This lib is necessary to install the _ps_mbo_ module.
You can forge the install link like this:

```php
/**
* Example on ModuleHelper.php
*/
if ($moduleName === 'ps_mbo') {
return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) .
$router->generate('ps_tech_vendor_boilerplate_api_resolver', [
'query' => 'installPsMbo',
]);
}
```

and call it like this:

```php
/**
* Example on BoilerplateResolverController.php
*
* Install ps_mbo module
*
* @return Response
*/
public function installPsMbo(): Response
{
$mboInstaller = new MBOInstaller(_PS_VERSION_);

return new Response(json_encode($mboInstaller->installModule(), JSON_FORCE_OBJECT), 200, [
'Content-Type' => 'application/json',
]);
}
```

- **Verify the dependencies**

You can now access all the technical properties of the dependencies and return their state to your template:

```php
/**
* Example on BoilerplateController.php
*
* Build informations about module
*
* @param string $moduleName
*
* @return array
*/
public function buildModuleInformation(string $moduleName)
{
return [
'technicalName' => $moduleName,
'displayName' => $this->getDisplayName($moduleName),
'isInstalled' => $this->isInstalled($moduleName),
'isEnabled' => $this->isEnabled($moduleName),
'isUpToDate' => $this->isUpToDate($moduleName),
'linkInstall' => $this->getInstallLink($moduleName),
'linkEnable' => $this->getEnableLink($moduleName),
'linkUpdate' => $this->getUpdateLink($moduleName),
];
}
```

`isUpToDate` comes from the ps_mbo module:

```php
/**
* Example on ModuleHelper.php
*
* returns true/false when module is out/up to date, and null when ps_mbo is not installed
*
* @param string $moduleName
*
* @return bool|null
*/
public function isUpToDate(string $moduleName)
{
$mboModule = \Module::getInstanceByName('ps_mbo');

if (!$mboModule) {
return null;
}

try {
$mboHelper = $mboModule->get('mbo.modules.helper');
} catch (\Exception $e) {
return null;
}

if (!$mboHelper) {
return null;
}

$moduleVersionInfos = $mboHelper->findForUpdates($moduleName);

return $moduleVersionInfos['upgrade_available'];
}
```

> [!CAUTION]
> A module not installed, not up to date or disabled must be a blocking situation to ensure the proper functioning of ps-eventbus
## Add context for the CDC

To allow the merchant to share its data with your services, you have to pair your module with a Cross Domain Component.
Expand Down
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@
"ext-xmlwriter": "*",
"ext-zip": "*",
"php": ">=7.1",
"prestashop/module-lib-service-container": "^2.0"
"vlucas/phpdotenv": "^3.6",
"prestashop/module-lib-service-container": "^2.0",
"prestashop/module-lib-mbo-installer": "^0.1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.35.1",
"prestashop/php-dev-tools": "^4.3.0",
"phpstan/phpstan": "^1.10.38"
"phpstan/phpstan": "^1.10.15",
"friendsofphp/php-cs-fixer": "^3.16.0",
"prestashop/php-dev-tools": "^4.3"
},
"autoload": {
"classmap": [
"ps_tech_vendor_boilerplate.php"
],
"psr-4": {
"PrestaShop\\Module\\Ps_tech_vendor_boilerplate\\": "src/",
"exclude-from-classmap\\": []
}
}
}
Loading

0 comments on commit 5584284

Please sign in to comment.