Skip to content

Commit 022641d

Browse files
committed
Optimize & more resilient
1 parent 4d87ce6 commit 022641d

File tree

11 files changed

+32
-65
lines changed

11 files changed

+32
-65
lines changed

Model/Config.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Magento\Directory\Helper\Data;
1010
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\ScopeInterface as AppScopeInterface;
1112
use Magento\Framework\Serialize\SerializerInterface;
1213
use Magento\Store\Api\Data\StoreInterface;
1314
use Magento\Store\Model\ScopeInterface;
@@ -37,21 +38,21 @@ public function getStorePathType(): PathType
3738
return PathType::from($this->scopeConfig->getValue(self::CONFIG_PATH_USE_STORE_PATH));
3839
}
3940

40-
public function getCountry(StoreInterface $store): string
41+
public function getCountry(AppScopeInterface|StoreInterface $scope): string
4142
{
4243
return (string)$this->scopeConfig->getValue(
4344
Data::XML_PATH_DEFAULT_COUNTRY,
4445
ScopeInterface::SCOPE_STORE,
45-
$store->getId()
46+
$scope->getId()
4647
);
4748
}
4849

49-
public function getLocale(StoreInterface $store): string
50+
public function getLocale(AppScopeInterface|StoreInterface $scope): string
5051
{
5152
return (string)$this->scopeConfig->getValue(
5253
Data::XML_PATH_DEFAULT_LOCALE,
5354
ScopeInterface::SCOPE_STORE,
54-
$store->getId()
55+
$scope->getId()
5556
);
5657
}
5758

Model/StoreSwitcher/ApplyStorePath.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public function __construct(
2020

2121
public function switch(StoreInterface $fromStore, StoreInterface $targetStore, string $redirectUrl): string
2222
{
23-
return $this->config->isEnabled() ? $this->uriUtils->replaceStoreCode($redirectUrl, $targetStore) : $redirectUrl;
23+
return $this->config->isEnabled() ? $this->uriUtils->replaceScopeCode($redirectUrl, $targetStore) : $redirectUrl;
2424
}
2525
}

Plugin/App/Request/PathInfo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
namespace Opengento\StorePathUrl\Plugin\App\Request;
88

99
use Magento\Framework\App\Request\PathInfo as PathInfoSubject;
10-
use Opengento\StorePathUrl\Service\StorePathFixer;
1110
use Opengento\StorePathUrl\Model\Config;
11+
use Opengento\StorePathUrl\Service\StorePathFixer;
1212

1313
class PathInfo
1414
{

Plugin/App/Request/StorePathInfoValidator.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

Plugin/Model/Store.php renamed to Plugin/Url/Scope.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,28 @@
44
*/
55
declare(strict_types=1);
66

7-
namespace Opengento\StorePathUrl\Plugin\Model;
7+
namespace Opengento\StorePathUrl\Plugin\Url;
88

9+
use Magento\Framework\Url\ScopeInterface;
910
use Magento\Framework\UrlInterface;
10-
use Magento\Store\Model\Store as SubjectStore;
1111
use Opengento\StorePathUrl\Model\Config;
1212
use Opengento\StorePathUrl\Service\UriUtils;
1313

14-
class Store
14+
class Scope
1515
{
1616
public function __construct(
1717
private Config $config,
1818
private UriUtils $uriUtils
1919
) {}
2020

2121
public function afterGetBaseUrl(
22-
SubjectStore $subject,
22+
ScopeInterface $subject,
2323
string $baseUrl,
2424
string $type = UrlInterface::URL_TYPE_LINK,
2525
?bool $secure = null
2626
): string {
2727
return $type === UrlInterface::URL_TYPE_LINK && $this->config->isEnabled()
28-
? $this->uriUtils->replaceStoreCode($baseUrl, $subject)
28+
? $this->uriUtils->replaceScopeCode($baseUrl, $subject)
2929
: $baseUrl;
3030
}
3131
}

Service/PathResolver.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Opengento\StorePathUrl\Service;
88

9+
use Magento\Framework\App\ScopeInterface;
910
use Magento\Store\Api\Data\StoreInterface;
1011
use Opengento\StorePathUrl\Model\Config;
1112
use Opengento\StorePathUrl\Model\Config\PathType;
@@ -18,14 +19,14 @@ class PathResolver
1819
{
1920
public function __construct(private Config $config) {}
2021

21-
public function resolve(StoreInterface $store): string
22+
public function resolve(ScopeInterface|StoreInterface $scope): string
2223
{
2324
return strtolower(match ($this->config->getStorePathType()) {
24-
PathType::StoreCode => $store->getCode(),
25-
PathType::CountryCode => $this->config->getCountry($store),
26-
PathType::LocaleUnderscore => $this->config->getLocale($store),
27-
PathType::LocaleHyphen => implode('-', explode('_', $this->config->getLocale($store))),
28-
PathType::Custom => $this->config->getCustomPathMapper()[(int)$store->getId()] ?? $store->getCode(),
25+
PathType::StoreCode => $scope->getCode(),
26+
PathType::CountryCode => $this->config->getCountry($scope),
27+
PathType::LocaleUnderscore => $this->config->getLocale($scope),
28+
PathType::LocaleHyphen => implode('-', explode('_', $this->config->getLocale($scope))),
29+
PathType::Custom => $this->config->getCustomPathMapper()[(int)$scope->getId()] ?? $scope->getCode(),
2930
});
3031
}
3132
}

Service/StorePathFixer.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Store\Model\Store;
1111

1212
use function parse_url;
13-
1413
use function str_starts_with;
1514

1615
use const PHP_URL_PATH;
@@ -30,10 +29,10 @@ public function fix(string $baseUrl, string $requestUri): string
3029
if ($baseUrl === '') {
3130
$path = parse_url($store->getBaseUrl(), PHP_URL_PATH);
3231
if (str_starts_with($requestUri . '/', $path)) {
33-
$requestUri = $this->uriUtils->replacePathCode($requestUri, $store);
32+
return $this->uriUtils->replacePathCode($requestUri, $store);
3433
}
3534
} elseif (str_starts_with($baseUrl . $requestUri, $store->getBaseUrl())) {
36-
$requestUri = $this->uriUtils->replacePathCode($requestUri, $store);
35+
return $this->uriUtils->replacePathCode($requestUri, $store);
3736
}
3837
}
3938
}

