init
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
|
||||
class Config
|
||||
{
|
||||
private const XML_PATH_ENABLED = 'language_url_store_resolver/general/enabled';
|
||||
private const XML_PATH_MARKET_COOKIE_NAME = 'language_url_store_resolver/general/market_cookie_name';
|
||||
private const XML_PATH_MARKET_COOKIE_LIFETIME = 'language_url_store_resolver/general/market_cookie_lifetime';
|
||||
|
||||
private const DEFAULT_COOKIE_NAME = 'market';
|
||||
private const DEFAULT_COOKIE_LIFETIME = 2592000;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $skipPrefixes = [
|
||||
'admin',
|
||||
'rest',
|
||||
'graphql',
|
||||
'soap',
|
||||
'static',
|
||||
'media',
|
||||
'pub',
|
||||
'stores/store',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var ScopeConfigInterface
|
||||
*/
|
||||
private $scopeConfig;
|
||||
|
||||
public function __construct(ScopeConfigInterface $scopeConfig)
|
||||
{
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLED, ScopeInterface::SCOPE_STORE);
|
||||
}
|
||||
|
||||
public function getMarketCookieName(): string
|
||||
{
|
||||
$cookieName = trim((string)$this->scopeConfig->getValue(
|
||||
self::XML_PATH_MARKET_COOKIE_NAME,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
));
|
||||
|
||||
return $cookieName !== '' ? $cookieName : self::DEFAULT_COOKIE_NAME;
|
||||
}
|
||||
|
||||
public function getMarketCookieLifetime(): int
|
||||
{
|
||||
$lifetime = (int)$this->scopeConfig->getValue(
|
||||
self::XML_PATH_MARKET_COOKIE_LIFETIME,
|
||||
ScopeInterface::SCOPE_STORE
|
||||
);
|
||||
|
||||
return $lifetime > 0 ? $lifetime : self::DEFAULT_COOKIE_LIFETIME;
|
||||
}
|
||||
|
||||
public function shouldSkipPath(string $path): bool
|
||||
{
|
||||
$path = strtolower(trim($path, '/'));
|
||||
|
||||
foreach ($this->skipPrefixes as $prefix) {
|
||||
if ($path === $prefix || strpos($path, $prefix . '/') === 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
class HtmlLinkProcessor
|
||||
{
|
||||
/**
|
||||
* @var UrlPathConverter
|
||||
*/
|
||||
private $urlPathConverter;
|
||||
|
||||
/**
|
||||
* @param UrlPathConverter $urlPathConverter
|
||||
*/
|
||||
public function __construct(UrlPathConverter $urlPathConverter)
|
||||
{
|
||||
$this->urlPathConverter = $urlPathConverter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert internal anchor URLs in complete page HTML.
|
||||
*
|
||||
* @param string $html
|
||||
* @param string $language
|
||||
* @return string
|
||||
*/
|
||||
public function process(string $html, string $language): string
|
||||
{
|
||||
if (stripos($html, '<a') === false) {
|
||||
return $html;
|
||||
}
|
||||
|
||||
$result = preg_replace_callback(
|
||||
'~(<a\b[^>]*\bhref\s*=\s*)(["\'])(.*?)\2~is',
|
||||
function (array $matches) use ($language): string {
|
||||
$href = $matches[3];
|
||||
$normalizedHref = trim($href);
|
||||
|
||||
if (!$this->isConvertibleHref($normalizedHref)) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
$convertedHref = $this->urlPathConverter->toLanguageUrl($normalizedHref, $language);
|
||||
if ($convertedHref === $normalizedHref) {
|
||||
return $matches[0];
|
||||
}
|
||||
|
||||
return $matches[1] . $matches[2] . $convertedHref . $matches[2];
|
||||
},
|
||||
$html
|
||||
);
|
||||
|
||||
return is_string($result) ? $result : $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether an href represents a navigational HTTP URL.
|
||||
*
|
||||
* @param string $href
|
||||
* @return bool
|
||||
*/
|
||||
private function isConvertibleHref(string $href): bool
|
||||
{
|
||||
if ($href === '' || $href[0] === '#' || $href[0] === '?') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (strpos($href, '{{') !== false || strpos($href, '${') !== false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return !preg_match('~^[a-z][a-z0-9+.-]*:~i', $href)
|
||||
|| preg_match('~^https?:~i', $href) === 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class LanguageResolver
|
||||
{
|
||||
/**
|
||||
* @var StoreViewRegistry
|
||||
*/
|
||||
private $storeViewRegistry;
|
||||
|
||||
/** @var StoreManagerInterface */
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(StoreViewRegistry $storeViewRegistry, StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->storeViewRegistry = $storeViewRegistry;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
public function resolveFromPath(string $pathInfo): ?string
|
||||
{
|
||||
$firstSegment = $this->getFirstSegment($pathInfo);
|
||||
|
||||
if ($firstSegment === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$language = strtolower($firstSegment);
|
||||
|
||||
$allowed = $this->storeViewRegistry->isLanguageInWebsite(
|
||||
$language,
|
||||
(int)$this->storeManager->getWebsite()->getId()
|
||||
);
|
||||
|
||||
return $allowed ? $language : null;
|
||||
}
|
||||
|
||||
private function getFirstSegment(string $pathInfo): ?string
|
||||
{
|
||||
$pathInfo = trim($pathInfo, '/');
|
||||
|
||||
if ($pathInfo === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$segments = explode('/', $pathInfo);
|
||||
|
||||
return $segments[0] !== '' ? $segments[0] : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Framework\Stdlib\Cookie\CookieMetadata;
|
||||
use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
|
||||
use Magento\Framework\Stdlib\CookieManagerInterface;
|
||||
|
||||
class MarketCookie
|
||||
{
|
||||
private const STORE_COOKIE_NAME = 'store';
|
||||
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var CookieManagerInterface
|
||||
*/
|
||||
private $cookieManager;
|
||||
|
||||
/**
|
||||
* @var CookieMetadataFactory
|
||||
*/
|
||||
private $cookieMetadataFactory;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
CookieManagerInterface $cookieManager,
|
||||
CookieMetadataFactory $cookieMetadataFactory
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->cookieManager = $cookieManager;
|
||||
$this->cookieMetadataFactory = $cookieMetadataFactory;
|
||||
}
|
||||
|
||||
public function get(): ?string
|
||||
{
|
||||
$value = $this->cookieManager->getCookie($this->config->getMarketCookieName());
|
||||
|
||||
return $value !== null ? trim($value) : null;
|
||||
}
|
||||
|
||||
public function set(string $market): void
|
||||
{
|
||||
$metadata = $this->cookieMetadataFactory->createPublicCookieMetadata([
|
||||
CookieMetadata::KEY_DURATION => $this->config->getMarketCookieLifetime(),
|
||||
CookieMetadata::KEY_SAME_SITE => 'Lax',
|
||||
]);
|
||||
$metadata->setPath('/');
|
||||
$metadata->setHttpOnly(false);
|
||||
|
||||
$this->cookieManager->setPublicCookie(
|
||||
$this->config->getMarketCookieName(),
|
||||
$market,
|
||||
$metadata
|
||||
);
|
||||
|
||||
if ($this->config->getMarketCookieName() !== self::STORE_COOKIE_NAME) {
|
||||
$storeMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata([
|
||||
CookieMetadata::KEY_DURATION => 31536000,
|
||||
CookieMetadata::KEY_SAME_SITE => 'Lax',
|
||||
]);
|
||||
$storeMetadata->setPath('/');
|
||||
$storeMetadata->setHttpOnly(false);
|
||||
|
||||
$this->cookieManager->setPublicCookie(
|
||||
self::STORE_COOKIE_NAME,
|
||||
$market,
|
||||
$storeMetadata
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Framework\App\RequestInterface;
|
||||
|
||||
class MarketResolver
|
||||
{
|
||||
/**
|
||||
* @var MarketCookie
|
||||
*/
|
||||
private $marketCookie;
|
||||
|
||||
/**
|
||||
* @var StoreCodeMapper
|
||||
*/
|
||||
private $storeCodeMapper;
|
||||
|
||||
public function __construct(MarketCookie $marketCookie, StoreCodeMapper $storeCodeMapper)
|
||||
{
|
||||
$this->marketCookie = $marketCookie;
|
||||
$this->storeCodeMapper = $storeCodeMapper;
|
||||
}
|
||||
|
||||
public function resolve(RequestInterface $request): ?string
|
||||
{
|
||||
$requestMarket = $this->normalizeMarket($request->getParam('market'));
|
||||
if ($requestMarket !== null) {
|
||||
return $requestMarket;
|
||||
}
|
||||
|
||||
$cookieMarket = $this->normalizeMarket($this->marketCookie->get());
|
||||
if ($cookieMarket !== null) {
|
||||
return $cookieMarket;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function resolveForLanguage(RequestInterface $request, string $language): ?string
|
||||
{
|
||||
$requestMarket = $this->normalizeMarketForLanguage($request->getParam('market'), $language);
|
||||
if ($requestMarket !== null) {
|
||||
return $requestMarket;
|
||||
}
|
||||
|
||||
$cookieMarket = $this->normalizeMarketForLanguage($this->marketCookie->get(), $language);
|
||||
if ($cookieMarket !== null) {
|
||||
return $cookieMarket;
|
||||
}
|
||||
|
||||
return $this->storeCodeMapper->getDefaultStoreCodeForLanguage($language);
|
||||
}
|
||||
|
||||
public function normalizeMarket($market): ?string
|
||||
{
|
||||
if (!is_scalar($market)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$market = trim((string)$market);
|
||||
|
||||
return $this->storeCodeMapper->isRealStoreCodeInScope($market) ? $market : null;
|
||||
}
|
||||
|
||||
public function normalizeMarketForLanguage($market, string $language): ?string
|
||||
{
|
||||
$market = $this->normalizeMarket($market);
|
||||
if ($market === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->storeCodeMapper->languageFromStoreCode($market) === strtolower($language) ? $market : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
class RequestPathProcessor
|
||||
{
|
||||
/**
|
||||
* @var LanguageResolver
|
||||
*/
|
||||
private $languageResolver;
|
||||
|
||||
public function __construct(LanguageResolver $languageResolver)
|
||||
{
|
||||
$this->languageResolver = $languageResolver;
|
||||
}
|
||||
|
||||
public function stripLanguagePrefix(string $pathInfo): string
|
||||
{
|
||||
if ($this->languageResolver->resolveFromPath($pathInfo) === null) {
|
||||
return $this->normalizePath($pathInfo);
|
||||
}
|
||||
|
||||
$hasTrailingSlash = substr($pathInfo, -1) === '/';
|
||||
$pathInfo = '/' . ltrim($pathInfo, '/');
|
||||
$segments = explode('/', trim($pathInfo, '/'), 2);
|
||||
$remainingPath = $segments[1] ?? '';
|
||||
|
||||
if ($remainingPath === '') {
|
||||
return '/';
|
||||
}
|
||||
|
||||
$path = '/' . ltrim($remainingPath, '/');
|
||||
|
||||
return $hasTrailingSlash && substr($path, -1) !== '/' ? $path . '/' : $path;
|
||||
}
|
||||
|
||||
public function replaceLanguagePrefix(string $pathInfo, string $language): string
|
||||
{
|
||||
$remainingPath = $this->stripLanguagePrefix($pathInfo);
|
||||
$language = strtolower(trim($language));
|
||||
|
||||
if ($remainingPath === '/') {
|
||||
return '/' . $language . '/';
|
||||
}
|
||||
|
||||
return '/' . $language . $remainingPath;
|
||||
}
|
||||
|
||||
private function normalizePath(string $pathInfo): string
|
||||
{
|
||||
$pathInfo = '/' . ltrim($pathInfo, '/');
|
||||
|
||||
return $pathInfo === '' ? '/' : $pathInfo;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class StoreCodeMapper
|
||||
{
|
||||
/**
|
||||
* @var StoreViewRegistry
|
||||
*/
|
||||
private $storeViewRegistry;
|
||||
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(StoreViewRegistry $storeViewRegistry, StoreManagerInterface $storeManager)
|
||||
{
|
||||
$this->storeViewRegistry = $storeViewRegistry;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
public function map(string $language, ?string $market = null): ?string
|
||||
{
|
||||
$language = strtolower(trim($language));
|
||||
|
||||
if ($market !== null
|
||||
&& $this->isRealStoreCodeInScope($market)
|
||||
&& $this->languageFromStoreCode($market) === $language
|
||||
) {
|
||||
return $market;
|
||||
}
|
||||
|
||||
return $this->getDefaultStoreCodeForLanguage($language);
|
||||
}
|
||||
|
||||
public function languageFromStoreCode(string $storeCode): ?string
|
||||
{
|
||||
return $this->storeViewRegistry->getLanguageByStoreCode($storeCode);
|
||||
}
|
||||
|
||||
public function marketFromStoreCode(string $storeCode): ?string
|
||||
{
|
||||
return $this->isRealStoreCodeInScope($storeCode) ? $storeCode : null;
|
||||
}
|
||||
|
||||
public function isRealStoreCodeInScope(string $storeCode): bool
|
||||
{
|
||||
return $this->storeViewRegistry->isStoreCodeInWebsite($storeCode, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function isLanguageAllowed(string $language): bool
|
||||
{
|
||||
return $this->storeViewRegistry->isLanguageInWebsite($language, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function getDefaultStoreCodeForLanguage(string $language): ?string
|
||||
{
|
||||
return $this->storeViewRegistry->getStoreCodeForLanguageInWebsite($language, $this->getWebsiteId());
|
||||
}
|
||||
|
||||
public function getDefaultLanguage(): ?string
|
||||
{
|
||||
try {
|
||||
$defaultStore = $this->storeManager->getWebsite()->getDefaultStore();
|
||||
return $defaultStore ? $this->languageFromStoreCode((string)$defaultStore->getCode()) : null;
|
||||
} catch (\Throwable $exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function isDefaultLanguage(string $language): bool
|
||||
{
|
||||
return strtolower(trim($language)) === $this->getDefaultLanguage();
|
||||
}
|
||||
|
||||
private function getWebsiteId(): int
|
||||
{
|
||||
return (int)$this->storeManager->getWebsite()->getId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Framework\App\Config\ScopeConfigInterface;
|
||||
use Magento\Store\Model\ScopeInterface;
|
||||
use Magento\Store\Model\ResourceModel\Store\CollectionFactory;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class StoreViewRegistry
|
||||
{
|
||||
/**
|
||||
* @var CollectionFactory
|
||||
*/
|
||||
private $storeCollectionFactory;
|
||||
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/** @var ScopeConfigInterface */
|
||||
private $scopeConfig;
|
||||
|
||||
/**
|
||||
* @var string[][]|null
|
||||
*/
|
||||
private $storesByLanguage;
|
||||
|
||||
/**
|
||||
* @var string[]|null
|
||||
*/
|
||||
private $languageByStoreCode;
|
||||
|
||||
/** @var string[][][]|null */
|
||||
private $storesByWebsiteLanguage;
|
||||
|
||||
/** @var int[]|null */
|
||||
private $websiteByStoreCode;
|
||||
|
||||
public function __construct(
|
||||
CollectionFactory $storeCollectionFactory,
|
||||
LoggerInterface $logger,
|
||||
ScopeConfigInterface $scopeConfig
|
||||
) {
|
||||
$this->storeCollectionFactory = $storeCollectionFactory;
|
||||
$this->logger = $logger;
|
||||
$this->scopeConfig = $scopeConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[][]
|
||||
*/
|
||||
public function getStoresByLanguage(): array
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return $this->storesByLanguage;
|
||||
}
|
||||
|
||||
public function getDefaultStoreCodeForLanguage(string $language): ?string
|
||||
{
|
||||
$language = strtolower(trim($language));
|
||||
$stores = $this->getStoresByLanguage();
|
||||
|
||||
return isset($stores[$language][0]) ? $stores[$language][0] : null;
|
||||
}
|
||||
|
||||
/** @return string[][] */
|
||||
public function getStoresByLanguageForWebsite(int $websiteId): array
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return $this->storesByWebsiteLanguage[$websiteId] ?? [];
|
||||
}
|
||||
|
||||
public function getStoreCodeForLanguageInWebsite(string $language, int $websiteId): ?string
|
||||
{
|
||||
$language = strtolower(trim($language));
|
||||
$stores = $this->getStoresByLanguageForWebsite($websiteId);
|
||||
|
||||
return $stores[$language][0] ?? null;
|
||||
}
|
||||
|
||||
public function isLanguageInWebsite(string $language, int $websiteId): bool
|
||||
{
|
||||
return $this->getStoreCodeForLanguageInWebsite($language, $websiteId) !== null;
|
||||
}
|
||||
|
||||
public function isStoreCodeInWebsite(string $storeCode, int $websiteId): bool
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return ($this->websiteByStoreCode[trim($storeCode)] ?? null) === $websiteId;
|
||||
}
|
||||
|
||||
public function getLanguageByStoreCode(string $storeCode): ?string
|
||||
{
|
||||
$this->load();
|
||||
$storeCode = trim($storeCode);
|
||||
|
||||
return $this->languageByStoreCode[$storeCode] ?? null;
|
||||
}
|
||||
|
||||
public function isKnownStoreCode(string $storeCode): bool
|
||||
{
|
||||
return $this->getLanguageByStoreCode($storeCode) !== null;
|
||||
}
|
||||
|
||||
public function isKnownLanguage(string $language): bool
|
||||
{
|
||||
$language = strtolower(trim($language));
|
||||
$stores = $this->getStoresByLanguage();
|
||||
|
||||
return isset($stores[$language]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAllowedLanguages(): array
|
||||
{
|
||||
return array_keys($this->getStoresByLanguage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAllowedStoreCodes(): array
|
||||
{
|
||||
$this->load();
|
||||
|
||||
return array_keys($this->languageByStoreCode);
|
||||
}
|
||||
|
||||
private function load(): void
|
||||
{
|
||||
if ($this->storesByLanguage !== null
|
||||
&& $this->languageByStoreCode !== null
|
||||
&& $this->storesByWebsiteLanguage !== null
|
||||
&& $this->websiteByStoreCode !== null
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->storesByLanguage = [];
|
||||
$this->languageByStoreCode = [];
|
||||
$this->storesByWebsiteLanguage = [];
|
||||
$this->websiteByStoreCode = [];
|
||||
|
||||
$collection = $this->storeCollectionFactory->create();
|
||||
$collection->setWithoutDefaultFilter();
|
||||
$collection->addStatusFilter(1);
|
||||
$collection->setOrder('sort_order', 'ASC');
|
||||
$collection->setOrder('store_id', 'ASC');
|
||||
|
||||
foreach ($collection as $store) {
|
||||
$storeCode = (string)$store->getCode();
|
||||
$storeId = (int)$store->getId();
|
||||
|
||||
if ($storeId === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$locale = (string)$this->scopeConfig->getValue(
|
||||
'general/locale/code',
|
||||
ScopeInterface::SCOPE_STORE,
|
||||
$storeCode
|
||||
);
|
||||
|
||||
if (!preg_match('/^([a-z]{2,3})(?:[_-][A-Za-z]{4})?(?:[_-][A-Z]{2})?$/', $locale, $matches)) {
|
||||
$this->logger->warning(
|
||||
'[LanguageUrlStoreResolver] Ignored active store view with unsupported locale.',
|
||||
['store_id' => $storeId, 'store_code' => $storeCode, 'locale' => $locale]
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$language = strtolower($matches[1]);
|
||||
$websiteId = (int)$store->getWebsiteId();
|
||||
$this->storesByLanguage[$language][] = $storeCode;
|
||||
$this->languageByStoreCode[$storeCode] = $language;
|
||||
$this->storesByWebsiteLanguage[$websiteId][$language][] = $storeCode;
|
||||
$this->websiteByStoreCode[$storeCode] = $websiteId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun3d\LanguageUrlStoreResolver\Model;
|
||||
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Store\Model\StoreManagerInterface;
|
||||
|
||||
class UrlPathConverter
|
||||
{
|
||||
/**
|
||||
* @var Config
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var StoreCodeMapper
|
||||
*/
|
||||
private $storeCodeMapper;
|
||||
|
||||
/**
|
||||
* @var StoreViewRegistry
|
||||
*/
|
||||
private $storeViewRegistry;
|
||||
|
||||
/**
|
||||
* @var StoreManagerInterface
|
||||
*/
|
||||
private $storeManager;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
StoreCodeMapper $storeCodeMapper,
|
||||
StoreViewRegistry $storeViewRegistry,
|
||||
StoreManagerInterface $storeManager
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->storeCodeMapper = $storeCodeMapper;
|
||||
$this->storeViewRegistry = $storeViewRegistry;
|
||||
$this->storeManager = $storeManager;
|
||||
}
|
||||
|
||||
public function toLanguageUrl(string $url, string $language): string
|
||||
{
|
||||
$parts = parse_url($url);
|
||||
if ($parts === false) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
if ($this->isExternalUrl($parts)) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$path = $parts['path'] ?? '/';
|
||||
$path = $path === '' ? '/' : $path;
|
||||
$basePath = $this->getBasePath();
|
||||
$relativePath = $this->stripBasePath($path, $basePath);
|
||||
|
||||
if ($this->config->shouldSkipPath($relativePath)) {
|
||||
return $url;
|
||||
}
|
||||
|
||||
$parts['path'] = $this->prependBasePath(
|
||||
$basePath,
|
||||
$this->convertRelativePath($relativePath, strtolower($language))
|
||||
);
|
||||
|
||||
return $this->buildUrl($parts);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $parts
|
||||
*/
|
||||
private function isExternalUrl(array $parts): bool
|
||||
{
|
||||
if (!isset($parts['host'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$baseParts = parse_url($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB));
|
||||
$baseHost = isset($baseParts['host']) ? strtolower($baseParts['host']) : '';
|
||||
|
||||
return strtolower((string)$parts['host']) !== $baseHost;
|
||||
}
|
||||
|
||||
private function convertRelativePath(string $path, string $language): string
|
||||
{
|
||||
$path = '/' . ltrim($path, '/');
|
||||
$segments = explode('/', trim($path, '/'), 2);
|
||||
$firstSegment = $segments[0] ?? '';
|
||||
$remainingPath = $segments[1] ?? '';
|
||||
|
||||
if ($this->storeCodeMapper->isRealStoreCodeInScope($firstSegment)) {
|
||||
$language = $this->storeCodeMapper->languageFromStoreCode($firstSegment) ?? $language;
|
||||
$path = $remainingPath === '' ? '/' : '/' . ltrim($remainingPath, '/');
|
||||
}
|
||||
|
||||
if ($this->storeCodeMapper->isLanguageAllowed(strtolower($firstSegment))) {
|
||||
$path = $remainingPath === '' ? '/' : '/' . ltrim($remainingPath, '/');
|
||||
}
|
||||
|
||||
if ($this->storeCodeMapper->isDefaultLanguage($language)) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return $path === '/' ? '/' . $language . '/' : '/' . $language . $path;
|
||||
}
|
||||
|
||||
private function getBasePath(): string
|
||||
{
|
||||
$baseParts = parse_url($this->storeManager->getStore()->getBaseUrl(UrlInterface::URL_TYPE_WEB));
|
||||
$basePath = isset($baseParts['path']) ? rtrim($baseParts['path'], '/') : '';
|
||||
|
||||
return $basePath !== '' ? $basePath : '/';
|
||||
}
|
||||
|
||||
private function stripBasePath(string $path, string $basePath): string
|
||||
{
|
||||
if ($basePath === '/' || $basePath === '') {
|
||||
return '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
if ($path === $basePath) {
|
||||
return '/';
|
||||
}
|
||||
|
||||
if (strpos($path, $basePath . '/') === 0) {
|
||||
return '/' . ltrim(substr($path, strlen($basePath)), '/');
|
||||
}
|
||||
|
||||
return '/' . ltrim($path, '/');
|
||||
}
|
||||
|
||||
private function prependBasePath(string $basePath, string $path): string
|
||||
{
|
||||
$path = '/' . ltrim($path, '/');
|
||||
|
||||
if ($basePath === '/' || $basePath === '') {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return rtrim($basePath, '/') . $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed[] $parts
|
||||
*/
|
||||
private function buildUrl(array $parts): string
|
||||
{
|
||||
$url = '';
|
||||
|
||||
if (isset($parts['host'])) {
|
||||
$url .= isset($parts['scheme']) ? $parts['scheme'] . '://' : '//';
|
||||
|
||||
if (isset($parts['user'])) {
|
||||
$url .= $parts['user'];
|
||||
if (isset($parts['pass'])) {
|
||||
$url .= ':' . $parts['pass'];
|
||||
}
|
||||
$url .= '@';
|
||||
}
|
||||
|
||||
$url .= $parts['host'];
|
||||
|
||||
if (isset($parts['port'])) {
|
||||
$url .= ':' . $parts['port'];
|
||||
}
|
||||
}
|
||||
|
||||
$url .= $parts['path'] ?? '/';
|
||||
|
||||
if (isset($parts['query']) && $parts['query'] !== '') {
|
||||
$url .= '?' . $parts['query'];
|
||||
}
|
||||
|
||||
if (isset($parts['fragment']) && $parts['fragment'] !== '') {
|
||||
$url .= '#' . $parts['fragment'];
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user