diff --git a/config/admin/services.yml b/config/admin/services.yml index 5ea4adc3..64714c35 100644 --- a/config/admin/services.yml +++ b/config/admin/services.yml @@ -7,12 +7,9 @@ services: ps_eventbus.service.presenter: class: 'PrestaShop\Module\PsEventbus\Service\PresenterService' public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' ps_eventbus.module.upgrade: class: 'PrestaShop\Module\PsEventbus\Module\Upgrade' arguments: - "@ps_eventbus" public: true - diff --git a/config/front/services.yml b/config/front/services.yml index a3f079cf..195d1e29 100644 --- a/config/front/services.yml +++ b/config/front/services.yml @@ -70,5 +70,3 @@ services: PrestaShop\Module\PsEventbus\Service\PresenterService: class: PrestaShop\Module\PsEventbus\Service\PresenterService public: true - arguments: - - '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService' diff --git a/ps_eventbus.php b/ps_eventbus.php index ebf61b5d..7d6e300d 100644 --- a/ps_eventbus.php +++ b/ps_eventbus.php @@ -26,6 +26,7 @@ use PrestaShop\Module\PsEventbus\Config\Config; use PrestaShop\ModuleLibServiceContainer\DependencyInjection\ServiceContainer; +use PrestaShop\PrestaShop\Adapter\SymfonyContainer; use PrestaShopBundle\EventListener\ActionDispatcherLegacyHooksSubscriber; if (!defined('_PS_VERSION_')) { @@ -255,12 +256,36 @@ public function uninstall() } /** + * This function allows you to patch bugs that can be found related to "ServiceNotFoundException". + * It ensures that you have access to the SymfonyContainer, and also that you have access to FO services. * @param string $serviceName * * @return mixed */ public function getService($serviceName) { + $splitServiceNamespace = explode('.', $serviceName); + $firstLevelNamespace = $splitServiceNamespace[0]; + + // if serviceName is not a service coming from ps_eventbus + if ($firstLevelNamespace !== 'ps_eventbus') { + // use symfony service container from prestashop + try { + $service = $this->serviceContainer->getService($serviceName); + } catch (\Exception $e) { + $container = SymfonyContainer::getInstance(); + + if ($container == null) { + throw new \PrestaShopException('Symfony container is null or invalid'); + } + + $service = $container->get($serviceName); + } + + return $service; + } + + // otherwise use the service container from the module return $this->serviceContainer->getService($serviceName); } @@ -1414,16 +1439,18 @@ public function hookActionDispatcherBefore($parameters) return; } - $route = $parameters['route']; - - // when translation is edited or reset, add to incremental sync - if ($route == 'api_translation_value_edit' || $route == 'api_translation_value_reset') { - $this->insertIncrementalSyncObject( - 0, - Config::COLLECTION_TRANSLATIONS, - date(DATE_ATOM), - $this->shopId - ); + if (array_key_exists('route', $parameters)) { + $route = $parameters['route']; + + // when translation is edited or reset, add to incremental sync + if ($route == 'api_translation_value_edit' || $route == 'api_translation_value_reset') { + $this->insertIncrementalSyncObject( + 0, + Config::COLLECTION_TRANSLATIONS, + date(DATE_ATOM), + $this->shopId + ); + } } } catch (\Exception $e) { return; diff --git a/src/Helper/ModuleHelper.php b/src/Helper/ModuleHelper.php index 9e73571c..f6c9ea0e 100644 --- a/src/Helper/ModuleHelper.php +++ b/src/Helper/ModuleHelper.php @@ -141,7 +141,7 @@ public function getInstallLink(string $moduleName) } /** @var Router $router * */ - $router = $this->module->get('router'); + $router = $this->module->getService('router'); if ($moduleName === 'ps_mbo') { return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) . @@ -171,7 +171,7 @@ public function getEnableLink(string $moduleName) } /** @var Router $router * */ - $router = $this->module->get('router'); + $router = $this->module->getService('router'); return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) . $router->generate('admin_module_manage_action', [ @@ -192,7 +192,7 @@ public function getUpdateLink(string $moduleName) // need to check if module is up to date, if not, return empty string /** @var Router $router * */ - $router = $this->module->get('router'); + $router = $this->module->getService('router'); return substr(\Tools::getShopDomainSsl(true) . __PS_BASE_URI__, 0, -1) . $router->generate('admin_module_manage_action', [ diff --git a/src/Service/PresenterService.php b/src/Service/PresenterService.php index 3ec2dabd..7b8dc04c 100644 --- a/src/Service/PresenterService.php +++ b/src/Service/PresenterService.php @@ -12,7 +12,7 @@ class PresenterService */ private $psAccountsAdapterService; - public function __construct(PsAccountsAdapterService $psAccountsAdapterService) + public function __construct() { $moduleManagerBuilder = ModuleManagerBuilder::getInstance(); if (!$moduleManagerBuilder) { @@ -20,16 +20,19 @@ public function __construct(PsAccountsAdapterService $psAccountsAdapterService) } $moduleManager = $moduleManagerBuilder->build(); if ($moduleManager->isInstalled('ps_accounts')) { + $psEventbus = \Module::getInstanceByName('ps_eventbus'); + $psAccountsAdapterService = $psEventbus->getService('PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService'); + $this->psAccountsAdapterService = $psAccountsAdapterService; } else { - $this->initPsAccount(); + $this->installPsAccount(); } } /** * @return void */ - public function initPsAccount() + public function installPsAccount() { $moduleManagerBuilder = ModuleManagerBuilder::getInstance();