7
7
namespace Opengento \StorePathUrl \Plugin \App \Request ;
8
8
9
9
use Magento \Framework \App \Request \Http ;
10
+ use Magento \Framework \Exception \NoSuchEntityException ;
11
+ use Magento \Framework \UrlInterface ;
10
12
use Magento \Store \Api \StoreRepositoryInterface ;
11
13
use Magento \Store \App \Request \StorePathInfoValidator as Subject ;
12
14
use Magento \Store \Model \Store ;
13
15
use Opengento \StorePathUrl \Model \Config ;
14
16
15
17
use function explode ;
18
+ use function parse_url ;
19
+ use function strtok ;
20
+ use function trim ;
21
+
22
+ use const PHP_URL_PATH ;
16
23
17
24
class StorePathInfoValidator
18
25
{
@@ -23,16 +30,52 @@ public function __construct(
23
30
24
31
public function beforeGetValidStoreCode (Subject $ subject , Http $ request , string $ pathInfo = '' ): array
25
32
{
26
- if ($ pathInfo !== '' && $ this ->config ->isEnabled ()) {
33
+ if ($ this ->config ->isEnabled ()) {
27
34
$ 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 ), '/ ' ), '/ ' );
33
37
}
38
+ $ pathInfo = $ pathInfo === false ? $ this ->resolveByWebUrl ($ uri ) : $ this ->resolveByLinkUrl ($ uri , $ pathInfo );
34
39
}
35
40
36
41
return [$ request , $ pathInfo ];
37
42
}
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
+ }
38
81
}
0 commit comments