Skip to content

Commit

Permalink
Use LanguageContext in ModuleRepository for better stability
Browse files Browse the repository at this point in the history
  • Loading branch information
jolelievre committed Oct 22, 2024
1 parent 53d2b70 commit 914e290
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 23 deletions.
17 changes: 6 additions & 11 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ parameters:
ignoreErrors:
-
message: "#^Namespace Context is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 4
count: 5
path: src/Core/Addon/Module/ModuleManagerBuilder.php

-
message: "#^Namespace Language is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
path: src/Core/Addon/Module/ModuleManagerBuilder.php

-
Expand Down Expand Up @@ -1050,16 +1055,6 @@ parameters:
count: 2
path: src/PrestaShopBundle/Controller/Admin/Improve/Design/MailThemeController.php

-
message: "#^Class Hook is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
path: src/PrestaShopBundle/Controller/Admin/Improve/Design/PositionsController.php

-
message: "#^Namespace Hook is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
path: src/PrestaShopBundle/Controller/Admin/Improve/Design/PositionsController.php

-
message: "#^Namespace Db is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 2
Expand Down
34 changes: 33 additions & 1 deletion src/Core/Addon/Module/ModuleManagerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@

use Context;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Language;
use PrestaShop\PrestaShop\Adapter\HookManager;
use PrestaShop\PrestaShop\Adapter\LegacyLogger;
use PrestaShop\PrestaShop\Adapter\Module\AdminModuleDataProvider;
use PrestaShop\PrestaShop\Adapter\Module\ModuleDataProvider;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Adapter\Tools;
use PrestaShop\PrestaShop\Core\Context\ApiClientContext;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Module\ModuleManager;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use PrestaShop\PrestaShop\Core\Module\SourceHandler\SourceHandlerFactory;
Expand Down Expand Up @@ -64,7 +66,14 @@ class ModuleManagerBuilder
protected static $translator = null;
protected static $instance = null;
protected static $cacheProvider = null;
/**
* @var ApiClientContext
*/
protected static $apiClientContext;
/**
* @var LanguageContext|null
*/
protected static $languageContext = null;

/**
* @var bool
Expand Down Expand Up @@ -128,7 +137,7 @@ public function buildRepository()
self::$cacheProvider,
new HookManager(),
_PS_MODULE_DIR_,
Context::getContext()->language->id
$this->getLanguageContext(),
);
}
}
Expand Down Expand Up @@ -197,6 +206,29 @@ private function getSymfonyRouter()
return new Router($loader, $routeFileName);
}

private function getLanguageContext(): LanguageContext
{
if (self::$languageContext) {
return self::$languageContext;
}

/** @var Language $language */
$language = Context::getContext()->language;
self::$languageContext = new LanguageContext(
$language->id,
$language->name,
$language->iso_code,
$language->locale,
$language->locale_code,
$language->is_rtl,
$language->date_format,
$language->date_format_full,
Context::getContext()->getCurrentLocale(),
);

return self::$languageContext;
}

