13
13
use Magento \Framework \Exception \NotFoundException ;
14
14
use Magento \Store \Api \Data \StoreInterface ;
15
15
use Magento \Store \Api \StoreCookieManagerInterface ;
16
+ use Magento \Store \Model \ScopeInterface ;
17
+ use Magento \Store \Model \StoreManager ;
16
18
use Magento \Store \Model \StoreManagerInterface ;
17
19
18
20
/**
@@ -83,36 +85,37 @@ public function beforeDispatch(
83
85
);
84
86
if (is_array ($ storeCode )) {
85
87
if (!isset ($ storeCode ['_data ' ]['code ' ])) {
86
- $ this ->processInvalidStoreRequested ();
88
+ $ this ->processInvalidStoreRequested ($ request );
87
89
}
88
90
$ storeCode = $ storeCode ['_data ' ]['code ' ];
89
91
}
90
92
if ($ storeCode === '' ) {
91
93
//Empty code - is an invalid code and it was given explicitly
92
94
//(the value would be null if the code wasn't found).
93
- $ this ->processInvalidStoreRequested ();
95
+ $ this ->processInvalidStoreRequested ($ request );
94
96
}
95
97
try {
96
98
$ currentStore = $ this ->storeManager ->getStore ($ storeCode );
99
+ $ this ->updateContext ($ request , $ currentStore );
97
100
} catch (NoSuchEntityException $ exception ) {
98
- $ this ->processInvalidStoreRequested ($ exception );
101
+ $ this ->processInvalidStoreRequested ($ request , $ exception );
99
102
}
100
-
101
- $ this ->updateContext ($ currentStore );
102
103
}
103
104
104
105
/**
105
106
* Take action in case of invalid store requested.
106
107
*
108
+ * @param RequestInterface $request
107
109
* @param \Throwable|null $previousException
108
110
* @return void
109
111
* @throws NotFoundException
110
112
*/
111
113
private function processInvalidStoreRequested (
114
+ RequestInterface $ request ,
112
115
\Throwable $ previousException = null
113
116
) {
114
117
$ store = $ this ->storeManager ->getStore ();
115
- $ this ->updateContext ($ store );
118
+ $ this ->updateContext ($ request , $ store );
116
119
117
120
throw new NotFoundException (
118
121
$ previousException
@@ -125,16 +128,28 @@ private function processInvalidStoreRequested(
125
128
/**
126
129
* Update context accordingly to the store found.
127
130
*
131
+ * @param RequestInterface $request
128
132
* @param StoreInterface $store
129
133
* @return void
130
134
* @throws \Magento\Framework\Exception\LocalizedException
131
135
*/
132
- private function updateContext (StoreInterface $ store )
136
+ private function updateContext (RequestInterface $ request , StoreInterface $ store )
133
137
{
138
+ switch (true ) {
139
+ case $ store ->isUseStoreInUrl ():
140
+ $ defaultStoreCode = $ store ->getCode ();
141
+ break ;
142
+ case ScopeInterface::SCOPE_STORE == $ request ->getServerValue (StoreManager::PARAM_RUN_TYPE ):
143
+ $ defaultStoreCode = $ request ->getServerValue (StoreManager::PARAM_RUN_CODE );
144
+ break ;
145
+ default :
146
+ $ defaultStoreCode = $ this ->storeManager ->getDefaultStoreView ()->getCode ();
147
+ break ;
148
+ }
134
149
$ this ->httpContext ->setValue (
135
150
StoreManagerInterface::CONTEXT_STORE ,
136
151
$ store ->getCode (),
137
- $ store -> isUseStoreInUrl () ? $ store -> getCode () : $ this -> storeManager -> getDefaultStoreView ()-> getCode ()
152
+ $ defaultStoreCode
138
153
);
139
154
140
155
/** @var StoreInterface $defaultStore */
0 commit comments