Skip to content

Commit

Permalink
fix: argument missing presenter (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
fox-john authored Apr 10, 2024
1 parent 8e6510a commit e4ae94a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 21 deletions.
3 changes: 0 additions & 3 deletions config/admin/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 0 additions & 2 deletions config/front/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,3 @@ services:
PrestaShop\Module\PsEventbus\Service\PresenterService:
class: PrestaShop\Module\PsEventbus\Service\PresenterService
public: true
arguments:
- '@PrestaShop\Module\PsEventbus\Service\PsAccountsAdapterService'
47 changes: 37 additions & 10 deletions ps_eventbus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_')) {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Helper/ModuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) .
Expand Down Expand Up @@ -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', [
Expand All @@ -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', [
Expand Down
9 changes: 6 additions & 3 deletions src/Service/PresenterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,27 @@ class PresenterService
*/
private $psAccountsAdapterService;

public function __construct(PsAccountsAdapterService $psAccountsAdapterService)
public function __construct()
{
$moduleManagerBuilder = ModuleManagerBuilder::getInstance();
if (!$moduleManagerBuilder) {
return;
}
$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();

Expand Down

0 comments on commit e4ae94a

Please sign in to comment.