1212namespace Symfony \Component \HttpFoundation \Session \Storage ;
1313
1414use Symfony \Component \HttpFoundation \Session \SessionBagInterface ;
15- use Symfony \Component \HttpFoundation \Session \Storage \Proxy \AbstractProxy ;
16- use Symfony \Component \HttpFoundation \Session \Storage \Proxy \SessionHandlerProxy ;
1715
1816/**
1917 * This provides a base class for session attribute storage.
@@ -38,7 +36,7 @@ class NativeSessionStorage implements SessionStorageInterface
3836 protected $ closed = false ;
3937
4038 /**
41- * @var AbstractProxy| \SessionHandlerInterface
39+ * @var \SessionHandlerInterface
4240 */
4341 protected $ saveHandler ;
4442
@@ -87,12 +85,8 @@ class NativeSessionStorage implements SessionStorageInterface
8785 * sid_bits_per_character, "5"
8886 * trans_sid_hosts, $_SERVER['HTTP_HOST']
8987 * trans_sid_tags, "a=href,area=href,frame=src,form="
90- *
91- * @param array $options Session configuration options
92- * @param \SessionHandlerInterface|null $handler
93- * @param MetadataBag $metaBag MetadataBag
9488 */
95- public function __construct (array $ options = array (), $ handler = null , MetadataBag $ metaBag = null )
89+ public function __construct (array $ options = array (), \ SessionHandlerInterface $ handler = null , MetadataBag $ metaBag = null )
9690 {
9791 session_cache_limiter ('' ); // disable by default because it's managed by HeaderBag (if used)
9892 ini_set ('session.use_cookies ' , 1 );
@@ -107,7 +101,7 @@ public function __construct(array $options = array(), $handler = null, MetadataB
107101 /**
108102 * Gets the save handler instance.
109103 *
110- * @return AbstractProxy| \SessionHandlerInterface
104+ * @return \SessionHandlerInterface
111105 */
112106 public function getSaveHandler ()
113107 {
@@ -146,31 +140,43 @@ public function start()
146140 */
147141 public function getId ()
148142 {
149- return $ this -> saveHandler -> getId ();
143+ return session_id ();
150144 }
151145
152146 /**
153147 * {@inheritdoc}
148+ *
149+ * @throws \LogicException When the session is active
154150 */
155151 public function setId ($ id )
156152 {
157- $ this ->saveHandler ->setId ($ id );
153+ if (\PHP_SESSION_ACTIVE === session_status ()) {
154+ throw new \LogicException ('Cannot change the ID of an active session ' );
155+ }
156+
157+ session_id ($ id );
158158 }
159159
160160 /**
161161 * {@inheritdoc}
162162 */
163163 public function getName ()
164164 {
165- return $ this -> saveHandler -> getName ();
165+ return session_name ();
166166 }
167167
168168 /**
169169 * {@inheritdoc}
170+ *
171+ * @throws \LogicException When the session is active
170172 */
171173 public function setName ($ name )
172174 {
173- $ this ->saveHandler ->setName ($ name );
175+ if (\PHP_SESSION_ACTIVE === session_status ()) {
176+ throw new \LogicException ('Cannot change the name of an active session ' );
177+ }
178+
179+ session_name ($ name );
174180 }
175181
176182 /**
@@ -217,9 +223,6 @@ public function save()
217223 // The default PHP error message is not very helpful, as it does not give any information on the current save handler.
218224 // Therefore, we catch this error and trigger a warning with a better error message
219225 $ handler = $ this ->getSaveHandler ();
220- if ($ handler instanceof SessionHandlerProxy) {
221- $ handler = $ handler ->getHandler ();
222- }
223226
224227 restore_error_handler ();
225228 trigger_error (sprintf ('session_write_close(): Failed to write session data with %s handler ' , get_class ($ handler )), E_USER_WARNING );
@@ -248,6 +251,8 @@ public function clear()
248251
249252 /**
250253 * {@inheritdoc}
254+ *
255+ * @throws \LogicException When the session is already started
251256 */
252257 public function registerBag (SessionBagInterface $ bag )
253258 {
@@ -267,7 +272,7 @@ public function getBag($name)
267272 throw new \InvalidArgumentException (sprintf ('The SessionBagInterface %s is not registered. ' , $ name ));
268273 }
269274
270- if (!$ this ->started && $ this -> saveHandler -> isActive ()) {
275+ if (!$ this ->started && \ PHP_SESSION_ACTIVE === session_status ()) {
271276 $ this ->loadSession ();
272277 } elseif (!$ this ->started ) {
273278 $ this ->start ();
@@ -357,37 +362,12 @@ public function setOptions(array $options)
357362 * @see http://php.net/sessionhandlerinterface
358363 * @see http://php.net/sessionhandler
359364 * @see http://github.com/drak/NativeSession
360- *
361- * @param \SessionHandlerInterface|null $saveHandler
362- *
363- * @throws \InvalidArgumentException
364365 */
365- public function setSaveHandler ($ saveHandler = null )
366+ public function setSaveHandler (\ SessionHandlerInterface $ saveHandler = null )
366367 {
367- if (!$ saveHandler instanceof AbstractProxy &&
368- !$ saveHandler instanceof \SessionHandlerInterface &&
369- null !== $ saveHandler ) {
370- throw new \InvalidArgumentException ('Must be instance of AbstractProxy; implement \SessionHandlerInterface; or be null. ' );
371- }
368+ $ this ->saveHandler = $ saveHandler ?: new \SessionHandler ();
372369
373- if ($ saveHandler instanceof AbstractProxy) {
374- @trigger_error (
375- 'Using session save handlers that are instances of AbstractProxy is deprecated since version 3.4 and will be removed in 4.0. ' ,
376- E_USER_DEPRECATED
377- );
378- }
379-
380- // Wrap $saveHandler in proxy and prevent double wrapping of proxy
381- if (!$ saveHandler instanceof AbstractProxy && $ saveHandler instanceof \SessionHandlerInterface) {
382- $ saveHandler = new SessionHandlerProxy ($ saveHandler );
383- } elseif (!$ saveHandler instanceof AbstractProxy) {
384- $ saveHandler = new SessionHandlerProxy (new \SessionHandler ());
385- }
386- $ this ->saveHandler = $ saveHandler ;
387-
388- if ($ this ->saveHandler instanceof \SessionHandlerInterface) {
389- session_set_save_handler ($ this ->saveHandler , false );
390- }
370+ session_set_save_handler ($ this ->saveHandler , false );
391371 }
392372
393373 /**
@@ -406,11 +386,12 @@ protected function loadSession(array &$session = null)
406386 $ session = &$ _SESSION ;
407387 }
408388
409- $ bags = array_merge ($ this ->bags , array ($ this ->metadataBag ));
389+ $ bags = $ this ->bags ;
390+ $ bags [] = $ this ->metadataBag ;
410391
411392 foreach ($ bags as $ bag ) {
412393 $ key = $ bag ->getStorageKey ();
413- $ session [$ key ] = isset ( $ session [$ key ]) ? $ session [ $ key ] : array ();
394+ $ session [$ key ] = $ session [$ key ] ?? array ();
414395 $ bag ->initialize ($ session [$ key ]);
415396 }
416397
0 commit comments