Files
esun3d/app/code/Esun3d/LanguageUrlStoreResolver/Model/Config.php
T
2026-09-15 10:35:56 +08:00

80 lines
2.0 KiB
PHP

<?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;
}
}