Skip to content

Commit

Permalink
Simply locale creation, that is not used anyway, to fix the performan…
Browse files Browse the repository at this point in the history
…ce drop from the initial refacto
  • Loading branch information
jolelievre committed Nov 14, 2024
1 parent dd932a6 commit bfa348f
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 47 deletions.
17 changes: 1 addition & 16 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ 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: 6
path: src/Core/Addon/Module/ModuleManagerBuilder.php

-
Expand Down Expand Up @@ -645,21 +645,11 @@ parameters:
count: 1
path: src/Core/Form/IdentifiableObject/DataHandler/OrderReturnFormDataHandler.php

-
message: "#^Class Customer 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/Grid/Data/Factory/CustomerDiscountGridDataFactory.php

-
message: "#^Namespace CartRule is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 2
path: src/Core/Grid/Data/Factory/CustomerDiscountGridDataFactory.php

-
message: "#^Namespace Customer 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/Grid/Data/Factory/CustomerDiscountGridDataFactory.php

-
message: "#^Class Employee is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
Expand Down Expand Up @@ -1025,11 +1015,6 @@ parameters:
count: 1
path: src/PrestaShopBundle/Command/ModuleCommand.php

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

-
message: "#^Class ImageManager is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 1
Expand Down
79 changes: 48 additions & 31 deletions src/Core/Addon/Module/ModuleManagerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
use Context;
use Doctrine\Common\Cache\Psr6\DoctrineProvider;
use Language;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\Currency\CurrencyDataProvider;
use PrestaShop\Decimal\Operation\Rounding;
use PrestaShop\PrestaShop\Adapter\HookManager;
use PrestaShop\PrestaShop\Adapter\LegacyLogger;
use PrestaShop\PrestaShop\Adapter\Module\AdminModuleDataProvider;
Expand All @@ -39,15 +38,12 @@
use PrestaShop\PrestaShop\Adapter\Tools;
use PrestaShop\PrestaShop\Core\Context\ApiClientContext;
use PrestaShop\PrestaShop\Core\Context\LanguageContext;
use PrestaShop\PrestaShop\Core\Localization\CLDR\DataLayer\LocaleReference;
use PrestaShop\PrestaShop\Core\Localization\CLDR\LocaleDataSource;
use PrestaShop\PrestaShop\Core\Localization\CLDR\LocaleRepository;
use PrestaShop\PrestaShop\Core\Localization\CLDR\Reader;
use PrestaShop\PrestaShop\Core\Localization\Currency\CurrencyDataSource;
use PrestaShop\PrestaShop\Core\Localization\Currency\DataLayer\CurrencyInstalled;
use PrestaShop\PrestaShop\Core\Localization\Currency\DataLayer\CurrencyReference;
use PrestaShop\PrestaShop\Core\Localization\Currency\Repository;
use PrestaShop\PrestaShop\Core\Localization\Locale;
use PrestaShop\PrestaShop\Core\Localization\Number\Formatter as NumberFormatter;
use PrestaShop\PrestaShop\Core\Localization\Specification\Number as NumberSpecification;
use PrestaShop\PrestaShop\Core\Localization\Specification\NumberCollection;
use PrestaShop\PrestaShop\Core\Localization\Specification\NumberSymbolList;
use PrestaShop\PrestaShop\Core\Localization\Specification\Price as PriceSpecification;
use PrestaShop\PrestaShop\Core\Module\ModuleManager;
use PrestaShop\PrestaShop\Core\Module\ModuleRepository;
use PrestaShop\PrestaShop\Core\Module\SourceHandler\SourceHandlerFactory;
Expand Down Expand Up @@ -226,7 +222,47 @@ private function getLanguageContext(): LanguageContext
/** @var Language $language */
$language = Context::getContext()->language;

$localeRepository = $this->getLocaleRepository();
// If locale is present in context we can use, if not we create a mock one
// the locale is not used by the ModuleRepository anyway only the language ID is relevant for its internal cache key generation
if (Context::getContext()->currentLocale) {
$locale = Context::getContext()->currentLocale;
} else {
$numberSymbolList = new NumberSymbolList(',', ' ', ';', '%', '-', '+', 'E', '^', '', '', 'NaN');
$priceSpecsCollection = new NumberCollection();
$priceSpecsCollection->add(
'EUR',
new PriceSpecification(
'#,##0.## ¤',
'-#,##0.## ¤',
['latn' => $numberSymbolList],
2,
2,
true,
3,
3,
'symbol',
'',
'EUR'
)
);
$numberSpecification = new NumberSpecification(
'#,##0.###',
'-#,##0.###',
[$numberSymbolList],
3,
2,
true,
2,
3
);
$locale = new Locale(
$language->locale,
$numberSpecification,
$priceSpecsCollection,
new NumberFormatter(Rounding::ROUND_HALF_UP, 'latn')
);
}

self::$languageContext = new LanguageContext(
$language->id,
$language->name,
Expand All @@ -236,31 +272,12 @@ private function getLanguageContext(): LanguageContext
$language->is_rtl,
$language->date_format_lite,
$language->date_format_full,
$localeRepository->getLocale($language->locale)
$locale
);

return self::$languageContext;
}

private function getLocaleRepository(): Locale\Repository
{
$localeDataReference = new LocaleReference(new Reader());
$localeDataSource = new LocaleDataSource($localeDataReference);
$cldrLocaleRepository = new LocaleRepository($localeDataSource);

$configuration = new Configuration();
$currencyReference = new CurrencyReference($cldrLocaleRepository);
$currencyDataProvider = new CurrencyDataProvider($configuration, (int) $configuration->get('PS_SHOP_DEFAULT'));
$currencyInstalled = new CurrencyInstalled($currencyDataProvider);
$currencyDataSource = new CurrencyDataSource($currencyReference, $currencyInstalled);
$currencyRepository = new Repository($currencyDataSource);

return new Locale\Repository(
$cldrLocaleRepository,
$currencyRepository,
);
}

protected function getConfigDir()
{
return _PS_ROOT_DIR_ . '/app/config';
Expand Down

0 comments on commit bfa348f

Please sign in to comment.