From 723229b006a4880756dfdb1552b8acd56e656de8 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Wed, 27 Mar 2024 23:32:46 +0100 Subject: [PATCH] Setup MAGE_RUN_CODE & MAGE_RUN_TYPE are not necessary anymore --- Plugin/App/Request/StorePathInfoValidator.php | 55 +++++++++++++++++-- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/Plugin/App/Request/StorePathInfoValidator.php b/Plugin/App/Request/StorePathInfoValidator.php index 1e3dbb5..934ef81 100644 --- a/Plugin/App/Request/StorePathInfoValidator.php +++ b/Plugin/App/Request/StorePathInfoValidator.php @@ -7,12 +7,19 @@ namespace Opengento\StorePathUrl\Plugin\App\Request; use Magento\Framework\App\Request\Http; +use Magento\Framework\Exception\NoSuchEntityException; +use Magento\Framework\UrlInterface; use Magento\Store\Api\StoreRepositoryInterface; use Magento\Store\App\Request\StorePathInfoValidator as Subject; use Magento\Store\Model\Store; use Opengento\StorePathUrl\Model\Config; use function explode; +use function parse_url; +use function strtok; +use function trim; + +use const PHP_URL_PATH; class StorePathInfoValidator { @@ -23,16 +30,52 @@ public function __construct( public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array { - if ($pathInfo !== '' && $this->config->isEnabled()) { + if ($this->config->isEnabled()) { $uri = explode('?', $request->getUriString())[0] . '/'; - /** @var Store $store */ - foreach ($this->storeRepository->getList() as $store) { - if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) { - $pathInfo = $store->getCode(); - } + if ($pathInfo === '') { + $pathInfo = strtok(trim(parse_url($uri, PHP_URL_PATH), '/'), '/'); } + $pathInfo = $pathInfo === false ? $this->resolveByWebUrl($uri) : $this->resolveByLinkUrl($uri, $pathInfo); } return [$request, $pathInfo]; } + + private function resolveByLinkUrl(string $uri, string $pathInfo): string + { + /** @var Store $store */ + foreach ($this->storeRepository->getList() as $store) { + if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) { + $pathInfo = $store->getCode(); + } + } + + return $pathInfo; + } + + private function resolveByWebUrl(string $uri): string + { + $matches = []; + + /** @var Store $store */ + foreach ($this->storeRepository->getList() as $store) { + if ($store->getId() && str_starts_with($uri, $store->getBaseUrl(UrlInterface::URL_TYPE_WEB))) { + try { + $website = $store->getWebsite(); + if ($website->getIsDefault()) { + if ($store->isDefault()) { + return $store->getCode(); + } + $matches[0] = $store->getCode(); + } elseif ($store->isDefault()) { + $matches[1] = $store->getCode(); + } else { + $matches[2] = $store->getCode(); + } + } catch (NoSuchEntityException) {} + } + } + + return $matches[0] ?? $matches[1] ?? $matches[2] ?? ''; + } }