Skip to content

Commit

Permalink
Merge pull request #772 from PrestaShopCorp/feature/MME-331/cloudsync…
Browse files Browse the repository at this point in the history
…_ps8

feat: install eventbus on PS8
  • Loading branch information
jinpresta authored Sep 11, 2024
2 parents 5a0adad + faa43b4 commit e4cbef3
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 21 deletions.
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_mbo</name>
<displayName><![CDATA[PrestaShop Marketplace in your Back Office]]></displayName>
<version><![CDATA[4.11.4]]></version>
<version><![CDATA[4.12.0]]></version>
<description><![CDATA[Discover the best PrestaShop modules to optimize your online store.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[administration]]></tab>
Expand Down
9 changes: 9 additions & 0 deletions config/services/eventbus.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_defaults:
public: true

mbo.ps_eventbus.installer:
class: 'PrestaShop\Module\Mbo\Service\ModuleInstaller'
public: true
arguments:
- "ps_eventbus"
4 changes: 2 additions & 2 deletions ps_mbo.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ps_mbo extends Module
/**
* @var string
*/
public const VERSION = '4.11.4';
public const VERSION = '4.12.0';

public const CONTROLLERS_WITH_CONNECTION_TOOLBAR = [
'AdminModulesManage',
Expand Down Expand Up @@ -94,7 +94,7 @@ class ps_mbo extends Module
public function __construct()
{
$this->name = 'ps_mbo';
$this->version = '4.11.4';
$this->version = '4.12.0';
$this->author = 'PrestaShop';
$this->tab = 'administration';
$this->module_key = '6cad5414354fbef755c7df4ef1ab74eb';
Expand Down
20 changes: 15 additions & 5 deletions src/Controller/Admin/ModuleCatalogController.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function indexAction(): Response
try {
$accountsFacade = $this->get('mbo.ps_accounts.facade');
$accountsService = $accountsFacade->getPsAccountsService();
$this->ensurePsAccountIsEnabled();
if ($this->ensurePsAccountIsEnabled()) $this->ensurePsEventbusEnabled();
} catch (\PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException $e) {
$accountsInstaller = $this->get('mbo.ps_accounts.installer');
// Seems the module is not here, try to install it
Expand Down Expand Up @@ -132,13 +132,23 @@ public function cdcErrorAction(): Response
);
}

private function ensurePsAccountIsEnabled(): void
private function ensurePsAccountIsEnabled(): bool
{
$accountsInstaller = $this->get('mbo.ps_accounts.installer');
if (!$accountsInstaller) return false;

if (null !== $accountsInstaller && !$accountsInstaller->isModuleEnabled()) {
$moduleManager = $this->get('prestashop.module.manager');
$moduleManager->enable($accountsInstaller->getModuleName());
$accountsEnabled = $accountsInstaller->isModuleEnabled();
if ($accountsEnabled) return true;

$moduleManager = $this->get('prestashop.module.manager');
return $moduleManager->enable($accountsInstaller->getModuleName());
}

private function ensurePsEventbusEnabled()
{
$installer = $this->get('mbo.ps_eventbus.installer');
if ($installer->install()) {
$installer->enable();
}
}
}
1 change: 1 addition & 0 deletions src/DependencyInjection/ContainerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function get(string $containerName): ContainerInterface
$loader->load('http_clients.yml');
$loader->load('distribution.yml');
$loader->load('accounts.yml');
$loader->load('eventbus.yml');
$loader->load('handler.yml');
$loader->load('api/distribution.yml');

Expand Down
13 changes: 2 additions & 11 deletions src/Module/ActionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,9 @@ class ActionsManager
*/
private $filesManager;

/**
* @var Repository
* @TODO : Not needed anymore
*/
private $moduleRepository;

public function __construct(
FilesManager $filesManager,
Repository $moduleRepository
) {
public function __construct(FilesManager $filesManager) {
$this->filesManager = $filesManager;
$this->moduleRepository = $moduleRepository;
}

/**
Expand All @@ -57,7 +48,7 @@ public function __construct(
*/
public function install(int $moduleId): void
{
$moduleZip = $this->filesManager->downloadModule($moduleId);
$moduleZip = $this->downloadModule($moduleId);

$this->filesManager->installFromSource($moduleZip);
}
Expand Down
106 changes: 106 additions & 0 deletions src/Service/ModuleInstaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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 ? '>' : '>='
);
}

}
37 changes: 36 additions & 1 deletion src/Traits/Hooks/UseDashboardZoneOne.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,19 @@ protected function loadPsAccounts(): string
try {
$accountsFacade = $this->get('mbo.ps_accounts.facade');
$accountsService = $accountsFacade->getPsAccountsService();
if ($this->ensurePsAccountIsEnabled()) $this->ensurePsEventbusEnabled();
} catch (\PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException $e) {
ErrorHelper::reportError($e);
$accountsInstaller = $this->get('mbo.ps_accounts.installer');
// Seems the module is not here, try to install it
$accountsInstaller->install();
$accountsFacade = $this->get('mbo.ps_accounts.facade');
try {
$accountsService = $accountsFacade->getPsAccountsService();
} catch (\Exception $e) {
// Installation seems to not work properly
$accountsService = $accountsFacade = null;
ErrorHelper::reportError($e);
}
}

if (null !== $accountsFacade && null !== $accountsService) {
Expand All @@ -113,4 +124,28 @@ protected function loadPsAccounts(): string

return $urlAccountsCdn;
}

/**
* Return true if ps_account is enabled
*
* @return bool
*/
private function ensurePsAccountIsEnabled(): bool {
$accountsInstaller = $this->get('mbo.ps_accounts.installer');
if (!$accountsInstaller) return false;

$accountsEnabled = $accountsInstaller->isModuleEnabled();
if ($accountsEnabled) return true;

$moduleManager = $this->get('prestashop.module.manager');
return $moduleManager->enable($accountsInstaller->getModuleName());
}

private function ensurePsEventbusEnabled()
{
$installer = $this->get('mbo.ps_eventbus.installer');
if ($installer->install()) {
$installer->enable();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
<script type="application/javascript" src="{{ cdc_url }}"></script>
{% endif %}

<script>
var psAccountLoaded = false;
</script>
{% if urlAccountsCdn is defined and urlAccountsCdn is not empty %}
{# PrestaShop Account #}
<script src="{{ urlAccountsCdn }}" rel=preload></script>
<script>
var psAccountLoaded = false;
if (window?.psaccountsVue) {
window?.psaccountsVue?.init();
psAccountLoaded = true;
Expand Down

0 comments on commit e4cbef3

Please sign in to comment.