protected function getConfigDir()
{
return _PS_ROOT_DIR_ . '/app/config';
Expand Down
11 changes: 3 additions & 8 deletions src/Core/Module/ModuleRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use PrestaShop\PrestaShop\Adapter\Module\AdminModuleDataProvider;
use PrestaShop\PrestaShop\Adapter\Module\Module;
use PrestaShop\PrestaShop\Adapter\Module\ModuleDataProvider;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Domain\Module\Exception\ModuleNotFoundException;
use Symfony\Component\Finder\Finder;
use Throwable;
Expand Down Expand Up @@ -75,25 +76,19 @@ class ModuleRepository implements ModuleRepositoryInterface
/** @var Module[] */
private $modulesFromHook;

/**
* @var int
*/
private $contextLangId;

public function __construct(
ModuleDataProvider $moduleDataProvider,
AdminModuleDataProvider $adminModuleDataProvider,
CacheProvider $cacheProvider,
HookManager $hookManager,
string $modulePath,
int $contextLangId
private LanguageContext $languageContext,
) {
$this->moduleDataProvider = $moduleDataProvider;
$this->adminModuleDataProvider = $adminModuleDataProvider;
$this->cacheProvider = $cacheProvider;
$this->hookManager = $hookManager;
$this->modulePath = $modulePath;
$this->contextLangId = $contextLangId;
}

public function getList(): ModuleCollection
Expand Down Expand Up @@ -246,7 +241,7 @@ protected function getCacheKey(string $moduleName, ?int $shopId = null): string
{
$shop = $shopId ? [$shopId] : Shop::getContextListShopID();

return $moduleName . implode('-', $shop) . $this->contextLangId;
return $moduleName . implode('-', $shop) . $this->languageContext->getId();
}

private function getModuleAttributes(string $moduleName, bool $isValid): array
Expand Down
9 changes: 9 additions & 0 deletions src/PrestaShopBundle/Install/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
use PrestaShop\PrestaShop\Adapter\Entity\Tools;
use PrestaShop\PrestaShop\Adapter\Entity\Validate;
use PrestaShop\PrestaShop\Adapter\Module\Module;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
use PrestaShop\PrestaShop\Core\Addon\Theme\ThemeManagerBuilder;
use PrestaShop\PrestaShop\Core\Context\LanguageContextBuilder;
use PrestaShop\PrestaShop\Core\Module\ConfigReader as ModuleConfigReader;
use PrestaShop\PrestaShop\Core\Theme\ConfigReader as ThemeConfigReader;
use PrestaShop\PrestaShop\Core\Version;
Expand Down Expand Up @@ -461,6 +463,13 @@ public function installDefaultData($shop_name, $iso_country = false, $all_langua

Context::getContext()->language = new LanguageLegacy($id_lang);

// Init LanguageContext
$container = SymfonyContainer::getInstance();
if ($container && $container->has(LanguageContextBuilder::class)) {
$builder = $container->get(LanguageContextBuilder::class);
$builder->setLanguageId((int) $id_lang);
}

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ services:
lazy: true
factory: [ '@PrestaShop\PrestaShop\Core\Context\EmployeeContextBuilder', 'build' ]

PrestaShop\PrestaShop\Core\Context\LanguageContextBuilder: ~
PrestaShop\PrestaShop\Core\Context\LanguageContextBuilder:
# Only public to make it available in Install process
public: true
PrestaShop\PrestaShop\Core\Context\LanguageContext:
lazy: true
factory: [ '@PrestaShop\PrestaShop\Core\Context\LanguageContextBuilder', 'build' ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ services:

PrestaShop\PrestaShop\Core\Module\ModuleRepository:
lazy: true
autowire: true
arguments:
- '@prestashop.adapter.data_provider.module'
- '@prestashop.adapter.admin.data_provider.module'
- '@doctrine.cache.provider'
- '@PrestaShop\PrestaShop\Adapter\HookManager'
- "@=service('prestashop.adapter.legacy.configuration').get('_PS_MODULE_DIR_')"
- '@=service("prestashop.adapter.legacy.context").getContext().language.id'

prestashop.core.admin.module.repository.eventsubscriber:
class: PrestaShop\PrestaShop\Core\Module\EventSubscriber
Expand Down
14 changes: 13 additions & 1 deletion tests/Integration/Core/Module/ModuleRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use PrestaShop\PrestaShop\Adapter\HookManager;
use PrestaShop\PrestaShop\Adapter\Module\AdminModuleDataProvider;
use PrestaShop\PrestaShop\Adapter\Module\ModuleDataProvider;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Localization\LocaleInterface;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Translation\Translator;
Expand Down Expand Up @@ -99,7 +101,17 @@ protected function setUp(): void
$cacheProvider,
$hookManager,
dirname(__DIR__, 3) . '/Resources/modules/',
1
new LanguageContext(
1,
'English',
'en',
'en-US',
'en-us',
false,
'm/d/Y',
'm/d/Y H:i:s',
$this->createMock(LocaleInterface::class),
),
);
}

Expand Down

0 comments on commit 914e290

Please sign in to comment.