Skip to content

Commit 723229b

Browse files
committed
Setup MAGE_RUN_CODE & MAGE_RUN_TYPE are not necessary anymore
1 parent 807354b commit 723229b

File tree

1 file changed

+49
-6
lines changed

1 file changed

+49
-6
lines changed

Plugin/App/Request/StorePathInfoValidator.php

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
namespace Opengento\StorePathUrl\Plugin\App\Request;
88

99
use Magento\Framework\App\Request\Http;
10+
use Magento\Framework\Exception\NoSuchEntityException;
11+
use Magento\Framework\UrlInterface;
1012
use Magento\Store\Api\StoreRepositoryInterface;
1113
use Magento\Store\App\Request\StorePathInfoValidator as Subject;
1214
use Magento\Store\Model\Store;
1315
use Opengento\StorePathUrl\Model\Config;
1416

1517
use function explode;
18+
use function parse_url;
19+
use function strtok;
20+
use function trim;
21+
22+
use const PHP_URL_PATH;
1623

1724
class StorePathInfoValidator
1825
{
@@ -23,16 +30,52 @@ public function __construct(
2330

2431
public function beforeGetValidStoreCode(Subject $subject, Http $request, string $pathInfo = ''): array
2532
{
26-
if ($pathInfo !== '' && $this->config->isEnabled()) {
33+
if ($this->config->isEnabled()) {
2734
$uri = explode('?', $request->getUriString())[0] . '/';
28-
/** @var Store $store */
29-
foreach ($this->storeRepository->getList() as $store) {
30-
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
31-
$pathInfo = $store->getCode();
32-
}
35+
if ($pathInfo === '') {
36+
$pathInfo = strtok(trim(parse_url($uri, PHP_URL_PATH), '/'), '/');
3337
}
38+
$pathInfo = $pathInfo === false ? $this->resolveByWebUrl($uri) : $this->resolveByLinkUrl($uri, $pathInfo);
3439
}
3540

3641
return [$request, $pathInfo];
3742
}
43+
44+
private function resolveByLinkUrl(string $uri, string $pathInfo): string
45+
{
46+
/** @var Store $store */
47+
foreach ($this->storeRepository->getList() as $store) {
48+
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl())) {
49+
$pathInfo = $store->getCode();
50+
}
51+
}
52+
53+
return $pathInfo;
54+
}
55+
56+
private function resolveByWebUrl(string $uri): string
57+
{
58+
$matches = [];
59+
60+
/** @var Store $store */
61+
foreach ($this->storeRepository->getList() as $store) {
62+
if ($store->getId() && str_starts_with($uri, $store->getBaseUrl(UrlInterface::URL_TYPE_WEB))) {
63+
try {
64+
$website = $store->getWebsite();
65+
if ($website->getIsDefault()) {
66+
if ($store->isDefault()) {
67+
return $store->getCode();
68+
}
69+
$matches[0] = $store->getCode();
70+
} elseif ($store->isDefault()) {
71+
$matches[1] = $store->getCode();
72+
} else {
73+
$matches[2] = $store->getCode();
74+
}
75+
} catch (NoSuchEntityException) {}
76+
}
77+
}
78+
79+
return $matches[0] ?? $matches[1] ?? $matches[2] ?? '';
80+
}
3881
}

0 commit comments

Comments
 (0)