Service/UriUtils.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Opengento\StorePathUrl\Service;
88

9+
use Magento\Framework\App\ScopeInterface;
910
use Magento\Store\Api\Data\StoreInterface;
1011

1112
use function ltrim;
@@ -21,14 +22,14 @@ class UriUtils
2122
{
2223
public function __construct(private PathResolver $pathResolver) {}
2324

24-
public function replaceStoreCode(string $url, StoreInterface $store): string
25+
public function replaceScopeCode(string $url, ScopeInterface|StoreInterface $scope): string
2526
{
26-
return $this->replaceLeadingPath($store->getCode(), $this->pathResolver->resolve($store), $url);
27+
return $this->replaceLeadingPath($scope->getCode(), $this->pathResolver->resolve($scope), $url);
2728
}
2829

29-
public function replacePathCode(string $url, StoreInterface $store): string
30+
public function replacePathCode(string $url, ScopeInterface|StoreInterface $scope): string
3031
{
31-
return $this->replaceLeadingPath($this->pathResolver->resolve($store), $store->getCode(), $url);
32+
return $this->replaceLeadingPath($this->pathResolver->resolve($scope), $scope->getCode(), $url);
3233
}
3334

3435
private function replaceLeadingPath(string $search, string $replace, string $uri): string

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"magento/framework": "*",
2020
"magento/module-config": "*",
2121
"magento/module-directory": "*",
22-
"magento/module-store": "*"
22+
"magento/module-store": "*",
23+
"magento/module-url-rewrite": "*"
2324
},
2425
"require-dev": {
2526
"magento/magento-coding-standard": "^33",

etc/di.xml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@
1010
<arguments>
1111
<argument name="storeSwitchers" xsi:type="array">
1212
<item name="revertStorePath" xsi:type="object" sortOrder="99">Opengento\StorePathUrl\Model\StoreSwitcher\RevertStorePath</item>
13-
<item name="rewriteUrl" xsi:type="object" sortOrder="100">Magento\UrlRewrite\Model\StoreSwitcher\RewriteUrl</item>
13+
<item name="rewriteUrl" xsi:type="object" sortOrder="100"/>
1414
<item name="applyStorePath" xsi:type="object" sortOrder="101">Opengento\StorePathUrl\Model\StoreSwitcher\ApplyStorePath</item>
1515
</argument>
1616
</arguments>
1717
</type>
18-
<type name="Magento\Store\Model\Store">
19-
<plugin name="Opengento_StorePathUrl::prepend_custom_path" type="Opengento\StorePathUrl\Plugin\Model\Store"/>
18+
<type name="Magento\Framework\Url\ScopeInterface">
19+
<plugin name="Opengento_StorePathUrl::prepend_custom_path" type="Opengento\StorePathUrl\Plugin\Url\Scope"/>
2020
</type>
2121
<type name="Magento\Framework\App\Request\PathInfo">
2222
<plugin name="Opengento_StorePathUrl::replace_country_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\PathInfo"/>
2323
</type>
24-
<type name="Magento\Store\App\Request\StorePathInfoValidator">
25-
<plugin name="Opengento_StorePathUrl::replace_by_store_code" type="Opengento\StorePathUrl\Plugin\App\Request\StorePathInfoValidator"/>
26-
</type>
2724
</config>

0 commit comments

Comments
 (0)