@@ -36,7 +36,7 @@ class SessionManager implements SessionManagerInterface
36
36
/**
37
37
* Validator
38
38
*
39
- * @var \Magento\Framework\Session\ ValidatorInterface
39
+ * @var ValidatorInterface
40
40
*/
41
41
protected $ validator ;
42
42
@@ -50,28 +50,28 @@ class SessionManager implements SessionManagerInterface
50
50
/**
51
51
* SID resolver
52
52
*
53
- * @var \Magento\Framework\Session\ SidResolverInterface
53
+ * @var SidResolverInterface
54
54
*/
55
55
protected $ sidResolver ;
56
56
57
57
/**
58
58
* Session config
59
59
*
60
- * @var \Magento\Framework\Session\ Config\ConfigInterface
60
+ * @var Config\ConfigInterface
61
61
*/
62
62
protected $ sessionConfig ;
63
63
64
64
/**
65
65
* Save handler
66
66
*
67
- * @var \Magento\Framework\Session\ SaveHandlerInterface
67
+ * @var SaveHandlerInterface
68
68
*/
69
69
protected $ saveHandler ;
70
70
71
71
/**
72
72
* Storage
73
73
*
74
- * @var \Magento\Framework\Session\ StorageInterface
74
+ * @var StorageInterface
75
75
*/
76
76
protected $ storage ;
77
77
@@ -92,6 +92,11 @@ class SessionManager implements SessionManagerInterface
92
92
*/
93
93
private $ appState ;
94
94
95
+ /**
96
+ * @var SessionStartChecker
97
+ */
98
+ private $ sessionStartChecker ;
99
+
95
100
/**
96
101
* @param \Magento\Framework\App\Request\Http $request
97
102
* @param SidResolverInterface $sidResolver
@@ -102,7 +107,10 @@ class SessionManager implements SessionManagerInterface
102
107
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
103
108
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
104
109
* @param \Magento\Framework\App\State $appState
110
+ * @param SessionStartChecker|null $sessionStartChecker
105
111
* @throws \Magento\Framework\Exception\SessionException
112
+ *
113
+ * @SuppressWarnings(PHPMD.ExcessiveParameterList)
106
114
*/
107
115
public function __construct (
108
116
\Magento \Framework \App \Request \Http $ request ,
@@ -113,7 +121,8 @@ public function __construct(
113
121
StorageInterface $ storage ,
114
122
\Magento \Framework \Stdlib \CookieManagerInterface $ cookieManager ,
115
123
\Magento \Framework \Stdlib \Cookie \CookieMetadataFactory $ cookieMetadataFactory ,
116
- \Magento \Framework \App \State $ appState
124
+ \Magento \Framework \App \State $ appState ,
125
+ SessionStartChecker $ sessionStartChecker = null
117
126
) {
118
127
$ this ->request = $ request ;
119
128
$ this ->sidResolver = $ sidResolver ;
@@ -124,11 +133,15 @@ public function __construct(
124
133
$ this ->cookieManager = $ cookieManager ;
125
134
$ this ->cookieMetadataFactory = $ cookieMetadataFactory ;
126
135
$ this ->appState = $ appState ;
136
+ $ this ->sessionStartChecker = $ sessionStartChecker ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (
137
+ SessionStartChecker::class
138
+ );
127
139
$ this ->start ();
128
140
}
129
141
130
142
/**
131
- * This method needs to support sessions with APC enabled
143
+ * This method needs to support sessions with APC enabled.
144
+ *
132
145
* @return void
133
146
*/
134
147
public function writeClose ()
@@ -163,47 +176,49 @@ public function __call($method, $args)
163
176
*/
164
177
public function start ()
165
178
{
166
- if (!$ this ->isSessionExists ()) {
167
- \Magento \Framework \Profiler::start ('session_start ' );
168
-
169
- try {
170
- $ this ->appState ->getAreaCode ();
171
- } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
172
- throw new \Magento \Framework \Exception \SessionException (
173
- new \Magento \Framework \Phrase (
174
- 'Area code not set: Area code must be set before starting a session. '
175
- ),
176
- $ e
177
- );
178
- }
179
+ if ($ this ->sessionStartChecker ->check ()) {
180
+ if (!$ this ->isSessionExists ()) {
181
+ \Magento \Framework \Profiler::start ('session_start ' );
182
+
183
+ try {
184
+ $ this ->appState ->getAreaCode ();
185
+ } catch (\Magento \Framework \Exception \LocalizedException $ e ) {
186
+ throw new \Magento \Framework \Exception \SessionException (
187
+ new \Magento \Framework \Phrase (
188
+ 'Area code not set: Area code must be set before starting a session. '
189
+ ),
190
+ $ e
191
+ );
192
+ }
179
193
180
- // Need to apply the config options so they can be ready by session_start
181
- $ this ->initIniOptions ();
182
- $ this ->registerSaveHandler ();
183
- if (isset ($ _SESSION ['new_session_id ' ])) {
184
- // Not fully expired yet. Could be lost cookie by unstable network.
185
- session_commit ();
186
- session_id ($ _SESSION ['new_session_id ' ]);
187
- }
188
- $ sid = $ this ->sidResolver ->getSid ($ this );
189
- // potential custom logic for session id (ex. switching between hosts)
190
- $ this ->setSessionId ($ sid );
191
- session_start ();
192
- if (isset ($ _SESSION ['destroyed ' ])
193
- && $ _SESSION ['destroyed ' ] < time () - $ this ->sessionConfig ->getCookieLifetime ()
194
- ) {
195
- $ this ->destroy (['clear_storage ' => true ]);
196
- }
194
+ // Need to apply the config options so they can be ready by session_start
195
+ $ this ->initIniOptions ();
196
+ $ this ->registerSaveHandler ();
197
+ if (isset ($ _SESSION ['new_session_id ' ])) {
198
+ // Not fully expired yet. Could be lost cookie by unstable network.
199
+ session_commit ();
200
+ session_id ($ _SESSION ['new_session_id ' ]);
201
+ }
202
+ $ sid = $ this ->sidResolver ->getSid ($ this );
203
+ // potential custom logic for session id (ex. switching between hosts)
204
+ $ this ->setSessionId ($ sid );
205
+ session_start ();
206
+ if (isset ($ _SESSION ['destroyed ' ])
207
+ && $ _SESSION ['destroyed ' ] < time () - $ this ->sessionConfig ->getCookieLifetime ()
208
+ ) {
209
+ $ this ->destroy (['clear_storage ' => true ]);
210
+ }
197
211
198
- $ this ->validator ->validate ($ this );
199
- $ this ->renewCookie ($ sid );
212
+ $ this ->validator ->validate ($ this );
213
+ $ this ->renewCookie ($ sid );
200
214
201
- register_shutdown_function ([$ this , 'writeClose ' ]);
215
+ register_shutdown_function ([$ this , 'writeClose ' ]);
202
216
203
- $ this ->_addHost ();
204
- \Magento \Framework \Profiler::stop ('session_start ' );
217
+ $ this ->_addHost ();
218
+ \Magento \Framework \Profiler::stop ('session_start ' );
219
+ }
220
+ $ this ->storage ->init (isset ($ _SESSION ) ? $ _SESSION : []);
205
221
}
206
- $ this ->storage ->init (isset ($ _SESSION ) ? $ _SESSION : []);
207
222
return $ this ;
208
223
}
209
224
0 commit comments